Recent svn commit statistics

Posted by Mike on Jan 13th, 2010

Here’s a little script-fu that can help determine how many lines of code were added vs. deleted for a single Subversion commit.

#!/bin/bash
if [ -z "$1" ]; then
    echo "usage: $0 revision"
    exit
fi
 
REV=$1
 
# Execute a svn diff on the revision, and search the output for deleted lines
# (begin with a '-', but aren't followed by another immediately),
# and likewise with any added lines
DIFF="$(svn diff -c $REV --diff-cmd=/usr/bin/diff)"
DEL_COUNTS=$(echo -e "$DIFF" | egrep '^[-][^-]' | wc -lm)
ADD_COUNTS=$(echo -e "$DIFF" | egrep '^[+][^+]' | wc -lm)
 
function get_field {
    # given an input string with 2 fields delimited by spaces, possibly with
    # leading whitespace, return the field in the position specified.
    # so,  `get_field "   123   45678" 1`  would return "122"
    # and  `get_field "   123   45678" 2`  would return "45678"
    INPUT=$1
    POSITION=$2
    echo $INPUT | sed "s/^[ ]*\([0-9]*\)[ ]*\([0-9]*\)/\\$POSITION/g"
}
 
DEL_LINES=$(get_field "$DEL_COUNTS" 1)
DEL_CHARS=$(get_field "$DEL_COUNTS" 2)
 
ADD_LINES=$(get_field "$ADD_COUNTS" 1)
ADD_CHARS=$(get_field "$ADD_COUNTS" 2)
 
echo "Removed $DEL_LINES lines ($DEL_CHARS characters)"
echo "Added $ADD_LINES lines ($ADD_CHARS characters)"

Save this as $HOME/bin/svnstat, then execute it passing in a revision, e.g. $ svnstat 1001

$ svnstat 61765
Removed 113 lines (4913 characters)
Added 63 lines (2975 characters)

If you want to then see your daily net lines of code, hook this up to an egrep‘d svn log, piped into xargs.

Update 1/13/2010:
I cleaned up the “get_field” function to use a single sed command with backreferences rather than piping the string through tr, sed, and cut.

Also updated the initial calculation of DIFF_COUNTS and ADD_COUNTS to only require 1 single svn diff execution. Used echo -e to solve the newline issue I was having on a first failed attempt.

  • Twitter
  • Facebook
  • StumbleUpon
  • Google Reader
  • Reddit
  • Share/Bookmark

One Response

  1. Anonymous Says:

    Ничо не понятно

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Categories