How to use patches created in windows (with CRLF) in linux?I
Standard linux patch hard-coded only for unix text files.
PS: I do no want con开发者_Go百科vert ALL to unix and then convert result back.
I've run into this problem before a few times. This is what I've discovered:
- The Linux patch command will not recognize a patchfile that has CRLF in the patch 'meta-lines'.
- The line-endings of the actual patch content must match the line endings of files being patched.
So this is what I did:
- Use dos2unix to convert patch files to LF line-endings only.
- Use dos2unix to convert the files being patched to LF line-endings only.
- Apply patch.
You can use unix2dos to convert patched files back to CRLF line-endings if you want to maintain that convention.
Use the --binary option. Here is the relevant snippet from the man page:
--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. This option is needed
on POSIX systems when applying patches generated on non-POSIX systems to non-POSIX files. (On
POSIX systems, file 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.)
Combined:
dos2unix patchfile.diff
dos2unix $(grep 'Index:' patchfile.diff | awk '{print $2}')
patch --verbose -p0 -i patchfile.diff
unix2dos $(grep 'Index:' patchfile.diff | awk '{print $2}')
The last line depends on whether you want to keep the CRLFs or not.
M.
PS. This should've been a reply to cscrimge's post. DS.
This is a solution one of our guys came up with in our office, so I'm not taking credit for it but it works for me here.
We have a situation of mixed linux and windows line endings in the same file sometimes, and we also create patch files from windows and apply them on linux.
If you are experience a patch problem after creating your patch file on windows or you have mixed line endings then do this:
dos2unix patch-file
dos2unix $(sed -n 's/^Index: //p' patch-file)
patch -p0 -i patch-file
perl -i.bak -pe's/\R/\n/g' inputfile
to convert any line ending to the standard.
精彩评论