GIT contribution per author (lines)
Asked Answered
L

3

14

I'm trying to print the per-line contribution of each author to a Git repository.

For that, I use the following command, adapted from How to count total lines changed by a specific author in a Git repository?

git ls-tree -r -z --name-only HEAD -- */*.c | xargs -0 -n1 git blame \
--line-porcelain HEAD |grep  "^author "|sort|uniq -c|sort -nr

However, I get the following error:

fatal: cannot stat path 'HEAD': No such file or directory.

What am I doing wrong?

Lacto answered 22/8, 2014 at 14:14 Comment(0)
L
14

Okay, after more investigation I found this on SO.

git ls-files -z | xargs -0n1 git blame -w | perl -n -e '/^.*?\((.*?)\s+[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n  

The answer came with support from Eric Z

RESULT

    234926 USER 1
     32453 USER 2
   2941234 USER 3
Lacto answered 25/8, 2014 at 7:29 Comment(0)
H
1

This means the first part of your expression is not giving any results. Try

git ls-tree -r -z --name-only HEAD -- */*.c

without the latter part; probably that gives you empty output. Fix that expression to list the files you want to work on... If I use that in a repository not containing any .c files; it gives me the same error as you. Either removing the option */*.c or fixing it to */*.cpp fixed it (depending on the outcome you want)

Hackberry answered 22/8, 2014 at 14:29 Comment(3)
I just get a long list of project files. no author info is includedLacto
if you use your whole command without the */*.c part, you still get the same error?Hackberry
if you don't, add the second part etc.. until the error appears; then we can debug further :)Hackberry
A
0

As an extension to @chris-maes accepted answer, this is how to group by file extension:

EXTS=$(git ls-files | sed 's/^.*\.//g' | sort | uniq)
for EXT in $EXTS; do
  echo -e "\n$EXT\n"
  git ls-files | 
    grep ".$EXT" | 
    xargs -r -n1 git blame -w | 
    perl -n -e '/^.*?\((.*?)\s+[\d]{4}/; print $1,"\n"' | 
    sort -f | uniq -c | sort -n
done

If you wish to filter to specific authors, use a posix regex

EXTS=$(git ls-files | sed 's/^.*\.//g' | sort | uniq)
for EXT in $EXTS; do
  echo -e "\n$EXT\n"
  git ls-files | 
    grep ".$EXT" | 
    xargs -r -n1 git blame -w | 
    perl -n -e '/^.*?\((.*?)\s+[\d]{4}/; print $1,"\n"' | 
    sort -f | uniq -c | sort -n |
    grep "James\|McGuigan"
done
Absorber answered 9/3, 2023 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.