Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a086625

Browse files
thetwojByron
authored andcommitted
Added new test to cover the issue this fix addresses (#891)
1 parent 544ceec commit a086625

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

‎git/test/test_diff.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
# This module is part of GitPython and is released under
66
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
77
import ddt
8+
import shutil
9+
import tempfile
810
from git import (
911
Repo,
1012
GitCommandError,
1113
Diff,
1214
DiffIndex,
1315
NULL_TREE,
16+
Submodule,
1417
)
1518
from git.cmd import Git
1619
from git.test.lib import (
@@ -19,7 +22,6 @@
1922
fixture,
2023
assert_equal,
2124
assert_true,
22-
2325
)
2426
from git.test.lib import with_rw_directory
2527

@@ -29,9 +31,15 @@
2931
@ddt.ddt
3032
class TestDiff(TestBase):
3133

34+
def setUp(self):
35+
self.repo_dir = tempfile.mkdtemp()
36+
self.submodule_dir = tempfile.mkdtemp()
37+
3238
def tearDown(self):
3339
import gc
3440
gc.collect()
41+
shutil.rmtree(self.repo_dir)
42+
shutil.rmtree(self.submodule_dir)
3543

3644
def _assert_diff_format(self, diffs):
3745
# verify that the format of the diff is sane
@@ -68,7 +76,8 @@ def test_diff_with_staged_file(self, rw_dir):
6876
r.git.commit(all=True, message="change on topic branch")
6977

7078
# there must be a merge-conflict
71-
self.failUnlessRaises(GitCommandError, r.git.cherry_pick, 'master')
79+
with self.assertRaises(GitCommandError):
80+
r.git.cherry_pick('master')
7281

7382
# Now do the actual testing - this should just work
7483
self.assertEqual(len(r.index.diff(None)), 2)
@@ -267,6 +276,43 @@ def test_diff_with_spaces(self):
267276
self.assertIsNone(diff_index[0].a_path, repr(diff_index[0].a_path))
268277
self.assertEqual(diff_index[0].b_path, u'file with spaces', repr(diff_index[0].b_path))
269278

279+
def test_diff_submodule(self):
280+
"""Test that diff is able to correctly diff commits that cover submodule changes"""
281+
# Init a temp git repo that will be referenced as a submodule
282+
sub = Repo.init(self.submodule_dir)
283+
with open(f"{self.submodule_dir}/subfile", "w") as sub_subfile:
284+
sub_subfile.write("")
285+
sub.index.add(["subfile"])
286+
sub.index.commit("first commit")
287+
288+
# Init a temp git repo that will incorporate the submodule
289+
repo = Repo.init(self.repo_dir)
290+
with open(f"{self.repo_dir}/test", "w") as foo_test:
291+
foo_test.write("")
292+
repo.index.add(['test'])
293+
Submodule.add(repo, "subtest", "sub", url=f"file://{self.submodule_dir}")
294+
repo.index.commit("first commit")
295+
repo.create_tag('1')
296+
297+
# Add a commit to the submodule
298+
submodule = repo.submodule('subtest')
299+
with open(f"{self.repo_dir}/sub/subfile", "w") as foo_sub_subfile:
300+
foo_sub_subfile.write("blub")
301+
submodule.module().index.add(["subfile"])
302+
submodule.module().index.commit("changed subfile")
303+
submodule.binsha = submodule.module().head.commit.binsha
304+
305+
# Commit submodule updates in parent repo
306+
repo.index.add([submodule])
307+
repo.index.commit("submodule changed")
308+
repo.create_tag('2')
309+
310+
diff = repo.commit('1').diff(repo.commit('2'))[0]
311+
# If diff is unable to find the commit hashes (looks in wrong repo) the *_blob.size
312+
# property will be a string containing exception text, an int indicates success
313+
self.assertIsInstance(diff.a_blob.size, int)
314+
self.assertIsInstance(diff.b_blob.size, int)
315+
270316
def test_diff_interface(self):
271317
# test a few variations of the main diff routine
272318
assertion_map = {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /