git-gutter
git-gutter copied to clipboard
Fails to stage hunk on Windows when file uses LF (unix) line endings
In a repository which uses LF line termination, using git-gutter on Windows appears to be problematic for staging hunks (and possibly other operations).
Scenario:
- Source file (LF line termination -- unix style) on Windows 10
-
M-x git-gutter:stage-hunk
- *git-gutter:diff* buffer shows CRLF end-of-line formatting
- "Stage current hunk? (y or n)" y
- Application of patch fails (i.e., "Failed: stating this hunk")
I investigated this a bit and discovered that the temporary "patch" file created in git-gutter:do-stage-hunk
ends up using CRLF line endings when written to the file system. Note that this patch file is different than if you just performed the git diff
command directly at the command line as the patch file contains CRLF and the git diff
command contains only LF endings. Thus, it appears the internal manipulation of the diff output within the Emacs buffers by git-gutter is likely what is causing the line-endings to be changed to CRLF.
This trips up the subsequent git apply
as that is the command which fails (with "patch failed" and "patch does not apply" output from git). I modified the source to prevent the temporary file from being deleted, so that I could experiment with attempting to apply the patch directly with git from the console. I then performed a conversion of the line endings of the patch file (i.e., dos2unix -U <temp file>
) and repeated the git apply
command. The subsequent git apply
then succeeds. So it appears that Windows git is sensitive to the line endings of the patch file created by git-gutter. I looked at the --whitespace=fix
and --ignore-whitespace
options for git apply
, but neither of those make sense in this scenario.
I also attempted to replicate the reverse behavior on Linux. Starting with a CRLF EOL file and performed a git-gutter:stage-hunk on that. The *git-gutter:diff* buffer showed the line ending as being LF, but the subsequent git apply
succeeded. Thus, this appears to be problematic only on Windows.
Steps to reproduce (on Windows):
- Create new git repo (git init)
- Create file and save it with LF line endings.
- Commit file as initial version
- Modify file in Emacs, save file, and attempt to use
git-gutter:stage-hunk
to stage the change. - The application of the patch should fail with "Failed: stating this hunk".
These were the versions of tools I used:
- git-gutter-20200326.1814
- Windows git 2.28.0.windows.1
- Emacs 27.1
- Windows 10