← Back to team overview

dulwich-users team mailing list archive

[PATCH 02/34] tests/utils: Build commit graphs with custom trees.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I3fdbf5da4ad62e1df42598e435b84361aa89b2a4
---
 dulwich/tests/test_utils.py |    9 +++++++++
 dulwich/tests/utils.py      |   26 +++++++++++++++++++++-----
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/dulwich/tests/test_utils.py b/dulwich/tests/test_utils.py
index b96928d..591ec05 100644
--- a/dulwich/tests/test_utils.py
+++ b/dulwich/tests/test_utils.py
@@ -59,3 +59,12 @@ class BuildCommitGraphTest(TestCase):
     def test_missing_parent(self):
         self.assertRaises(ValueError, build_commit_graph, self.store,
                           [[1], [3, 2], [2, 1]])
+
+    def test_trees(self):
+        a1 = make_object(Blob, data='aaa1')
+        a2 = make_object(Blob, data='aaa2')
+        c1, c2 = build_commit_graph(self.store, [[1], [2, 1]],
+                                    trees={1: [('a', a1)],
+                                           2: [('a', a2, 0100644)]})
+        self.assertEqual((0100644, a1.id), self.store[c1.tree]['a'])
+        self.assertEqual((0100644, a2.id), self.store[c2.tree]['a'])
diff --git a/dulwich/tests/utils.py b/dulwich/tests/utils.py
index a2cf9de..c057dac 100644
--- a/dulwich/tests/utils.py
+++ b/dulwich/tests/utils.py
@@ -28,10 +28,12 @@ import tempfile
 import time
 import types
 
+from dulwich.index import (
+    commit_tree,
+    )
 from dulwich.objects import (
     FixedSha,
     Commit,
-    Tree,
     )
 from dulwich.pack import (
     OFS_DELTA,
@@ -236,7 +238,7 @@ def build_pack(f, objects_spec, store=None):
     return expected
 
 
-def build_commit_graph(object_store, commit_spec):
+def build_commit_graph(object_store, commit_spec, trees=None):
     """Build a commit graph from a concise specification.
 
     Sample usage:
@@ -256,9 +258,15 @@ def build_commit_graph(object_store, commit_spec):
         remaining elements are its parents. The commit numbers are only
         meaningful for the call to make_commits; since real commit objects are
         created, they will get created with real, opaque SHAs.
+    :param trees: An optional dict of commit number -> tree spec for building
+        trees for commits. The tree spec is an iterable of (path, blob, mode) or
+        (path, blob) entries; if mode is omitted, it defaults to the normal file
+        mode (0100644).
     :return: The list of commit objects created.
     :raise ValueError: If an undefined commit identifier is listed as a parent.
     """
+    if trees is None:
+        trees = {}
     commit_time = 0
     nums = {}
     commits = []
@@ -271,17 +279,25 @@ def build_commit_graph(object_store, commit_spec):
             missing_parent, = e.args
             raise ValueError('Unknown parent %i' % missing_parent)
 
-        tree = Tree()
+        blobs = []
+        for entry in trees.get(commit_num, []):
+            if len(entry) == 2:
+                path, blob = entry
+                entry = (path, blob, F)
+            path, blob, mode = entry
+            blobs.append((path, blob.id, mode))
+            object_store.add_object(blob)
+        tree_id = commit_tree(object_store, blobs)
+
         commit_obj = make_commit(
           message=('Commit %i' % commit_num),
           parents=parent_ids,
-          tree=tree.id,
+          tree=tree_id,
           commit_time=commit_time)
 
         commit_time += 1
         nums[commit_num] = commit_obj.id
         object_store.add_object(commit_obj)
-        object_store.add_object(tree)
         commits.append(commit_obj)
 
     return commits
-- 
1.7.3.1



References