简体   繁体   English

如何在Shell脚本中获取git diff命令的output

[英]How to get the output of git diff command in Shell Script

I am trying to perform Git diff in two branches with the same file.我正在尝试在具有相同文件的两个分支中执行 Git diff。 I want to get the difference of two files.我想得到两个文件的区别。

  • If there is a fatal error如果出现致命错误
  • If there is a difference/ not matched如果有差异/不匹配
  • If there is NO difference/ matched如果没有差异/匹配

I tried saving the command in a variable to get the result.我尝试将命令保存在变量中以获取结果。 Example: GIT_DIFF=$(git diff RB_202005:/test.txt RB_202006:/test.txt) and then printing the variable (Example: echo $GIT_DIFF) but nothing is being returned.示例:GIT_DIFF=$(git diff RB_202005:/test.txt RB_202006:/test.txt) 然后打印变量(示例:echo $GIT_DIFF)但没有返回任何内容。

Since you are only interested in the cases "no diff", "diff", "error", I would run a因为你只对“无差异”、“差异”、“错误”的情况感兴趣,所以我会运行一个

git diff --exit-code --quiet .....

--exit-code sets the exit code in the way the normal diff would do. --exit-code以普通diff的方式设置退出代码。

--quiet suppresses the output. --quiet抑制 output。

If the exit code is 0, you don't have differences.如果退出代码为 0,则没有差异。

If the exit code is 1, you have differences.如果退出代码为 1,则说明存在差异。

If the exit code is 2 or 128, you have fatal errors.如果退出代码是 2 或 128,则您有致命错误。

UPDATED As the OP pointed out in the comment, git-diff --exit-code produces status code 128, if the file to be compared does not exist.更新正如 OP 在评论中指出的那样,如果要比较的文件不存在, git-diff --exit-code会产生状态代码 128。 According to the man-page, it should produce the same exit code as the standard diff , which would be 2. Hence it is best to treat any exit code larger than 1 as standard error.根据手册页,它应该产生与标准diff相同的退出代码,即 2。因此最好将任何大于 1 的退出代码视为标准错误。 This would also catch the case that git itself is not found (in which case the shell would likely report exit code 127).这也会捕获git本身未找到的情况(在这种情况下,shell 可能会报告退出代码 127)。

This would not always work with the new (Git 2.30, Q1 2021) git diff -I , should you want to ignore a diff pattern.这并不总是适用于新的(Git 2.30,2021 年第一季度) git diff -I ,如果你想忽略差异模式。

" git diff -I<pattern> -exit-code ( man ) " should exit with 0 status when all the changes match the ignored pattern, but it didn't.当所有更改都匹配忽略的模式时,“ git diff -I<pattern> -exit-code ( man ) ”应该以 0 状态退出,但事实并非如此。

See commit 50f0439 (16 Dec 2020) by Junio C Hamano ( gitster ) .请参阅Junio C Hamano ( gitster )提交 50f0439 (2020 年 12 月 16 日)。
(Merged by Junio C Hamano -- gitster -- in commit 59fcf74 , 18 Dec 2020) (由Junio C Hamano 合并gitster 提交 59fcf74,2020年 12 月 18 日)

diff : correct interaction between --exit-code and -I<pattern> diff--exit-code-I<pattern>之间的正确交互

Just like " git diff -w --exit-code " ( man ) should exit with 0 when ignoring whitespace differences results in no changes shown, if ignoring certain changes with " git diff -I<pattern> --exit-code ( man ) " result in an empty patch, we should exit with 0.就像“ git diff -w --exit-code( man )应该以 0 退出,当忽略空白差异导致不显示任何更改时,如果忽略某些更改与 “ git diff -I<pattern> --exit-code ( man ) ”导致一个空补丁,我们应该以 0 退出。

The test suite did not cover the interaction between " --exit-code " and " -w ";测试套件没有涵盖“ --exit-code ”和“ -w ”之间的交互; add one while adding a new test for " --exit-code " + " -I ".在为“ --exit-code ”+“ -I I”添加新测试时添加一个。

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

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