Skip to content

Enable user to override default diff -M arg #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)