如何在linux中使用在Windows(使用CRLF)中创建的补丁?我

时间:2010-01-16 08:23:01

标签: diff patch

仅针对unix文本文件进行硬编码的标准linux补丁。

PS:我不想将ALL转换为unix,然后将结果转换回来。

5 个答案:

答案 0 :(得分:14)

使用--binary选项。以下是手册页中的相关摘录:

  --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.)

答案 1 :(得分:13)

我在几次之前遇到过这个问题。这是我发现的:

  • Linux patch命令无法识别补丁'meta-lines'中包含CRLF的补丁文件。
  • 实际补丁内容的行尾必须与正在修补的文件的行尾相匹配。

所以这就是我所做的:

  1. 使用dos2unix仅将补丁文件转换为LF行结尾。
  2. 使用dos2unix将正在修补的文件转换为LF行结尾。
  3. 应用补丁。
  4. 如果要维护该约定,可以使用unix2dos将修补后的文件转换回CRLF行尾。

答案 2 :(得分:8)

组合:

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}')

最后一行取决于您是否要保留CRLF。

微米。

PS。这应该是对cscrimge帖子的回复。 DS。

答案 3 :(得分:0)

perl -i.bak -pe's/\R/\n/g' inputfile将任何结尾的行转换为标准。

答案 4 :(得分:0)

这是我们的一个人在我们的办公室提出的解决方案,所以我不赞成它,但它对我有用。

我们有时会在同一个文件中混合使用linux和windows行结尾的情况,我们也会从windows创建补丁文件并将它们应用到linux上。

如果您在Windows上创建补丁文件后遇到补丁问题,或者您有混合行结尾,请执行以下操作:

dos2unix patch-file dos2unix $(sed -n 's/^Index: //p' patch-file) patch -p0 -i patch-file

相关问题