瀏覽代碼

Add get_refs() call.

Jelmer Vernooij 16 年之前
父節點
當前提交
36ad1840a7
共有 2 個文件被更改,包括 17 次插入1 次删除
  1. 10 1
      dulwich/repo.py
  2. 7 0
      dulwich/tests/test_repository.py

+ 10 - 1
dulwich/repo.py

@@ -119,7 +119,7 @@ class Repo(object):
         if ref[-1] == '\n':
           ref = ref[:-1]
         return self.ref(ref)
-      assert len(contents) == 41, 'Invalid ref'
+      assert len(contents) == 41, 'Invalid ref in %s' % file
       return contents[:-1]
     finally:
       f.close()
@@ -130,6 +130,15 @@ class Repo(object):
       if os.path.exists(file):
         return self._get_ref(file)
 
+  def get_refs(self):
+    ret = {"HEAD": self.head()}
+    for dir in ["refs/heads", "refs/tags"]:
+        for name in os.listdir(os.path.join(self.basedir(), dir)):
+          path = os.path.join(self.basedir(), dir, name)
+          if os.path.isfile(path):
+            ret["/".join([dir, name])] = self._get_ref(path)
+    return ret
+
   def set_ref(self, name, value):
     file = os.path.join(self.basedir(), name)
     open(file, 'w').write(value+"\n")

+ 7 - 0
dulwich/tests/test_repository.py

@@ -41,6 +41,13 @@ class RepositoryTests(unittest.TestCase):
     self.assertEqual(r.ref('master'),
                      'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
 
+  def test_get_refs(self):
+    r = self.open_repo('a')
+    self.assertEquals({
+        'HEAD': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097', 
+        'refs/heads/master': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097'
+        }, r.get_refs())
+
   def test_head(self):
     r = self.open_repo('a')
     self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')