Skip to content

Commit

Permalink
Merge pull request #1551 from mellowed100/main
Browse files Browse the repository at this point in the history
Enable user to override default diff -M arg
  • Loading branch information
Byron committed Feb 2, 2023
2 parents 63a60b3 + c0e69a4 commit 6ed2285
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion git/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def diff(
args.append("--abbrev=40") # we need full shas
args.append("--full-index") # get full index paths, not only filenames

args.append("-M") # check for renames, in both formats
# remove default '-M' arg (check for renames) if user is overriding it
if not any(x in kwargs for x in ('find_renames', 'no_renames', 'M')):
args.append("-M")

if create_patch:
args.append("-p")
else:
Expand Down
70 changes: 70 additions & 0 deletions test/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,73 @@ def test_diff_interface(self):
cp = c.parents[0]
diff_index = c.diff(cp, ["does/not/exist"])
self.assertEqual(len(diff_index), 0)

@with_rw_directory
def test_rename_override(self, rw_dir):
"""Test disabling of diff rename detection"""

# create and commit file_a.txt
repo = Repo.init(rw_dir)
file_a = osp.join(rw_dir, "file_a.txt")
with open(file_a, "w", encoding='utf-8') as outfile:
outfile.write("hello world\n")
repo.git.add(Git.polish_url(file_a))
repo.git.commit(message="Added file_a.txt")

# remove file_a.txt
repo.git.rm(Git.polish_url(file_a))

# create and commit file_b.txt with similarity index of 52
file_b = osp.join(rw_dir, "file_b.txt")
with open(file_b, "w", encoding='utf-8') as outfile:
outfile.write("hello world\nhello world")
repo.git.add(Git.polish_url(file_b))
repo.git.commit(message="Removed file_a.txt. Added file_b.txt")

commit_a = repo.commit('HEAD')
commit_b = repo.commit('HEAD~1')

# check default diff command with renamed files enabled
diffs = commit_b.diff(commit_a)
self.assertEqual(1, len(diffs))
diff = diffs[0]
self.assertEqual(True, diff.renamed_file)
self.assertEqual('file_a.txt', diff.rename_from)
self.assertEqual('file_b.txt', diff.rename_to)

# check diff with rename files disabled
diffs = commit_b.diff(commit_a, no_renames=True)
self.assertEqual(2, len(diffs))

# check fileA.txt deleted
diff = diffs[0]
self.assertEqual(True, diff.deleted_file)
self.assertEqual('file_a.txt', diff.a_path)

# check fileB.txt added
diff = diffs[1]
self.assertEqual(True, diff.new_file)
self.assertEqual('file_b.txt', diff.a_path)

# check diff with high similarity index
diffs = commit_b.diff(commit_a, split_single_char_options=False, M='75%')
self.assertEqual(2, len(diffs))

# check fileA.txt deleted
diff = diffs[0]
self.assertEqual(True, diff.deleted_file)
self.assertEqual('file_a.txt', diff.a_path)

# check fileB.txt added
diff = diffs[1]
self.assertEqual(True, diff.new_file)
self.assertEqual('file_b.txt', diff.a_path)

# check diff with low similarity index
diffs = commit_b.diff(commit_a, split_single_char_options=False, M='40%')
self.assertEqual(1, len(diffs))
diff = diffs[0]
self.assertEqual(True, diff.renamed_file)
self.assertEqual('file_a.txt', diff.rename_from)
self.assertEqual('file_b.txt', diff.rename_to)

0 comments on commit 6ed2285

Please sign in to comment.