git on shared hosting with git-http-backend

I managed to write the correct .htaccess in order to allow git-http-backend without changing apache main config file. The git-http-backend contains description only of a scenario where you have to change the apache config file.
This was tested on hostmonster, but supposed to work on others like bluehost or dreamhost etc.

First you have to make sure that at least git version 1.6.6 is installed on your system. You may build it yourself. We assume that is installed to

$HOME/system/usr/

Now create a script git-http-backend.sh in the directory

$HOME/public_html/website.com/git/

with the following content:


#!/bin/sh
#first we export the GIT_PROJECT_ROOT
export GIT_PROJECT_ROOT=$HOME/gitprojects/
#and run your git-http-backend
$HOME/system/usr/libexec/git-core/git-http-backend

#uncomment the following line if you want to see environment variable values
#echo $REMOTE_USER $QUERY_STRING $REQUEST_METHOD $PATH_INFO >> $HOME/logs/log

the .htaccess file should contain:

Options +ExecCgi
AuthName "Private Git Access"
AuthType Basic
#you need to add users and passwords to the .htpasswd file
#you have to add the same user and password on the client machine under ~/.netrc
AuthUserFile <<YOUR HOME DIR>>/public_html/website.com/git/.htpasswd
Require valid-user
RewriteEngine on
RewriteBase /git
SetHandler cgi-script
RewriteRule ^([a-zA-Z0-9._]*\.git/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\.(pack|idx))|git-(upload|receive)-pack))$ /git/git-http-backend.sh/$1

note that:
1. <<YOUR HOME DIRE>> needs to be replaced by your home directory (or other folder you have access…)
2. the $HOME/public_html/website/git is the physical address of the website, and $HOME/gitprojects is the directory where you host the projects
3. you will be able now to clone your projects under:

4. do not forget to add a file git-daemon-export-ok in the *.git directory you want to “export”

5. make sure that both on your local computer and on your shared host you have at least git 1.6.6 with curl and expat

git clone http://website.com/git/myproject.git

01

[...] git on shared hosting with git-http-backend [...]

04
May 4th, 2010 12:07 pm

mobiphil,

Thanks for your how-to. It’s tantalizing, but being a perpetual Apache noob, I don’t get it.

Where should the .htaccess file go? What is the directory myproject.git? Where is it?

I’m trying to get your fix to work on BlueHost, by the way.

Regards,
Tom

05
mobiphil
May 6th, 2010 9:44 am

Tom,

1. .htaccess should go to $HOME/public_html/website.com/git/ in the example. (on your web host)
2. myproject.git is a git project root on the server, under $HOME/gitprojects.
3. note that the git project and the cgi script are not in the same directory.

If this is not enough, I would suggest to read more carefully Apache documentation about .htaccess and git (and git-http-backend). I will review at a certain moment this how-to, and will extend it to reflect your observation about this little confusing detail.

06
May 7th, 2010 12:33 pm

mobiphil,

Thanks for your response. I have read the docs, but I’ll read them some more later.

I checked that I have the files in the right place and tried again (git clone http://tomelam@example.com/git/myproject.git). I’m getting the error

fatal: http://tomelam@onthesamepage.org/git/myproject.git/info/refs not found: did you run git update-server-info on the server?

I shouldn’t have to run ‘git update-server-info’, should I? Anyway, I did run it in ~/gitprojects/myproject.git and git didn’t complain until I again tried to clone the repo. But I got the same ‘…/info/refs not found’ error.

WireShark captures this (filtered for http):
GET /git/myproject.git/info/refs?service=git-upload-pack HTTP/1.1
HTTP/1.1 404 Not Found (text/html)
GET /git/myproject.git/info/refs HTTP/1.1
HTTP/1.1 404 Not Found (text/html)

I will try to figure out if the rewrite rule is the problem, but your help would be appreciated.

Thanks,
Tom
~

07
May 7th, 2010 10:16 pm

s/example.com/onthesamepage.com/

08
May 8th, 2010 11:49 am

I noticed that git is not referring to a URL with the proper .htaccess, so I inserted ‘website.com’ in the URL passed to the git command.

I also noticed that my HTTP GET request wasn’t being authenticated, so I tried the command ‘git clone http://tomelam:passwordxxx@onthesame.org/website.com/git/myproject.git‘. Now I’m getting the error ‘fatal: http://tomelam:bmc4387@onthesamepage.org/website.com/git/myproject.git/info/refs not found: did you run git update-server-info on the server?’ and WireShark captures the HTTP exchange:

GET /website.com/git/myproject.git/info/refs?service=git-upload-pack HTTP/1.1
HTTP/1.1 401 Authorization Required (text/html)
GET /website.com/git/myproject.git/info/refs?service=git-upload-pack HTTP/1.1
HTTP/1.1 404 Not Found
GET /website.com/git/myproject.git/info/refs HTTP/1.1
HTTP/1.1 404 Not Found

I expected the GET request for /website.com/git/myproject.git/info/refs?service=git-upload-pack to be rewritten as a request for /git/git-http-backend.sh, and apparently it is, because I uncommented the logging in git-http-backend.sh and the following lines get printed to ~/logs/log:

service=git-upload-pack GET /myproject.git/info/refs
GET /myproject.git/info/refs

09
mobiphil
May 9th, 2010 4:29 am

Tom:

are you sure that you are on the latest git version both on the server and client?

10
May 10th, 2010 1:07 pm

mobiphil,

I just upgraded git on the server and client to 1.7.1 from 1.7.0, but the symptoms are unchanged.

11
mobiphil
May 10th, 2010 2:40 pm

1. the “did you run git update-server-info” is a misleading error in this case, and in several other situations. The git guys should remove that
2. if the git-http-backend.sh is called, then it means that the real git-http-backend is not finding your project file, so you need to check if GIT_PROJECT_ROOT points to the right directory in the shell script

12
May 20th, 2010 10:42 am

mobiphil,

Got clone to work. Should have paid more attention to the git-http-backend man page. I added the file git-daemon-export-ok to /website.com/git/myproject.git and all went well.

Now I’m working on getting push to work. I’m cd’ing into the cloned repository, making and committing a change, and running the command:
git push http://tomelam:password@onthesamepage.org/website.com/git/myproject.git

and getting return code 22, ‘fatal: git-http-push failed’.

Tom

14
June 17th, 2010 2:56 pm

Tom:

I added some time later a new git repo… I had the same error. The I remembered that I have to add the git-daemon-export-ok file. The I came here to update the instruction and to tell you what you should take care.. but I see that this solved your problem already.

Leave Your Comment

Name*
Mail*
Website
Comment