From 7a8ba96c29b66e0c1d11c6bc885ac6efbd06a115 Mon Sep 17 00:00:00 2001 From: Stefan Gangefors Date: Tue, 27 Mar 2018 17:36:15 +0200 Subject: [PATCH 1/2] Enable git diff-tree with empty second tree-ish param Executing `git diff-tree` and omitting the second tree-ish argument wasn't possible before. This commit enables this behaviour by allowing the caller to send in an empty string. Allowing an empty string keeps backwards compatibility for all other allowed input types. --- AUTHORS | 1 + git/diff.py | 4 +++- git/test/test_diff.py | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index b5f0ebdc1..6b78aeefd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -26,5 +26,6 @@ Contributors are: -Mikuláš Poul -Charles Bouchard-Légaré -Yaroslav Halchenko +-Stefan Gangefors Portions derived from other open source works and are clearly marked. diff --git a/git/diff.py b/git/diff.py index d7221ac7d..bf56169c4 100644 --- a/git/diff.py +++ b/git/diff.py @@ -82,6 +82,7 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): :param other: Is the item to compare us with. If None, we will be compared to the working tree. + If empty string, it will be compared to it's parent with diff-tree. If Treeish, it will be compared against the respective tree If Index ( type ), it will be compared against the index. If git.NULL_TREE, it will compare against the empty tree. @@ -132,7 +133,8 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): diff_cmd = self.repo.git.diff_tree elif other is not None: args.insert(0, '-r') # recursive diff-tree - args.insert(0, other) + if other != '': + args.insert(0, other) diff_cmd = self.repo.git.diff_tree args.insert(0, self) diff --git a/git/test/test_diff.py b/git/test/test_diff.py index d21dde624..8d3ee63e3 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -266,3 +266,17 @@ def test_diff_interface(self): cp = c.parents[0] diff_index = c.diff(cp, ["does/not/exist"]) self.assertEqual(len(diff_index), 0) + + def test_diff_tree_empty_other(self): + initial_commit = self.rorepo.commit('9066712dca21ef35c534e068f2cc4fefdccf1ea3') + + # Test that we can use an empty string as other in a diff. + # It should do a diff-tree without the second tree-ish param + diff_index = initial_commit.diff('') + self.assertEqual(len(diff_index), 16) + self.assertEqual(diff_index[0].change_type, 'M') + self.assertTrue(diff_index[12].deleted_file) + self.assertTrue(diff_index[14].renamed) + self.assertEqual(diff_index[14].rename_from, 'test/asserts.py') + self.assertEqual(diff_index[14].rename_to, 'test/testlib/asserts.py') + self.assertEqual(diff_index[15].change_type, 'A') From 4669d6d1fcc67d64f55fe850cd6c43893d44836d Mon Sep 17 00:00:00 2001 From: Stefan Gangefors Date: Tue, 27 Mar 2018 17:43:28 +0200 Subject: [PATCH 2/2] Fix docstring for git.diff.diff() --- git/diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/diff.py b/git/diff.py index bf56169c4..a69c0926d 100644 --- a/git/diff.py +++ b/git/diff.py @@ -81,7 +81,7 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): :param other: Is the item to compare us with. - If None, we will be compared to the working tree. + If None, it will be compared to the working tree. If empty string, it will be compared to it's parent with diff-tree. If Treeish, it will be compared against the respective tree If Index ( type ), it will be compared against the index.