Browse Source

Use ConfigFile in Repo.get_config().

Jelmer Vernooij 13 years ago
parent
commit
a577926b69
4 changed files with 35 additions and 4 deletions
  1. 16 0
      dulwich/config.py
  2. 7 3
      dulwich/repo.py
  3. 10 0
      dulwich/tests/test_config.py
  4. 2 1
      dulwich/tests/test_repository.py

+ 16 - 0
dulwich/config.py

@@ -20,3 +20,19 @@
 
 """
 
+
+class ConfigFile(object):
+    """A Git configuration file, like .git/config or ~/.gitconfig."""
+
+    def __init__(self):
+        """Create a new ConfigFile."""
+
+    def __eq__(self, other):
+        return isinstance(other, self.__class__)
+
+    @classmethod
+    def from_file(cls, path):
+        ret = cls()
+        f = open(path, 'r')
+        # FIXME?
+        f.close()

+ 7 - 3
dulwich/repo.py

@@ -883,9 +883,13 @@ class BaseRepo(object):
         return self.commit(sha).parents
 
     def get_config(self):
-        import ConfigParser
-        p = ConfigParser.RawConfigParser()
-        p.read(os.path.join(self._controldir, 'config'))
+        from dulwich.config import ConfigFile
+        try:
+            p = ConfigFile.from_file(os.path.join(self._controldir, 'config'))
+        except (IOError, OSError), e:
+            if e.errno == errno.ENOENT:
+                return ConfigFile()
+            raise
         return dict((section, dict(p.items(section)))
                     for section in p.sections())
 

+ 10 - 0
dulwich/tests/test_config.py

@@ -18,4 +18,14 @@
 
 """Tests for reading and writing configuraiton files."""
 
+from dulwich.config import ConfigFile
+from dulwich.tests import TestCase
 
+
+class ConfigFileTests(TestCase):
+
+    def test_empty(self):
+        ConfigFile()
+
+    def test_eq(self):
+        self.assertEquals(ConfigFile(), ConfigFile())

+ 2 - 1
dulwich/tests/test_repository.py

@@ -33,6 +33,7 @@ from dulwich.object_store import (
     tree_lookup_path,
     )
 from dulwich import objects
+from dulwich.config import ConfigFile
 from dulwich.repo import (
     check_ref_format,
     DictRefsContainer,
@@ -318,7 +319,7 @@ class RepositoryTests(TestCase):
 
     def test_get_config(self):
         r = self._repo = open_repo('ooo_merge.git')
-        self.assertEquals({}, r.get_config())
+        self.assertEquals(ConfigFile(), r.get_config())
 
     def test_common_revisions(self):
         """