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

发布于 2024-08-18 14:00:05 字数 75 浏览 4 评论 0原文

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

PS:我不想将所有内容转换为unix,然后将结果转换回来。

Standard linux patch hard-coded only for unix text files.

PS: I do no want convert ALL to unix and then convert result back.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆悲凉 2024-08-25 14:00:05

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

  • Linux patch 命令不会识别补丁“元行”中包含 CRLF 的补丁文件。
  • 实际补丁内容的行结束符必须与正在修补的文件的行结束符匹配。

这就是我所做的:

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

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

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:

  1. Use dos2unix to convert patch files to LF line-endings only.
  2. Use dos2unix to convert the files being patched to LF line-endings only.
  3. Apply patch.

You can use unix2dos to convert patched files back to CRLF line-endings if you want to maintain that convention.

风柔一江水 2024-08-25 14:00:05

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

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.)
枕花眠 2024-08-25 14:00:05

组合:

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。

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.

爱的故事 2024-08-25 14:00:05

这是我们办公室的一个人提出的解决方案,所以我不会因此而获得荣誉,但它在这里对我有用。

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

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


dos2unix 补丁文件
dos2unix $(sed -n 's/^Index: //p' 补丁文件)
patch -p0 -i 补丁文件

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

蓝海似她心 2024-08-25 14:00:05

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

perl -i.bak -pe's/\R/\n/g' inputfile to convert any line ending to the standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文