Skip to content

Commit 6ba8b8b

Browse files
committed
Added in new properties Diff.renamed, Diff.rename_from, and Diff.rename_to
1 parent 5e062f4 commit 6ba8b8b

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

lib/git/commit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ def diff(cls, repo, a, b = None, paths = None):
204204
if b:
205205
paths.insert(0, b)
206206
paths.insert(0, a)
207-
text = repo.git.diff(full_index=True, *paths)
207+
text = repo.git.diff('-M', full_index=True, *paths)
208208
return diff.Diff.list_from_string(repo, text)
209209

210210
@property
211211
def diffs(self):
212212
if not self.parents:
213-
d = self.repo.git.show(self.id, full_index=True, pretty='raw')
213+
d = self.repo.git.show(self.id, '-M', full_index=True, pretty='raw')
214214
if re.search(r'diff --git a', d):
215215
if not re.search(r'^diff --git a', d):
216216
p = re.compile(r'.+?(diff --git a)', re.MULTILINE | re.DOTALL)

lib/git/diff.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class Diff(object):
1212
A Diff contains diff information between two commits.
1313
"""
1414

15-
def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode, b_mode, new_file, deleted_file, diff):
15+
def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode,
16+
b_mode, new_file, deleted_file, rename_from,
17+
rename_to, diff):
1618
self.repo = repo
1719
self.a_path = a_path
1820
self.b_path = b_path
@@ -30,6 +32,9 @@ def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode, b_mode, new
3032
self.b_mode = b_mode
3133
self.new_file = new_file
3234
self.deleted_file = deleted_file
35+
self.rename_from = rename_from
36+
self.rename_to = rename_to
37+
self.renamed = rename_from != rename_to
3338
self.diff = diff
3439

3540
@classmethod
@@ -54,13 +59,13 @@ def list_from_string(cls, repo, text):
5459
header = diff_header(diff)
5560

5661
a_path, b_path, similarity_index, rename_from, rename_to, \
57-
old_mode, new_mode, new_file_mode, deleted_file_mode, \
62+
old_mode, new_mode, new_file_mode, deleted_file_mode, \
5863
a_commit, b_commit, b_mode = header.groups()
5964
new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode)
6065

6166
diffs.append(Diff(repo, a_path, b_path, a_commit, b_commit,
6267
old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode,
63-
new_file, deleted_file, diff[header.end():]))
68+
new_file, deleted_file, rename_from, rename_to, diff[header.end():]))
6469

6570
return diffs
6671

test/fixtures/diff_rename

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
commit 2524c44334a8ba6b2ab8f3f0a478f04c5b073cc8
2+
tree e126e7b4203dadf083f5eb8e2f34c255b51d8bee
3+
parent d789e23b9ea8d90221d13c46f7c228d729385f92
4+
author Michael Trier <[email protected]> 1229389391 -0500
5+
committer Michael Trier <[email protected]> 1229389391 -0500
6+
7+
Renamed AUTHORS to CONTRIBUTORS because it's cooler.
8+
9+
diff --git a/AUTHORS b/CONTRIBUTORS
10+
similarity index 100%
11+
rename from AUTHORS
12+
rename to CONTRIBUTORS

test/git/test_commit.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,23 @@ def test_diff(self, git):
5252
assert_equal(True, diffs[5].new_file)
5353

5454
assert_true(git.called)
55-
assert_equal(git.call_args, (('diff', 'master'), {'full_index': True}))
55+
assert_equal(git.call_args, (('diff', '-M', 'master'), {'full_index': True}))
56+
57+
@patch_object(Git, '_call_process')
58+
def test_diff_with_rename(self, git):
59+
git.return_value = fixture('diff_rename')
60+
61+
diffs = Commit.diff(self.repo, 'rename')
62+
63+
assert_equal(1, len(diffs))
64+
65+
diff = diffs[0]
66+
assert_true(diff.renamed)
67+
assert_equal(diff.rename_from, 'AUTHORS')
68+
assert_equal(diff.rename_to, 'CONTRIBUTORS')
69+
70+
assert_true(git.called)
71+
assert_equal(git.call_args, (('diff', '-M', 'rename'), {'full_index': True}))
5672

5773
@patch_object(Git, '_call_process')
5874
def test_diff_with_two_commits(self, git):
@@ -63,7 +79,7 @@ def test_diff_with_two_commits(self, git):
6379
assert_equal(3, len(diffs))
6480

6581
assert_true(git.called)
66-
assert_equal(git.call_args, (('diff', '59ddc32', '13d27d5'), {'full_index': True}))
82+
assert_equal(git.call_args, (('diff', '-M', '59ddc32', '13d27d5'), {'full_index': True}))
6783

6884
@patch_object(Git, '_call_process')
6985
def test_diff_with_files(self, git):
@@ -75,7 +91,7 @@ def test_diff_with_files(self, git):
7591
assert_equal('lib/grit/diff.rb', diffs[0].a_path)
7692

7793
assert_true(git.called)
78-
assert_equal(git.call_args, (('diff', '59ddc32', '--', 'lib'), {'full_index': True}))
94+
assert_equal(git.call_args, (('diff', '-M', '59ddc32', '--', 'lib'), {'full_index': True}))
7995

8096
@patch_object(Git, '_call_process')
8197
def test_diff_with_two_commits_and_files(self, git):
@@ -87,7 +103,7 @@ def test_diff_with_two_commits_and_files(self, git):
87103
assert_equal('lib/grit/commit.rb', diffs[0].a_path)
88104

89105
assert_true(git.called)
90-
assert_equal(git.call_args, (('diff', '59ddc32', '13d27d5', '--', 'lib'), {'full_index': True}))
106+
assert_equal(git.call_args, (('diff', '-M', '59ddc32', '13d27d5', '--', 'lib'), {'full_index': True}))
91107

92108
@patch_object(Git, '_call_process')
93109
def test_diffs(self, git):
@@ -113,7 +129,8 @@ def test_diffs(self, git):
113129
assert_equal(True, diffs[5].new_file)
114130

115131
assert_true(git.called)
116-
assert_equal(git.call_args, (('diff', '038af8c329ef7c1bae4568b98bd5c58510465493',
132+
assert_equal(git.call_args, (('diff', '-M',
133+
'038af8c329ef7c1bae4568b98bd5c58510465493',
117134
'91169e1f5fa4de2eaea3f176461f5dc784796769',
118135
), {'full_index': True}))
119136

@@ -142,7 +159,7 @@ def test_diffs_on_initial_import(self, git):
142159
assert_equal(True, diffs[5].new_file)
143160

144161
assert_true(git.called)
145-
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3'), {'full_index': True, 'pretty': 'raw'}))
162+
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '-M'), {'full_index': True, 'pretty': 'raw'}))
146163

147164
@patch_object(Git, '_call_process')
148165
def test_diffs_on_initial_import_with_empty_commit(self, git):
@@ -154,7 +171,7 @@ def test_diffs_on_initial_import_with_empty_commit(self, git):
154171
assert_equal([], diffs)
155172

156173
assert_true(git.called)
157-
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3'), {'full_index': True, 'pretty': 'raw'}))
174+
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '-M'), {'full_index': True, 'pretty': 'raw'}))
158175

159176
@patch_object(Git, '_call_process')
160177
def test_diffs_with_mode_only_change(self, git):
@@ -169,7 +186,7 @@ def test_diffs_with_mode_only_change(self, git):
169186
assert_equal('100755', diffs[0].b_mode)
170187

171188
assert_true(git.called)
172-
assert_equal(git.call_args, (('show', '91169e1f5fa4de2eaea3f176461f5dc784796769'), {'full_index': True, 'pretty': 'raw'}))
189+
assert_equal(git.call_args, (('show', '91169e1f5fa4de2eaea3f176461f5dc784796769', '-M'), {'full_index': True, 'pretty': 'raw'}))
173190

174191
@patch_object(Git, '_call_process')
175192
def test_stats(self, git):

test/git/test_diff.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@
1010
class TestDiff(object):
1111
def setup(self):
1212
self.repo = Repo(GIT_REPO)
13-
13+
1414
def test_list_from_string_new_mode(self):
1515
output = fixture('diff_new_mode')
1616
diffs = Diff.list_from_string(self.repo, output)
1717
assert_equal(1, len(diffs))
1818
assert_equal(10, len(diffs[0].diff.splitlines()))
19+
20+
def test_diff_with_rename(self):
21+
output = fixture('diff_rename')
22+
diffs = Diff.list_from_string(self.repo, output)
23+
24+
assert_equal(1, len(diffs))
25+
26+
diff = diffs[0]
27+
assert_true(diff.renamed)
28+
assert_equal(diff.rename_from, 'AUTHORS')
29+
assert_equal(diff.rename_to, 'CONTRIBUTORS')
30+

0 commit comments

Comments
 (0)