|
5 | 5 | # This module is part of GitPython and is released under
|
6 | 6 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php
|
7 | 7 | importddt
|
| 8 | +importshutil |
| 9 | +importtempfile |
8 | 10 | fromgitimport (
|
9 | 11 | Repo,
|
10 | 12 | GitCommandError,
|
11 | 13 | Diff,
|
12 | 14 | DiffIndex,
|
13 | 15 | NULL_TREE,
|
| 16 | +Submodule, |
14 | 17 | )
|
15 | 18 | fromgit.cmdimportGit
|
16 | 19 | fromgit.test.libimport (
|
|
19 | 22 | fixture,
|
20 | 23 | assert_equal,
|
21 | 24 | assert_true,
|
22 |
| - |
23 | 25 | )
|
24 | 26 | fromgit.test.libimportwith_rw_directory
|
25 | 27 |
|
|
29 | 31 | @ddt.ddt
|
30 | 32 | classTestDiff(TestBase):
|
31 | 33 |
|
| 34 | +defsetUp(self): |
| 35 | +self.repo_dir=tempfile.mkdtemp() |
| 36 | +self.submodule_dir=tempfile.mkdtemp() |
| 37 | + |
32 | 38 | deftearDown(self):
|
33 | 39 | importgc
|
34 | 40 | gc.collect()
|
| 41 | +shutil.rmtree(self.repo_dir) |
| 42 | +shutil.rmtree(self.submodule_dir) |
35 | 43 |
|
36 | 44 | def_assert_diff_format(self,diffs):
|
37 | 45 | # verify that the format of the diff is sane
|
@@ -68,7 +76,8 @@ def test_diff_with_staged_file(self, rw_dir):
|
68 | 76 | r.git.commit(all=True,message="change on topic branch")
|
69 | 77 |
|
70 | 78 | # there must be a merge-conflict
|
71 |
| -self.failUnlessRaises(GitCommandError,r.git.cherry_pick,'master') |
| 79 | +withself.assertRaises(GitCommandError): |
| 80 | +r.git.cherry_pick('master') |
72 | 81 |
|
73 | 82 | # Now do the actual testing - this should just work
|
74 | 83 | self.assertEqual(len(r.index.diff(None)),2)
|
@@ -267,6 +276,43 @@ def test_diff_with_spaces(self):
|
267 | 276 | self.assertIsNone(diff_index[0].a_path,repr(diff_index[0].a_path))
|
268 | 277 | self.assertEqual(diff_index[0].b_path,u'file with spaces',repr(diff_index[0].b_path))
|
269 | 278 |
|
| 279 | +deftest_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 | +withopen(f"{self.submodule_dir}/subfile","w")assub_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 | +withopen(f"{self.repo_dir}/test","w")asfoo_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 | +withopen(f"{self.repo_dir}/sub/subfile","w")asfoo_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 | + |
270 | 316 | deftest_diff_interface(self):
|
271 | 317 | # test a few variations of the main diff routine
|
272 | 318 | assertion_map= {}
|
|