Skip to content

Commit 63ce95e

Browse files
committed
Get correcly rename change_type.
Also store the rename score
1 parent 05e3b0e commit 63ce95e

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

git/diff.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ class Diff(object):
251251

252252
__slots__ = ("a_blob", "b_blob", "a_mode", "b_mode", "a_rawpath", "b_rawpath",
253253
"new_file", "deleted_file", "raw_rename_from", "raw_rename_to",
254-
"diff", "change_type")
254+
"diff", "change_type", "score")
255255

256256
def __init__(self, repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode,
257257
b_mode, new_file, deleted_file, raw_rename_from,
258-
raw_rename_to, diff, change_type):
258+
raw_rename_to, diff, change_type, score):
259259

260260
self.a_mode = a_mode
261261
self.b_mode = b_mode
@@ -291,6 +291,7 @@ def __init__(self, repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode,
291291

292292
self.diff = diff
293293
self.change_type = change_type
294+
self.score = score
294295

295296
def __eq__(self, other):
296297
for name in self.__slots__:
@@ -445,7 +446,7 @@ def _index_from_patch_format(cls, repo, proc):
445446
new_file, deleted_file,
446447
rename_from,
447448
rename_to,
448-
None, None))
449+
None, None, None))
449450

450451
previous_header = header
451452
# end for each header we parse
@@ -471,6 +472,11 @@ def handle_diff_line(line):
471472

472473
meta, _, path = line[1:].partition('\t')
473474
old_mode, new_mode, a_blob_id, b_blob_id, change_type = meta.split(None, 4)
475+
# Change type can be R100
476+
# R: status letter
477+
# 100: score (in case of copy and rename)
478+
change_type = change_type[0]
479+
score = ''.join(change_type[1:]) or None
474480
path = path.strip()
475481
a_path = path.encode(defenc)
476482
b_path = path.encode(defenc)
@@ -487,15 +493,16 @@ def handle_diff_line(line):
487493
elif change_type == 'A':
488494
a_blob_id = None
489495
new_file = True
490-
elif change_type[0] == 'R': # parses RXXX, where XXX is a confidence value
496+
elif change_type == 'R':
491497
a_path, b_path = path.split('\t', 1)
492498
a_path = a_path.encode(defenc)
493499
b_path = b_path.encode(defenc)
494500
rename_from, rename_to = a_path, b_path
495501
# END add/remove handling
496502

497503
diff = Diff(repo, a_path, b_path, a_blob_id, b_blob_id, old_mode, new_mode,
498-
new_file, deleted_file, rename_from, rename_to, '', change_type)
504+
new_file, deleted_file, rename_from, rename_to, '',
505+
change_type, score)
499506
index.append(diff)
500507

501508
handle_process_output(proc, handle_diff_line, None, finalize_process, decode_streams=False)

git/test/test_diff.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def test_diff_with_rename(self):
108108
self.assertIsNotNone(diff.renamed)
109109
self.assertEqual(diff.rename_from, 'this')
110110
self.assertEqual(diff.rename_to, 'that')
111+
self.assertEqual(diff.change_type, 'R')
111112
self.assertEqual(len(list(diffs.iter_change_type('R'))), 1)
112113

113114
def test_diff_of_modified_files_not_added_to_the_index(self):

0 commit comments

Comments
 (0)