How can I use gitstats to find out how many SLOC a Git repo has in total and per commiter?

orokusaki picture orokusaki · Feb 15, 2012 · Viewed 16k times · Source

I just installed GitStats, and I'm at that point where I have to say, "Now, what?". I see examples on the site of user lines of code, etc., but no examples of how to get simple stats like that. I don't need a chart or anything. I just want to be able to console output the results in a list of user -> lines of code or something. Any help is much appreciated.

Answer

orokusaki picture orokusaki · Feb 15, 2012

Update (July 11th, 2014)

I'm not sure what version i had installed when I first answered this question, but the latest version gave me an authors.html file when I ran gitstats /path/to/repo/.git /path/to/output/dir/ that contained exactly the info I was looking for.

Original Answer

It's pretty simple, I found. You just type:

gitstats /path/to/the/repo.git --outputpath=directory_where_you_want_the_output

It outputs the entire report with charts, navigation via tabs, etc.

Note: You cannot tell how many lines each user has contributed (at least with the version of gitstats that an apt-get install gitstats got me). The output was useful, and is a great way to learn about your code base and its contributors. I did the following, to get the number of lines of a particular user:

git log --author="Some Author <[email protected]>" --oneline --shortstat > some_author.txt

Then, I used Python to parse the data (since there were hundreds of commits):

>>> import re
>>> file = open('some_author.txt', 'r')
>>> adds, dels = 0, 0
>>> for line in file.readlines():
...     am, dm = re.search(r'\d+(?= insertions)', line), re.search(r'\d+(?= deletions)', line)
...     if am is not None:
...         adds += int(am.group())
...         dels += int(dm.group())
... 
>>> adds, dels
(5036, 1653)
>>> file.close()