My diff contains trailing whitespace - how to get rid of it?
Asked Answered
D

8

63

I've tried editing a php file in TextWrangler with line endings set to Unix, in NetBeans, and in vim. When I save the diff to a patch and then try to apply it, it gives whitespace errors. When I type git diff I can see ^M at the ends of my lines, but if I manually remove these in vim, it says my patch file is corrupted, and then the patch doesn't apply at all.

I create a patch with the following command:

git diff > patchname.patch

And I apply it by checking out a clean version of the file to be patched and typing

git apply patchname.patch

How can I create this patch without whitespace errors? I've created patches before and never run into this issue.

Dercy answered 24/1, 2013 at 20:16 Comment(4)
You might want to give more details about your process of creating a patch.Larceny
I added details. Not sure how helpful that will be, but it can't hurt.Dercy
I am facing the same issue and have done some research. It looks like git adds these whitespaces automatically to git diff and fit show, so any patch made with such code will have trailing whitespaces. If you have linux kernel git repository locally (linus kernel tree), you can run git show 402bae597e. It will show you a trailing whitespace before static DEVICE_ATTR_RO(flags); and the closing braces in previous line, even though they are not present in the code.Esoteric
@Larceny This problem can be reproduced by adding a line to start or beginning of a method (where you have something written in the beginning of a line e.g. opening/closing brace or function prototype). Then run git diff. You can see extra whitespaces before unchanged files. These then get introduced to the patch when git format-patch is usedEsoteric
R
48

Are you sure those are hard errors? By default, git will warn about whitespace errors, but will still accept them. If they are hard errors then you must have changed some settings. You can use the --whitespace= flag to git apply to control this on a per-invocation basis. Try

git apply --whitespace=warn patchname.patch

That will force the default behavior, which is to warn but accept. You can also use --whitespace=nowarn to remove the warnings entirely.

The config variable that controls this is apply.whitespace.


For reference, the whitespace errors here aren't errors with your patch. It's a code style thing that git will, by default, complain about when applying patches. Notably, it dislikes trailing whitespace. Similarly git diff will highlight whitespace errors (if you're outputting to a terminal and color is on). The default behavior is to warn, but accept the patch anyway, because not every project is fanatical about whitespace.

Romany answered 25/1, 2013 at 1:0 Comment(7)
I understand that. However, I am confused as to where this whitespace is coming from. I didn't ask whether I should care about it, I asked how to fix it.Dercy
The whitespace is coming from the patch you're trying to apply. Which means it came from the diff you originally took.Romany
I know that. I am trying to find out how to create a diff without ending whitespace in it.Dercy
@beth: By, uh, removing the trailing whitespace in your source before creating the diff? Or you could just go ahead and use git apply --whitespace=fix patchname.patch, which will fix the whitespace errors it sees for you.Romany
Okay, just imagine that it is important to me not to have trailing whitespace at all. I mentioned attempting to remove it in several different editors. "By removing trailing whitespace" isn't really an answer to the question "how do I remove trailing whitespace". Sorry if I was unclear.Dercy
@beth: You mentioned trying to remove it from the diff. Manually editing diffs is a tricky process. But again, you can use git apply --whitespace=fix patchname.patch and git will fix the whitespace for you.Romany
White space was at on the very last line but it didn't fix itMaybellmaybelle
P
61

git apply --reject --whitespace=fix mychanges.path

Darian Lewin commented:

Useful to automatically fix whitespace errors. --whitespace=fix ensures whitespace errors are fixed before path is applied, and --reject ensures atomicity so no working directory files are modified if patch will not apply.

Source: git-scm.com/docs/git-apply

Preclude answered 9/12, 2014 at 6:48 Comment(2)
Useful to automatically fix whitespace errors. --whitespace=fix ensures whitespace errors are fixed before path is applied, and --reject ensures atomicity so no working directory files are modified if patch will not apply. Source: git-scm.com/docs/git-applyMercaptide
The comment and answer are partly wrong. git-apply already ensures atomicity by default, but specifying --reject will cause Git to ignore atomicity, and partly apply the patch, modifying your files, even if there are failures. It will leave behind .rej files containing the hunks that could not be applied, so you can apply them manually.Oestriol
R
48

Are you sure those are hard errors? By default, git will warn about whitespace errors, but will still accept them. If they are hard errors then you must have changed some settings. You can use the --whitespace= flag to git apply to control this on a per-invocation basis. Try

git apply --whitespace=warn patchname.patch

That will force the default behavior, which is to warn but accept. You can also use --whitespace=nowarn to remove the warnings entirely.

The config variable that controls this is apply.whitespace.


For reference, the whitespace errors here aren't errors with your patch. It's a code style thing that git will, by default, complain about when applying patches. Notably, it dislikes trailing whitespace. Similarly git diff will highlight whitespace errors (if you're outputting to a terminal and color is on). The default behavior is to warn, but accept the patch anyway, because not every project is fanatical about whitespace.

Romany answered 25/1, 2013 at 1:0 Comment(7)
I understand that. However, I am confused as to where this whitespace is coming from. I didn't ask whether I should care about it, I asked how to fix it.Dercy
The whitespace is coming from the patch you're trying to apply. Which means it came from the diff you originally took.Romany
I know that. I am trying to find out how to create a diff without ending whitespace in it.Dercy
@beth: By, uh, removing the trailing whitespace in your source before creating the diff? Or you could just go ahead and use git apply --whitespace=fix patchname.patch, which will fix the whitespace errors it sees for you.Romany
Okay, just imagine that it is important to me not to have trailing whitespace at all. I mentioned attempting to remove it in several different editors. "By removing trailing whitespace" isn't really an answer to the question "how do I remove trailing whitespace". Sorry if I was unclear.Dercy
@beth: You mentioned trying to remove it from the diff. Manually editing diffs is a tricky process. But again, you can use git apply --whitespace=fix patchname.patch and git will fix the whitespace for you.Romany
White space was at on the very last line but it didn't fix itMaybellmaybelle
A
10

Try patch -p1 < filename.patch

Assail answered 21/3, 2014 at 8:38 Comment(6)
What does this do? Where does the .diff file come from?Dercy
its just a patch fileAssail
I think your carat is backwards or something is missing.Dercy
The patch command doesn't tell git that files have been added or removed. The suggestion by zednight (git apply --reject --whitespace=fix mychanges.path) is better, because it deals with added and removed files properly.Missie
The original sign (<) is correct, this way the destination files are determined by the patch file itself. See man patch: patch [options] [originalfile [patchfile]] but usually just patch -pnum <patchfileAlejandroalejo
Please add more details to your answer.Greave
H
6

Many times i have faced such issues when my team mate works on Linux/Windows or uses git send-email.

I always used to follow below command.

git apply -3 --whitespace=fix yourpatch.patch

or

git am -s -3 --whitespace=fix yourpatch.patch

-3 option will try three-way merge and it will help to solve other issues also.

Heriot answered 10/12, 2020 at 9:18 Comment(1)
Seems like this is not working if the very last line is only a newlineMaybellmaybelle
G
3

the one line solution is:

emacs <filename> -f delete-trailing-whitespace -f save-buffer -f kill-emacs

source: https://wiki.gnome.org/Projects/GnomeShell/Development/WorkingWithPatches

Giarla answered 19/1, 2015 at 0:20 Comment(0)
N
2

I think the question of how to cope with the whitespace has been adequately answered, but you asked where it came from. You mentioned ^M at the ends of lines: that’s how Git shows Windows line endings. Maybe try running dos2unix on your source files before creating patches, or use an editor which maintains the original line endings.

Nunatak answered 20/6, 2018 at 14:1 Comment(1)
Actually I think it has not been yetReconcilable
S
0

you may use combinediff with -w option from patchutils [1]

git diff | combinediff - /dev/null -w > patchname.patch

[1]
https://github.com/twaugh/patchutils
https://repology.org/project/patchutils/versions

Suffragan answered 25/10, 2021 at 11:2 Comment(0)
C
0

You can try running fromdos command to fix whitespace errors. Basically, it'll convert the diff file from DOS to Unix format. So, you just need to run:

fromdos mychanges.patch

Then, if you run:

git apply mychanges.patch

there'll be no trailing whitespace errors.

Edit: I think user TRig suggested the same thing, fromdos unix command is an alternative to the suggestion of using dos2unix.

Canaan answered 20/10, 2022 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.