DBMail on twitter

Tuesday, January 19. 2010

I've added a post-receive hook to my GIT repository at git.dbmail.eu so you can stay in touch with dbmail changes via twitter.

You can follow me at http://twitter.com/pjstevns.

The script I'm using is really simple. Some others might also find this useful:

#!/bin/sh

# copyright Paul Stevens, 2010, paul@nfg.nl
# licence GPLv2
#
# example hook script to send out twitter messages.
# This script will send out messages summarizing new revisions
# introduced by the change received
#
#
# Config
# ------
# hooks.twitterid
#   the username on twitter
# hooks.twitterpw
#   the password on twitter
# hooks.hashtag
#   insert a hashtag at the start of the message
# hooks.hashurl
#   replace hash signs in commit messages with an url. This
#   is used to link hash ids in messages to a bugtracker since
#   they typically refer to a bug-id.
#


GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
if [ -z "$GIT_DIR" ]; then
	echo >&2 "fatal: post-receive: GIT_DIR not set"
	exit 1
fi
projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
# Check if the description is unchanged from it's default, and shorten it to a
# more manageable length if it is
if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
	then
	projectdesc="UNNAMED PROJECT"
fi


generate_message()
{
	oldrev=$(git rev-parse $1)
        newrev=$(git rev-parse $2)
	refname="$3"
	message=`git log --pretty=oneline ${oldrev}..${newrev} $refname|cut -f2- -d' '|sed 's/$/, /g'`
	hashurl=`echo "$hashurl"|sed 's/?/?/'`
	if [ -n "$hashtag" ]; then
		echo -n "#${hashtag} "
	fi
	echo $message|sed -e "s,#,${hashurl},g" -e 's/,$//'
}		

send_twitter()
{
	message="$@"
	curl --basic --user ${twitterid}:${twitterpw} --data status="$message" 
		https://twitter.com/statuses/update.xml >/dev/null
}




twitterid=$(git repo-config hooks.twitterid)
twitterpw=$(git repo-config hooks.twitterpw)
hashurl=$(git repo-config hooks.hashurl)
hashtag=$(git repo-config hooks.hashtag)

# --- Main loop
# Allow dual mode: run from the command line just like the update hook, or if
# no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
        # Output to the terminal in command line mode - if someone wanted to
        # resend an email; they could redirect the output to sendmail themselves
        generate_message $1 $2 $3
else
        while read oldrev newrev refname
        do
                message=`generate_message $oldrev $newrev $refname`
		send_twitter "$message"
        done
fi