补丁未创建新文件

时间:2018-09-26 04:56:27

标签: bash patch

我正在学习如何使用patch,并且在修补两个目录时遇到了奇怪的行为:

  • original_directory包含一个包含三行的文件(file1)。
  • updated_directory包含两个文件:file1,其中包含两行额外内容,另外一个file2(包含七行内容)。

我发布了diff -Nur original_directory/ updated_directory/ > original_directory.patch。但是,当我执行补丁(patch -p0 < original_directory.patch)时,会得到以下输出:

patching file updated_directory/file1
Reversed (or previously applied) patch detected!  Assume -R? [n] n
Apply anyway? [n] n
Skipping patch.
1 out of 1 hunk ignored -- saving rejects to file updated_directory/file1.rej
The next patch would create the file updated_directory/file2,
which already exists!  Assume -R? [n] n
Apply anyway? [n] n
Skipping patch.
1 out of 1 hunk ignored

但是,当我发出cd original_directory/; patch -p1 < ../original_directory.patch时,一切正常!

我敢肯定patch -p0 < original_directory.patch在前几天工作了...

1 个答案:

答案 0 :(得分:0)

此行为是正常现象。您的补丁文件可能类似于:

diff -Nur original_directory/file1 updated_directory/file1
--- original_directory/file1    2018-09-26 15:00:33.929223318 +0900
+++ updated_directory/file2 2018-09-26 15:00:41.555279201 +0900
...
...
diff -Nur original_directory/file2 updated_directory/file2
--- original_directory/file2    1970-01-01 09:00:00.000000000 +0900
+++ updated_directory/file2 2018-09-26 15:00:49.325037178 +0900
...
...

使用-p1,将删除已更改文件路径的第一部分,因此patch在当前目录中看到对file2file1进行更改的说明

使用-p0时,不会从路径中删除任何路径组件,因此patch看到了对updated_directory/file2updated_directory/file1进行更改的说明,但是这些文件已经具有必需的变化。