How to fix "Hunk #1 FAILED at 1 (different line endings)" message?

59,107

Solution 1

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

Solution 2

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.
Share:
59,107

Related videos on Youtube

user1709408
Author by

user1709408

Updated on September 18, 2022

Comments

  • user1709408
    user1709408 over 1 year

    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

  • user1709408
    user1709408 over 8 years
    --ignore-whitespace (no second dash) doesn't help too, i updated question
  • Danijel
    Danijel over 4 years
    This didn't work for me in Cygwin. What worked is applying dos2unix on a target file that was to be patched.
  • Danijel
    Danijel over 4 years
    Didn't work for me, on Cygwin too.
  • buzz3791
    buzz3791 almost 4 years
    Thanks, Thomas. Using your guidance, I ended up splitting my patch into files that have LF line endings and files that have CRLF line endings. Then used the patch --binary flag for the patch with CRLF. Context: creating a patch for the TCL 8.4.20 source when compiling with Visual Studio 2019 on Windows 10. The TCL source has a mixture of files with CRLF endings (e.g. Visual Studio project files and nmake makefiles) and files with LF endings (*.c and *.h files, etc.).
  • user5534993
    user5534993 about 3 years
    For me it worked to just to apply unix2dos to the patch file before applying the patch.