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

后端 未结 2 980
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 11:55

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

2条回答
  •  渐次进展
    2020-12-23 12:38

    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.

提交回复
热议问题