Serving git repositories the smart way

Tags: howtos, projects, programming

Published on
« Previous post: Cleaning quoted text from forwarded … — Next post: Converting a PDF to the CMYK colour … »

At work, we want to serve our git repositories without requiring the committers to have an account on our servers. Since we already have a web server running, we decided on using a combination of git plus apache2 plus webdav. Unfortunately, with HTTP being a rather inefficient protocol, we quickly ran into performance problems. Unbeknownst to us, git introduced git-http-backend in version 1.6.6. The smart HTTP transport we had so longed for was finally there. The configuration could not be easier. Assuming you have two repositories foo and bar that you want to serve (a further assumption is that two different groups of people shall have access to each of the repositories), you need to modify the Apache configuration as follows:

ScriptAlias /foo/ /usr/lib/git-core/git-http-backend 
ScriptAlias /bar/ /usr/lib/git-core/git-http-backend 

<Location /foo>
	SetEnv GIT_PROJECT_ROOT /path/to/foo
	SetEnv GIT_HTTP_EXPORT_ALL
	AuthType Basic
	AuthName "foo"
	AuthUserFile /etc/apache2/htpasswd_foo
	...
</Location>

<Location /bar>
	SetEnv GIT_PROJECT_ROOT /path/to/bar
	SetEnv GIT_HTTP_EXPORT_ALL
	AuthType Basic
	AuthName "bar"
	AuthUserFile /etc/apache2/htpasswd_bar
	...
</Location>

The above configuration snippets only work if your webserver has mod_alias, mod_cgi, and mod_env enabled. If this is not the case, enable them via a2enmod.

If all prerequisites are satisfied, there will be two completely independent repositories, served using the new smart HTTP backend, with different (non-overlapping) user groups. See the official documentation for more usage scenarios. In particular, the scenario where gitweb is used in conjunction with git-http-backend is pretty interesting.