An SVN post-commit hook shell script to copy the log message and revision number to the clipboard (o

After a commit to my local development Subversion repository, I like to add the message from the commit and revision number as a note to the current entry in my time-tracking app (Timelog4) in the form “MESSAGE [rev REVISION-NUMBER]”, so I whipped up a quick shell script to grab that info as a post-commit hook and copy it the clipboard ready to be pasted into Timelog:

#!/bin/bash
MESSAGE=$(/usr/bin/svn log "$1" -r HEAD --limit 1 --incremental | sed '1,3d')
REVNUM=$(/usr/bin/svn log "$1" -r HEAD --limit 1 --quiet | grep -o '^r[0-9]*' | sed "s/r//")
OUTPUT="$MESSAGE [rev $REVNUM]"
echo "$OUTPUT" | pbcopy
growlnotify -a 'Cornerstone' -m "$OUTPUT" "Log message & rev no. copied"

I’m using Cornerstone 2 as a visual SVN browser/tool and had trouble making my script working by putting it in the normal ‘repodirectory/hooks/’ directory (I think it’s to do with Cornerstone using a baked-in SVN client and not passing the POSIX arguments to the shell script quite right) but it works fine if invoked using these instruction: Running Post-Commit Actions.

The code

There doesn’t seem to be a simple way to just get either the revision number or the just the message using the SVN ‘log’ command (if you are using any of the scripting bindings they do provide these bits of data in a structured way but I wanted a simple shell script with no dependencies – apart from the optional Growl line – see below), so lines 2 & 3 grab those bits of the last log entry and put them into a couple of variables. Line 4 sets up the output which will get copied to the clipboard, so this is the line where you would customise how you want the message and revision number to be formatted. Line 5 uses ‘pbcopy’ (an OS X command line command) to add the OUTPUT to the clipboard. Line 6 is an optional bit which I find useful – it uses Growl (an OS X notification utility) and growlnotify (a command-line tool to post Growl notifications) to post a notification of what has been copied to the clipboard – if you don’t want this functionality just delete this line.

Written by , Saturday 5th March 2011

Comments

There are no comments

Posting comments after three months has been disabled.