def test_tree_diff_submodule(self): f = BytesIO() store = MemoryObjectStore() tree1 = Tree() tree1.add( b"asubmodule", S_IFGITLINK, b"06d0bdd9e2e20377b3180e4986b14c8549b393e4", ) tree2 = Tree() tree2.add( b"asubmodule", S_IFGITLINK, b"cc975646af69f279396d4d5e1379ac6af80ee637", ) store.add_objects([(o, None) for o in [tree1, tree2]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual( [ b"diff --git a/asubmodule b/asubmodule", b"index 06d0bdd..cc97564 160000", b"--- a/asubmodule", b"+++ b/asubmodule", b"@@ -1 +1 @@", b"-Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4", b"+Subproject commit cc975646af69f279396d4d5e1379ac6af80ee637", ], f.getvalue().splitlines(), )
def logsummary(repo=".", paths=None, outstream=sys.stdout, max_entries=None, reverse=False, stats=False): """Write commit logs with optional diff stat summaries Args: repo: Path to repository paths: Optional set of specific paths to print entries for outstream: Stream to write log output to reverse: Reverse order in which entries are printed max_entries: Optional maximum number of entries to display stats: Print diff stats """ with open_repo_closing(repo) as r: walker = r.get_walker(max_entries=max_entries, paths=paths, reverse=reverse) for entry in walker: def decode(x): return commit_decode(entry.commit, x) print_commit(entry.commit, decode, outstream) if stats: commit = entry.commit if commit.parents: parent_commit = r[commit.parents[0]] base_tree = parent_commit.tree else: base_tree = None adiff = b"" with BytesIO() as diffstream: write_tree_diff( diffstream, r.object_store, base_tree, commit.tree) diffstream.seek(0) adiff = diffstream.getvalue() dsum = diffstat(adiff.split(b'\n')) outstream.write(dsum.decode('utf-8')) outstream.write("\n\n")
def _get_patch_set(self, old_commit, new_commit): patch_diff = BytesIO() write_tree_diff(patch_diff, self.repo.object_store, old_commit.tree, new_commit.tree) patch_diff.seek(0) patch = PatchSet(patch_diff, encoding="utf-8") return patch
def diff(self, revision): commit = self.repo[revision] f = StringIO() if len(commit.parents) == 0: parent_tree = Tree().id else: parent_tree = self.store[commit.parents[0]].tree write_tree_diff(f, self.store, parent_tree, commit.tree) return (self._revision_from_commit(commit), f.getvalue())
def classic_tree_diff(object_store, old_tree, new_tree, filter_binary=None): """Does a classic diff and returns the output in a buffer """ output = StringIO() # Write to output (our string) patch.write_tree_diff(output, object_store, old_tree, new_tree) return output.getvalue()
def show_commit(repo, commit, outstream=sys.stdout): """Show a commit to a stream. :param repo: A `Repo` object :param commit: A `Commit` object :param outstream: Stream to write to """ print_commit(commit, outstream) parent_commit = repo[commit.parents[0]] write_tree_diff(outstream, repo.object_store, parent_commit.tree, commit.tree)
def diff_tree(repo, old_tree, new_tree, outstream=OUTPUT_STREAM): """Compares the content and mode of blobs found via two tree objects. :param repo: Path to repository :param old_tree: Id of old tree :param new_tree: Id of new tree :param outstream: Stream to write to """ with open_repo_closing(repo) as r: write_tree_diff(outstream, r.object_store, old_tree, new_tree)
def diff(self, rev_a: str, rev_b: str, binary=False) -> str: from dulwich.patch import write_tree_diff commit_a = self.repo[os.fsencode(rev_a)] commit_b = self.repo[os.fsencode(rev_b)] buf = BytesIO() write_tree_diff(buf, self.repo.object_store, commit_a.tree, commit_b.tree) return buf.getvalue().decode("utf-8")
def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout): """Compares the content and mode of blobs found via two tree objects. :param repo: Path to repository :param old_tree: Id of old tree :param new_tree: Id of new tree :param outstream: Stream to write to """ with open_repo_closing(repo) as r: write_tree_diff(outstream, r.object_store, old_tree, new_tree)
def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout): """Compares the content and mode of blobs found via two tree objects. :param repo: Path to repository :param old_tree: Id of old tree :param new_tree: Id of new tree :param outstream: Stream to write to """ r = open_repo(repo) write_tree_diff(outstream, r.object_store, old_tree, new_tree)
def show_commit(repo, commit, decode, outstream=OUTPUT_STREAM): """Show a commit to a stream. :param repo: A `Repo` object :param commit: A `Commit` object :param decode: Function for decoding bytes to unicode string :param outstream: Stream to write to """ print_commit(commit, decode=decode, outstream=outstream) parent_commit = repo[commit.parents[0]] write_tree_diff(outstream, repo.object_store, parent_commit.tree, commit.tree)
def show_commit(repo, commit, decode, outstream=sys.stdout): """Show a commit to a stream. :param repo: A `Repo` object :param commit: A `Commit` object :param decode: Function for decoding bytes to unicode string :param outstream: Stream to write to """ print_commit(commit, decode=decode, outstream=outstream) parent_commit = repo[commit.parents[0]] write_tree_diff(outstream, repo.object_store, parent_commit.tree, commit.tree)
def test_tree_diff(self): f = BytesIO() store = MemoryObjectStore() added = Blob.from_string(b"add\n") removed = Blob.from_string(b"removed\n") changed1 = Blob.from_string(b"unchanged\nremoved\n") changed2 = Blob.from_string(b"unchanged\nadded\n") unchanged = Blob.from_string(b"unchanged\n") tree1 = Tree() tree1.add(b"removed.txt", 0o644, removed.id) tree1.add(b"changed.txt", 0o644, changed1.id) tree1.add(b"unchanged.txt", 0o644, changed1.id) tree2 = Tree() tree2.add(b"added.txt", 0o644, added.id) tree2.add(b"changed.txt", 0o644, changed2.id) tree2.add(b"unchanged.txt", 0o644, changed1.id) store.add_objects([(o, None) for o in [ tree1, tree2, added, removed, changed1, changed2, unchanged, ]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual( [ b"diff --git a/added.txt b/added.txt", b"new file mode 644", b"index 0000000..76d4bb8", b"--- /dev/null", b"+++ b/added.txt", b"@@ -0,0 +1 @@", b"+add", b"diff --git a/changed.txt b/changed.txt", b"index bf84e48..1be2436 644", b"--- a/changed.txt", b"+++ b/changed.txt", b"@@ -1,2 +1,2 @@", b" unchanged", b"-removed", b"+added", b"diff --git a/removed.txt b/removed.txt", b"deleted file mode 644", b"index 2c3f0b3..0000000", b"--- a/removed.txt", b"+++ /dev/null", b"@@ -1 +0,0 @@", b"-removed", ], f.getvalue().splitlines(), )
def run(self, args): opts, args = getopt(args, "", []) if args == []: print("Usage: dulwich diff COMMITID") sys.exit(1) r = Repo(".") commit_id = args[0] commit = r[commit_id] parent_commit = r[commit.parents[0]] write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
def test_tree_diff(self): f = StringIO() store = MemoryObjectStore() added = Blob.from_string("add\n") removed = Blob.from_string("removed\n") changed1 = Blob.from_string("unchanged\nremoved\n") changed2 = Blob.from_string("unchanged\nadded\n") unchanged = Blob.from_string("unchanged\n") tree1 = Tree() tree1.add(0644, "removed.txt", removed.id) tree1.add(0644, "changed.txt", changed1.id) tree1.add(0644, "unchanged.txt", changed1.id) tree2 = Tree() tree2.add(0644, "added.txt", added.id) tree2.add(0644, "changed.txt", changed2.id) tree1.add(0644, "unchanged.txt", changed1.id) store.add_objects([(o, None) for o in [ tree1, tree2, added, removed, changed1, changed2, unchanged]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEquals([ 'diff --git a/changed.txt b/changed.txt', 'index bf84e48..1be2436 644', '--- a/changed.txt', '+++ b/changed.txt', '@@ -1,2 +1,2 @@', ' unchanged', '-removed', '+added', 'diff --git a/removed.txt /dev/null', 'deleted mode 644', 'index 2c3f0b3..e69de29', '--- a/removed.txt', '+++ /dev/null', '@@ -1,1 +1,0 @@', '-removed', 'diff --git a/unchanged.txt /dev/null', 'deleted mode 644', 'index bf84e48..e69de29', '--- a/unchanged.txt', '+++ /dev/null', '@@ -1,2 +1,0 @@', '-unchanged', '-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())
def show(repo=".", committish=None, outstream=sys.stdout): """Print the changes in a commit. :param repo: Path to repository :param committish: Commit to write :param outstream: Stream to write to """ if committish is None: committish = "HEAD" r = open_repo(repo) commit = r[committish] parent_commit = r[commit.parents[0]] print_commit(commit, outstream) write_tree_diff(outstream, r.object_store, parent_commit.tree, commit.tree)
def classic_tree_diff(object_store, old_tree, new_tree): """Does a classic diff and returns the output in a buffer """ output = StringIO() # Write to output (our string) patch.write_tree_diff( output, object_store, old_tree, new_tree ) return output.getvalue()
def test_tree_diff(self): f = BytesIO() store = MemoryObjectStore() added = Blob.from_string(b"add\n") removed = Blob.from_string(b"removed\n") changed1 = Blob.from_string(b"unchanged\nremoved\n") changed2 = Blob.from_string(b"unchanged\nadded\n") unchanged = Blob.from_string(b"unchanged\n") tree1 = Tree() tree1.add(b"removed.txt", 0o644, removed.id) tree1.add(b"changed.txt", 0o644, changed1.id) tree1.add(b"unchanged.txt", 0o644, changed1.id) tree2 = Tree() tree2.add(b"added.txt", 0o644, added.id) tree2.add(b"changed.txt", 0o644, changed2.id) tree2.add(b"unchanged.txt", 0o644, changed1.id) store.add_objects([ (o, None) for o in [tree1, tree2, added, removed, changed1, changed2, unchanged] ]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual([ b'diff --git /dev/null b/added.txt', b'new mode 644', b'index 0000000..76d4bb8 644', b'--- /dev/null', b'+++ b/added.txt', b'@@ -1,0 +1,1 @@', b'+add', b'diff --git a/changed.txt b/changed.txt', b'index bf84e48..1be2436 644', b'--- a/changed.txt', b'+++ b/changed.txt', b'@@ -1,2 +1,2 @@', b' unchanged', b'-removed', b'+added', b'diff --git a/removed.txt /dev/null', b'deleted mode 644', b'index 2c3f0b3..0000000', b'--- a/removed.txt', b'+++ /dev/null', b'@@ -1,1 +1,0 @@', b'-removed', ], f.getvalue().splitlines())
def test_tree_diff(self): f = BytesIO() store = MemoryObjectStore() added = Blob.from_string(b"add\n") removed = Blob.from_string(b"removed\n") changed1 = Blob.from_string(b"unchanged\nremoved\n") changed2 = Blob.from_string(b"unchanged\nadded\n") unchanged = Blob.from_string(b"unchanged\n") tree1 = Tree() tree1.add(b"removed.txt", 0o644, removed.id) tree1.add(b"changed.txt", 0o644, changed1.id) tree1.add(b"unchanged.txt", 0o644, changed1.id) tree2 = Tree() tree2.add(b"added.txt", 0o644, added.id) tree2.add(b"changed.txt", 0o644, changed2.id) tree2.add(b"unchanged.txt", 0o644, changed1.id) store.add_objects([(o, None) for o in [tree1, tree2, added, removed, changed1, changed2, unchanged]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual( [ b"diff --git /dev/null b/added.txt", b"new mode 644", b"index 0000000..76d4bb8 644", b"--- /dev/null", b"+++ b/added.txt", b"@@ -1,0 +1,1 @@", b"+add", b"diff --git a/changed.txt b/changed.txt", b"index bf84e48..1be2436 644", b"--- a/changed.txt", b"+++ b/changed.txt", b"@@ -1,2 +1,2 @@", b" unchanged", b"-removed", b"+added", b"diff --git a/removed.txt /dev/null", b"deleted mode 644", b"index 2c3f0b3..0000000", b"--- a/removed.txt", b"+++ /dev/null", b"@@ -1,1 +1,0 @@", b"-removed", ], f.getvalue().splitlines(), )
def test_tree_diff(self): f = BytesIO() store = MemoryObjectStore() added = Blob.from_string(b"add\n") removed = Blob.from_string(b"removed\n") changed1 = Blob.from_string(b"unchanged\nremoved\n") changed2 = Blob.from_string(b"unchanged\nadded\n") unchanged = Blob.from_string(b"unchanged\n") tree1 = Tree() tree1.add(b"removed.txt", 0o644, removed.id) tree1.add(b"changed.txt", 0o644, changed1.id) tree1.add(b"unchanged.txt", 0o644, changed1.id) tree2 = Tree() tree2.add(b"added.txt", 0o644, added.id) tree2.add(b"changed.txt", 0o644, changed2.id) tree2.add(b"unchanged.txt", 0o644, changed1.id) store.add_objects([(o, None) for o in [ tree1, tree2, added, removed, changed1, changed2, unchanged]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual([ b'diff --git a/added.txt b/added.txt', b'new file mode 644', b'index 0000000..76d4bb8', b'--- /dev/null', b'+++ b/added.txt', b'@@ -0,0 +1 @@', b'+add', b'diff --git a/changed.txt b/changed.txt', b'index bf84e48..1be2436 644', b'--- a/changed.txt', b'+++ b/changed.txt', b'@@ -1,2 +1,2 @@', b' unchanged', b'-removed', b'+added', b'diff --git a/removed.txt b/removed.txt', b'deleted file mode 644', b'index 2c3f0b3..0000000', b'--- a/removed.txt', b'+++ /dev/null', b'@@ -1 +0,0 @@', b'-removed', ], f.getvalue().splitlines())
def show_commit(repo, commit, decode, outstream=sys.stdout): """Show a commit to a stream. :param repo: A `Repo` object :param commit: A `Commit` object :param decode: Function for decoding bytes to unicode string :param outstream: Stream to write to """ print_commit(commit, decode=decode, outstream=outstream) if commit.parents: parent_commit = repo[commit.parents[0]] base_tree = parent_commit.tree else: base_tree = None diffstream = BytesIO() write_tree_diff(diffstream, repo.object_store, base_tree, commit.tree) diffstream.seek(0) outstream.write(diffstream.getvalue().decode( commit.encoding or DEFAULT_ENCODING, 'replace'))
def test_tree_diff_submodule(self): f = BytesIO() store = MemoryObjectStore() tree1 = Tree() tree1.add("asubmodule", S_IFGITLINK, "06d0bdd9e2e20377b3180e4986b14c8549b393e4") tree2 = Tree() tree2.add("asubmodule", S_IFGITLINK, "cc975646af69f279396d4d5e1379ac6af80ee637") store.add_objects([(o, None) for o in [tree1, tree2]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual([ 'diff --git a/asubmodule b/asubmodule', 'index 06d0bdd..cc97564 160000', '--- a/asubmodule', '+++ b/asubmodule', '@@ -1,1 +1,1 @@', '-Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4', '+Submodule commit cc975646af69f279396d4d5e1379ac6af80ee637', ], f.getvalue().splitlines())
def test_tree_diff_submodule(self): f = BytesIO() store = MemoryObjectStore() tree1 = Tree() tree1.add(b"asubmodule", S_IFGITLINK, Sha1Sum("06d0bdd9e2e20377b3180e4986b14c8549b393e4")) tree2 = Tree() tree2.add(b"asubmodule", S_IFGITLINK, Sha1Sum("cc975646af69f279396d4d5e1379ac6af80ee637")) store.add_objects([(o, None) for o in [tree1, tree2]]) write_tree_diff(f, store, tree1.id, tree2.id) self.assertEqual([ b'diff --git a/asubmodule b/asubmodule', b'index 06d0bdd..cc97564 160000', b'--- a/asubmodule', b'+++ b/asubmodule', b'@@ -1,1 +1,1 @@', b'-Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4', b'+Submodule commit cc975646af69f279396d4d5e1379ac6af80ee637', ], f.getvalue().splitlines())
def show_commit(repo, commit, decode, outstream=sys.stdout): """Show a commit to a stream. Args: repo: A `Repo` object commit: A `Commit` object decode: Function for decoding bytes to unicode string outstream: Stream to write to """ print_commit(commit, decode=decode, outstream=outstream) if commit.parents: parent_commit = repo[commit.parents[0]] base_tree = parent_commit.tree else: base_tree = None diffstream = BytesIO() write_tree_diff( diffstream, repo.object_store, base_tree, commit.tree) diffstream.seek(0) outstream.write(commit_decode(commit, diffstream.getvalue()))
def toDict(self, short = False): authorDate = GitCommit._toDate( self.commit.author_time, self.commit.author_timezone ) commitDate = GitCommit._toDate( self.commit.commit_time, self.commit.commit_timezone ) data = { "sha" : self.commit.id, "message" : self.commit.message, "tree" : self.commit.tree, "parents" : self.commit.parents, "author" : { "name" : self.commit.author, "date" : authorDate }, "committer" : { "name" : self.commit.committer, "date" : commitDate }, } if short: return data import StringIO changes = StringIO.StringIO() write_tree_diff( changes, self._repo.object_store, self._repo.commit(self.commit.parents[0]).tree, self.commit.tree ) data['changes'] = changes.getvalue() changes.close() return data
def show_commit(repo, commit, decode, outstream=sys.stdout): """Show a commit to a stream. :param repo: A `Repo` object :param commit: A `Commit` object :param decode: Function for decoding bytes to unicode string :param outstream: Stream to write to """ print_commit(commit, decode=decode, outstream=outstream) if commit.parents: parent_commit = repo[commit.parents[0]] base_tree = parent_commit.tree else: base_tree = None diffstream = BytesIO() write_tree_diff( diffstream, repo.object_store, base_tree, commit.tree) diffstream.seek(0) outstream.write( diffstream.getvalue().decode( commit.encoding or DEFAULT_ENCODING, 'replace'))
def generate_diff_from_checkpoints(localRepo, bookid, leftchkpoint, rightchkpoint): repo_home = pathof(localRepo) repo_home = repo_home.replace("/", os.sep) repo_path = os.path.join(repo_home, "epub_" + bookid) success = True if os.path.exists(repo_path): os.chdir(repo_path) with open_repo_closing(".") as r: tags = r.refs.as_dict(b"refs/tags") commit1 = r[r[tags[utf8_str(leftchkpoint)]].object[1]] commit2 = r[r[tags[utf8_str(rightchkpoint)]].object[1]] output = io.BytesIO() try: write_tree_diff(output, r.object_store, commit1.tree, commit2.tree) except Exception as e: print("diff failed in python") print(str(e)) success = False pass if success: return output.getvalue() return ''
"""Diff. This trivial script demonstrates how to extract the unified diff for a single commit in a local repository. Example usage: python examples/diff.py """ import sys from dulwich.repo import Repo from dulwich.patch import write_tree_diff REPO_PATH = "." COMMIT_ID = b"a6602654997420bcfd0bee2a0563d9416afe34b4" REPOSITORY = Repo(REPO_PATH) commit = REPOSITORY[COMMIT_ID] parent_commit = REPOSITORY[commit.parents[0]] outstream = getattr(sys.stdout, 'buffer', sys.stdout) write_tree_diff( outstream, REPOSITORY.object_store, parent_commit.tree, commit.tree)
#!/usr/bin/python # This trivial script demonstrates how to extract the unified diff for a single # commit in a local repository. # # Example usage: # python examples/diff.py from dulwich.repo import Repo from dulwich.patch import write_tree_diff import sys repo_path = "." commit_id = "a6602654997420bcfd0bee2a0563d9416afe34b4" r = Repo(repo_path) commit = r[commit_id] parent_commit = r[commit.parents[0]] write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
#!/usr/bin/python # This trivial script demonstrates how to extract the unified diff for a single # commit in a local repository. # # Example usage: # python examples/diff.py from dulwich.repo import Repo from dulwich.patch import write_tree_diff import sys repo_path = "." commit_id = b"a6602654997420bcfd0bee2a0563d9416afe34b4" r = Repo(repo_path) commit = r[commit_id] parent_commit = r[commit.parents[0]] outstream = getattr(sys.stdout, 'buffer', sys.stdout) write_tree_diff(outstream, r.object_store, parent_commit.tree, commit.tree)