2

I want to count up total lines of every committer in a git repository. I only get a solution below:

git log --format='%aN' | sort -u | \
  while read name; do
    echo -en "$name\t"
    git log --author="$name" --pretty=tformat: --numstat | \
    awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
  done

It can calculate out ALL HISTORY of every committer's total lines. But I want to calculate out in CURRENT snapshot, every committer's total lines. I don't know how to do it.

Do you have a solution about this problem?

Steffo
  • 305
  • 5
  • 13
geekzhu
  • 37
  • 3
  • I think you're on the right track (some variation of `git log...` + a sed or awk script). Look [here](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History) and [here](https://git-scm.com/docs/git-log) for some additional options. – paulsm4 Dec 05 '18 at 03:57
  • Possible duplicate of [GIT contribution per author (lines)](https://stackoverflow.com/questions/25449075/git-contribution-per-author-lines) – phd Dec 05 '18 at 10:29
  • https://stackoverflow.com/search?q=%5Bgit%5D+count+contribution+per+author – phd Dec 05 '18 at 10:30

2 Answers2

2

This is a bit overkill and slow but you can do something like this.

git log --format='%aN' | sort -u | \
  while read name; do
    echo -en "$name\t"
    for FILE in $(git ls-files) ; do git blame $FILE | grep "$name" ; done | wc -l
  done
Steffo
  • 305
  • 5
  • 13
Pavan Yalamanchili
  • 12,021
  • 2
  • 35
  • 55
  • I suggest adding some stderr redirections `2> /dev/null` so that git errors do not cover the author names :) – Steffo Jun 08 '21 at 15:59
0

I found the accepted answer slow and broke for me with GPG signed commits.

This worked:

git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
Steffo
  • 305
  • 5
  • 13
rich
  • 18,987
  • 11
  • 75
  • 101