You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
git show result misses newline at end of diff #1377
The last \n is unnecessary for most operations, and this process is reasonable. However, this is problematic for git show, especially for the binary files.
I think there are three options:
Do not remove the last \n at any time.
Do not remove the last \n for the git show output.
Do not change the code, and instead add some notes to the documentation
I'd like to send a PR to resolve this issue, so could you tell me your intention toward this?
Thanks a lot for contributing a test and additional analysis, @toku-sa-n, I'd love to see this culminate in a PR.
Here are my thoughts:
Do not remove the last \n at any time.
This would be a potentially breaking change even though it would certainly be the correct way to do it.
Do not remove the last \n for the git show output.
This would require a parameter to be added to execute() to control whether or not the newline should be stripped. This is backwards compatible if the default is True and allows callers to avoid the issue after running into it.
Providing a means to alleviate this seems like the minimum action to perform here.
If it's never desirable to strip newlines for binary output it might be possible to detect if something is binary by looking at the first 1024 bytes or so of the output and avoid stripping newlines then. There is probably a package for that, similar to the file program in unix. Maybe this could be investigated and further discussed in the PR. I'd be particularly interested in the performance impact of such doing so.
Adding a flag and keeping the default as False even for show, which is what ended up happening in this thread, is not really a solution. People will keep hitting this issue, debugging for a few hours, and some of the times not figuring out there is a flag to address this and doing some horrible workaround instead.
Will you accept a PR to toggle the default for show (and only for show because it's the only place it matters)?
Will you accept a PR to fire a deprecation warning if it's False on show (saying the default is about to change), and then in say a year another PR to toggle the default?
I'm speaking fro personal pain here:
I just encountered a vendor-provided codebase that failed on some edge case in git show, and examining the issue I noticed they used GitPython for everything except git show and a separate library for that, so I reverted to GitPython and after an additional half an hour of debuggning figured out why the file hashes sometimes came out different and googled "gitpython show trailing newlines". Evidently the person who wrote that codebase didn't think to google this, or perhaps they encountered it before this bug was reported and "fixed".
Activity
Byron commentedon Nov 13, 2021
Thanks a lot for the wonderful description of the issue and for spending the time that undoubtedly when into it.
It's likely that GitPython strips
gitoutput somewhere in its command execution machinery, and I wonder why it was added in the first place.toku-sa-n commentedon Apr 4, 2022
Hello, I've also encountered this problem (see my SO question and the answer). I found the line where GitPython strips the last
\n.GitPython/git/cmd.py
Lines 946 to 948 in 0b33576
The test below fails for the current main branch, and succeeds after removing
[:-1].The last
\nis unnecessary for most operations, and this process is reasonable. However, this is problematic forgit show, especially for the binary files.I think there are three options:
\nat any time.\nfor thegit showoutput.I'd like to send a PR to resolve this issue, so could you tell me your intention toward this?
Byron commentedon Apr 5, 2022
Thanks a lot for contributing a test and additional analysis, @toku-sa-n, I'd love to see this culminate in a PR.
Here are my thoughts:
This would be a potentially breaking change even though it would certainly be the correct way to do it.
This would require a parameter to be added to
execute()to control whether or not the newline should be stripped. This is backwards compatible if the default isTrueand allows callers to avoid the issue after running into it.Providing a means to alleviate this seems like the minimum action to perform here.
If it's never desirable to strip newlines for binary output it might be possible to detect if something is binary by looking at the first 1024 bytes or so of the output and avoid stripping newlines then. There is probably a package for that, similar to the
fileprogram in unix. Maybe this could be investigated and further discussed in the PR. I'd be particularly interested in the performance impact of such doing so.Thanks everyone.
SonOfLilit commentedon Jul 25, 2024
Adding a flag and keeping the default as
Falseeven forshow, which is what ended up happening in this thread, is not really a solution. People will keep hitting this issue, debugging for a few hours, and some of the times not figuring out there is a flag to address this and doing some horrible workaround instead.Will you accept a PR to toggle the default for
show(and only forshowbecause it's the only place it matters)?Will you accept a PR to fire a deprecation warning if it's
Falseonshow(saying the default is about to change), and then in say a year another PR to toggle the default?I'm speaking fro personal pain here:
I just encountered a vendor-provided codebase that failed on some edge case in git show, and examining the issue I noticed they used GitPython for everything except
git showand a separate library for that, so I reverted to GitPython and after an additional half an hour of debuggning figured out why the file hashes sometimes came out different and googled "gitpython show trailing newlines". Evidently the person who wrote that codebase didn't think to google this, or perhaps they encountered it before this bug was reported and "fixed".Byron commentedon Jul 25, 2024
Please feel free to submit a PR, ideally with an accompanying test to show what it's doing. Breaking changes can't be done though.