如果成功逆转补丁,是否总是意味着补丁已被完全应用?

Jay*_*Jay 9 patch

这是在两个问题上触摸“检查文件或文件夹已被修补”和“制作patch返回跳过已经应用的补丁时0 ”既不但是有一个令人满意的答案。

我正在编写脚本并想测试以下补丁:

完全应用:继续

部分应用:退出

未应用:如果可以成功应用则继续,否则退出

问题是处理部分应用的情况:

mkdir test && cd test

cat << EOF > foobar.patch
--- /dev/null
+++ foo
@@ -0,0 +1 @@
+foo
--- /dev/null
+++ bar
@@ -0,0 +1 @@
+bar
EOF

patch --forward -i foobar.patch
rm foo
Run Code Online (Sandbox Code Playgroud)

所以 bar 存在但 foo 不存在,因为在某些时候它被删除了。现在,如果我在试运行中向前应用补丁,退出代码为 1,因为它没有成功应用。

$ patch --dry-run --forward --force -i foobar.patch
checking file foo
The next patch would create the file bar,
which already exists!  Skipping patch.
1 out of 1 hunk ignored
$ echo $?
1
Run Code Online (Sandbox Code Playgroud)

不过,这并不能告诉我补丁是否已完全应用,只是它在试运行中失败了。我不知道为什么将其标记为 stackoverflow 答案是正确的。我尝试倒车,但由于它是一个非交互式脚本,因此只能使用强制:

$ patch --dry-run --reverse --force -i foobar.patch
The next patch, when reversed, would delete the file foo,
which does not exist!  Applying it anyway.
checking file foo
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED
checking file bar
$ echo $?
1
Run Code Online (Sandbox Code Playgroud)

那么它是否总是认为,如果我尝试在试运行中强行反转补丁并且成功则补丁被完全应用,如果失败则它没有完全应用(或根本没有应用)?因为如果是这样,那么我可以做类似的事情

patch --dry-run --reverse --force -i foobar.patch ||
(patch --dry-run --forward --force -i foobar.patch &&
 patch --forward --force -i foobar.patch) ||
exit 1
Run Code Online (Sandbox Code Playgroud)

小智 3

有了这个差异:

diff --git a/bar b/bar
new file mode 100644
index 0000000..e69de29
diff --git a/foo b/foo
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/foo
@@ -0,0 +1 @@
+foo
Run Code Online (Sandbox Code Playgroud)

有时候是这样的:

$ cd /tmp/test
$ patch --forward -i foobar.patch
patching file bar
patching file foo
$ echo $?
0
$ rm bar
$ patch --dry-run --reverse --force -i foobar.patch
The next patch, when reversed, would delete the file bar,
which does not exist!  Applying it anyway.
checking file bar
checking file foo
$ echo $?
0
Run Code Online (Sandbox Code Playgroud)

所以你的问题的答案是否定的。