0

I would like git diff to compare only within a regex. For example:

Contents of file at Revision A:

A 0 1 
A 1 1 2
A 0 1 2 3
A 1 1 2 3 4

Contents of file at Revision B:

B 0 1
B 0 1 2
B 1 1 2 4
B 1 1 2 4 8 16

I might want to ignore the first word (ie 'A' or 'B'), match the next two words, and ignore the rest of the line. In the above, only the second and third lines should be reported.

How to grep the git diff? indicates that git -G used to do what I want but it's behavior has changed. Is there a way to get this to happen (short of doing something like diff <(git show ... | awk ...) <(git show ... | awk ...))?

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • If you're comparing two files (not revisions), would it not make more sense to use diff? Why involve git? – Schwern Jan 15 '15 at 07:26
  • Sorry, the example was a bit misleading. I'm actually trying to compare two revisions. I'll fix the example to be more representative. – Noel Yap Jan 15 '15 at 13:46

1 Answers1

2

diff lets you change the format of the diff. You want to change the content, and in a specialized way. Using diff, git show and awk (or similar) to accomplish this is correct. It follows the Unix philosophy of piping text between small tools.

What I would suggest is to set this up either as a git command alias or write a shell script like ~/bin/git-column-diff. Then you can git column-diff file1 file2.

You could abuse the textconv feature to do the filtering for you, but that would require the files having special extensions, and it would interfere with diffing the files all the time, and you'd have to always want the same columns diffed.

You could write a program that does your customized diff and set GIT_EXTERNAL_DIFF to point to this program.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I went with the `diff <(git show | awk) <(git show | awk)` solution. The extra info helped in making this decision. I'll think about creating an alias if this need happens often enough. Thanks. – Noel Yap Jan 15 '15 at 14:21