浏览代码

Add parse_submodules function.

Jelmer Vernooij 8 年之前
父节点
当前提交
e557e763e2
共有 3 个文件被更改,包括 33 次插入0 次删除
  1. 4 0
      NEWS
  2. 15 0
      dulwich/config.py
  3. 14 0
      dulwich/tests/test_config.py

+ 4 - 0
NEWS

@@ -5,6 +5,10 @@
   * Fix ShaFile.id after modification of a copied ShaFile.
     (Félix Mattrat, Jelmer Vernooij)
 
+ IMPROVEMENTS
+
+  * Add `dulwich.config.parse_submodules` function.
+
 0.13.0	2016-04-24
 
  IMPROVEMENTS

+ 15 - 0
dulwich/config.py

@@ -417,3 +417,18 @@ class StackedConfig(Config):
         if self.writable is None:
             raise NotImplementedError(self.set)
         return self.writable.set(section, name, value)
+
+
+def parse_submodules(config):
+    """Parse a gitmodules GitConfig file, returning submodules.
+
+   :param config: A `ConfigFile`
+   :return: list of tuples (submodule path, url, name),
+       where name is quoted part of the section's name.
+    """
+    for section in config.keys():
+        section_kind, section_name = section
+        if section_kind == 'submodule':
+            sm_path = config.get(section, 'path')
+            sm_url = config.get(section, 'url')
+            yield (sm_path, sm_url, section_name)

+ 14 - 0
dulwich/tests/test_config.py

@@ -29,6 +29,7 @@ from dulwich.config import (
     _escape_value,
     _parse_string,
     _unescape_value,
+    parse_submodules,
     )
 from dulwich.tests import (
     TestCase,
@@ -292,3 +293,16 @@ class CheckSectionNameTests(TestCase):
         self.assertTrue(_check_section_name(b"foo"))
         self.assertTrue(_check_section_name(b"foo-bar"))
         self.assertTrue(_check_section_name(b"bar.bar"))
+
+
+class SubmodulesTests(TestCase):
+
+    def testSubmodules(self):
+        cf = ConfigFile.from_file(BytesIO(b"""\
+[submodule "core/lib"]
+	path = core/lib
+	url = https://github.com/phhusson/QuasselC.git
+"""))
+        got = list(parse_submodules(cf))
+        self.assertEqual([
+            ('core/lib', 'https://github.com/phhusson/QuasselC.git', 'core/lib')], got)