linux - 补丁对差异文件过于严格

标签 linux ubuntu diff gnu patch

我需要一种自动方式来向配置文件添加一行。我使用 diff 创建了一个 .diff 文件,我想用 patch 应用它。但是,我需要修补的文件可能会在其他地方稍作修改。例如。在配置文件的其他地方添加了一些其他行。

我假设 patch 实际上会对此很聪明,并弄清楚补丁是否仍然可以在没有冲突的情况下应用,但是每当我尝试将补丁应用到配置文件时我都会收到错误消息与原始文件略有不同。

我是否需要将任何其他标志传递给 patchdiff 以使其不那么严格?我正在使用

生成 .diff 文件
diff -ruN oldfile.conf newfile.conf > mypatch.diff

然后使用打上补丁

patch myconfig.conf mypatch.diff

我想要的是补丁将尝试相当自由地应用补丁,如果它不知道如何应用,则只返回警告,而不是错误。目前我看到这些错误:

jeroen@jeroen-ubuntu:~/Desktop$ diff -ruN default.conf default_new.conf > mypatch.diff
jeroen@jeroen-ubuntu:~/Desktop$ patch default2.conf mypatch.diff 
patching file default2.conf
Hunk #1 FAILED at 28.
1 out of 1 hunk FAILED -- saving rejects to file default2.conf.rej
jeroen@jeroen-ubuntu:~/Desktop$

最佳答案

I assumed that patch would actually be smart about this and figure out if the patch could still be applied without conflicts, but I am getting an error whenever I try to apply the patch to a config file that is slightly different from the original file.

其实是这样的。但是,这在很大程度上取决于您对“略有不同”的定义。基本上,更改需要远离补丁修改的地方。

您可以尝试使用 -F 参数调用 patch

-F num or --fuzz=num

Set the maximum fuzz factor. This option only applies to diffs that have context, and causes patch to ignore up to that many lines of con- text in looking for places to install a hunk. Note that a larger fuzz factor increases the odds of a faulty patch. The default fuzz fac- tor is 2. A fuzz factor greater than or equal to the number of lines of context in the context diff, ordinarily 3, ignores all context.

但我不建议这样做。

只要你想

an automated way to add a line to a configuration file

您是否考虑过使用 perlawked 来完成这项工作?我认为 patch 不是适合它的工具。

关于linux - 补丁对差异文件过于严格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657102/

相关文章:

windows - 对从 DLL 或 SO 导出的符号的写入权限

ruby - 如何在 ubuntu 14.04 32 位中将 ruby​​ 从 1.9.3 更新到 2.3.1?

mongodb - Mongoexport 保持在 0%

diff - 需要一个工具来递归比较两个.ear文件

diff - 与剪贴板的三向差异

linux - 用于读取包含 IP 地址和登录的文件的脚本

linux - tr -ct 有什么作用?

python - 加载在 linux 中 pickle 的数据在 mac 中失败

python - pysftp 无法创建日志文件 - 权限被拒绝

linux - 如何实现diff功能?