Parcourir la source

Add Repo.get_description method.

Jelmer Vernooij il y a 11 ans
Parent
commit
763fd77f13
3 fichiers modifiés avec 47 ajouts et 0 suppressions
  1. 2 0
      NEWS
  2. 32 0
      dulwich/repo.py
  3. 13 0
      dulwich/tests/test_repository.py

+ 2 - 0
NEWS

@@ -17,6 +17,8 @@
   * Support passing a single revision to BaseRepo.get_walker() rather than a list of revisions. 
     (Alberto Ruiz)
 
+  * Add `Repo.get_description` method. (Jelmer Vernooij)
+
 0.9.0	2013-05-31
 
  BUG FIXES

+ 32 - 0
dulwich/repo.py

@@ -979,6 +979,14 @@ class BaseRepo(object):
         """
         raise NotImplementedError(self.get_config)
 
+    def get_description(self):
+        """Retrieve the description for this repository.
+
+        :return: String with the description of the repository
+            as set by the user.
+        """
+        raise NotImplementedError(self.get_description)
+
     def get_config_stack(self):
         """Return a config stack for this repository.
 
@@ -1451,6 +1459,23 @@ class Repo(BaseRepo):
             ret.path = path
             return ret
 
+    def get_description(self):
+        """Retrieve the description of this repository.
+
+        :return: A string describing the repository or None.
+        """
+        path = os.path.join(self._controldir, 'description')
+        try:
+            f = GitFile(path, 'rb')
+            try:
+                return f.read()
+            finally:
+                f.close()
+        except (IOError, OSError), e:
+            if e.errno != errno.ENOENT:
+                raise
+            return None
+
     def __repr__(self):
         return "<Repo at %r>" % self.path
 
@@ -1543,6 +1568,13 @@ class MemoryRepo(BaseRepo):
         from dulwich.config import ConfigFile
         return ConfigFile()
 
+    def get_description(self):
+        """Retrieve the repository description.
+
+        This defaults to None, for no description.
+        """
+        return None
+
     @classmethod
     def init_bare(cls, objects, refs):
         """Create a new bare repository in memory.

+ 13 - 0
dulwich/tests/test_repository.py

@@ -165,6 +165,19 @@ class RepositoryTests(TestCase):
         r = self._repo = open_repo('a.git')
         self.assertTrue("HEAD" in r)
 
+    def test_get_no_description(self):
+        r = self._repo = open_repo('a.git')
+        self.assertIs(None, r.get_description())
+
+    def test_get_description(self):
+        r = self._repo = open_repo('a.git')
+        f = open(os.path.join(r.path, 'description'), 'w')
+        try:
+            f.write("Some description")
+        finally:
+            f.close()
+        self.assertEquals("Some description", r.get_description())
+
     def test_contains_missing(self):
         r = self._repo = open_repo('a.git')
         self.assertFalse("bar" in r)