git: Make git-wrapper create symlinks instead of copying them.
[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 HOOKS_PATH="$BASE_PATH/hooks"
13 HOOKS=post-update
14
15 function init_repos() {
16         mkdir "$1" &> /dev/null  || return 1 
17         (cd $1 && git --bare init &> /dev/null) || return 1
18         for hook in $HOOKS; do
19             ln -s "$HOOKS_PATH/$hook" "$1/hooks/" || return 1
20         done
21 }
22
23 # We keep an array of arguments, so we can handle quoting an spaces in
24 # arguments properly.
25 ARGS=()
26 i=0
27 until [ "$#" -eq 0 ]
28 do
29         if echo $1 | egrep "^--" &>/dev/null; then
30                 # This is an option argument, leave it untouched
31                 ARGS[$i]="$1"
32         elif echo $1 | egrep ".git$" &>/dev/null; then
33                 # This is a path to a git repository
34                 DIR="$1"
35                 # Prepend BASE_PATH if the path does not start with it already.
36                 if ! echo $DIR | egrep "^$BASE_PATH" &>/dev/null; then
37                         DIR="$BASE_PATH/$DIR"
38                 fi
39         
40                 # When the client wants us to receive a pack, create the git
41                 # repository if it does not exist yet   
42                 if [ "`basename $0`" = "git-receive-pack" -a ! -e "$DIR" ]; then
43                         init_repos "$DIR" || exit 1
44                 fi
45                 ARGS[$i]="$DIR"
46         else
47                 # Not a path to a git repository, leave untouched.
48                 ARGS[$i]="$1"
49         fi
50         ((i++))
51         shift
52 done
53
54 exec /usr/bin/`basename $0` "${ARGS[@]}"