aboutsummaryrefslogtreecommitdiff
path: root/gitwrapper.sh
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-07-19 01:34:53 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-07-19 01:34:53 +0200
commit8d1d0706679062a5bd3257727af46f66a65e1b5f (patch)
tree385b373b3c0302a0663c5fd91572617489efa740 /gitwrapper.sh
parent70cda51e11ca328fdd037399dbb865bb97ec7e6a (diff)
downloadreposync-8d1d0706679062a5bd3257727af46f66a65e1b5f.tar.gz
gitwrapper
Diffstat (limited to 'gitwrapper.sh')
-rwxr-xr-xgitwrapper.sh39
1 files changed, 39 insertions, 0 deletions
diff --git a/gitwrapper.sh b/gitwrapper.sh
new file mode 100755
index 0000000..f619143
--- /dev/null
+++ b/gitwrapper.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+# gitwrapper.sh
+#
+# A wrapper for the 'git upload-pack' command
+# to automatically create repositories if they are
+# pushed to
+#
+# Set command="" in .ssh/authorized_keys:
+#
+# command="/path/to/wrapper.sh myrepos" ssh-rsa ... user@example
+#
+
+[ -z "$1" ] && >&2 echo Invalid configuration in authorized_keys && exit 1
+ALLOWED_REPODIR="$1"
+
+if [ -z "$SSH_ORIGINAL_COMMAND" ]; then
+ bash
+ exit $?
+fi
+
+repo_path=$(sed -n 's/^git upload-pack \(.*\)$/\1/p' <<< "$SSH_ORIGINAL_COMMAND")
+if [ ! -z "$repo_path" ]; then
+ if grep -q '\.\.' <<< "$repo_path"; then
+ >&2 echo Invalid file name.
+ exit 1
+ fi
+
+ reponame_regex='^\w+\.git$'
+ if [ "$(dirname "$repo_path")" != "$ALLOWED_REPODIR" ] || \
+ [[ ! "$(basename "$repo_path")" =~ $reponame_regex ]]; then
+ >&2 echo Invalid repository
+ exit 1
+ fi
+
+ [ ! -e "$repo_path" ] && git init --bare "$repo_path"
+fi
+
+eval $SSH_ORIGINAL_COMMAND