有没有办法比较两个差异或补丁?
有没有办法测试两个差异或补丁是否等效?
假设您有以下 git 提交历史记录,其中功能 F 和 G 可以完全重新设置为 E:
G
/
A--B--C--D--E
\
F
由于当前部署过程中的限制,我们有以下有点相关的图表(它不受版本控制) )
G'
/
------------E'
\
F'
F'和G'最终将按照一些待确定的顺序应用于头部E',因此最终会像
------------E'--G'--F'
Is there a way to test that diff from E' to G' is the same as the patch由 G 的 git 提交产生乙?
我完全意识到,在理想的世界中,版本控制可以解决这个问题,我们正在实现这一目标,但这不是我们目前的处境。
您基本上可以在单独的签出中播放两个补丁并比较输出,但这似乎有点笨拙。我假设比较差异本身是行不通的,因为行号可能会改变。即使 G' 和 F' 重新基于 E',F' 的补丁最终也会应用到 G',从而使补丁的 diff 上下文不同。
Is there a way to test if two diffs or patches are equivalent?
Let's say you have the following git commit history, where features F and G are cleanly rebaseable to E:
G
/
A--B--C--D--E
\
F
Due to limitations in our current deployment process, we have the following, somewhat related graph (it's not version controlled)
G'
/
------------E'
\
F'
F' and G' will ultimately be applied to the head E', in some to be determined order, so it would end up like
------------E'--G'--F'
Is there a way to test that the diff from E' to G' is the same as the patch produced by the git commit of G from B?
I fully realize that in an ideal world, revision control would solve this, and we're getting there, but that's not where we are currently.
You could essentially play both patches on separate checkouts and compare the outputs, but that seems kind of clunky. And comparing the diffs themselves, I'm assuming, wouldn't work because line numbers could change. Even if G' and F' were rebased to E', the patch for F' would ultimately be applied to G', making the diff context of the patch different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 git 2.19 开始,有一个额外的工具: git range-diff
所以您可能想要:
即使对于每个范围中的多个补丁也应该有效。
Since git 2.19 there is an extra tool for that: git range-diff
So you probably want:
This should work even for more than one patch in each range.
为了读者的利益,这里是对@mrbrdo的答案的更新,并做了一些调整:
git sdiff
别名以便于访问。sdiff
代表show diff
。^{}
后缀忽略带注释的标签标头。diff
选项,例如-u
等。在使用
git
的每个帐户中运行一次:之后您可以使用this:
请注意,这需要
bash
版本 3 或更高版本。解释:
git config --global alias.sdiff
将名为git sdiff
的别名添加到全局~/.gitconfig
中。!
将别名作为 shell 命令运行bash -c
我们需要bash
(或ksh
),因为<(..)
在dash
中不起作用(又名。<代码>/bin/sh)。<代码>O=(); A=();而x=“$1”;转移;在 -*) O+=("$x"); 中执行 case $x *) A+=("$x^{}");;经济及社会事务委员会; done; 分隔选项(
-something
)和参数(其他所有内容)。选项位于数组O
中,而参数位于数组A
中。请注意,所有参数也会附加^{}
,这会跳过注释(以便您可以使用带注释的标签)。g(){ git show "${A[$1]}" &&返回;回显失败${A[$1]}; git show "${A[$2]}"; };
创建一个辅助函数,它对第一个参数执行 git show 操作。如果失败,则输出“FAIL first-argument”,然后输出第二个参数。这是一个在出现故障时减少开销的技巧。 (适当的错误管理太过分了。)diff "${O[@]}" <(g 0 1) <(g 1 0)
运行diff
使用针对第一个参数和第二个参数的给定选项(使用所述 FAIL 错误回退到另一个参数以减少diff
)。--
允许将diff
-options (-something
) 传递给此别名/脚本。错误:
未检查参数数量。第二个参数后面的所有内容都将被忽略,如果您给出的参数太少,您只会看到
FAIL
或根本看不到任何内容。错误表现得有点奇怪并且使输出混乱。如果您不喜欢这样,请使用
2>/dev/null
运行它(或适当更改脚本)。如果出现问题,它不会返回错误。
您可能希望默认情况下将其提供给寻呼机。
请注意,定义更多别名很容易,例如:
我希望这不需要进一步解释。
对于更多类似的别名,也许可以查看 我的 GitHub 存储库
For the benefit of the reader, here is an update to the answer of @mrbrdo with a little tweak:
git sdiff
alias for easy access of this.sdiff
stands forshow diff
.^{}
suffix.diff
options to be used like-u
etc.Run this once in each of your accounts in which you use
git
:Afterwards you can use this:
Please note that this needs
bash
version 3 or above.Explained:
git config --global alias.sdiff
adds an alias namedgit sdiff
into the global~/.gitconfig
.!
runs the alias as shell commandbash -c
we needbash
(orksh
), as<(..)
does not work indash
(aka./bin/sh
).O=(); A=(); while x="$1"; shift; do case $x in -*) O+=("$x");; *) A+=("$x^{}");; esac; done;
separates options (-something
) and arguments (everything else). Options are in arrayO
while arguments are in arrayA
. Note that all arguments get^{}
attached, too, which skipps over annotations (such that you can use annotated tags).g(){ git show "${A[$1]}" && return; echo FAIL ${A[$1]}; git show "${A[$2]}"; };
creates a helper function, which doesgit show
for the first argument. If that fails, it outputs "FAIL first-argument" and then outputs the second argument. This is a trick to reduce the overhead in case something fails. (A proper error managment would be too much.)diff "${O[@]}" <(g 0 1) <(g 1 0)
runsdiff
with the given options against the first argument and the second argument (with said FAIL-error fallback to the other argument to reduce thediff
).--
allows to passdiff
-options (-something
) to this alias/script.Bugs:
Number of arguments is not checked. Everything behind the 2nd argument is ignored, and if you give it too few, you just see
FAIL
or nothing at all.Errors act a bit strange and clutter the output. If you do not like this, run it with
2>/dev/null
(or change the script appropriately).It does not return an error if something breaks.
You probably want to feed this into a pager by default.
Please note that it is easy to define some more aliases like:
I hope this does not need to be explained further.
For more aliases like that, perhaps have a look at my GitHub repo
我一直在寻找答案,虽然
diff <(git show XYZ) <(git show ABC)
有效,但它仍然在 diff 中显示提交 ID,您需要使用结果。使用 git diff-tree ,您可以执行以下操作:这仅比较两个差异,您不必考虑进行比较的提交 ID。
I was looking for an answer and while
diff <(git show XYZ) <(git show ABC)
works, it still shows the commit ID in the diff and you need to account for that with the result. Withgit diff-tree
you can do this:This compares only the two diffs and you don't have to account for the commit ID being diffed.