Patch

當新舊包含不同數量 的子目錄時使用更新檔 -p 選項

  • January 15, 2021

我收到了一個需要應用的更新檔,但是文件名很難預測要使用的 -p 選項:

--- /home/user/foo/bar/baz.c
+++ /data/myproj/project/version/foo/bar/baz.c

我將它應用到包含 foo/bar/baz.c 的目錄中,但我不確定要使用哪個更新檔號。-p4失敗了,儘管這是我對第二行的期望。p2也失敗了,但這就是我從第一行所期望的。

我應該使用哪一個?或者我是否應該期望僅當 +++ 和 — 的前導目錄數量相同時才適用更新檔?

或者我是否應該期望僅當 +++ 和 — 的前導目錄數量相同時才適用更新檔?

是的,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>

引用自:https://unix.stackexchange.com/questions/629357