def test_blob_add(self): f = BytesIO() write_blob_diff(f, (None, None, None), (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n"))) self.assertEqual([ b'diff --git /dev/null b/bar.txt', b'new mode 644', b'index 0000000..a116b51 644', b'--- /dev/null', b'+++ b/bar.txt', b'@@ -1,0 +1,2 @@', b'+new', b'+same' ], f.getvalue().splitlines())
def test_blob_remove(self): f = BytesIO() write_blob_diff(f, ("bar.txt", 0o644, Blob.from_string("new\nsame\n")), (None, None, None)) self.assertEqual([ '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_blob_diff(self): f = BytesIO() write_blob_diff(f, ("foo.txt", 0o644, Blob.from_string("old\nsame\n")), ("bar.txt", 0o644, Blob.from_string("new\nsame\n"))) self.assertEqual([ "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_blob_remove(self): f = BytesIO() write_blob_diff(f, ("bar.txt", 0o644, Blob.from_string(b"new\nsame\n")), (None, None, None)) self.assertEqual([ b'diff --git a/bar.txt /dev/null', b'deleted mode 644', b'index a116b51..0000000', b'--- a/bar.txt', b'+++ /dev/null', b'@@ -1,2 +1,0 @@', b'-new', b'-same' ], f.getvalue().splitlines())
def test_blob_add(self): f = BytesIO() write_blob_diff(f, (None, None, None), ("bar.txt", 0o644, Blob.from_string(b"new\nsame\n"))) self.assertEqual([ b'diff --git /dev/null b/bar.txt', b'new mode 644', b'index 0000000..a116b51 644', b'--- /dev/null', b'+++ b/bar.txt', b'@@ -1,0 +1,2 @@', b'+new', b'+same' ], f.getvalue().splitlines())
def test_blob_diff(self): f = BytesIO() write_blob_diff(f, ("foo.txt", 0o644, Blob.from_string(b"old\nsame\n")), ("bar.txt", 0o644, Blob.from_string(b"new\nsame\n"))) self.assertEqual([ b"diff --git a/foo.txt b/bar.txt", b"index 3b0f961..a116b51 644", b"--- a/foo.txt", b"+++ b/bar.txt", b"@@ -1,2 +1,2 @@", b"-old", b"+new", b" same" ], f.getvalue().splitlines())
def _show_diff(self, specific_files, extra_trees): from dulwich.patch import write_blob_diff iterator = self.new_tree.iter_changes(self.old_tree, specific_files=specific_files, extra_trees=extra_trees, require_versioned=True) has_changes = 0 def get_encoded_path(path): if path is not None: return path.encode(self.path_encoding, "replace") def get_file_mode(tree, path, kind, executable): if path is None: return 0 return object_mode(kind, executable) def get_blob(present, tree, path): if present: with tree.get_file(path) as f: return Blob.from_string(f.read()) else: return None trees = (self.old_tree, self.new_tree) for (file_id, paths, changed_content, versioned, parent, name, kind, executable) in iterator: # The root does not get diffed, and items with no known kind (that # is, missing) in both trees are skipped as well. if parent == (None, None) or kind == (None, None): continue path_encoded = (get_encoded_path(paths[0]), get_encoded_path(paths[1])) present = ((kind[0] not in (None, 'directory')), (kind[1] not in (None, 'directory'))) if not present[0] and not present[1]: continue contents = (get_blob(present[0], trees[0], paths[0]), get_blob(present[1], trees[1], paths[1])) renamed = (parent[0], name[0]) != (parent[1], name[1]) mode = (get_file_mode(trees[0], path_encoded[0], kind[0], executable[0]), get_file_mode(trees[1], path_encoded[1], kind[1], executable[1])) write_blob_diff(self.to_file, (path_encoded[0], mode[0], contents[0]), (path_encoded[1], mode[1], contents[1])) has_changes |= (changed_content or renamed) return has_changes
def test_blob_remove(self): f = BytesIO() write_blob_diff(f, (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")), (None, None, None)) self.assertEqual( [ b"diff --git a/bar.txt /dev/null", b"deleted mode 644", b"index a116b51..0000000", b"--- a/bar.txt", b"+++ /dev/null", b"@@ -1,2 +1,0 @@", b"-new", b"-same", ], f.getvalue().splitlines(), )
def test_blob_add(self): f = BytesIO() write_blob_diff(f, (None, None, None), (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n"))) self.assertEqual( [ b"diff --git /dev/null b/bar.txt", b"new mode 644", b"index 0000000..a116b51 644", b"--- /dev/null", b"+++ b/bar.txt", b"@@ -1,0 +1,2 @@", b"+new", b"+same", ], f.getvalue().splitlines(), )
def test_blob_remove(self): f = BytesIO() write_blob_diff( f, (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")), (None, None, None), ) self.assertEqual( [ b"diff --git a/bar.txt b/bar.txt", b"deleted file mode 644", b"index a116b51..0000000", b"--- a/bar.txt", b"+++ /dev/null", b"@@ -1,2 +0,0 @@", b"-new", b"-same", ], f.getvalue().splitlines(), )
def test_blob_add(self): f = BytesIO() write_blob_diff( f, (None, None, None), (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")), ) self.assertEqual( [ b"diff --git a/bar.txt b/bar.txt", b"new file mode 644", b"index 0000000..a116b51", b"--- /dev/null", b"+++ b/bar.txt", b"@@ -0,0 +1,2 @@", b"+new", b"+same", ], f.getvalue().splitlines(), )
def blob_diff(object_store, *args, **kwargs): fd = StringIO() patch.write_blob_diff(fd, *args, **kwargs) return fd.getvalue()