Saturday, October 22, 2011

[ Tools | Linux | SVN ] Generate statistics about SVN commits for selected period of time.

It's very difficult and routine to gather statistics about how many lines of code were added, removed or changed by SVN commits for selected period of time in past.
I will explain how you may do it and also will share my shell script to you.

I've tried to find any useful utility or tool, but found nothing in web. My time was ticking out so I decide to solve this task by myself.
First, I decide to use simple console client for Subversion.
sudo apt-get install subversion

This program have one useful command (execute it in svn directory):
svn diff < FROM >:< TO >

It helps you to generate all changes between two different revisions (or dates).
You may read more details about this command and it's configurations:
svn help diff

But this function can't split all changes to different categories such as: added, removed or changed.
That's why you need to create your own script to parse it's results.

Also, you may use my script [ svn_stats.sh ].
All descriptions about how to use it you may read inside comments.

It will generate such files:
-rw-r--r-- all_changes.log
-rw-r--r-- lines_added.log
-rw-r--r-- lines_removed.log

If you need to count lines, just use this command in directory with these files:
wc -l ./*

And it will show you such results:
309 all_changes.log
300 lines_added.log
2 lines_removed.log


That's it! I hope it will help you!
P.S.
If you know more simple solution, PLEASE! Write and describe me about it! :)

svn_stats.sh sources:
#!/bin/sh
# This tool helps to gather information from svn - how many lines were added, changed or removed for selected period of time.
#
# To use it - execute such command in svn folder which need to be scanned:
# sh svn_stats.sh < FROM > < TO >

# Valid values for < FROM > and < TO > arguments
# REVISION: just number of revision you want to select, e.g. 1095
# DATE: to select value as date use such syntax: {2011-10-25}
#
# NOTE:
# < TO > argument is optional. If it was not set then HEAD value will be in use by default.
#
# Examples
# Gather statistics about what changes have been made during september:
# sh svn_stats.sh {2011-09-01} {2011-09-31}
#
# Gather statistics about what changes have been made since 1 september till now:
# sh svn_stats.sh {2011-09-01}
# or:
# sh svn_stats.sh {2011-09-01} HEAD
#
# Gather statistics about what changes have been made since 1095 revision till now:
# sh svn_stats.sh 1095
#
# Gather statistics about what changes have been made since 1095 revision till 1 september:
# sh svn_stats.sh 1095 {2011-09-01}


# root working directory for this tool
# You may redefine it to any another folder
STAT_PATH=~/.svn_stats
# this path is used for storing of all information which gathered from svn repository.
STAT_PATH_CHANGES=$STAT_PATH/changes


# check and prepare stat root directory
if [ -d $STAT_PATH ];
then
    echo 'stat directory: ' $STAT_PATH;
else
    echo 'stat directory is not exist and will be created: ' $STAT_PATH;
    mkdir $STAT_PATH;
fi


# check and prepare stat directory for changes
if [ -d $STAT_PATH_CHANGES ];
then
    echo 'stat directory for changes: ' $STAT_PATH_CHANGES;
else
    echo 'stat directory for changes is not exist and will be created: ' $STAT_PATH_CHANGES;
    mkdir $STAT_PATH_CHANGES;
fi


# define working paths
SVN_PWD=`pwd`
SVN_ROOT_PATH_NAME=`basename $SVN_PWD`
# this path is used for storing of all information which gathered from svn repository.
SVN_CHANGE_PATH=$STAT_PATH_CHANGES/$SVN_ROOT_PATH_NAME


# clear if it exists and prepare directory for current svn folder
rm -r $SVN_CHANGE_PATH
mkdir $SVN_CHANGE_PATH


# check if TO argument was set and set it to HEAD value if not.
FROM=$1
TO=''
if [ -n "$2" ];
then
    echo 'TO ' $2;
    TO=$2;
else
    echo 'TO HEAD';
    TO='HEAD';
fi


# gather common statistics about all changes
svn diff -x -bw -r $FROM:$TO>$SVN_CHANGE_PATH/all_changes.log
# parse to extract only added lines
cat $SVN_CHANGE_PATH/all_changes.log | grep '^\+'>$SVN_CHANGE_PATH/lines_added.log
# parse to extract only removed lines
cat $SVN_CHANGE_PATH/all_changes.log | grep '^\-'>$SVN_CHANGE_PATH/lines_removed.log

2 comments:

zac said...

Nice script. Thanks for sharing.
Can we have somthing like this to execute for specific revisions.
Say if i want to know lines of code for particular defect ID.

I can know revision numbers for specific defect using svnlog -XML

Gadge+ said...

Sorry, but I'm not working with SVN anymore for almost 5 years, so can't say how to improve this script or modify for particular exact cases. I would suggest to read documentation of SVN utility:

svn help diff