1920599af45e46adb61b37b7f83e9126804be48c
[matthijs/servers/drsnuggles.git] / usr / local / bin / git-wrapper
1 #!/bin/sh
2
3 # This script prepends BASE_PATH to any arguments that are not options (start
4 # with --) and do not start with BASE_PATH already.
5 # It then executes its equivalent in /usr/bin with those updated arguments.
6 #
7 # This script is meant to provide a base path for accessing git repositories
8 # through ssh. This is similar to running git-daemon(1) with --base-path and
9 # --base-path-relaxed. 
10
11 BASE_PATH='/data/vcs/git'
12
13 function init_repos() {
14         mkdir "$1" &> /dev/null  || return 1 
15         (cd $1 && git --bare init &> /dev/null) || return 1
16 }
17
18 # We keep an array of arguments, so we can handle quoting an spaces in
19 # arguments properly.
20 ARGS=()
21 i=0
22 until [ "$#" -eq 0 ]
23 do
24         if echo $1 | egrep "^--" &>/dev/null; then
25                 # This is an option argument, leave it untouched
26                 ARGS[$i]="$1"
27         elif echo $1 | egrep ".git$" &>/dev/null; then
28                 # This is a path to a git repository
29                 DIR="$1"
30                 # Prepend BASE_PATH if the path does not start with it already.
31                 if ! echo $DIR | egrep "^$BASE_PATH" &>/dev/null; then
32                         DIR="$BASE_PATH/$DIR"
33                 fi
34         
35                 # When the client wants us to receive a pack, create the git
36                 # repository if it does not exist yet   
37                 if [ "`basename $0`" = "git-receive-pack" -a ! -e "$DIR" ]; then
38                         init_repos "$DIR"
39                 fi
40                 ARGS[$i]="$DIR"
41         else
42                 # Not a path to a git repository, leave untouched.
43                 ARGS[$i]="$1"
44         fi
45         ((i++))
46         shift
47 done
48
49 exec /usr/bin/`basename $0` "${ARGS[@]}"