10

Is there a way to make Git indent/beautify/pretty print two versions of C++ source files before diffing them?

I don't want Git to show me the myriads of changes introduced after someone auto-formatted the code.

Example usage: I hit git difftool --indent-before-diffing path/to/file and get the changes after both the original version of path/to/file and the modified version of path/to/file have been indented.

Hinton
  • 2,320
  • 4
  • 26
  • 32

1 Answers1

13

If you can find an application that does the indenting for you, you could use the method described here for odt files:

Add the following line to your .gitattributes file:

*.odt diff=odt

Now set up the odt diff filter in .git/config:

[diff "odt"]
    binary = true
    textconv = /usr/local/bin/odt-to-txt

So for C++ files it would be something like this:

*.cpp diff=cpp

And in the .git/config:

[diff "cpp"]
    textconv = /path/to/indenter

As pointed out in the comments, GNU Indent can be used for indenting.

1615903
  • 32,635
  • 12
  • 70
  • 99
  • 1
    This is an extremely good answer!! Thanks! It still has the slight downside that I can't easily (i.e. by a command-line switch) turn the feature off. I'll mark this as the correct answer in a few days unless someone comes up with a similar solution that can be enabled/disabled directly in the `git diff` command. – Hinton May 03 '13 at 11:28
  • 1
    Such an "application" would be the GNU `indent`, in the unlikely case that someone hasn't heard of it before ... – Hinton May 03 '13 at 11:31