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
diff --git a/turnip/api/store.py b/turnip/api/store.py
index 1291c13..5767958 100644
--- a/turnip/api/store.py
+++ b/turnip/api/store.py
@@ -362,16 +362,17 @@ def get_merge_diff(repo_store, repo_name, sha1_base,
362 from_tree = repo[sha1_base].tree362 from_tree = repo[sha1_base].tree
363 merged_index = repo.merge_commits(sha1_base, sha1_head)363 merged_index = repo.merge_commits(sha1_base, sha1_head)
364 conflicts = _add_conflicted_files(repo, merged_index)364 conflicts = _add_conflicted_files(repo, merged_index)
365 patch = merged_index.diff_to_tree(365 diff = merged_index.diff_to_tree(
366 from_tree, context_lines=context_lines).patch366 from_tree, context_lines=context_lines)
367 diff.find_similar()
368 patch = diff.patch
367 if patch is None:369 if patch is None:
368 patch = u''370 patch = u''
369 shas = [sha1_base, sha1_head]371 shas = [sha1_base, sha1_head]
370 commits = [get_commit(repo_store, repo_name, sha, repo)372 commits = [get_commit(repo_store, repo_name, sha, repo)
371 for sha in shas]373 for sha in shas]
372 diff = {'commits': commits, 'patch': patch,374 return {'commits': commits, 'patch': patch,
373 'conflicts': sorted(conflicts)}375 'conflicts': sorted(conflicts)}
374 return diff
375376
376377
377def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):378def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):
@@ -385,16 +386,17 @@ def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):
385 shas = [sha1_from, sha1_to]386 shas = [sha1_from, sha1_to]
386 commits = [get_commit(repo_store, repo_name, sha, repo)387 commits = [get_commit(repo_store, repo_name, sha, repo)
387 for sha in shas]388 for sha in shas]
388 patch = repo.diff(389 diff = repo.diff(
389 commits[0]['sha1'], commits[1]['sha1'], False, 0,390 commits[0]['sha1'], commits[1]['sha1'], False, 0,
390 context_lines).patch391 context_lines)
392 diff.find_similar()
393 patch = diff.patch
391 if patch is None:394 if patch is None:
392 patch = u''395 patch = u''
393 diff = {396 return {
394 'commits': commits,397 'commits': commits,
395 'patch': patch,398 'patch': patch,
396 }399 }
397 return diff
398400
399401
400def get_log(repo_store, repo_name, start=None, limit=None, stop=None):402def get_log(repo_store, repo_name, start=None, limit=None, stop=None):
diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
index 065b067..4e3c808 100644
--- a/turnip/api/tests/test_api.py
+++ b/turnip/api/tests/test_api.py
@@ -361,6 +361,19 @@ class ApiTestCase(TestCase):
361 self.assertIn(b'-foo', resp.body)361 self.assertIn(b'-foo', resp.body)
362 self.assertIn(b'+bar', resp.body)362 self.assertIn(b'+bar', resp.body)
363363
364 def test_repo_diff_detects_renames(self):
365 """get_diff finds renamed files."""
366 repo = RepoFactory(self.repo_store)
367 c1 = repo.add_commit('foo\n', 'foo.txt')
368 repo.repo.index.remove('foo.txt')
369 c2 = repo.add_commit('foo\n', 'bar.txt', parents=[c1])
370
371 path = '/repo/{}/compare/{}..{}'.format(
372 self.repo_path, quote('{}^'.format(c2)), c2)
373 resp = self.app.get(path)
374 self.assertIn(
375 b'diff --git a/foo.txt b/bar.txt\n', resp.json_body['patch'])
376
364 def test_repo_diff_merge(self):377 def test_repo_diff_merge(self):
365 """Ensure expected changes exist in diff patch."""378 """Ensure expected changes exist in diff patch."""
366 repo = RepoFactory(self.repo_store)379 repo = RepoFactory(self.repo_store)
@@ -438,6 +451,18 @@ class ApiTestCase(TestCase):
438 self.repo_path, repo.nonexistent_oid(), c1), expect_errors=True)451 self.repo_path, repo.nonexistent_oid(), c1), expect_errors=True)
439 self.assertEqual(404, resp.status_code)452 self.assertEqual(404, resp.status_code)
440453
454 def test_repo_diff_merge_detects_renames(self):
455 """get_merge_diff finds renamed files."""
456 repo = RepoFactory(self.repo_store)
457 c1 = repo.add_commit('foo\n', 'foo.txt')
458 repo.repo.index.remove('foo.txt')
459 c2 = repo.add_commit('foo\n', 'bar.txt', parents=[c1])
460
461 resp = self.app.get('/repo/{}/compare-merge/{}:{}'.format(
462 self.repo_path, c1, c2))
463 self.assertIn(
464 b'diff --git a/foo.txt b/bar.txt\n', resp.json_body['patch'])
465
441 def test_repo_get_commit(self):466 def test_repo_get_commit(self):
442 factory = RepoFactory(self.repo_store)467 factory = RepoFactory(self.repo_store)
443 message = 'Computers make me angry.'468 message = 'Computers make me angry.'

Subscribers

People subscribed via source and target branches