Skip to content

Instantly share code, notes, and snippets.

@anth-3
Last active August 29, 2015 14:24
Show Gist options
  • Save anth-3/158c9827521a50c1eddc to your computer and use it in GitHub Desktop.
Save anth-3/158c9827521a50c1eddc to your computer and use it in GitHub Desktop.
#!/bin/sh
# Subversion Statistics BASH Script
# Version 0.4.3
# anth*3 <anthony3@holdener.com>
#
# Copyright (c) 2014 anth*3.
#
# This tool gathers the number of lines added and removed from svn for a given
# target range and will output the number of adds, removes, and total altered
# lines. It will ignore empty (whitespace) lines when doing its parsing.
#
# SSBS is based on the original concept by Cyberfall <cyber-fall.blogspot.com>
# from a post on his blog.
#
# This script is licensed under the MIT License.
#
# Usage: execute the script from within the SVN folder to scan
# ./svn_stats.sh <FROM> [<TO>]
#
# Valid values for <FROM> and <TO> arguments:
# REVISION: the number of the revision you want to select, e.g. 1095
# DATE: date value in YYYY-MM-DD format, surrounded with curly brackets:
# {2011-10-25}
# Note:
# The <TO> argument is optional. If it is not passed to the script, then the
# value will default to HEAD.
#
# Examples:
# Gather statistics about what changes have been made during September:
# sh svn_stats.sh {2014-09-01} {2014-09-30}
#
# Gather statistics about what changes have been made since September 1st:
# sh svn_stats.sh {2014-09-01}
# or:
# sh svn_stats.sh {2014-09-01} HEAD
#
# Gather statistics about what changes have been made since Revision 1095:
# sh svn_stats.sh 1095
#
# Gather statistics about what changes have been made from Revision 1095 until
# September 1st:
# sh svn_stats.sh 1095 {2014-09-01}
echo ''
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo 'Subversion Statistics BASH Script (SSBS)'
echo 'v0.4.3'
echo ''
echo 'anth*3 <anthony3@holdener.com>'
echo 'Copyright (c) 2014 anth*3'
echo ''
echo 'MIT License'
echo ''
echo 'original concept by Cyberfall <cyber-fall.blogspot.com>'
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ''
# The root working path for this tool (change it to suit your needs
STATS_PATH=~/.svn_stats
# This path is used for storing all of the information that is gathered from
# Subversion.
STATS_PATH_INFO=$STATS_PATH/info
# Check and prepare the statistics root path
if [ -d $STATS_PATH ]; then
echo 'stats path: '$STATS_PATH;
else
echo 'stats path does not exist, creating: '$STATS_PATH;
mkdir $STATS_PATH;
fi
# Check and prepare stats directory for changes
if [ -d $STATS_PATH_INFO ]; then
echo 'stats path for information: '$STATS_PATH_INFO;
else
echo 'stats path for changes does not exist, creating: '$STATS_PATH_INFO;
mkdir $STATS_PATH_INFO;
fi
# Define the working path (this is why you need to be in the SVN directory)
SVN_PWD=$(pwd)
SVN_ROOT_PATH_NAME=`basename $SVN_PWD`
# This path is used for differentiating any different branches that may be
# parsed by this script.
SVN_INFO_PATH=$STATS_PATH_INFO/$SVN_ROOT_PATH_NAME
# Clear the directory if it exists and recreate (faster than cleaning up
# individual files?)
rm -r $SVN_INFO_PATH
mkdir $SVN_INFO_PATH
echo 'svn path for information: '$SVN_INFO_PATH
# Check if TO argument is set and default it to HEAD if it is not
FROM=$1
TO=''
if [ -n "$2" ]; then
TO=$2;
else
TO='HEAD';
fi
echo ''
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo ''
echo 'Parsing SVN diff FROM '$FROM' TO '$TO'...'
echo ''
# Gather all of the statistics about all changes
svn diff -x -bw -r $FROM:$TO > $SVN_INFO_PATH/svn_all.log
# Parse the all changes file to extract only added lines
cat $SVN_INFO_PATH/svn_all.log | grep '^\+' > $SVN_INFO_PATH/svn_add.log
# Parse the all changes file to extract only removed lines
cat $SVN_INFO_PATH/svn_all.log | grep '^\-' > $SVN_INFO_PATH/svn_sub.log
ADD=$(wc -l $SVN_INFO_PATH/svn_add.log | awk '{print $1}')
SUB=$(wc -l $SVN_INFO_PATH/svn_sub.log | awk '{print $1}')
((ALT=ADD+SUB))
echo $ADD' lines added'
echo $SUB' lines removed'
echo '-------------------------------'
echo $ALT' total lines altered'
echo ''
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment