From 59ad90694b5393ce7f6790ade9cb58c24b8028e5 Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Thu, 17 Oct 2019 22:15:45 -0500 Subject: Adding diff support for copied files, still working on test --- git/test/test_diff.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index e47b9331..079f8bea 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -112,6 +112,27 @@ class TestDiff(TestBase): self.assertEqual(diff.score, 100) self.assertEqual(len(list(diffs.iter_change_type('R'))), 1) + def test_diff_with_copied_file(self): + output = StringProcessAdapter(fixture('diff_copied_mode')) + diffs = Diff._index_from_patch_format(self.rorepo, output) + self._assert_diff_format(diffs) + + assert_equal(1, len(diffs)) + + diff = diffs[0] + print(diff) + assert_true(diff.copied_file) + assert isinstance(str(diff), str) + + output = StringProcessAdapter(fixture('diff_copied_mode_raw')) + diffs = Diff._index_from_raw_format(self.rorepo, output) + self.assertEqual(len(diffs), 1) + diff = diffs[0] + self.assertEqual(diff.change_type, 'C') + self.assertEqual(diff.score, 100) + self.assertEqual(len(list(diffs.iter_change_type('C'))), 1) + + def test_diff_with_change_in_type(self): output = StringProcessAdapter(fixture('diff_change_in_type')) diffs = Diff._index_from_patch_format(self.rorepo, output) -- cgit v1.2.1 From 71b3845807458766cd715c60a5f244836f4273b6 Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Thu, 17 Oct 2019 23:04:22 -0500 Subject: Fixed new test for copied files --- git/test/test_diff.py | 1 - 1 file changed, 1 deletion(-) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index 079f8bea..4d71443f 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -120,7 +120,6 @@ class TestDiff(TestBase): assert_equal(1, len(diffs)) diff = diffs[0] - print(diff) assert_true(diff.copied_file) assert isinstance(str(diff), str) -- cgit v1.2.1 From a3c3efd20c50b2a9db98a892b803eb285b2a4f83 Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Fri, 18 Oct 2019 10:27:38 -0500 Subject: Expanded new test for copied file --- git/test/test_diff.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index 4d71443f..e1b345b5 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -121,6 +121,8 @@ class TestDiff(TestBase): diff = diffs[0] assert_true(diff.copied_file) + assert_true(diff.a_path, u'test1.txt') + assert_true(diff.b_path, u'test2.txt') assert isinstance(str(diff), str) output = StringProcessAdapter(fixture('diff_copied_mode_raw')) @@ -129,9 +131,10 @@ class TestDiff(TestBase): diff = diffs[0] self.assertEqual(diff.change_type, 'C') self.assertEqual(diff.score, 100) + self.assertEqual(diff.a_path, u'test1.txt') + self.assertEqual(diff.b_path, u'test2.txt') self.assertEqual(len(list(diffs.iter_change_type('C'))), 1) - def test_diff_with_change_in_type(self): output = StringProcessAdapter(fixture('diff_change_in_type')) diffs = Diff._index_from_patch_format(self.rorepo, output) -- cgit v1.2.1 From a086625da1939d2ccfc0dd27e4d5d63f47c3d2c9 Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Sun, 20 Oct 2019 19:15:45 -0500 Subject: Added new test to cover the issue this fix addresses (#891) --- git/test/test_diff.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index e1b345b5..1bab4510 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -5,12 +5,15 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php import ddt +import shutil +import tempfile from git import ( Repo, GitCommandError, Diff, DiffIndex, NULL_TREE, + Submodule, ) from git.cmd import Git from git.test.lib import ( @@ -19,7 +22,6 @@ from git.test.lib import ( fixture, assert_equal, assert_true, - ) from git.test.lib import with_rw_directory @@ -29,9 +31,15 @@ import os.path as osp @ddt.ddt class TestDiff(TestBase): + def setUp(self): + self.repo_dir = tempfile.mkdtemp() + self.submodule_dir = tempfile.mkdtemp() + def tearDown(self): import gc gc.collect() + shutil.rmtree(self.repo_dir) + shutil.rmtree(self.submodule_dir) def _assert_diff_format(self, diffs): # verify that the format of the diff is sane @@ -68,7 +76,8 @@ class TestDiff(TestBase): r.git.commit(all=True, message="change on topic branch") # there must be a merge-conflict - self.failUnlessRaises(GitCommandError, r.git.cherry_pick, 'master') + with self.assertRaises(GitCommandError): + r.git.cherry_pick('master') # Now do the actual testing - this should just work self.assertEqual(len(r.index.diff(None)), 2) @@ -267,6 +276,43 @@ class TestDiff(TestBase): self.assertIsNone(diff_index[0].a_path, repr(diff_index[0].a_path)) self.assertEqual(diff_index[0].b_path, u'file with spaces', repr(diff_index[0].b_path)) + def test_diff_submodule(self): + """Test that diff is able to correctly diff commits that cover submodule changes""" + # Init a temp git repo that will be referenced as a submodule + sub = Repo.init(self.submodule_dir) + with open(f"{self.submodule_dir}/subfile", "w") as sub_subfile: + sub_subfile.write("") + sub.index.add(["subfile"]) + sub.index.commit("first commit") + + # Init a temp git repo that will incorporate the submodule + repo = Repo.init(self.repo_dir) + with open(f"{self.repo_dir}/test", "w") as foo_test: + foo_test.write("") + repo.index.add(['test']) + Submodule.add(repo, "subtest", "sub", url=f"file://{self.submodule_dir}") + repo.index.commit("first commit") + repo.create_tag('1') + + # Add a commit to the submodule + submodule = repo.submodule('subtest') + with open(f"{self.repo_dir}/sub/subfile", "w") as foo_sub_subfile: + foo_sub_subfile.write("blub") + submodule.module().index.add(["subfile"]) + submodule.module().index.commit("changed subfile") + submodule.binsha = submodule.module().head.commit.binsha + + # Commit submodule updates in parent repo + repo.index.add([submodule]) + repo.index.commit("submodule changed") + repo.create_tag('2') + + diff = repo.commit('1').diff(repo.commit('2'))[0] + # If diff is unable to find the commit hashes (looks in wrong repo) the *_blob.size + # property will be a string containing exception text, an int indicates success + self.assertIsInstance(diff.a_blob.size, int) + self.assertIsInstance(diff.b_blob.size, int) + def test_diff_interface(self): # test a few variations of the main diff routine assertion_map = {} -- cgit v1.2.1 From 44496c6370d8f9b15b953a88b33816a92096ce4d Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Sun, 20 Oct 2019 19:52:03 -0500 Subject: Removing f-strings to maintain 3.4 and 3.5 compatability --- git/test/test_diff.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index 1bab4510..56e51289 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -280,23 +280,23 @@ class TestDiff(TestBase): """Test that diff is able to correctly diff commits that cover submodule changes""" # Init a temp git repo that will be referenced as a submodule sub = Repo.init(self.submodule_dir) - with open(f"{self.submodule_dir}/subfile", "w") as sub_subfile: + with open(self.submodule_dir + "/subfile", "w") as sub_subfile: sub_subfile.write("") sub.index.add(["subfile"]) sub.index.commit("first commit") # Init a temp git repo that will incorporate the submodule repo = Repo.init(self.repo_dir) - with open(f"{self.repo_dir}/test", "w") as foo_test: + with open(self.repo_dir + "/test", "w") as foo_test: foo_test.write("") repo.index.add(['test']) - Submodule.add(repo, "subtest", "sub", url=f"file://{self.submodule_dir}") + Submodule.add(repo, "subtest", "sub", url="file://" + self.submodule_dir) repo.index.commit("first commit") repo.create_tag('1') # Add a commit to the submodule submodule = repo.submodule('subtest') - with open(f"{self.repo_dir}/sub/subfile", "w") as foo_sub_subfile: + with open(self.repo_dir + "/sub/subfile", "w") as foo_sub_subfile: foo_sub_subfile.write("blub") submodule.module().index.add(["subfile"]) submodule.module().index.commit("changed subfile") -- cgit v1.2.1 From 38c624f74061a459a94f6d1dac250271f5548dab Mon Sep 17 00:00:00 2001 From: JJ Graham Date: Mon, 21 Oct 2019 17:28:00 -0500 Subject: Adding assertions to existing test case to cover this change --- git/test/test_diff.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'git/test/test_diff.py') diff --git a/git/test/test_diff.py b/git/test/test_diff.py index 56e51289..e4e7556d 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -68,7 +68,12 @@ class TestDiff(TestBase): with open(fp, 'w') as fs: fs.write("Hola Mundo") - r.git.commit(all=True, message="change on master") + r.git.add(Git.polish_url(fp)) + self.assertEqual(len(r.index.diff("HEAD", create_patch=True)), 1, + "create_patch should generate patch of diff to HEAD") + r.git.commit(message="change on master") + self.assertEqual(len(r.index.diff("HEAD", create_patch=True)), 0, + "create_patch should generate no patch, already on HEAD") r.git.checkout('HEAD~1', b='topic') with open(fp, 'w') as fs: -- cgit v1.2.1