Monday, August 29, 2011

Monitoring subversion repositories for commits

If your subversion repository doesn't have commit emails turned on - don't fret!  It's fairly simple to monitor commits yourself with a simple bash script ( and growl and a launch agent if your on OSX).

Below is my bash script, that monitors a list of repositories for revision changes:

[bash]
#/bin/bash

# SETUP
growlNotify=/usr/local/bin/growlnotify

if [ ! -f $growlNotify ];
then
echo "Cannot find growlnotify, script will abort."
exit 1
fi

# loop through watch_list
while read repo_url; do
# format SVN repo URL to clean name
# first, strip underscores
CLEAN=${repo_url//_/}
# next, replace spaces with underscores
CLEAN=${CLEAN// /_}
# now, clean out anything that's not alphanumeric or an underscore
CLEAN=${CLEAN//[^a-zA-Z0-9_]/}

# rename $CLEAN to $CLEAN_old
mv tmp/$CLEAN tmp/${CLEAN}_old > /dev/null 2>&1

# run svn info repo, save output to $CLEAN
svn info $repo_url > tmp/$CLEAN

# run diff on $CLEAN and $CLEAN_old
diff tmp/$CLEAN tmp/${CLEAN}_old > /dev/null 2>&1

# if difference, show growl
if [ $? = 1 ]
then
SVN_REV=`awk '/Last Changed Rev: ([0-9]*)/{print $4}' tmp/$CLEAN`
SVN_NAME=`awk '/Path: (.*)/{print $2}' tmp/$CLEAN`
SVN_LOG=`svn log $repo_url -r$SVN_REV`

$growlNotify -n SVNMonkey -m "$SVN_NAME $SVN_LOG"
fi
done < watch_list
[/bash]

For the OSX, I have a launch agent that runs every 300 seconds (5 min). You can either recreate it yourself (I recommend using the free version of Lingon), or you can modify the following plist and use the following commands to install it.

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.npike.svnmonkey</string>
<key>ProgramArguments</key>
<array>
<string>/Volumes/npike 1/phunware/Dropbox/android/tools/svn_monkey/svn_monkey.sh</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
<key>WorkingDirectory</key>
<string>/Volumes/npike 1/phunware/Dropbox/android/tools/svn_monkey</string>
</dict>
</plist>
[/xml]

Notes:

  • ProgramArguments - Should be the full path to the bash script.

  • StartInterval - The amount of time in seconds you wish the script to re-run

  • WorkingDirectory - The full path to the directory where the bash script lives



Once you have the script saved to your mac (in its own directory), follow these instructions:

  1. Make sure growlnotify is installed (for all users). This in the "Extras" folder in the growl download zip

  2. Make sure the plist reflects the correct paths/locations on your machine

  3. Make sure you create a "tmp" folder in the same directory as the bash script.

  4. Open a terminal window to the same directory as the bash script.

  5. Install the launch agent:
    [shell]
    launchctl load net.npike.svnmonkey.plist
    [/shell]

  6. Start the launch agent:
    [shell]launchctl start net.npike.svnmonkey[/shell]


For completeness, you can also download a zip file which contains:

  • The bash script

  • Example watch_list

  • plist for running as a launchd script on OSX

  • Readme for getting the launchd script running on OSX

  • Expected directory structure (needs an empty tmp folder relative to the script)



Download svn_monkey.zip

No comments:

Post a Comment