git: Automatically create non-existing repositories.
[matthijs/servers/drsnuggles.git] / usr / local / bin / git-prepend-base
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 # We keep an array of arguments, so we can handle quoting an spaces in
14 # arguments properly.
15 ARGS=()
16 i=0
17 until [ "$#" -eq 0 ]
18 do
19         if echo $1 | egrep "^--" &>/dev/null; then
20                 # This is an option argument, leave it untouched
21                 ARGS[$i]="$1"
22         elif echo $1 | egrep ".git$" &>/dev/null; then
23                 # This is a path to a git repository
24                 DIR="$1"
25                 # Prepend BASE_PATH if the path does not start with it already.
26                 if ! echo $DIR | egrep "^$BASE_PATH" &>/dev/null; then
27                         DIR="$BASE_PATH/$DIR"
28                 fi
29         
30                 # Create the git repository if it does not exist yet    
31                 if [ ! -e "$DIR" ]; then
32                         mkdir "$DIR" &> /dev/null && (cd $DIR && git --bare init &> /dev/null)
33                 fi
34                 ARGS[$i]="$DIR"
35         else
36                 # Not a path to a git repository, leave untouched.
37                 ARGS[$i]="$1"
38         fi
39         ((i++))
40         shift
41 done
42
43 exec /usr/bin/`basename $0` "${ARGS[@]}"