【问题标题】:How to use patches created in windows (with CRLF) in linux?I如何在 linux 中使用在 windows (with CRLF) 中创建的补丁?
【发布时间】:2010-01-16 08:23:00
【问题描述】:

标准 linux 补丁硬编码仅适用于 unix 文本文件。

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

【问题讨论】:

  • dos2unix 命令会修复文件吗?
  • 问题可以通过将所有源转换为 unix 并在转换回来后解决。但这很糟糕。
  • 为什么在 Unix 系统上有(显然很多)带有 CRLF 行结尾的文本文件?
  • 以前在win32上交叉开发的ARM源代码。

标签: diff patch


【解决方案1】:

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

  • Linux 补丁命令无法识别补丁“元行”中包含 CRLF 的补丁文件。
  • 实际补丁内容的行尾必须与正在修补的文件的行尾一致。

这就是我所做的:

  1. 仅使用 dos2unix 将补丁文件转换为 LF 行结尾。
  2. 使用 dos2unix 将正在修补的文件仅转换为 LF 行结尾。
  3. 应用补丁。

如果您想保持该约定,可以使用 unix2dos 将修补文件转换回 CRLF 行结尾。

【讨论】:

    【解决方案2】:

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

      --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。太糟糕了,没有(?)类似的东西:尝试在没有二进制标志的情况下进行修补,对于那些失败的二进制标志重试。
    • “patch
    【解决方案3】:

    综合:

    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。

    M.

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

    【讨论】:

    • 对我有用的是上面的方法,但是 grepping 是 '+++' 而不是 'Index:'。
    【解决方案4】:

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

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

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 谢谢,有兴趣,但不适用于我的情况。
      • 如果差异基于行,它会回答标题中的问题,因为它们通常是。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多