Changes to 3 files · Browse files at 6c50e44d7932 Showing diff from parent 2004e1021756 Diff from another changeset...
@@ -28,6 +28,9 @@ import tempfile
import urllib2
+from dulwich.diff_tree import (
+ tree_changes,
+ )
from dulwich.errors import (
NotTreeError,
)
@@ -129,52 +132,14 @@ :param object_store: Object store to use for retrieving tree contents
:param tree: SHA1 of the root tree
:param want_unchanged: Whether unchanged files should be reported
- :return: Iterator over tuples with (oldpath, newpath), (oldmode, newmode), (oldsha, newsha)
- """
- todo = set([(source, target, "")])
- while todo:
- (sid, tid, path) = todo.pop()
- if sid is not None:
- stree = self[sid]
- else:
- stree = {}
- if tid is not None:
- ttree = self[tid]
- else:
- ttree = {}
- for name, oldmode, oldhexsha in stree.iteritems():
- oldchildpath = posixpath.join(path, name)
- try:
- (newmode, newhexsha) = ttree[name]
- newchildpath = oldchildpath
- except KeyError:
- newmode = None
- newhexsha = None
- newchildpath = None
- if (want_unchanged or oldmode != newmode or
- oldhexsha != newhexsha):
- if stat.S_ISDIR(oldmode):
- if newmode is None or stat.S_ISDIR(newmode):
- todo.add((oldhexsha, newhexsha, oldchildpath))
- else:
- # entry became a file
- todo.add((oldhexsha, None, oldchildpath))
- yield ((None, newchildpath), (None, newmode), (None, newhexsha))
- else:
- if newmode is not None and stat.S_ISDIR(newmode):
- # entry became a dir
- yield ((oldchildpath, None), (oldmode, None), (oldhexsha, None))
- todo.add((None, newhexsha, newchildpath))
- else:
- yield ((oldchildpath, newchildpath), (oldmode, newmode), (oldhexsha, newhexsha))
-
- for name, newmode, newhexsha in ttree.iteritems():
- childpath = posixpath.join(path, name)
- if not name in stree:
- if not stat.S_ISDIR(newmode):
- yield ((None, childpath), (None, newmode), (None, newhexsha))
- else:
- todo.add((None, newhexsha, childpath))
+ :return: Iterator over tuples with
+ (oldpath, newpath), (oldmode, newmode), (oldsha, newsha)
+ """
+ for change in tree_changes(self, source, target,
+ want_unchanged=want_unchanged):
+ yield ((change.old.path, change.new.path),
+ (change.old.mode, change.new.mode),
+ (change.old.sha, change.new.sha))
def iter_tree_contents(self, tree_id, include_trees=False):
"""Iterate the contents of a tree and all subtrees.
|
@@ -89,6 +89,25 @@ r = self.store[testobject.id]
self.assertEquals(r, testobject)
+ def test_tree_changes(self):
+ blob_a1 = make_object(Blob, data='a1')
+ blob_a2 = make_object(Blob, data='a2')
+ blob_b = make_object(Blob, data='b')
+ for blob in [blob_a1, blob_a2, blob_b]:
+ self.store.add_object(blob)
+
+ blobs_1 = [('a', blob_a1.id, 0100644), ('b', blob_b.id, 0100644)]
+ tree1_id = commit_tree(self.store, blobs_1)
+ blobs_2 = [('a', blob_a2.id, 0100644), ('b', blob_b.id, 0100644)]
+ tree2_id = commit_tree(self.store, blobs_2)
+ change_a = (('a', 'a'), (0100644, 0100644), (blob_a1.id, blob_a2.id))
+ self.assertEquals([change_a],
+ list(self.store.tree_changes(tree1_id, tree2_id)))
+ self.assertEquals(
+ [change_a, (('b', 'b'), (0100644, 0100644), (blob_b.id, blob_b.id))],
+ list(self.store.tree_changes(tree1_id, tree2_id,
+ want_unchanged=True)))
+
def test_iter_tree_contents(self):
blob_a = make_object(Blob, data='a')
blob_b = make_object(Blob, data='b')
|
@@ -267,6 +267,13 @@ tree1, tree2, added, removed, changed1, changed2, unchanged]])
write_tree_diff(f, store, tree1.id, tree2.id)
self.assertEquals([
+ 'diff --git /dev/null b/added.txt',
+ 'new mode 644',
+ 'index e69de29..76d4bb8 644',
+ '--- /dev/null',
+ '+++ b/added.txt',
+ '@@ -1,0 +1,1 @@',
+ '+add',
'diff --git a/changed.txt b/changed.txt',
'index bf84e48..1be2436 644',
'--- a/changed.txt',
@@ -282,11 +289,4 @@ '+++ /dev/null',
'@@ -1,1 +1,0 @@',
'-removed',
- 'diff --git /dev/null b/added.txt',
- 'new mode 644',
- 'index e69de29..76d4bb8 644',
- '--- /dev/null',
- '+++ b/added.txt',
- '@@ -1,0 +1,1 @@',
- '+add'
- ], f.getvalue().splitlines())
+ ], f.getvalue().splitlines())
|
Loading...