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