Browse Source

Add more tests for write_object_diff, NEWS entry.

Jelmer Vernooij 14 years ago
parent
commit
7dec7b20d0
3 changed files with 88 additions and 1 deletions
  1. 3 0
      NEWS
  2. 9 1
      dulwich/patch.py
  3. 76 0
      dulwich/tests/test_patch.py

+ 3 - 0
NEWS

@@ -12,6 +12,9 @@
   * BaseObjectStore.determine_wants_all no longer breaks on zero SHAs.
     (Jelmer Vernooij)
 
+  * write_tree_diff() now supports submodules.
+    (Jelmer Vernooij)
+
  IMPROVEMENTS
 
   * Sphinxified documentation. (Lukasz Balcerzak)

+ 9 - 1
dulwich/patch.py

@@ -105,8 +105,14 @@ def unified_diff(a, b, fromfile='', tofile='', n=3):
 
 def write_object_diff(f, store, (old_path, old_mode, old_id),
                                 (new_path, new_mode, new_id)):
-    """Write file contents diff.
+    """Write the diff for an object.
 
+    :param f: File-like object to write to
+    :param store: Store to retrieve objects from, if necessary
+    :param (old_path, old_mode, old_hexsha): Old file
+    :param (new_path, new_mode, new_hexsha): New file
+
+    :note: the tuple elements should be None for nonexistant files
     """
     def shortid(hexsha):
         if hexsha is None:
@@ -153,6 +159,8 @@ def write_blob_diff(f, (old_path, old_mode, old_blob),
     :param f: File-like object to write to
     :param (old_path, old_mode, old_blob): Previous file (None if nonexisting)
     :param (new_path, new_mode, new_blob): New file (None if nonexisting)
+
+    :note: The use of write_object_diff is recommended over this function.
     """
     def blob_id(blob):
         if blob is None:

+ 76 - 0
dulwich/tests/test_patch.py

@@ -33,6 +33,7 @@ from dulwich.patch import (
     git_am_patch_split,
     write_blob_diff,
     write_commit_patch,
+    write_object_diff,
     write_tree_diff,
     )
 from dulwich.tests import (
@@ -312,3 +313,78 @@ class DiffTests(TestCase):
             '-Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
             '+Submodule commit cc975646af69f279396d4d5e1379ac6af80ee637',
             ], f.getvalue().splitlines())
+
+    def test_object_diff_blob(self):
+        f = StringIO()
+        b1 = Blob.from_string("old\nsame\n")
+        b2 = Blob.from_string("new\nsame\n")
+        store = MemoryObjectStore()
+        store.add_objects([(b1, None), (b2, None)])
+        write_object_diff(f, store, ("foo.txt", 0644, b1.id),
+                                    ("bar.txt", 0644, b2.id))
+        self.assertEquals([
+            "diff --git a/foo.txt b/bar.txt",
+            "index 3b0f961..a116b51 644",
+            "--- a/foo.txt",
+            "+++ b/bar.txt",
+            "@@ -1,2 +1,2 @@",
+            "-old",
+            "+new",
+            " same"
+            ], f.getvalue().splitlines())
+
+    def test_object_diff_add_blob(self):
+        f = StringIO()
+        store = MemoryObjectStore()
+        b2 = Blob.from_string("new\nsame\n")
+        store.add_object(b2)
+        write_object_diff(f, store, (None, None, None),
+                                    ("bar.txt", 0644, b2.id))
+        self.assertEquals([
+            'diff --git /dev/null b/bar.txt',
+             'new mode 644',
+             'index 0000000..a116b51 644',
+             '--- /dev/null',
+             '+++ b/bar.txt',
+             '@@ -1,0 +1,2 @@',
+             '+new',
+             '+same'
+            ], f.getvalue().splitlines())
+
+    def test_object_diff_remove_blob(self):
+        f = StringIO()
+        b1 = Blob.from_string("new\nsame\n")
+        store = MemoryObjectStore()
+        store.add_object(b1)
+        write_object_diff(f, store, ("bar.txt", 0644, b1.id),
+                                    (None, None, None))
+        self.assertEquals([
+            'diff --git a/bar.txt /dev/null',
+            'deleted mode 644',
+            'index a116b51..0000000',
+            '--- a/bar.txt',
+            '+++ /dev/null',
+            '@@ -1,2 +1,0 @@',
+            '-new',
+            '-same'
+            ], f.getvalue().splitlines())
+
+    def test_object_diff_kind_change(self):
+        f = StringIO()
+        b1 = Blob.from_string("new\nsame\n")
+        store = MemoryObjectStore()
+        store.add_object(b1)
+        write_object_diff(f, store, ("bar.txt", 0644, b1.id),
+            ("bar.txt", 0160000, "06d0bdd9e2e20377b3180e4986b14c8549b393e4"))
+        self.assertEquals([
+            'diff --git a/bar.txt b/bar.txt',
+            'old mode 644',
+            'new mode 160000',
+            'index a116b51..06d0bdd 160000',
+            '--- a/bar.txt',
+            '+++ b/bar.txt',
+            '@@ -1,2 +1,1 @@',
+            '-new',
+            '-same',
+            '+Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
+            ], f.getvalue().splitlines())