Skip to content

Instantly share code, notes, and snippets.

@naddeoa
Created February 14, 2015 16:33
Show Gist options
  • Save naddeoa/c9ee543649ccf206a6de to your computer and use it in GitHub Desktop.
Save naddeoa/c9ee543649ccf206a6de to your computer and use it in GitHub Desktop.
Get the difference in line numbers between two get commits
#!/bin/sh
# Get the difference between to git commit's total lines of code
# $1 a git commit
# $2 another git commit, after $1 chronologically
# Example:
# git line-diff HEAD HEAD~
# git line-diff 1aab45 453b7a
#
# If you put this script on your path somewhere and name it git-line-diff, then
# git will run it when you type `git line-diff`
# Format a number with commas
# Its a little inneficient to reverse the string twice with rev, but it
# simplifies the sed regex a lot and performance isn't really an issue.
function integer_format(){
echo $1 | rev | sed 's/\(...\)\([^$]\)/\1,\2/g' | rev
}
# The magic number '4b825dc642cb6eb9a060e54bf8d69288fbee4904' is an empty git tree
# http://stackoverflow.com/questions/9765453/gits-semi-secret-empty-tree
HEAD_LINES=`git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904..$1 | tail -1 | sed -n 's/.*, \([0-9]*\) .*/\1/p'`
SECOND_LINES=`git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904..$2 | tail -1 | sed -n 's/.*, \([0-9]*\) .*/\1/p'`
FORMATTED_HEAD=`integer_format $HEAD_LINES`
FORMATTED_SECOND=`integer_format $SECOND_LINES`
echo "$1 total lines: $FORMATTED_HEAD"
echo "$2 total lines: $FORMATTED_SECOND"
DIFFERENCE=`expr $HEAD_LINES - $SECOND_LINES`
echo "Difference: `integer_format $DIFFERENCE`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment