Created
November 5, 2012 21:35
-
-
Save xeoncross/4020489 to your computer and use it in GitHub Desktop.
Git - calculate how many lines of code were added/changed by someone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this in the project repo from the command-line | |
# http://stackoverflow.com/a/4593065/99923 | |
git log --shortstat --author "Xeoncross" --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}' |
To address @lack3r 's valid concerns, you can insert '0 insertions ', e.g.: sed 's/changed, \([0-9]\+ deletions\)/changed, 0 insertions(+), \1/g'
.
Together with @kirhgoff 's modified grep, this gives us:
git log --shortstat --author "user" \
| egrep "file[s] changed" \
| sed 's/changed, \([0-9]\+ deletions\)/changed, 0 insertions(+), \1/g' \
| awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'
@neuged: Changes to your command:
- Added missing asterisk to egrep
- added since and until parameters
git log --shortstat --author "username" --since "5 days ago" --until "today" \
| egrep "file[s]* changed" \
| sed 's/changed, \([0-9]\+ deletions\)/changed, 0 insertions(+), \1/g' \
| awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'
How do I make an alias of this? Because we use both '
and "
, I do not know a syntax to get a valid command. This is what I have now:
git config --global alias.my-contribution "!git log ..."
building on the previous solutions, if you want to skip certain files you can add -- ':!pattern'
git log --stat 0000000..HEAD -- ':!*test.go' | rg "files changed" | awk ...
Alias for ZSH folks:
alias pastmonth="git log --shortstat --author \"FL33TW00D\" --since \"31 days ago\" --until \"today\" | \
grep -E \"file[s]* changed\" | \
sed -E 's/changed, ([0-9]+) deletions/changed, 0 insertions(+), \1 deletions/g' | \
awk '{files+=\$1; inserted+=\$4; deleted+=\$6} END {print \"files changed\", files, \"lines inserted:\", inserted, \"lines deleted:\", deleted}'"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good try. However, the command does not produce correct results.
First, it can misinterpret deletions as additions.
Please consider the following stat:
2 files changed, 2 deletions(-)
In case a commit does not have any additions, but only removals, as the one shown above, the number of lines removed will be added to the total of the lines added.
Given the example provided above, the command will report that 2 lines were added, and 0 were deleted, while in fact, it was the opposite.
Second, it misses the commits where 1 file was changed as mentioned by @kirhgoff.