Saturday, August 7, 2010

BASH post-commit script for posting SVN commits to twitter (with Trac / link)

EDIT: Changes to the twitter API broke this script. See workaround and new script here: Workaround for BASH twitter script with OAuth and

Recently I got the idea of using twitter to follow commits to various SVN repositories. I thought this might be of interest to someone out there. It's free to use (under the Apache 2.0 license).

We have Trac pages set up for our projects, so I use to shorten URLs to the changeset. If this isn't applicable to your project, you can remove it from the script. To get it working, simply place this script in the "hooks" directory of your SVN repository, and name it "post-commit". It must also be executable. I would recommend restricting read access to your user, and the apache user (or whatever SVN runs under).

Here's the format of the tweets: [revision number] [username] [branch] ([num files updated, added, delated]): [comment] [trac changeset url]

Tweets longer than 140 characters will be automatically shortened (without ruining the trac URL)

Here's an example:

opensha_svn 6920 kmilner releases (6U, 0A, 0D): merged from trunk: updated build version to 1.0.0

Here's the code (don't forget to fill in the blanks with your information!):



# this is the path to your SVN binaries, you'll probably have to change this
# this is the trac URL. be sure to excape special characters like ':' with '%3A' and '/' with '%2F'
# I left an example to help with formatting, you should fill it in!
# this is your login name
# this is your API key
# twitter usename
# twitter password

# ---------------------------------
# This gathers some info on the commit, and "tweets" it to the twitter account
# "opensha_svn" at: .
# This is half for fun, and half to make it easier for Kevin to track commits.
# ---------------------------------
COMMENT=`$SVNDIR/svnlook log -r${REV} ${REPOS}`
AUTHOR=`$SVNDIR/svnlook author -r${REV} ${REPOS}`
UP=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c U`
ADD=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c A`
DEL=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c D`

numTrunk=`$SVNDIR/svnlook dirs-changed -r${REV} ${REPOS} | grep -c trunk`

if [[ $numTrunk -gt 0 ]];then
codebase=`$SVNDIR/svnlook dirs-changed -r${REV} ${REPOS} | cut -f2 --delimiter="/" | head -n 1`

tweet="$REV $AUTHOR $codebase (${UP}U, ${ADD}A, ${DEL}D): $COMMENT"

if [[ $TRAC_URL ]];then
bitly=`curl "${BITLY_LOGIN}&apiKey=${BITLY_API}&uri=${TRAC_URL}&format=txt"`

bitlychars=`echo -n $bitly | wc --chars`
if [[ $bitlychars -gt 25 ]];then

if [[ `echo -n "$tweet $bitly" | wc -c` -gt 140 ]];then
let chars=136-$bitlychars
tweet="`echo -n $tweet | head -c $chars`... $bitly"
tweet="$tweet $bitly"

echo "tweet of len `echo -n "$tweet" | wc -c`: $tweet"

curl --basic --user ${TWITTER_USERNAME}:${TWITTER_PASSWORD} --data status="${tweet}"

New Blog

I have a new blog! I had an old website for a while, but that was mostly just for learning php/linux server administration, but lately I've been wanting a place to post cool stuff that I come across. Most of it will probably be little tech tricks, rants on USC football, travel reports, etc. Anyway, I hope you enjoy it!