![当新旧目录包含不同数量的子目录时使用 patch -p 选项](https://linux22.com/image/186501/%E5%BD%93%E6%96%B0%E6%97%A7%E7%9B%AE%E5%BD%95%E5%8C%85%E5%90%AB%E4%B8%8D%E5%90%8C%E6%95%B0%E9%87%8F%E7%9A%84%E5%AD%90%E7%9B%AE%E5%BD%95%E6%97%B6%E4%BD%BF%E7%94%A8%20patch%20-p%20%E9%80%89%E9%A1%B9.png)
我收到了需要应用的补丁,但是文件名使得很难预测要使用什么 -p 选项:
--- /home/user/foo/bar/baz.c
+++ /data/myproj/project/version/foo/bar/baz.c
我将其应用到包含 foo/bar/baz.c 的目录中,但我不确定要使用哪个补丁号。-p4
失败了,尽管这是我对第二行的预期。p2
也失败了,但这正是我从第一行所期望的。
我应该使用哪一个?或者我是否应该期望补丁仅在 +++ 和 --- 的前导目录数相同的情况下才适用?
答案1
或者我应该期望补丁仅在 +++ 和 --- 的前导目录数量相同的情况下才适用?
是的,man patch
提到了:
If the recipient is supposed to use the -pN option, do not send output
that looks like this:
diff -Naur v2.0.29/prog/README prog/README
--- v2.0.29/prog/README Mon Mar 10 15:13:12 1997
+++ prog/README Mon Mar 17 14:58:22 1997
because the two file names have different numbers of slashes, and dif‐
ferent versions of patch interpret the file names differently. To
avoid confusion, send output that looks like this instead:
diff -Naur v2.0.29/prog/README v2.0.30/prog/README
--- v2.0.29/prog/README Mon Mar 10 15:13:12 1997
+++ v2.0.30/prog/README Mon Mar 17 14:58:22 1997
您在这里可以做的是标准化补丁并像这样应用它:
sed 's,--- /home/user/foo/,--- /data/myproj/project/version/foo/,' orig.patch | patch <other_patch_options>