Updating Trac tickets in svn commits
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:
1 2 | 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:
1 2 3 4 5 | REPOS="$1" REV="$2" TRAC_ENV="/var/trac/code" /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:
1 2 | sudo svn export http://svn.edgewall.com/repos/trac/trunk/contrib/trac-post-commit-hook \ trac-post-commit-hook.py |
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:
1 | 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:
1 | sudo chmod -R a+w /var/trac/code/db |
Test
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:
1 | (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.
January 25, 2009 - 1:41 PM Comments (4)







