It has been another long time between posts, but here is as promised, but very delayed, the second part to this post.

Working with git repositories in trac

It is incredibly simple to use a git repository instead of svn with trac, you just need to have the right plugin installed and enabled. Once again I’ll give install details for FreeBSD and Ubuntu.

Firstly we need to install the trac git plugin. For FreeBSD there is as usual a dedicated port install for what we need. As of writing the plugin version is r7034. As the root user run:

cd /usr/ports/www/trac-gitplugin
make install clean

Ubuntu users, as root or using sudo:

apt-get install trac-git

With the trac git plugin installed, all we need to do is create a new git repository if we haven’t got one already and create a new trac project pointing to the git repository (or edit the trac.ini config file of an existing trac project). So following from the FreeBSD logic in the previous post, lets create a new git repository in /usr/local/git as root.

cd /usr/local
mkdir git
cd git
mkdir MyGitProject
git init MyGitProject

The git repository above was created by the root user, if you are accessing the git repository over ssh and want to be able to push changes to it, you will need to make sure that the user has read and write access to the folder and everything in the .git folder. What I have done in the past is create a group on the system called developers and add all the user accounts to the group. Then change the group of the git repository to developers and give it group write access. You have to make sure the whole folder and all files are writable by this group or you will get errors trying to push to the repository. An alternative is to just create the git repository using one user account or change the owner of the whole repository to a specific user account and only use that user account to push and pull to the server.

With the repository created, we can now create a new trac project, this is the same steps as in the previous post but we change the repository type from svn to git.

cd /usr/local/trac
mkdir MyTracGitProject
trac-admin MyTracGitProject initenv

The trac-admin program runs in interactive mode and will ask you questions to set up your repository, example questions and responses are given below.

Project Name [My Project]> My Trac Git Project
Database connection string [sqlite:db/trac.db]>
Repository type [svn]> git
Path to repository [/path/to/repos]> /usr/local/git/MyGitProject/.git

Now we just need to edit the trac.ini, this is in the trac project folder, so in our case /usr/local/trac/MyTracGitProject/conf/trac.ini. Make sure that under the [trac] section repository_type is git and the repository_dir is correctly pointing to the .git folder. Also FreeBSD gets the location of the git binary wrong, under the [git] section change git_bin from /usr/bin/git to /usr/local/bin/git. Finally, add the following if your trac.ini file does not already have it.

tracext.git.* = enabled

Now we just need to change the owner of the new trac project to the web user and edit the apache config for the new trac project location. See the previous post for how to do that.

Adding Mylyn support for trac

Mylyn for Eclipse is great, if you are not using it already, you should be. It basically allows you to create tasks, those tasks you can activate and work on, whilst a task is activated it looks at what files you have open and have been working on. So that the next time you activate the task, you see only the files that are most relevant to the task, for more info on Mylyn go here. Mylyn is integrated in the more recent editions of Eclipse, but there is a Mylyn trac plugin that allows you to create and view tickets in your trac repository. How nice is that? If someone reports a new bug, you can see it appear right in your IDE, work on it and close it, all from within Eclipse.

To get the Mylyn support working in trac you need to install and enable the xml-rpc plugin. As usual FreeBSD has a port for it.

cd /usr/ports/www/trac-xmlrpc
make install clean

Ubuntu users can run:

easy_install -Z -U http://trac-hacks.org/svn/xmlrpcplugin/trunk # 0.11

At the end of the install process keep a look out for the location where the python egg was installed, we need to copy that file to our plugins folder in the trac project. This was /usr/local/lib/python2.6/site-packages/TracXMLRPC-1.0.6-py2.6.egg on FreeBSD.

Edit the trac.ini file, under the [component] section add:

tracrpc.* = enabled

Now when you go to the admin section of your trac site you should see the xml-rpc plugin available under Plugins.

Close and comment on trac tickets in your SVN commit messages

You can have trac close and add comments to your tickets simply by including certain text triggers in your SVN commit messages, such as fixes #231, refs bug:29. To get this working you need to include a special script in your svn repository location. First download this python script from the trac web site. Save it in a location which is easily accessible, on FreeBSD I put the script in the trac share folder /usr/local/share/trac.

Now go to your svn repository location in the hooks folder you should see a lot of files with a .tmpl extension. One of them is post-commit.tmpl, we need to copy this and edit it as follows:

cp post-commit.tmpl post-commit
edit post-commit

Ignore all the comments, at the end make sure the file reads as below:


/usr/local/bin/python /usr/local/share/trac/trac-post-commit.py -p "$TRAC_ENV" -r "$REV"
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf

Replace [TracProjectName] with your trac project name or change the path if yours is different. I specified the full path to the python command, on freebsd when this shell script is triggered the script does not inherit you environment PATH, so absolute paths to commands need to be specified, at least on FreeBSD anyway. Change the path of the trac-post-commit.py file to wherever you saved it. I commented out the mailer.py script, this is another post-commit script that you can run which sends an email detailing the commit. This script should be shipped with your subversion installation, on FreeBSD it is in /usr/local/share/subversion/hook-scripts/mailer/mailer.py.

Finally we need to make sure that the python script is executable and owned by the web user like everything else in the svn repository.

chmod 755 post-commit
chown www post-commit

Now when you do your next commit to the repository try closing or referencing one of the trac tickets. You can refer to a ticket by prefixing the ticket number with #, ticket:, issue: or bug:. You can close a ticket using by using a command prefix close, closed, closes, fix, fixed, fixes before the ticket reference. To comment on a ticket but not close it use any of these command prefixes: addresses, re, references, refs or see.

You can customise these commands or ticket references, just edit the trac-post-commit.py file, the following is what you’ll need to change:

ticket_prefix = '(?:#|(?:ticket|issue|bug)[: ]?)'

    _supported_cmds = {'close':      '_cmdClose',
                       'closed':     '_cmdClose',
                       'closes':     '_cmdClose',
                       'fix':        '_cmdClose',
                       'fixed':      '_cmdClose',
                       'fixes':      '_cmdClose',
                       'addresses':  '_cmdRefs',
                       're':         '_cmdRefs',
                       'references': '_cmdRefs',
                       'refs':       '_cmdRefs',
                       'see':        '_cmdRefs'}

So, thats all for now. Apparently you can also do git post-commit hooks as well, but as I am not using git that much I haven’t tried it out yet. Happy coding with your svn/git trac repositories.

No Comment.

Add Your Comment