dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00288
[PATCH 10/28] diff: Add TreeEntry.add() and delete() factory functions.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
Change-Id: I7272a497bf4c0ca97fb1c599dfe80601a0be5c6d
---
dulwich/diff.py | 9 ++++++++-
dulwich/tests/test_diff.py | 40 +++++++++++++++++-----------------------
2 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/dulwich/diff.py b/dulwich/diff.py
index 00ed08a..98668ce 100644
--- a/dulwich/diff.py
+++ b/dulwich/diff.py
@@ -46,6 +46,13 @@ _MAX_SCORE = 100
class TreeChange(TreeChangeTuple):
"""Class encapsulating a single change between two trees."""
+ @classmethod
+ def add(cls, new):
+ return cls(CHANGE_ADD, _NULL_ENTRY, new)
+
+ @classmethod
+ def delete(cls, old):
+ return cls(CHANGE_DELETE, old, _NULL_ENTRY)
def _tree_entries(path, tree):
result = []
@@ -156,7 +163,7 @@ def tree_changes(store, tree1_id, tree2_id, want_unchanged=False):
if entry1 != _NULL_ENTRY and entry2 != _NULL_ENTRY:
if stat.S_IFMT(entry1.mode) != stat.S_IFMT(entry2.mode):
# File type changed: report as delete/add.
- yield TreeChange(CHANGE_DELETE, entry1, _NULL_ENTRY)
+ yield TreeChange.delete(entry1)
entry1 = _NULL_ENTRY
change_type = CHANGE_ADD
elif entry1 == entry2:
diff --git a/dulwich/tests/test_diff.py b/dulwich/tests/test_diff.py
index 260301f..dae1b9a 100644
--- a/dulwich/tests/test_diff.py
+++ b/dulwich/tests/test_diff.py
@@ -19,11 +19,8 @@
"""Tests for diff utilities."""
from dulwich.diff import (
- CHANGE_ADD,
CHANGE_MODIFY,
- CHANGE_DELETE,
CHANGE_UNCHANGED,
- _NULL_ENTRY,
TreeChange,
_merge_entries,
tree_changes,
@@ -127,12 +124,12 @@ class TreeChangesTest(TestCase):
tree = self.commit_tree([('a', blob_a, 0100644),
('x/b', blob_b, 0100755)])
self.assertChangesEqual(
- [TreeChange(CHANGE_ADD, _NULL_ENTRY, ('a', 0100644, blob_a.id)),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('x/b', 0100755, blob_b.id))],
+ [TreeChange.add(('a', 0100644, blob_a.id)),
+ TreeChange.add(('x/b', 0100755, blob_b.id))],
self.empty_tree, tree)
self.assertChangesEqual(
- [TreeChange(CHANGE_DELETE, ('a', 0100644, blob_a.id), _NULL_ENTRY),
- TreeChange(CHANGE_DELETE, ('x/b', 0100755, blob_b.id), _NULL_ENTRY)],
+ [TreeChange.delete(('a', 0100644, blob_a.id)),
+ TreeChange.delete(('x/b', 0100755, blob_b.id))],
tree, self.empty_tree)
def test_tree_changes_modify_contents(self):
@@ -158,8 +155,8 @@ class TreeChangesTest(TestCase):
tree1 = self.commit_tree([('a', blob_a1, 0100644)])
tree2 = self.commit_tree([('a', blob_a2, 0120000)])
self.assertChangesEqual(
- [TreeChange(CHANGE_DELETE, ('a', 0100644, blob_a1.id), _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('a', 0120000, blob_a2.id))],
+ [TreeChange.delete(('a', 0100644, blob_a1.id)),
+ TreeChange.add(('a', 0120000, blob_a2.id))],
tree1, tree2)
def test_tree_changes_to_tree(self):
@@ -168,8 +165,8 @@ class TreeChangesTest(TestCase):
tree1 = self.commit_tree([('a', blob_a, 0100644)])
tree2 = self.commit_tree([('a/x', blob_x, 0100644)])
self.assertChangesEqual(
- [TreeChange(CHANGE_DELETE, ('a', 0100644, blob_a.id), _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('a/x', 0100644, blob_x.id))],
+ [TreeChange.delete(('a', 0100644, blob_a.id)),
+ TreeChange.add(('a/x', 0100644, blob_x.id))],
tree1, tree2)
def test_tree_changes_complex(self):
@@ -200,14 +197,11 @@ class TreeChangesTest(TestCase):
self.assertChangesEqual(
[TreeChange(CHANGE_MODIFY, ('a', 0100644, blob_a_1.id),
('a', 0100644, blob_a_2.id)),
- TreeChange(CHANGE_DELETE, ('b/x/2', 0100644, blob_bx2_1.id),
- _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('b/y', 0100644, blob_by_2.id)),
- TreeChange(CHANGE_DELETE, ('b/y/1', 0100644, blob_by1_1.id),
- _NULL_ENTRY),
- TreeChange(CHANGE_DELETE, ('b/y/2', 0100644, blob_by2_1.id),
- _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('c', 0100644, blob_c_2.id))],
+ TreeChange.delete(('b/x/2', 0100644, blob_bx2_1.id)),
+ TreeChange.add(('b/y', 0100644, blob_by_2.id)),
+ TreeChange.delete(('b/y/1', 0100644, blob_by1_1.id)),
+ TreeChange.delete(('b/y/2', 0100644, blob_by2_1.id)),
+ TreeChange.add(('c', 0100644, blob_c_2.id))],
tree1, tree2)
def test_tree_changes_name_order(self):
@@ -226,10 +220,10 @@ class TreeChangesTest(TestCase):
])
self.assertChangesEqual(
- [TreeChange(CHANGE_DELETE, ('a', 0100644, blob.id), _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('a/x', 0100644, blob.id)),
- TreeChange(CHANGE_DELETE, ('a.', 0100644, blob.id), _NULL_ENTRY),
- TreeChange(CHANGE_ADD, _NULL_ENTRY, ('a./x', 0100644, blob.id))],
+ [TreeChange.delete(('a', 0100644, blob.id)),
+ TreeChange.add(('a/x', 0100644, blob.id)),
+ TreeChange.delete(('a.', 0100644, blob.id)),
+ TreeChange.add(('a./x', 0100644, blob.id))],
tree1, tree2)
def test_tree_changes_prune(self):
--
1.7.3.2.168.gd6b63
References