[clug] Using Trac with Git

Brendan Jurd direvus at gmail.com
Mon Aug 31 23:49:53 MDT 2009


Hi cluggers,

Back at the January CLUG meeting, Paul gave a talk demonstrating how
easy it is to set up post-commit hooks between Subversion and Trac
(thanks Paul!) so that when you write a commit message that says
something like "this fixes ticket #174", it automatically drops your
commit message into the ticket and marks it as resolved.

Well I've recently migrated one of my projects from Subversion to Git,
and I decided to see about getting this working with the new Git+Trac
setup.

It turned out to be refreshingly simple.

Much like with Subversion, the Trac trac-post-commit-hook script does
all the heavy lifting for you; it taps directly into the Trac
environment using Trac's own Python API and looks up the commit
message, parses it for references to tickets and appends changes to
those tickets.

All you have to do is make sure that the Git repository calls this
script appropriately.

Git has a hooks system which is quite flexible.  I ended up
plaigarising an existing example script for sending commit emails to a
mailing list with the "post-receive" hook [1].

So really all I needed to do was drop the following into
hooks/post-receive in my master repository, make it executable:

trac_hook="/path/to/trac-post-commit-hook"
env_path="/path/to/trac-environment"

if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
        /usr/bin/python $trac_hook -p $env_path -r $2
else
        while read oldrev newrev refname
        do
                /usr/bin/python $trac_hook -p $env_path -r $newrev
        done
fi

After that, the whole thing Just Worked.

The only thing you need to be wary of is that the Debian trac package
doesn't include the trac-post-commit-hook script.  It's completely
daft, and they've had a bug report in there since this time last year
[2].  So if you're using Debian/Ubuntu you'll need to install this
script yourself.  Other than it really is a breeze.

Cheers,
BJ

[1] http://git.kernel.org/?p=git/git.git;a=blob_plain;f=contrib/hooks/post-receive-email;hb=HEAD
[2] http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=495019


More information about the linux mailing list