Home > Quickie > Sending Mercurial commit messages to Twitter

Sending Mercurial commit messages to Twitter

May 25th, 2009

[Update 16/02/2011 - This no longer works now that Twitter has disabled basic_auth. Oh well....]
[Update 21/07/2009 - Twilight is now called Daylight.]

If you follow what I do on this blog and on my podcast with Philippe Guitard, you know that I like Mercurial (hg), one of the newer distributed version control systems.

In particular, I really like Murky as a GUI front-end to Mercurial. In fact, I’m working on the French localization with Olivier Kaluzny.

I want to create a tweet from @ichibiapp or @daylightapp for every commit. I do this with other apps and subversion, and find it pretty handy to catch new commits to production. Plus, if you follow these products on Twitter, you too can get on the inside track of the software releases.

If you want to do the same for your Mercurial repositories, here’s how:

  1. Put this shell script, called commit-twitter, in your $PATH and mark it as executable (chmod a+x commit-twitter):
    #!/bin/sh
    # Simple script to send the first line of the commit message as a tweet
    if [ $# -ne 2 ]
    then
      echo "Usage: `basename $0` twitter-username twitter-password"
      exit 1
    fi
    TWITTER_USER=$1
    TWITTER_PASS=$2
    TWEET=$(hg log -r $HG_NODE --template '{desc|firstline}')
    curl -s -u "$TWITTER_USER:$TWITTER_PASS" -d "status=$TWEET" http://twitter.com/statuses/update.xml & > /dev/null
    


    The script takes two arguments, username and password, and checks that you have those two arguments. Then it extracts the tweet from the log using the HG_ environment variables set by Mercurial.
    Finally, it uses curl to generate the tweet, using silent mode (-s), running in the background with & (to return immediately) and sending the output to /dev/null.

  2. Add the following lines to your repository’s .hg/hgrc file, creating it if it does not exist:
    [hooks]
    commit.tweet = commithook-twitter <twitter-username> <twitter-password>
    
  3. There is no Step 3!

Voilà! Every time you commit to Mercurial, the first line of your commit message will be posted as a tweet to the specified twitter account.

Categories: Quickie Tags:
Comments are closed.