svnlogger v0.1

This one is for the Unix freaks (that includes me !).  Remember the textual “ChangeLog” files we used to see in a lot of open source softwares ? It used to have the list of changes made to the software along with revisions number, contributor’s name and timestamp. I always liked the format of a particular kind of ChangeLog which showed all the details in a compact manner without losing any, helps me keep track of ‘What’s new!’ in my next version of the software. Anyways, so how do I make it ? For those of you who have been thinking that they’re hand-written, you’re so wrong ! After all, how can you expect an open source freak to do something ‘manually’ when he can easily automate any task in his computer using cool scripts ?? Anyways, usually they have some kind of Version Tracking System like CVS, SVN or GIT. I happen to use SVN most of the time, actually always, and since I needed to maintain a ChangeLog as well, I found there was a nice command called “svn log” which would generate a textual log of all the commits along with all the information I needed to see. Well, everything was there, but not in a way I wanted it to be. ‘svn log’ is good enough for ‘grep’ but not so good in terms of direct readability. So I wrote this small shell script that is basically a wrapper around the svn log but it uses AWK to reformat the data in a way I like it !

Copy paste the code below into a file called ‘svnlogger.sh’ and then execute it like

> sh svnlogger.sh <path-to-svn-repo> <path-to-changelog>

If you don’t mention either of <path-to-svn-repo> or <path-to-changelog>, it would take the current directory as default. Here’s a sample of the ChangeLog generated :

Revision 285  by  abhishekdelta  on  2011-03-11 20:59:2
Description : Fixed URL Rewrite bugs #115 and #117
——————————————————————————–
Revision 284  by  chakradarraju  on  2011-03-11 18:21:0
Description : Ticket #122, activating/deactivating/deleting multiple users
——————————————————————————–
Revision 283  by  abhishekdelta  on  2011-03-11 04:21:2
Description : Auto detection of Rewrite engine during installation #98
——————————————————————————–
Revision 282  by  chakradarraju  on  2011-03-09 05:36:4
Description : Ticket #120 – Permissions improvement
——————————————————————————–
Revision 281  by  scriptle  on  2011-03-07 22:21:5
Description : User Public Profile – Redirection Only.
——————————————————————————–

….

Here goes the code :

 #!/bin/bash
 #
 # Prints the usage details
 #
 print_help()
 {
 echo "svnlogger 0.1 by Abhishek Shrivastava [i.abhi27[at]gmail.com]";
 echo "Usage : $0 [options] [svn root] [changelog]"
 echo "options include:";
 echo " -h : Use HTML output";
 echo " -x : Use TEXT output (default)";
 echo " -t path : use path as template folder instead of default (Only with -h)";
 echo "svnroot : The root directory of SVN Repo. Default = ./";
 echo "changelog : The path of changelog file. Default = ./ChangeLog";
 return
 }

#
 # Prints an error
 #

print_error()
 {
 case "$1" in
 1) echo "Error : -x and -h cannot be both set";;
 2) echo "Error : -h must be set before using -t";;
 esac
 exit 1;
 }

#
 # Generates text log
 #

generate_text_log()
 {
 SVNROOT=$1
 CHANGELOG=$2
 : ${SVNROOT:="./"}
 : ${CHANGELOG:="./ChangeLog"}
 svn log -r HEAD:1 $SVNROOT | grep -v "^$" | grep -v ^- | awk -F "|" '
 BEGIN{
 username=""
 date=""
 revno=""
 }
 {
 if($1 ~ /^r[0-9]*/)
 {
 username=$2
 date=substr($3,1,19)
 revno=substr($1,2)
 }
 else
 {
 print "Revision " revno " by " username " on " date
 print "Description : " $1
 print "--------------------------------------------------------------------------------"
 }
 }
 END{
 print "Log generated using svnlogger v0.1 by abhishekdelta"
 }' > $CHANGELOG
 }

#
 # Generate HTML log
 #

generate_html_log()
 {
 echo "hey"
 }
 #
 # Main procedure begins here
 #

#
 # Parse arguments
 #
 while getopts hxt: opt
 do
 case "$opt" in
 h) if [ -z "$TEXT" ] ;
 then
 HTML="true"
 else print_error 1;
 fi ;;
 t) if [ ! -z "$HTML" ] ;
 then
 TEMPLATE="$OPTARG"
 else print_error 2;
 fi ;;
 x) if [ -z "$HTML" ] ;
 then
 TEXT="true"
 else print_error 1;
 fi ;;
 [?]) print_help; exit 1;;
 esac
 done

#
 # Generate Log
 #
 if [ -z "$HTML" ] || [ $TEXT -eq "true" ] ;
 then
 echo "Generating text log ..... "
 generate_text_log;
 echo " done!"
 else
 echo "Generating html log ..... "
 generate_html_log;
 echo " done!"
 fi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s