]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - scripts/gitcreate
gitcreate: automatically re-run as repo owner
[repo_shell.git] / scripts / gitcreate
index 2c655157aa0a373f0007314318e091b06803fdfe..daa5eb4bd806b7aa0aebf50118ca9a7faa1cd858 100755 (executable)
@@ -1,9 +1,20 @@
 #!/bin/sh
 # gitcreate is part of the repo_shell distribution.
 
-source /etc/repo_shell.cfg
-if [ -z "$owner" -o -z "$git_root" -o -z "$git_acl_file" ]; then
-    echo "$0: please configure /etc/repo_shell.cfg"
+eval $([ -f /etc/repo_shell.conf ] && sed -e 's| ||g' < /etc/repo_shell.conf)
+if [ -z "$owner" -o -z "$git_root" ]; then
+    echo "$0: please configure /etc/repo_shell.conf"
+    exit 1
+fi
+gitacls=.gitacls
+gitaclspath="$git_root/$gitacls"
+githooks=.githooks
+githookspath="$git_root/$githooks"
+
+# If running as root, rerun via sudo as user $owner to ensure proper permissions
+if [ "$(whoami)" == "root" ]; then
+    sudo -u $owner $0 $*
+    exit $?
 fi
 
 if [ "$(whoami)" != "$owner" ]; then
@@ -11,21 +22,58 @@ if [ "$(whoami)" != "$owner" ]; then
     exit 1
 fi
 
+unset yes
+if [ "$1" = "-y" ]; then
+  yes=y
+  shift
+fi
+
 if [ $# -ne 1 ]; then
     echo "usage: $0 <repopath>" >&2
     exit 1
 fi
 
-if [ -e $git_root/$1 ]; then
-    echo "$0: repository $1 already exists" >&2
+repopath=$1
+
+if [ -e $git_root/$repopath ]; then
+    echo "$0: repository $repopath already exists" >&2
     exit 1
 fi
 
+# Do not create subdirectories without asking first
+repodir=$(dirname $repopath)
+if [ ! -d "$git_root/$repodir" ]; then
+  if [ ! $yes ]; then
+    echo -n "Create git subdir '$repodir' (y/N)? "
+    read ans
+    ans=$(expr "$ans" : '\(.\).*') # works with dash too
+    if [ "$ans" != "y" -a "$ans" != "Y" ];then
+      echo "repository creation aborted at user request"
+      exit 0
+    fi
+  fi
+  mkdir -p "$git_root/$repodir" 2>/dev/null
+  if [ ! -d "$git_root/$repodir" ]; then
+    echo "%0: repository not created, git subdir '$repodir' create failed" >&2
+    exit 1
+  fi
+fi
+
 # Create the respository
 umask 027
-git --git-dir "$git_root/$1.git" init --bare
+git --git-dir "$git_root/$repopath" init --bare
 [ $? -ne 0 ] && exit 1
 
-echo "Repository created.
-Be sure to edit $git_acl_file to allow user access, if needed."
+# Create symbolic links to any hook scripts in $githookspath
+hooks="pre-receive post-receive update post-update"
+backdir=$(echo "$repopath/hooks" | sed -e 's|[^/]*|..|g')
+hookscripts=$(cd $githookspath 2>/dev/null && ls -d $hooks 2>/dev/null)
+for s in $hookscripts; do
+    echo "Linking default hook script $s"
+    ln -s $backdir/$githooks/$s $git_root/$repopath/hooks/
+done
+
+[ -f "$gitaclspath" ] && echo "Check $gitaclspath for proper access permissions"
+echo "Repository created."
+[ ! -f "$gitaclspath" ] && echo "WARNING: $gitaclspath DOES NOT EXIST!"
 exit 0