Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Diff Indent/Pretty Print/Beautify Before Diff

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.

like image 538
Hinton Avatar asked May 03 '13 11:05

Hinton


People also ask

What is Pretty Diff?

Pretty Diff - The difference tool Pretty Diff tool can minify, beautify (pretty-print), or diff between minified and beautified code. This tool can even beautify and minify React JSX and many other languages. Pretty Diff, a language aware file comparison tool, beautifier, minifier and parser.

What is git diff in Git?

git diff [<options>] [--] [<path>… ] This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.

How do I view changes in Git git diff?

git diff [<options>] <commit> [--] [<path>… ] This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

What is the difference between git-diff-index and git-diff-tree?

These commands all compare two sets of things; what is compared differs: git-diff-index <tree-ish> compares the <tree-ish> and the files on the filesystem. compares the <tree-ish> and the index. git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>… ]


1 Answers

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.

like image 86
1615903 Avatar answered Sep 23 '22 00:09

1615903