Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total additions and deletions on a given branch for an given author in git

Tags:

git

Is there a way in git to count the total deletions and additions for a given user on a given branch? Something like that is on github, in the graph section there is a chart that shows you the total additions and deletions but only on the master branch... i think if they did it this mus be possible in git also, so, does someone know how to do that?

Thank you in advance.

like image 353
Starlays Avatar asked Oct 24 '12 18:10

Starlays


People also ask

What is git Shortlog?

The git shortlog command is a special version of git log intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who's been working on what.

What are insertions in git?

It is just number of lines inserted and number of lines deleted in that particular commit. Note that a modified line maybe treated as an insert and a delete. Git log manual: --shortstat.

What is git summary?

Git is a version control (VCS) system for tracking changes to projects. Version control systems are also called revision control systems or source code management (SCM) systems.


2 Answers

I don't think Git has any built-in command that does this. But with the help of some other standard utilities, it can be done. Here is an example that filters Git's log output through awk to get the summary of total insertions and deletions:

git log --author=$USER --shortstat $BRANCH | \ awk '/^ [0-9]/ { f += $1; i += $4; d += $6 } \ END { printf("%d files changed, %d insertions(+), %d deletions(-)", f, i, d) }' 
like image 115
Dan Moulding Avatar answered Oct 04 '22 15:10

Dan Moulding


I rolled my own based on this to cover the case with newer git where 0 insertions or deletions is not printed:

git log --author=$USER --shortstat $BRANCH |    ruby -e 'puts $<.read.scan(/(\d+) \w+\(([+-])\)/).group_by(&:last).   map{|s,ds|s+ds.map(&:first).map(&:to_i).inject(0,&:+).to_s}.join(", ")' 

This just prints the insertion and deletion totals, like: +7108, -6742

If you want the files changed total too, this slightly hacky¹ version will do:

git log --author=$USER --stat=99999999 $BRANCH |    ruby -e 'f,a,d = $<.read.scan(/^ (.*?)\s+\|\s+\d+\s(\+*)(\-*)$/).transpose;   puts [f.uniq.length, " files, +", a.join.length, ", -", d.join.length].join' 

The output looks like this: 97 files, +3701, -3598

The files total is the number of unique file names across all the commits, not the sum of the number of files changed on each commit, which is what the original answer gives you.


¹ The 999… thing is because we're literally counting the number of +s and -s, and we need git not to cap them to the default width, so we give it a very long width to work with.

like image 32
kch Avatar answered Oct 04 '22 16:10

kch