Delegate SHA peeling to the object store.
authorDave Borowitz <dborowitz@google.com>
Sun, 8 Aug 2010 22:34:06 +0000 (00:34 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 8 Aug 2010 22:34:06 +0000 (00:34 +0200)
Once a ref is resolved to a SHA, we no longer need access to the
RefsContainer, so the ObjectStore can do all the work.

NEWS
dulwich/object_store.py
dulwich/repo.py
dulwich/tests/test_object_store.py

diff --git a/NEWS b/NEWS
index 1fba2e6066214474da21b105d20cf20b42f65652..e711e83592d3182524f5b37ddf7f46a5df0aca20 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,8 @@
 
   * New dulwich.pack.MemoryPackIndex class. (Jelmer Vernooij)
 
+  * Delegate SHA peeling to the object store.  (Dave Borowitz)
+
  TESTS
 
   * Use GitFile when modifying packed-refs in tests. (Dave Borowitz)
index 925edd0ad305e74d5e77f5758c15f43ef0ff4af8..61192d7e9479c8874534ed5a2575ef4e36fbcff1 100644 (file)
@@ -41,6 +41,7 @@ from dulwich.objects import (
     sha_to_hex,
     hex_to_filename,
     S_ISGITLINK,
+    object_class,
     )
 from dulwich.pack import (
     Pack,
@@ -243,6 +244,21 @@ class BaseObjectStore(object):
         """
         return self.iter_shas(self.find_missing_objects(have, want, progress))
 
+    def peel_sha(self, sha):
+        """Peel all tags from a SHA.
+
+        :param sha: The object SHA to peel.
+        :return: The fully-peeled SHA1 of a tag object, after peeling all
+            intermediate tags; if the original ref does not point to a tag, this
+            will equal the original SHA1.
+        """
+        obj = self[sha]
+        obj_class = object_class(obj.type_name)
+        while obj_class is Tag:
+            obj_class, sha = obj.object
+            obj = self[sha]
+        return obj
+
 
 class PackBasedObjectStore(BaseObjectStore):
 
index b53ee74002d8be3dc02cc9bc9c272f607cf2849e..335c1be32bd94acd6618e2c9c29424a74de31247 100644 (file)
@@ -924,20 +924,15 @@ class BaseRepo(object):
     def get_peeled(self, ref):
         """Get the peeled value of a ref.
 
-        :param ref: the refname to peel
-        :return: the fully-peeled SHA1 of a tag object, after peeling all
+        :param ref: The refname to peel.
+        :return: The fully-peeled SHA1 of a tag object, after peeling all
             intermediate tags; if the original ref does not point to a tag, this
             will equal the original SHA1.
         """
         cached = self.refs.get_peeled(ref)
         if cached is not None:
             return cached
-        obj = self[ref]
-        obj_class = object_class(obj.type_name)
-        while obj_class is Tag:
-            obj_class, sha = obj.object
-            obj = self.get_object(sha)
-        return obj.id
+        return self.object_store.peel_sha(self.refs[ref]).id
 
     def revision_history(self, head):
         """Returns a list of the commits reachable from head.
index b0cb0a78560485fa707ca30900300e0211dc4040..646b3a5c04cbf062bb8abf67f55763668eb615b6 100644 (file)
@@ -27,7 +27,9 @@ from dulwich.index import (
     commit_tree,
     )
 from dulwich.objects import (
+    object_class,
     Blob,
+    Tag,
     )
 from dulwich.object_store import (
     DiskObjectStore,
@@ -127,6 +129,22 @@ class ObjectStoreTests(object):
         actual = self.store.iter_tree_contents(tree_id, include_trees=True)
         self.assertEquals(expected, list(actual))
 
+    def make_tag(self, name, obj):
+        tag = make_object(Tag, name=name, message='',
+                          tag_time=12345, tag_timezone=0,
+                          tagger='Test Tagger <test@example.com>',
+                          object=(object_class(obj.type_name), obj.id))
+        self.store.add_object(tag)
+        return tag
+
+    def test_peel_sha(self):
+        self.store.add_object(testobject)
+        tag1 = self.make_tag('1', testobject)
+        tag2 = self.make_tag('2', testobject)
+        tag3 = self.make_tag('3', testobject)
+        for obj in [testobject, tag1, tag2, tag3]:
+            self.assertEqual(testobject, self.store.peel_sha(obj.id))
+
 
 class MemoryObjectStoreTests(ObjectStoreTests, TestCase):