浏览代码

diff_tree: Factor out _is_tree function for TreeEntry objects.

Change-Id: I2cbf009a8d63d3a1b0c23adb6855c5757c133f03
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Dave Borowitz 14 年之前
父节点
当前提交
cf2e6ea5e4
共有 2 个文件被更改,包括 19 次插入2 次删除
  1. 9 2
      dulwich/diff_tree.py
  2. 10 0
      dulwich/tests/test_diff_tree.py

+ 9 - 2
dulwich/diff_tree.py

@@ -104,6 +104,13 @@ def _merge_entries(path, tree1, tree2):
     return result
 
 
+def _is_tree(entry):
+    mode = entry.mode
+    if mode is None:
+        return False
+    return stat.S_ISDIR(mode)
+
+
 def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
     """Recursively walk all the entries of two trees.
 
@@ -124,8 +131,8 @@ def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
     todo = [(TreeEntry('', mode1, tree1_id), TreeEntry('', mode2, tree2_id))]
     while todo:
         entry1, entry2 = todo.pop()
-        is_tree1 = entry1.mode and stat.S_ISDIR(entry1.mode)
-        is_tree2 = entry2.mode and stat.S_ISDIR(entry2.mode)
+        is_tree1 = _is_tree(entry1)
+        is_tree2 = _is_tree(entry2)
         if prune_identical and is_tree1 and is_tree2 and entry1 == entry2:
             continue
 

+ 10 - 0
dulwich/tests/test_diff_tree.py

@@ -30,6 +30,7 @@ from dulwich.diff_tree import (
     _similarity_score,
     _tree_change_key,
     RenameDetector,
+    _is_tree,
     )
 from dulwich.index import (
     commit_tree,
@@ -120,6 +121,15 @@ class TreeChangesTest(DiffTestCase):
           (('c', 0100755, blob_c2.id), (None, None, None)),
           ], _merge_entries('', tree2, tree1))
 
+    def test_is_tree(self):
+        self.assertFalse(_is_tree(TreeEntry(None, None, None)))
+        self.assertFalse(_is_tree(TreeEntry('a', 0100644, 'a' * 40)))
+        self.assertFalse(_is_tree(TreeEntry('a', 0100755, 'a' * 40)))
+        self.assertFalse(_is_tree(TreeEntry('a', 0120000, 'a' * 40)))
+        self.assertTrue(_is_tree(TreeEntry('a', 0040000, 'a' * 40)))
+        self.assertRaises(TypeError, _is_tree, TreeEntry('a', 'x', 'a' * 40))
+        self.assertRaises(AttributeError, _is_tree, 1234)
+
     def assertChangesEqual(self, expected, tree1, tree2, **kwargs):
         actual = list(tree_changes(self.store, tree1.id, tree2.id, **kwargs))
         self.assertEqual(expected, actual)