Configuring a Git repository with Redmine

July 8th, 2009 at 11:49 pm • permalink13 comments

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.

settings-select

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.

settings-path

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.

settings-path-example

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

http://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.

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.

  1. Redmine 0.9.0 and duplicate content issue
  2. Helperful: a large collection of Rails Helpers
  3. Configuring Rails 3 to use HTTPS and SSL
  4. Helperful 0.3.0
  5. TabsOnRails and Helperful migrated to Gemcutter

Filed in Programming • Tags: , , ,

Comments

micah says:

I would be careful about running the Redmine fetch_changesets every minute because if you have a number of different projects, or large amounts of changes, this process can take more than one minute to complete. It might be better to run it every 10 minutes, or if needed more regularly, as a post-commit hook in your repository.

Aline says:

good tutorial… congratulations!!!

Mike says:

Have you figured out how to associate revision numbers with tickets? Such as I am working on ticket x and have revisions 1,2,3 associated with it.

Git doesn’t use revision numbers. Each commit is uniquely identified by a SHA1 hash.

In Redmine you can use commit:123456 (where 123456 is the hash) instead of r123

justin says:

Hi,
the Redmine docs state Redmine requires a bare clone, but a bare clone loses the remote origin details so fetch fails.

I believe the correct way to set this up (with Git >= 1.6.0) is to git clone –mirror git@github.com:myexcellentproject.git

I found details on this here: http://www.redmine.org/wiki/redmine/HowTo_keep_in_sync_your_git_repository_for_redmine

cheers

Hi Justin,
I don’t see any reason why you need to create a bare clone.

In fact, the articles creates the bare clone then adds the remote origin to be able to fetch updates.

cloneofsnake says:

Great tutorial! Are you familiar with http://github.com/koppen/redmine_github_hook/ ?

Instead of setting up a cron job to regularly hit a remote repo (in this case Github only), it pushes updates to the one linked to redmine whenever changes are pushed to github.

I’ve set everything up but in my redmine/projects/proj_name/repository page, it looks like it can see the latest revisions on github, but the changes aren’t being pushed to redmine’s repo.

Any ideas?

Without additional information, it’s too hard to guess the problem. I suggest you to check the Rails log.

PaulN says:

What a helpful post and comments. Thanks everyone!

/p

[...] Configuring a Git repository with Redmine 2010/08/10 | Chulki Lee | Tags: git, redmine | No Comments » [...]

Ricardo says:

Locally, I cloned bare and non-bare repos from an remote git repo.
When I place either paths in the settings->repository tab, I get the following messages:

“An error occurred when trying to access the repository: No such file or directory – git –git-dir “/e/inetpub/wwwroot/.git/” branch –no-color’

“An error occurred when trying to access the repository: No such file or directory – git –git-dir “/e/inetpub/ecom-redmine.git/” branch –no-color”

I am running BitNami Redmine stack [Latest stable release: 1.1.2 (2011-03-07)] on Windows Server 2003.
I gave “Everyone” Full control of both folders under the windows file system security.

What am I doing wrong?

Ashok says:

Hi Simone,

I am using below the environment: on ubuntu 12.04.

Environment:
Redmine version 2.1.0.stable
Ruby version 1.9.3 (i686-linux)
Rails version 3.2.8
Environment production
Database adapter Mysql2
Redmine plugins:
redmine_scm 0.4.1

I have installed Remine without any issue. In the “repository” Tab, I am not seeing any field for scm and field to the repo path, like in this tutorial shows.

Please suggest where is the issue.

Thank in advance,

Patrice T. says:

Just to let know others that if you have redmine installed on the same server as your git server, you dont need to perform the part: fetching new commits…

Add a Comment




Follow Me
    Random Quote