The ability to update and close Trac tickets associated with a given repo whenever a commit is made to that repo is pretty handy. No more committing, going to the Trac site, updating. I found several guides on setting this up floating around, this one being the most informative. However, I’d like to clearly state the steps here, as I found most of the guides rather muddled, when the process is really quite simple.
In these examples, I will assume:
- Your SVN repo (master): /var/svn/repositories/code
- Your Trac instance: /var/trac/code
Add and edit a post-commit script
In /var/svn/repositories/code/hooks, there should be a post-commit.tmpl added by the initialization of the repo. Copy this and edit the copy:
sudo cp post-commit.tmpl post-commit
sudo vim post-commit
The file consists of lots of informative comments, and some uncommented stuff a the bottom. Delete all the uncommented stuff and add:
/usr/bin/python /var/svn/repositories/code/hooks/trac-post-commit-hook.py \
-p "$TRAC_ENV" -r "$REV"
Add a trac-post-commit-hook.py file
In /var/svn/repositories/code/hooks, export the trac-post-commit-script:
sudo svn export http://svn.edgewall.com/repos/trac/trunk/contrib/trac-post-commit-hook \
You shouldn’t need to edit this.
Depending on your permissions setup for your SVN repo, you will need to change the ownership of the trac-post-commit script, as well as the post-commit script made above. In my case, I have svn:svn on all associated files, so at this point I ran:
sudo chown svn:svn /var/svn/repositories/code/hooks/*
Change perms on your Trac db dir
When you perform the commit, svn will execute what is in post-commit, which hits the trac-post-commit script, which then tries to update your Trac instance. For that to be possible, it needs access to your Trac db. How to do that totally depends on what access method you have for svn. In my case, I use svn+ssh, so it is my user (and others that commit, so configuring for an ‘svn’ group is easier) that needs access. If you aren’t sure, or to test, this should work:
sudo chmod -R a+w /var/trac/code/db
Now when you perform a commit to something in the code repo in our example and the change is associated with, say, ticket 456, your comment could be:
* Fixed problem X, logged state Y. closes ticket:456
And immediately thereafter, ticket 456 would have a comment such as:
(In [REVNUMBER]) * Fixed problem X, logged state Y. closes ticket:456
and be closed.
The Trac FAQ entry on hooks to commits has additional troubleshooting steps for configuration. As long as you don’t have a crazy svn and/or Trac setup, however, this should be relatively easy to get going.