我正在学习如何使用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
前几天才工作...
这种行为是意料之中的,也是常见的。您的补丁文件可能看起来像这样:
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
会看到对当前目录中的file2
和file1
进行更改的说明。
使用-p0
,没有路径组件从路径中删除,因此patch
会看到对updated_directory/file2
和updated_directory/file1
进行更改的说明,但这些文件已经具有所需的更改。