How do I get patch to ignore carriage returns?
Asked Answered
P

5

28

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?

Packsaddle answered 13/10, 2011 at 13:37 Comment(6)
Does passing your patch file through dos2unix mess it up?Eliott
@Matt, I think the line endings in the patch are already Unix line endings and that is why it is having trouble applying it. I've tried unix2dos on the patch, which had no effect, but applying dos2unix on the file makes the patch apply, but I don't want to commit it with all the whitespace changes.Packsaddle
The file to be patched has Windows line endings?Eliott
Well... don't see anything better than converting both files to Unix-style line endings, and converting the result back to Windows style...Eliott
@Matt, That's a good workaround but there must be a better way. If you create that comment as an answer I will upvote it.Packsaddle
@Eliott unix2dos did the trick for me. So while not an answer for the original asker of the question, it was an answer for me when I had the same question.Jerboa
J
14

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.

Jettiejettison answered 7/6, 2012 at 14:39 Comment(1)
Excellent find. This is invaluable for my work with git in Windows (MinGW) on a repo of CRLF files.Orthoscope
G
8

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

Gomer answered 12/10, 2012 at 19:32 Comment(2)
He wanted to ignore crlf for applying patches (patch command), and not for constructing them (diff command).Twayblade
For diff - please see this question instead: #40974670Nullipore
E
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
Emulsion answered 6/7, 2018 at 20:44 Comment(0)
F
1

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.

Francinefrancis answered 2/9, 2015 at 14:7 Comment(0)
S
-3

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.

Sociolinguistics answered 20/10, 2021 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.