Last Updated: February 25, 2016
·
1.37K
· kuroir

Git: LOC Added and Removed

Did you ever wonder, how much code did we write and removed in the last 3 months?

I did. And since I love Git, and I also happen to love Awk. I figured out we could use both to get the data we want.

git log --numstat --pretty="%H" --since="3 months ago" --until="now"  | awk 'NF==3 {if ( ! match($3, /\.(css|js)$/) ) { plus+=$1; minus+=$2} } END {printf("+%d, -%d\n", plus, minus)}'

For those curious on what's happening there:

git log --numstat --pretty="%H"

We use git-log to give us the lines that were changed in each file per commit. The format is basically:

# # file_name.txt
# # file_name.txt
HASHCODE

We make git output the files that were changed ( added, removed and file ) per commit.

--since="3 months ago" --until="now" 

We define a time range.

awk 'NF==3

We just want to parse those lines that have 3 fields ( look at the format above ).

{if ( ! match($3, /\.(css|js)$/) ) { 

Filter files, with a RegEx.

plus+=$1; minus+=$2} } END 

Add the given numbers to the plus and minus variables.

{printf("+%d, -%d\n", plus, minus)}'

Output the totals.

3 Responses
Add your response

You are missing an apostrophe at the end ;)

over 1 year ago ·

Nice one.

You are missing ' at the end of first code block.

over 1 year ago ·

Good Catch! Both of you!

I copied this from iTerm after writing it, guess I missed the quote. Fixed.

Cheers.

over 1 year ago ·