greywolf82
greywolf82

Reputation: 22193

Count logical lines of code with git

Is there a way to count the logical lines of code of a c++/java project with git? In addition I need to count them for author as well. Any tips?

Upvotes: 1

Views: 457

Answers (2)

Schwern
Schwern

Reputation: 165416

Is there a way to count the logical lines of code of a c++/java project with git?

Short answer? No. Git has no special understanding of the code. There are tools which do, and you can simply run them on a checkout.

I need to count them for author as well.

You can use git blame to tell you who wrote what line, but these will not be logical lines. A single statement split across multiple lines will be counted multiple times.

Upvotes: 0

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4913

If you are looking at the current commit leave out the bracketed statements.

The following command counts all lines in the project.

[commit=<some-commit>]
for file in $(git ls-files [--with-tree=$commit])
do 
  git cat-file -p [$commit]:$file
done | 
wc -l

The following command will count all lines by author.

[commit=<some-commit>]
for file in $(git ls-files [--with-tree=$commit])
do 
  git blame [$commit] --line-porcelain -- $file
done | 
sed -n 's/^author //p' | 
sort | 
uniq -c | 
sort -r

Thanks to the the Git documentation for git blame for the counting of author lines per file. You can scrape other useful information as well if you modify the field you are looking at.

Upvotes: 2

Related Questions