Search code examples
gitpatchgit-diffdiffstat

How to have git stats on a diff file?


While in a git repository, I can do :

git log --oneline --stat

and I'll have a nice output looking like:

ca0ab77 commit message
a/file/changed | 19 -------------------
1 file changed, 19 deletions(-)

In my case, I am working outside a git repository and I only have the .diff (the output of git diff <commit-ish>) files and I would like to have the same stats (Files changed, additions, deletions).

Is there a tool to do it ? Or should I write a regex ?


Solution

  • The histogram with +/- signs and the "1 file changed, ..." is known as the diffstat. The diffstat tool (man page) can produce these if you feed it your diff:

    $ diffstat -p1 my-patch.diff
    src/some_file.c |    4 ++--
    1 file changed, 2 insertions(+), 2 deletions(-)
    

    If you only want the last line, use -s:

    $ diffstat -s my-patch.diff
    1 file changed, 2 insertions(+), 2 deletions(-)
    

    Using -p1 is probably a good idea if you're feeding it git diffs which have a/b prefixes. If you don't use -p, diffstat strips common prefixes from the filenames. Sometimes this can give you unexpected results (especially if your patch adds or deletes files by comparing them with /dev/null).

    Diffstat can also give you coloured histograms if you use the -C option.