如何使用 git diff -G?

发布于 2024-10-19 02:17:26 字数 1683 浏览 4 评论 0 原文

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

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

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

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

-G<正则表达式>
查找添加或删除的行与给定的 匹配的差异。

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

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

git diff -GOutputFingerprint

但这是错误的:

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

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

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

此外,简单地在 -GOutputFingerprint 之间放置一个空格code> 不起作用:

> 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 的示例吗?

I'm writing a little test suite that runs the tool to be tested over a bunch of input files. For each input file, a corresponding output file is being created by the tool (both are in XML). Input and output files are checked in on a Git repo.

The output files carry the time when the tool was compiled, so the output files are surely modified after they were re-created by the tool being tested.

To have a quick glimpse if the output has changed (when I modified the sources of the tool), I'd like to check whether the content of the node OutputFingerprint has changed (simple hash over the contents of the relevant parts of the output file).

Reading the manual for git-diff, I found that there's a -G option:

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

Unfortunately, they provide no example for how to use the -G option.

My first thought was to simply write

git diff -GOutputFingerprint

but that's wrong:

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

Next thought was to put the regex in slashes, which is also not successful:

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

Also, simply putting a space between -G and OutputFingerprint doesn't work:

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

When I call git diff without any params, I get the modification:

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

Another thought was to use git diff -S".*OutputFingerprint.*" --pickaxe-regex, but this is also not successful.

git --version gives me git version 1.7.3.1.msysgit.0 if that is relevant.

When I ask git diff --help, the provided manual shows me the -G option.

So, could anyone provide me an example for how to use git diff -G correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-10-26 02:17:26
git diff -Garegex

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

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

OP eckes 报告它可以工作(仅适用于 msysgit1.7.4+),并添加 diff-G 选项的 code> 手册页 包括:

“查找添加删除行与给定正则表达式匹配的差异”,
这与 diff 输出相关其中更改的行被打印为添加的一行和删除的行


示例:我添加一个新行“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 是不可能的(无论如何它已经使用了正则表达式)。

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

pickaxe:当-G--pickaxe-regex组合时死亡

签字人:Ævar Arnfjörð Bjarmason

-G--pickaxe-regex 选项组合在一起时,我们只需忽略 --pickaxe-regex 选项。
让我们按照我们的文档的建议去死吧,因为 -G 始终是正则表达式。

--pickaxe-regex 添加到 d01d8c6 ("支持镐匹配正则表达式", 2006-03-29, Git v1.3.0-rc4 -- merge) 仅存在 -S 选项。
然后当 f506b8e ("git log/diff: 在补丁文本中添加 grep 的 -G", 2010-08-23, Git v1.7.4-rc0 -- merge--pickaxe-regex 的文档都没有相应更新,也没有添加类似此断言的内容。

自从5bc3f0b(“diffcore-pickaxe文档:文档-S和-G正确”,2013-05-31,Git v1.8.4-rc0 -- 合并 )我们声称 --pickaxe-regex 只能与 -S 一起使用,但默默地容忍将其与 -G 结合使用,我们还是去死吧。

git diff -Garegex

runs fine with msysgit 1.7.4, but only in a bash session (in a DOS session, it returns nothing)

It will display the diff if the regex matches a line added or removed from the staged version.
added or removed: not changed.
Meaning in your case, it won't work

The OP eckes reports it works (with msysgit1.7.4+ only), adding that the diff man page for -G option includes:

"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.


Example: I add a new line 'aaa' (without git add: just a simple edit)

$ 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

Note that with Git 2.33 (Q3 2021), using --pickaxe-regex (normally reserved for -S) won't be possible with -G (which already uses a regex anyway).

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) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 4da281e, 13 Jul 2021)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文