简体   繁体   English

检查 git apply 是否已经应用

[英]check if git apply was already applied

This question is about git apply这个问题是关于git apply

Is there a way to distinguish between: it failed because...有没有办法区分:它失败了,因为......

  1. ... the patch is already applied ...补丁已经应用
  2. ... the source code is really different ...源代码真的不一样

The directory i work in is not a git directory, therefore i can't use git log or something else.我工作的目录不是 git 目录,因此我不能使用git log或其他东西。

I know about the -R (reverse) option, therefore my current workaround is this:我知道-R (反向)选项,因此我目前的解决方法是:

git apply abc.patch || git apply abc.patch -R --check && echo already applied

This way git apply abc.patch -R --check && git apply abc.patch will only be executed if git apply abc.patch fails, it then checks if it failed because the patch is already applied ( git apply abc.patch -R --check ) and if this is the case, it echoes "already applied"这样git apply abc.patch -R --check && git apply abc.patch只会在git apply abc.patch失败时执行,然后检查它是否因为补丁已经应用而失败( git apply abc.patch -R --check ),如果是这种情况,它会回应“已经应用”

But i don't like it, isn't there someting like a builtin-solution in git's apply?但我不喜欢它,在 git 的 apply 中不是有类似内置解决方案的东西吗?

The short answer is no, or at least, not generally.简短的回答是否定的,或者至少一般来说不是。

Consider: a patch is a set of directions saying "remove this line" and "add these other lines".考虑:补丁是一组说明“删除此行”和“添加这些其他行”的说明。 Suppose the patch for file reads, in its entirety:假设整个file读取补丁:

diff --git a/file b/file
index 87cdbbd..3d8696b 100644
--- a/file
+++ b/file
@@ -7,4 +7,3 @@ this file is dull
 this file is dull
 this file is dull
 this file is dull
-this file is dull

In other words, the input file was very dull, at least towards the end, where it just kept repeating "this file is dull".换句话说,输入文件非常枯燥,至少在最后,它只是不断重复“这个文件很枯燥”。

The change to the file was to delete one of those dull lines.对文件的更改是删除其中一条乏味的行。 The context is more, equally dull lines, followed by "end of file".上下文更多,同样乏味的线条,然后是“文件结尾”。

Since the patch was generated, someone modified the top of the file so that it's no longer just 10 (or 9) dull lines, but it still ends with at least four dull lines.自从补丁生成后,有人修改了文件的顶部,使其不再只是 10(或 9)条沉闷的行,但它仍然以至少 4 条沉闷的行结束。 The file is now over 50 lines long with most of the top ones pretty exciting, comparatively at least.该文件现在已经超过 50 行,至少相对而言,大多数顶级文件都非常令人兴奋。

Can you , as a smart human, tell whether or not the patch to this file has been applied yet?能,作为一个聪明的人,知道与否补丁到该文件是否已经被应用? All I will tell you is that the patch may or may not have been applied , along with the other changes that added many exciting lines at the top of the file.我要告诉您的是,补丁可能已应用也可能未应用,以及在文件顶部添加了许多令人兴奋的行的其他更改。

If you can't tell, why would you believe Git can?如果你不知道,你为什么会相信 Git 可以? I don't know about you, but I can't tell if the change that added the exciting lines also deleted the final dull one.我不了解你,但不知道添加激动人心的台词的变化是否也删除了最后的沉闷台词。

(Now, in some cases—especially if you have that index 87cdbbd..3d8696b 100644 line—there is a way to tell, provided that you also have a committed version of the file whose hash ID is 87cdbbd , because now we can extract that particular version of the file. But you haven't said whether you have the index line, and if so, whether you also have a blob with a matching hash ID.) (现在,在某些情况下,特别是如果你有一个index 87cdbbd..3d8696b 100644线路一种方法来告诉,前提是你也有其哈希ID是该文件的提交版本87cdbbd ,因为现在我们可以提取文件的特定版本。但你没有说你是否有索引行,如果有,你是否还有一个带有匹配哈希 ID 的 blob。)

Actually, git apply --reverse --check is the "built-in to git" solution you are looking for.实际上, git apply --reverse --check您正在寻找的“git 内置”解决方案。 For cases like the many identical lines as described in the other answer, all you need to do is make sure your patch file has more/enough context to disambiguate (eg with git diff -U60 ).对于其他答案中描述的许多相同行的情况,您需要做的就是确保您的补丁文件具有更多/足够的上下文来消除歧义(例如使用git diff -U60 )。

Example: if a patch says "remove one of these 50 identical lines leaving 49" there are well-defined outcomes:示例:如果补丁说“删除这 50 条相同的行中的一条,留下 49 行”,则有明确定义的结果:

  • With less than 49 of those lines or more than 50 of those lines, the patch does not apply: both get apply --check and git apply --reverse --check will fail.如果这些行少于 49 行或超过 50 行,则补丁不适用: get apply --checkgit apply --reverse --check都将失败。
  • With exactly 49 of those lines, the patch has already been applied: git apply --check will fail and git apply --reverse --check will succeed.正好有 49 行,补丁已经被应用: git apply --check将失败而git apply --reverse --check将成功。
  • With exactly 50 of those lines, the patch hasn't been applied: git apply --check will succeed and git apply --reverse --check will fail.其中正好有 50 行,补丁尚未应用: git apply --check将成功,而git apply --reverse --check将失败。
  • If get apply --check and git apply --reverse --check both succeed, you need to increase the context for the patch to disambiguate.如果get apply --checkgit apply --reverse --check都成功,则需要增加补丁的上下文以消除歧义。

You could even programmatically test repeatedly incrementing the context for the patch until it works as expected if you weren't doing it by hand.如果您不是手动操作,您甚至可以以编程方式重复增加补丁的上下文,直到它按预期工作为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM