-
-
Notifications
You must be signed in to change notification settings - Fork 954
-
Hi,
I've tried to use the following to disable git diff renamed files search:
repo.index.diff('origin/HEAD', no_renames=True)
This did not work, and after some digging, i've found that GitPython hardcodes the -M
flag:
https://github.com/gitpython-developers/GitPython/blob/master/git/diff.py#L106
Why is this flag hardcoded and is there a way around it?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions
With this API there is no way around it unless the git command is used directly such as in repo.git.diff(...)
.
If there is motivation, a PR could be added to disable rename tracking which I presume is in the interest of increased performance.
Replies: 3 comments 13 replies
-
With this API there is no way around it unless the git command is used directly such as in repo.git.diff(...)
.
If there is motivation, a PR could be added to disable rename tracking which I presume is in the interest of increased performance.
Beta Was this translation helpful? Give feedback.
All reactions
-
@Byron there are two issues here:
- Sometimes it is necessary to to add
no-renames
to prevent false positives in git's rename detection - for example, deleting a file and creating another file with similar content (this will be detected as rename, rather than delete + add). - It seems that there is a bug GitPython's diff filter when used with renames. To recreate, delete a file and create another file with similar contents, compare the result of
git diff --name-only --diff-filter=M -M
with GitPython'siter_change_type('M')
. GitPython will show the deleted/renamed file as modified.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 3
-
Hello! I've hit this issue as well. I'd like to try the repo.git.diff() approach as a workaround. Using the results from repo.git.diff(), is there an easy way to build a DiffIndex()?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
-
# remove -M arg if user supplies 'no_renames' or 'M' kwargs
if not any(x in kwargs for x in ('no_renames','M')):
args.append("-M") # check for renames, in both formats
# manually convert '-M'
if 'M' in kwargs:
args.append(f"-M{str(kwargs['M'])}")
del(kwargs['M'])
commitA.diff(commitB, M='75%')
produces:
git diff-tree 58259d006066372eb337cce81caf97365eb30f1e 22315b3c433865970e0a2fcd1b599786c2e01703 -r --abbrev=40 --full-index -M75% --raw -z --no-color
commitA.diff(commitB, no_renames=True)
produces:
git diff-tree --no-renames 58259d006066372eb337cce81caf97365eb30f1e 22315b3c433865970e0a2fcd1b599786c2e01703 -r --abbrev=40 --full-index --raw -z --no-color
default behavior stays the same
commitA.diff(commitB)
produces:
git diff-tree 58259d006066372eb337cce81caf97365eb30f1e 22315b3c433865970e0a2fcd1b599786c2e01703 -r --abbrev=40 --full-index -M --raw -z --no-color
Beta Was this translation helpful? Give feedback.
All reactions
-
Maybe that could be generalized (and tested) to become a contribution - it looks like a genuine improvement without adding too much complexity.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hey Bryon,
So I took another look into the command line formatting issue. It turns out there is an option to format those single char kawrgs.
split_single_char_options controls the inclusion of a space character when constructing the git command line.
original:
commitA.diff(commitB, M='75%')
produces the -M 75%: (space included)
git diff-tree -M 75% 69a8e0b10c8695d7f073c3b32c647aa8e892b1e2 140ef4c33fa5a899cd8d1dfb15a03c342a49a95c -r --abbrev=40 --full-index --raw -z --no-color
Including option to format single char kwargs:
commitA.diff(commitB, split_single_char_options=False ,M='75%')
produces the -M75% (space removed)
git diff-tree -M75% 69a8e0b10c8695d7f073c3b32c647aa8e892b1e2 140ef4c33fa5a899cd8d1dfb15a03c342a49a95c -r --abbrev=40 --full-index --raw -z --no-color
So it looks like all we need is to remove the default -M when the user wants to override it with either --find-renames= or -M or --no-renames
# 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")
The remaining kwargs can be processed through the normal routines.
Beta Was this translation helpful? Give feedback.
All reactions
-
It may also be worth adding the split_single_char_options option in the docs.
Beta Was this translation helpful? Give feedback.
All reactions
-
I see, thanks so much for investigating this. A PR to improve the situation would definitely be welcome. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions
-
would also like for this to be added!
Beta Was this translation helpful? Give feedback.
All reactions
-
Hey nwcm! The PR was finished last week. You can probably download the source now, or wait for the official 3.1.31 release.
#1551
Usage examples can be found in test/test_diff.py test_rename_override().
Beta Was this translation helpful? Give feedback.