'How do I get patch to ignore carriage returns?

I'm attempting to apply a patch to a file with Windows line endings on a Linux system and I'm getting conflicts due to the carriage returns in the file.

The -l option (ignore whitespace) isn't ignoring the EOL characters. Is there anyway to get patch to ignore windows style line endings?



Solution 1:[1]

Try using the --binary option, from the manpage (emphasis mine)

--binary

Write all files in binary mode, except for standard output and /dev/tty. When reading, disable the heuristic for transforming CRLF line endings into LF line endings. (On POSIX -conforming systems, reads and writes never transform line endings. On Windows, reads and writes do transform line endings by default, and patches should be generated by diff --binary when line endings are significant.)

I don't fully understand the above, but it worked for me on a Linux machine to apply a Unix patch onto a DOS file.

Solution 2:[2]

Here's a link http://www.chemie.fu-berlin.de/chemnet/use/info/diff/diff_2.html

The -w' and--ignore-all-space' options ignore difference even if one file has white space >where the other file has none. White space characters include tab, newline, vertical tab, >form feed, carriage return, and space

Run diff like: diff -w file1.txt file2.txt

Solution 3:[3]

I work around this using the following commands to convert all files of interest to unix line endings.

dos2unix `grep Index\: mixed-line-ending.patch | sed -e 's/Index\://'`
dos2unix mixed-line-ending.patch
patch -p0 < mixed-line-ending.patch

Solution 4:[4]

I had this problem with a diff that was manually copied and pasted from git diff console output, into a patch file with LFs. To get that patch file to work again - to be able to be applied on the actual files that were using CRs and LFs - several things had to be done manually:

  • find all instances of "^M" and drop them
  • add CR to all lines within the hunks - but not the meta format lines (@@ etc)
  • on all lines within hunks that were empty, add the missing space in the first column

joe syntax highlighting was very helpful there, because it colored hunks properly as soon as I fixed them.

Solution 5:[5]

Tell patch to ignore white space:

     -l, --ignore-whitespace
             Causes the pattern matching to be done loosely, in case the tabs
             and spaces have been munged in your input file.  Any sequence of
             whitespace in the pattern line will match any sequence in the
             input file.  Normal characters must still match exactly.  Each
             line of the context must still match a line in the input file.

This ignores mismatches of EOLs too -- at least, on FreeBSD, using patch version 2.0-12u11.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon
Solution 2 Саша Зезюлинский
Solution 3 Jack Kelly
Solution 4 Josip Rodin
Solution 5