Merge ~cjwatson/turnip:diff-detect-renames into turnip:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 8db2b0bc7cda5531b5479a6969fd285e63a66768
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/turnip:diff-detect-renames
Merge into: turnip:master
Diff against target: 91 lines (+35/-8)
2 files modified
turnip/api/store.py (+10/-8)
turnip/api/tests/test_api.py (+25/-0)
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+330036@code.launchpad.net

Commit message

Detect renames when generating diffs

LP: #1712754

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/turnip/api/store.py b/turnip/api/store.py
2index 1291c13..5767958 100644
3--- a/turnip/api/store.py
4+++ b/turnip/api/store.py
5@@ -362,16 +362,17 @@ def get_merge_diff(repo_store, repo_name, sha1_base,
6 from_tree = repo[sha1_base].tree
7 merged_index = repo.merge_commits(sha1_base, sha1_head)
8 conflicts = _add_conflicted_files(repo, merged_index)
9- patch = merged_index.diff_to_tree(
10- from_tree, context_lines=context_lines).patch
11+ diff = merged_index.diff_to_tree(
12+ from_tree, context_lines=context_lines)
13+ diff.find_similar()
14+ patch = diff.patch
15 if patch is None:
16 patch = u''
17 shas = [sha1_base, sha1_head]
18 commits = [get_commit(repo_store, repo_name, sha, repo)
19 for sha in shas]
20- diff = {'commits': commits, 'patch': patch,
21+ return {'commits': commits, 'patch': patch,
22 'conflicts': sorted(conflicts)}
23- return diff
24
25
26 def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):
27@@ -385,16 +386,17 @@ def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):
28 shas = [sha1_from, sha1_to]
29 commits = [get_commit(repo_store, repo_name, sha, repo)
30 for sha in shas]
31- patch = repo.diff(
32+ diff = repo.diff(
33 commits[0]['sha1'], commits[1]['sha1'], False, 0,
34- context_lines).patch
35+ context_lines)
36+ diff.find_similar()
37+ patch = diff.patch
38 if patch is None:
39 patch = u''
40- diff = {
41+ return {
42 'commits': commits,
43 'patch': patch,
44 }
45- return diff
46
47
48 def get_log(repo_store, repo_name, start=None, limit=None, stop=None):
49diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
50index 065b067..4e3c808 100644
51--- a/turnip/api/tests/test_api.py
52+++ b/turnip/api/tests/test_api.py
53@@ -361,6 +361,19 @@ class ApiTestCase(TestCase):
54 self.assertIn(b'-foo', resp.body)
55 self.assertIn(b'+bar', resp.body)
56
57+ def test_repo_diff_detects_renames(self):
58+ """get_diff finds renamed files."""
59+ repo = RepoFactory(self.repo_store)
60+ c1 = repo.add_commit('foo\n', 'foo.txt')
61+ repo.repo.index.remove('foo.txt')
62+ c2 = repo.add_commit('foo\n', 'bar.txt', parents=[c1])
63+
64+ path = '/repo/{}/compare/{}..{}'.format(
65+ self.repo_path, quote('{}^'.format(c2)), c2)
66+ resp = self.app.get(path)
67+ self.assertIn(
68+ b'diff --git a/foo.txt b/bar.txt\n', resp.json_body['patch'])
69+
70 def test_repo_diff_merge(self):
71 """Ensure expected changes exist in diff patch."""
72 repo = RepoFactory(self.repo_store)
73@@ -438,6 +451,18 @@ class ApiTestCase(TestCase):
74 self.repo_path, repo.nonexistent_oid(), c1), expect_errors=True)
75 self.assertEqual(404, resp.status_code)
76
77+ def test_repo_diff_merge_detects_renames(self):
78+ """get_merge_diff finds renamed files."""
79+ repo = RepoFactory(self.repo_store)
80+ c1 = repo.add_commit('foo\n', 'foo.txt')
81+ repo.repo.index.remove('foo.txt')
82+ c2 = repo.add_commit('foo\n', 'bar.txt', parents=[c1])
83+
84+ resp = self.app.get('/repo/{}/compare-merge/{}:{}'.format(
85+ self.repo_path, c1, c2))
86+ self.assertIn(
87+ b'diff --git a/foo.txt b/bar.txt\n', resp.json_body['patch'])
88+
89 def test_repo_get_commit(self):
90 factory = RepoFactory(self.repo_store)
91 message = 'Computers make me angry.'

Subscribers

People subscribed via source and target branches