Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git contributors of each file

Tags:

git

sh

I want to list every contributors for each file in the repository.

Here is currently what I do:

find . | xargs -L 1 git blame -f | cut -d' ' -f 2-4 | sort | uniq

This is very slow. Is there a better solution ?

like image 584
log0 Avatar asked Jul 31 '12 10:07

log0


2 Answers

Taking ДМИТРИЙ's answer as a base, I'd say the following :

git ls-tree -r --name-only master ./ | while read file ; do
    echo "=== $file"
    git log --follow --pretty=format:%an -- $file | sort | uniq
done

Enhancement is that it follows file's rename in its history, and behaves correctly if files contain spaces (| while read file)

like image 86
CharlesB Avatar answered Oct 14 '22 09:10

CharlesB


I would write a small script that analyzes the output of git log --stat --pretty=format:'%cN'; something along the lines of:

#!/usr/bin/env perl

my %file;
my $contributor = q();

while (<>) {
    chomp;
    if (/^\S/) {
        $contributor = $_;
    }
    elsif (/^\s*(.*?)\s*\|\s*\d+\s*[+-]+/) {
        $file{$1}{$contributor} = 1;
    }
}

for my $filename (sort keys %file) {
    print "$filename:\n";
    for my $contributor (sort keys %{$file{$filename}}) {
        print "  * $contributor\n";
    }
}

(Written just quickly; does not cover cases like binary files.)

If you stored this script, e.g., as ~/git-contrib.pl, you could call it with:

git log --stat=1000,1000 --pretty=format:'%cN' | perl ~/git-contrib.pl

Advantage: call git only once, which implies that it is reasonably fast. Disadvantage: it’s a separate script.

like image 26
igor Avatar answered Oct 14 '22 07:10

igor