Git IRC bot in 5 minutes


Say you like Git as much as I do. At some point, you have decided to set up your own Git server to serve your own projects.

But then, people have started to get interested and are contributing as well: you have probably moved from a bare Git server to something like Gitolite which is very handy when it comes to managing users and repositories.

IRC bot reporting new branch and commits

And now you have an IRC channel for your project, and would like to let everyone see when a commit is pushed to your server. In other words, you need an IRC bot announcing git commits as they are pushed!

Set up


First, you need a program which connects to your IRC channel and stay there all the time. We will use ii for that. This IRC software is simple, small, beautiful, KISS. When run, it creates an architecture of files to match the channels you are connected to. Messages sent in those channels will written in the out files, and if you write into the in files, your messages will end up in the channel. Pretty neat. So just install it on your server, it is probably already in the repositories of your distribution.

Then, you want it to be run when your computer starts, and automagically reconnects to the IRC channel if anything went wrong. Just make sure that the following bash script gets executed when your computer starts (doing so depends on your system).

#!/bin/bash # configuration path=/tmp/gitircbot # where the files created by ii go server='' channel='#yourproject' bot_pseudo='GitIRCBotMadeSimple' bot_realname='My super Git IRC bot that I created in 5 minutes' # launch the bot while true do # connect to the IRC server ii -s "$server" -i "$path" -n "$bot_pseudo" -f "$bot_realname" & pid="$!" # connect to the IRC channel sleep 5 echo "/j $channel" > "$path/$server/in" # if the bot disconnects, go back to square one wait "$pid" done

Git hook

At that point, you only need your Git server to write to one of the out files generated by ii when someone pushed some commits. This is quite easy: you only need to create a Git hook named post-receive on the server with the following content:

#!/bin/bash # configuration path=/tmp/gitircbot # where the files created by ii go server='' channel='#yourproject' # file to append messages to out="$path/$server/$channel/in" # read input, extract branch&repo names read oldrev newrev ref branch=${ref/refs\/heads\//} repo=$(basename $(pwd)|sed 's/\.git$//') # feel free to hardcode it # commits to consider if [ "$oldrev" == '0000000000000000000000000000000000000000' ] then # new branch! echo "[git] New branch: $repo/$branch" >> $out # get all revisions specific to this branch commits="$(git for-each-ref --format='%(refname)' 'refs/heads/*' \ |grep -v "$ref"|sed 's/^/\^/') $newrev" else # range of revisions commits="$oldrev..$newrev" fi # count number of commits num=$(git log --format='%h' $commits|wc -l) s=$([ "$num" != '1' ] && echo 's') # plural # generate messages echo "[git] $num commit$s pushed to $repo/$branch" >> $out git log --format=' %h %an %s%n' $commits --reverse >> $out

Nothing really complex here, just plain bash code getting data from git calls. It should work in most of the cases: only some rare actions (e.g. creating of a new tag) are expected to send non accurate messages to the IRC channel.

If you want to change how commits are displayed, I can point you to the pretty formats section of the manual page for git log.


Now you should have a working IRC bot sending commit messages to your channel. If you keep the commit author in its messages, you will get highlighted for free!

As you have seen, that was pretty simple. Separating the connection to IRC from the core logic of the bot allows you to play around with the git hook without having to restart ii. And since making the bot sending new messages is as easy as writing to a file, it should not be difficult to add new services. If your project has a blog, why not fetching the RSS or Atom feed and pushing it back to your IRC channel?

This article is released under the CC BY-SA licence.

The source code in this article is released under the CC 0 licence.

Short URL: