49

I have a GIT repository and I want to calculate how many lines of code were added/changed by one person or a group of persons during some period of time. Is it possible to calculate with git?

Lu4
  • 14,873
  • 15
  • 79
  • 132
  • 4
    Don't use this for estimating "performance" btw, see http://blogs.msdn.com/b/oldnewthing/archive/2010/11/16/10091537.aspx – ismail Jan 04 '11 at 11:04
  • 2
    Thank you for your comment, I had no intention of measuring a person's salary with this type of measurement. This will be done for having the whole picture of person's work, and nobody will ever know that that I'm doing one – Lu4 Jan 04 '11 at 11:40
  • 6
    It's not the size of the lines, it's how you use them. – ingyhere Sep 01 '12 at 10:00
  • 3
    One of the problems with this measurement is that sometimes developers check in large files to the project which they haven't written which then makes it seems like they've written many lines of code – ykay says Reinstate Monica Jun 02 '16 at 09:35

8 Answers8

85

You can use git log and some shell-fu:

git log --shortstat --author "Aviv Ben-Yosef" --since "2 weeks ago" --until "1 week ago" \
    | grep "files\? changed" \
    | awk '{files+=$1; inserted+=$4; deleted+=$6} END \
           {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'

Explanation: git log --shortstat displays a short statistic about each commit, which, among other things, shows the number of changed files, inserted and deleted lines. We can then filter it for a specific committer (--author "Your Name") and a time range (--since "2 weeks ago" --until "1 week ago").

Now, in order to actually sum up the stats instead of seeing the entry per commit, we do some shell scripting to do it. First, we use grep to filter only the lines with the diffs. These lines look like this:

 8 files changed, 169 insertions(+), 81 deletions(-)

or this:

 1 file changed, 4 insertions(+), 4 deletions(-)

We then sum these using awk: for each line we add the files changed (1st word), inserted lines (4th word) and deleted lines (6th word) and then print them after summing it all up.

Edit: forward slashes were added in the top snippet so it can be copy and pasted into a command line.

Community
  • 1
  • 1
abyx
  • 69,862
  • 18
  • 95
  • 117
18

You can generate stats using Gitstats. It has an 'Authors' section which includes number of lines add/removed by the top 20 authors (top 20 by commit count).

Edit: There's also Git: Blame Statistics

Community
  • 1
  • 1
Dogbert
  • 212,659
  • 41
  • 396
  • 397
6

Run this command:

git log --pretty=format:'' --numstat --author 'Lu4' | awk 'NF' | awk '{insertions+=$1; deletions+=$2} END {print NR, "files changed,", insertions, "insertions(+),", deletions, "deletions(+)"}';

This command is very close to the clever one in abyx's answer, but it also handles the edge case found by Wallace Sidhrée. Sometimes, a commit involves deletions only (i.e., no insertions). The command in abyx's answer incorrectly reads those deletions as insertions. The command here reads them correctly because it uses --numstat instead of --shortstat. Unlike --shortstat, --numstat includes both the insertions and deletions for those commits.

Note that both commands include binary files in the file count but exclude the number of lines inserted and deleted inside those binaries.


Here is another useful trick. Create a file called gitstats with this content:

#!/usr/bin/env bash

git log --pretty=format:'' --numstat "$@" | awk 'NF' | awk '{insertions+=$1; deletions+=$2} END {print NR, "files changed,", insertions, "insertions(+),", deletions, "deletions(+)"}';

Then you can run that command with any extra options to git log you want. Here are some examples:

./gitstats;
./gitstats --since '1 month ago';
./gitstats --since '1 month ago' --until '1 day ago';
./gitstats --author 'Lu4' --since '1 month ago' --until '1 day ago';

(The file can be named something other than gitstats, of course.)

GreenRaccoon23
  • 3,603
  • 7
  • 32
  • 46
3

For particular dates, you can use --since "2012-08-27" --until "2012-09-01"

Like

git log --shortstat --author "Fabian" --since "2012-08-27" --until "2012-09-01" | grep "files changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'

Check this explanation

http://gitref.org/inspect/

Luke
  • 11,426
  • 43
  • 60
  • 69
zurcacielos
  • 1,495
  • 2
  • 9
  • 7
2

you can do that:

1) Run:

nano contribution.sh

2) fill :

if [ $# -eq 1 ]
then
        git log --author=$1 --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 }' - > logs.txt
        cat logs.txt
else
        echo "ERROR: you should pass username at argument"
fi

3) Run :

chmod +x contribution.sh

4) Now you can see your contribution with:

./contribution.sh your_git_username
Walterwhites
  • 1,287
  • 13
  • 9
1

You can try Atlassian's Fisheye/Crucible which integrates with Git (as well as other code repos). Then everyone's contributions -- including their LOC -- are displayed publicly in an easily readable Web app. For small groups, it's pretty cheap, too.

Open source the information and let it speak for itself.

ingyhere
  • 11,818
  • 3
  • 38
  • 52
  • fisheye doesn't currently support LOC metrics on GIT or mercurial repositories unfortunately. Only SVN seems to be supported for these stats. https://confluence.atlassian.com/display/FISHEYE/Cannot+View+Lines+of+Code+Information+in+FishEye – Gaylord Zach Mar 30 '13 at 22:17
  • Ouch! I stand corrected. My faith in Atlassian is a little shaken, too. – ingyhere Apr 16 '13 at 23:04
  • The latest versions of Fisheye now support Git! – ingyhere Sep 01 '15 at 20:05
1

I wrote some cli tool for this (https://www.npmjs.com/package/whodid)

$ npm install -g whodid
$ cd your-proj-dir

and then

$ whodid --include-merge=false --since=1.week
victor.cheval
  • 157
  • 1
  • 2
1

I'm not sure the line of code would be a good metric but if you are looking for just total commits and compare it against other engineers then you could use this: no plugin or addon is needed...just pure shell script, it was tested in zshell

Note: you must run this from the repo folder

#!/bin/env zsh
team_total=$(git shortlog -s -n |sed 's/\t/,/g'|cut -f1 -d, |bc -l|awk '{sum+=$1}END{print sum}');
tmp_counter='/tmp/counter.txt';
tmp_user='/tmp/users.txt';
tmp_percentage='/tmp/counters_users'
# if you are running this again it make the file empty or you can rm it 
rm $tmp_percentage $tmp_user $tmp_counter
git shortlog -s -n |sed 's/\t/,/g'|cut -f2 -d, >>$tmp_user;
git shortlog -s -n |sed 's/\t/,/g'|cut -f1 -d, >>$tmp_counter;

cat $tmp_counter | while read LINE; do
    printf '%.2f %%  \n' $(echo \($LINE/$team_total\)\*100 |bc -l ) >>$tmp_percentage
done
echo 'commits %      | contributor     | # of commits';paste  $tmp_percentage $tmp_user $tmp_counter

here is the sample report:

enter image description here

grepit
  • 21,260
  • 6
  • 105
  • 81