git - 我如何使用 git diff -G?

标签 git msysgit git-diff

我正在编写一个小测试套件,用于运行要在一堆输入文件上进行测试的工具。对于每个输入文件,该工具正在创建一个相应的输出文件(两者都是 XML 格式)。输入和输出文件 checkin Git 存储库。

输出文件带有编译工具的时间,因此输出文件在被测试工具重新创建后肯定会被修改。

为了快速了解输出是否已更改(当我修改工具的源代码时),我想检查节点 OutputFingerprint 的内容是否已更改(简单哈希输出文件相关部分的内容)。

阅读 git-diff 的手册,我发现有一个 -G 选项:

-G<regex>
Look for differences whose added or removed line matches the given <regex>.

不幸的是,它们没有提供如何使用 -G 选项的示例。

我的第一个想法是简单地写

git diff -GOutputFingerprint

但这是错误的:

> git diff -GOutputFingerprint
error: invalid option: -GOutputFingerprint

接下来的想法是将正则表达式放在斜杠中,但也没有成功:

> git diff -G/OutputFingerprint/
error: invalid option: -GC:/Program Files/Git/OutputFingerprint/

此外,简单地在 -GOutputFingerprint 之间放置一个空格是行不通的:

> git diff -G OutputFingerprint
fatal: ambiguous argument 'OutputFingerprint': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

当我在没有任何参数的情况下调用 git diff 时,我得到了修改:

-    <OutputFingerprint>e45807ca76fc5bb78e9e928c6fb7eed6</OutputFingerprint>
+    <OutputFingerprint>d0846851bc9d52b37f7919def78660a2</OutputFingerprint>

另一个想法是使用 git diff -S".*OutputFingerprint.*"--pickaxe-regex,但这也不成功。

git --version 给我 git version 1.7.3.1.msysgit.0 如果相关的话。

当我询问 git diff --help 时,提供的手册向我显示了 -G 选项。

那么,谁能给我一个如何正确使用 git diff -G 的例子?

最佳答案

git diff -Garegex

在 msysgit 1.7.4 上运行良好,但仅在 bash session 中(在 DOS session 中,它不返回任何内容)

如果正则表达式与暂存版本中添加删除的行相匹配,它将显示diff
添加删除:未更改
意思是在你的情况下,它不会工作

OP eckes 报告它有效(仅适用于 msysgit1.7.4+),并补充说 -G 选项的 diff man page 包括:

"Look for differences whose added or removed line matches the given regex",
which relates to the diff output where a changed line is printed as one added and one removed line.


示例:我添加了一个新行 'aaa'(没有 git add:只是一个简单的编辑)

$ git diff -Gaaa

diff --git a/conf/fragments/xxx_repos.conf b/conf/fragments/xxx_repos.coindex 263fa85..3475f9b 100644
--- a/conf/fragments/xxx_repos.conf
+++ b/conf/fragments/xxx_repos.conf
@@ -10,3 +10,4 @@ repo xxx-sdsfwk
     RW+                                = vonc user1 user2
     RW                                 = @xxx_users
 xxx-sdsfwk "Owner" = "for..."
+aaa

请注意,对于 Git 2.33(2021 年第 3 季度),使用 --pickaxe-regex(通常为 -S 保留)将无法使用 - G(反正已经使用了正则表达式)。

See commit 5d93460 , commit 22233d4 , commit f97fe35 , commit fa59e7b , commit 9e20442 , commit a8d5eb6 , commit 5b0672a , commit 5d35a95 , commit 52e011c , commit 2e197a7 , commit 03c1f14 , commit d90d441 , commit 102fdd2 , commit a47fcbe , commit d26ec88 , commit 188e9e2 , commit 7cd5d5b , commit 064952f , commit 69ae930 , commit c960939 , commit 6d0a401 , commit ecbff14 (12 Apr 2021 年)作者:Ævar Arnfjörð Bjarmason (avar)
(由 Junio C Hamano -- gitster -- merge 到 commit 4da281e ,2021 年 7 月 13 日)

pickaxe: die when -G and --pickaxe-regex are combined

Signed-off-by: Ævar Arnfjörð Bjarmason

When the -G and --pickaxe-regex options are combined we simply ignore the --pickaxe-regex option.
Let's die instead as suggested by our documentation, since -G is always a regex.

When --pickaxe-regex was added in d01d8c6 ("Support for pickaxe matching regular expressions", 2006-03-29, Git v1.3.0-rc4 -- merge) only the -S option existed.
Then when -G was added in f506b8e ("git log/diff: add -G that greps in the patch text", 2010-08-23, Git v1.7.4-rc0 -- merge) neither the documentation for --pickaxe-regex was updated accordingly, nor was something like this assertion added.

Since 5bc3f0b ("diffcore-pickaxe doc: document -S and -G properly", 2013-05-31, Git v1.8.4-rc0 -- merge) we've claimed that --pickaxe-regex should only be used with -S, but have silently tolerated combining it with -G, let's die instead.

关于git - 我如何使用 git diff -G?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5088907/

相关文章:

Git diff 不显示新添加文件的更改

git diff - 显示行尾更改?

git - Jenkins 使用变量 ${GIT_BRANCH} 作为 sonarqube 参数构建而不使用 "origin/"

linux - sudo -H -u git gitosis-init < ~/id_rsa.pub |错误 : no such file or directory

git - 如何清除 GIT 存储库

windows - 在 windows 上轻松解析 msysgit 中的文件权限

git - msysgit 的麻烦

git - 如何告诉 `git diff <commit>` 区分过去在 `<commit>` 中跟踪的未跟踪文件?

svn - 版本控制与自己协调

Github - 如何在我的个人资料中显示协作项目?