How to force diffstat to always show number of inserts, deletes and modifications even for zero values?

275 Views Asked by At

I'm doing a diffstat on my merge to see how many inserts, deletes and modifications I made like so:

git show 526162eed6 --first-parent --unified=0 | diffstat -m

This lists all the files and gives a summary at the end:

a/b/c | 10 ++++++++++
a/b/d |  5 +++++
...
10 files changed, 50 insertions(+), 10 modification(!)

However, I'd like to see all values even if they were zero:

10 files changed, 50 insertions(+), 0 deletions(-), 10 modifications(!)

How can I do this? The current workaround I have is to output a CSV via ... | diffstat -mt and manually add up the columns via awk. Is there a simpler way?

1

There are 1 best solutions below

2
knittl On

I couldn't find an option to do what you want. diffstat is a tool producing human readable output, not intended for machine consumption.

If you absolutely must parse/massage its output, you could use a very dirty hack (not recommended, can break anytime). Define shell functions:

stats() {
  read -r stat
  echo "$stat" | grep -o '[0-9]\+ file' | grep -o '[0-9]\+' || echo '0'
  echo 'files changed,' # does not match original output for 1 file
  echo "$stat" | grep -o '[0-9]\+ ins' | grep -o '[0-9]\+' || echo '0'
  echo 'insertions(+),'
  echo "$stat" | grep -o '[0-9]\+ del' | grep -o '[0-9]\+' || echo '0'
  echo 'deletions(-),'
  echo "$stat" | grep -o '[0-9]\+ mod' | grep -o '[0-9]\+' || echo '0'
  echo 'modifications(!)'
}
diffstats() {
  diffstat -sm | stats | paste -sd ' '
}

and then:

git diff | diffstats