Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use 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?

like image 685
eckes Avatar asked Feb 23 '11 09:02

eckes


1 Answers

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.

like image 156
VonC Avatar answered Sep 21 '22 02:09

VonC