I am trying to create a patch with the command

git diff sourcefile >/var/lib/laymab/overlay/category/ebuild/files/thepatch.patch

when I apply the patch, it gives me

$ patch -v
GNU patch 2.7.5

$ /usr/bin/patch -p1 </var/lib/laymab/overlay/category/ebuild/files/thepatch.patch
patching file sourcefile
Hunk #1 FAILED at 1 (different line endings).
Hunk #2 FAILED at 23 (different line endings).
Hunk #3 FAILED at 47 (different line endings).
Hunk #4 FAILED at 65 (different line endings).
Hunk #5 FAILED at 361 (different line endings).
5 out of 5 hunks FAILED -- saving rejects to file sourcefile.rej

I tried to apply dos2unix to both src file and patch file, but the message don't gone...

UPD: --ignore-whitespace doesn't help too

PATCH COMMAND:  patch -p1 -g0 -E --no-backup-if-mismatch --ignore-whitespace --dry-run -f < '/var/lib/layman/dotnet/dev-dotnet/slntools/files/remove-wix-project-from-sln-file-v2.patch'

checking file Main/SLNTools.sln
Hunk #1 FAILED at 14 (different line endings).
Hunk #2 FAILED at 49 (different line endings).
Hunk #3 FAILED at 69 (different line endings).
Hunk #4 FAILED at 102 (different line endings).
4 out of 4 hunks FAILED

UPD: found a very good article: https://stackoverflow.com/a/4425433/1709408

  • Try sed -i.bak -e 's/\r$//g' something. I don't think dos2unix handles mixed end-of-lines as aggressively as you may want. – Arthur2e5 Oct 29 '15 at 18:57
  • Outright evil; if you have your patch with CF-LF line endings, same as files, it will first happily strip the CR from your patch, then throw a fit that line endings (which it just broke) don't match. – SF. Feb 8 at 15:24

I had the same problem using the patchcommand that comes with MSYS2 on Windows. In my case both the source file and the patch had CRLF line-ending, and converting both to LF didn't work either. What worked was the following:

$ dos2unix patch-file.patch
$ patch -p1 < patch-file.patch
$ unix2dos modified-files...

patch will convert the line-endings to LF on all the patched files, so it's necessary to convert them back to CRLF.

Obs: the patch version I'm using is 2.7.5


You can usually work around this using the -l option:

use the -l or --ignore-whitespace option, which makes patch compare blank characters (i.e. spaces and tabs) loosely so that any nonempty sequence of blanks in the patch file matches any nonempty sequence of blanks in the input files

This is a standard feature (see POSIX patch description).

However, OP amended the question to comment on How line ending conversions work with git core.autocrlf between different operating systems, and added an example hinting that the problem is seen with files on Windows (in contrast to the Unix-style example). While patch tries to accommodate mismatches between CRLF and LF line-endings, it has a bias to presume that the latter is used. If the patch file had CRLF endings, while the files to be patched did not, it would recover as in this example log:

(Stripping trailing CRs from patch.)
patching file xterm.log.html
(Stripping trailing CRs from patch.)
patching file xterm.man
(Stripping trailing CRs from patch.)
patching file xtermcfg.hin

Checking the source code, in the similar function, GNU patch treats whitespace as space and Tab, with some special handling according to whether the lines have a trailing LF. CR is not mentioned. It does pay attention in check_line_endings, but uses that information only as part of a message to help diagnose a rejection. It strips the trailing CRs in pget_line unless the --binary option is given.

GNU patch does not have an option to tell it to transform a patch with LF endings into CRLF to apply to files whose line-endings are CRLF. To use it reliably for this case, the choices are

  • convert all of the files to use LF endings, or
  • convert all of the files to use CRLF endings and add the --binary option.
  • 5
    --ignore-whitespace (no second dash) doesn't help too, i updated question – user1709408 Dec 3 '15 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.