Configuring a Git repository with Redmine

One of the features I enjoy the most about Redmine is the built-in support for the most common SCM tools including Subversion, Git and Mercurial. As a Rubyist, you are probably using Git for the most of your Ruby/Rails projects and chances are you'd like to have Redmine synchronized with your Git repositories.

Configuring a Git repository with Redmine it's really straightforward but if you come from the Subversion world, there are a couple of things you need to know to better understand how Redmine interacts with remote repositories.

Repository Settings

First step is to configure the repository settings in Redmine. Open your project page, then go to Settings > Repository.

Choose the Git option, then you'll be asked to enter the path to the Git repository. The path to the repository is the absolute path to the .git folder that represents the repository and contains all the files .git uses to manage your project history.

Due to the way how Git works, this path must point to a folder on your local server. Unlike Subversion, you can't type the address to a remote repository.

Let me show you an example. Assuming I want to configure my helperful GEM repository, I first need to clone the repository on my server then type the absolute filesystem path, for instance

/home/weppos/git/helperful/.git

Using the remote clone URL won't work because Git can't send status commands over the network to remote repository. You can only push to or pull from remote repositories but you need a local clone to work on. This is how Git works.

Again, don't try to fill the field with one of the following addresses:

https://github.com/weppos/helperful/tree/master
git://github.com/weppos/helperful.git

To summarize, clone your repository then configure Redmine with the absolute filesystem path to your .git directory.

$ mkdir -p /home/weppos/git
$ cd /home/weppos/git
$ git clone git://github.com/weppos/helperful.git
...
$ cd helperful
$ pwd
/home/weppos/git/helperful

Hit the create button and go to the Repository page. If your configuration is correct, the page should display the most recent repository commits.

Fetching new commits

If you have used Redmine with Subversion before, you probably noticed that Redmine automatically fetches the latest commits each time you visit the Repository section.

Basically, when you open the Repository page, Redmine contacts the remote SVN repository and asks for the latest revision number. Then, it compares the value with the last changeset in the database and, if new commits are available, it fetches and parses them on-the-fly.

Again, Git is not a centralized repository and Redmine doesn't support remote Git repositories. This means, you need to setup a separate process to update your local Git repository and notify Redmine of new commits.

The most simple way to achieve this goal is configuring a crontab on your server.

# redmine-changesets.sh
cd /home/weppos/git/helperful && git pull origin master
cd /path/to/redmine && /usr/bin/ruby1.8 script/runner "Repository.fetch_changesets" -e production
# Run the script every minute
* * * * * /home/weppos/redmine-changesets.sh

The script is quite self-explanatory. First you run git pull to update the local git repository fetching the changes from the upstream repository, for example your Github account.

Then you force Redmine to scan the repository for new commits with the Repository.fetch_changesets command.

That's it. You can browse my Codestuff to see how Redmine works with Subversion and Git repositories.