interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / bazinga / omega-read
| 1 | #!/bin/sh |
| 2 | # The clone and both main archives use the same public, filtered repository. |
| 3 | set -eu |
| 4 | export GIT_PROJECT_ROOT=/var/www/interplane-git |
| 5 | export GIT_HTTP_EXPORT_ALL=1 |
| 6 | notfound() { printf 'Status: 404 Not Found\r\n\r\n'; exit 0; } |
| 7 | route=${PATH_INFO#/} |
| 8 | name=${route%%/*} |
| 9 | case "$name" in ''|[!a-zA-Z0-9]*|*[!a-zA-Z0-9._-]*) notfound ;; esac |
| 10 | repo="$GIT_PROJECT_ROOT/$name.git" |
| 11 | [ -d "$repo" ] && [ ! -L "$repo" ] || notfound |
| 12 | PATH_INFO="/${route#*/}" |
| 13 | export GIT_CONFIG_COUNT=3 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0="$repo" |
| 14 | export GIT_CONFIG_KEY_1=http.receivepack GIT_CONFIG_VALUE_1=false |
| 15 | # http-backend invokes upload-pack on dot after entering this fixed public repo. |
| 16 | cd "$repo" |
| 17 | export GIT_CONFIG_KEY_2=safe.directory GIT_CONFIG_VALUE_2=. |
| 18 | case "$REQUEST_METHOD $PATH_INFO" in |
| 19 | "GET /info/refs"|"POST /git-upload-pack") |
| 20 | case "$REQUEST_METHOD:$QUERY_STRING" in |
| 21 | GET:service=git-upload-pack|GET:|POST:) ;; |
| 22 | *) printf "Status: 403 Forbidden\r\n\r\n"; exit 0 ;; |
| 23 | esac |
| 24 | export PATH_INFO="/$name.git$PATH_INFO" |
| 25 | exec /usr/lib/git-core/git-http-backend |
| 26 | ;; |
| 27 | "GET /archive/"*|"HEAD /archive/"*|"GET /refs/heads/"*|"HEAD /refs/heads/"*|"GET /commits/"*|"HEAD /commits/"*) |
| 28 | case "$PATH_INFO" in |
| 29 | *.zip) format=zip; mime=application/zip ;; |
| 30 | *.tar.gz) format=tar.gz; mime=application/gzip ;; |
| 31 | *) notfound ;; |
| 32 | esac |
| 33 | case "$PATH_INFO" in |
| 34 | /commits/*) |
| 35 | commit=${PATH_INFO#/commits/} |
| 36 | commit=${commit%."$format"} |
| 37 | case "$commit" in *[!0-9a-f]*) notfound ;; esac |
| 38 | [ "${#commit}" -eq 40 ] || notfound |
| 39 | [ "$(git cat-file -t "$commit" 2>/dev/null)" = commit ] || notfound |
| 40 | visible=$(git for-each-ref --count=1 --contains="$commit" --format='%(refname)' refs/heads refs/tags) || notfound |
| 41 | [ -n "$visible" ] || notfound |
| 42 | label=$commit |
| 43 | ;; |
| 44 | *) |
| 45 | branch=${PATH_INFO#/refs/heads/} |
| 46 | branch=${branch#/archive/} |
| 47 | branch=${branch%."$format"} |
| 48 | case "$branch" in ''|*[!a-zA-Z0-9._/-]*) notfound ;; esac |
| 49 | git check-ref-format "refs/heads/$branch" >/dev/null || notfound |
| 50 | commit=$(git --git-dir="$repo" rev-parse --verify "refs/heads/$branch^{commit}" 2>/dev/null) || notfound |
| 51 | label=$(printf '%s' "$branch" | tr / -) |
| 52 | ;; |
| 53 | esac |
| 54 | printf "Content-Type: %s\r\nContent-Disposition: attachment; filename=%s-%s.%s\r\nETag: \"%s\"\r\nCache-Control: no-cache\r\n\r\n" "$mime" "$name" "$label" "$format" "$commit" |
| 55 | [ "$REQUEST_METHOD" = HEAD ] || exec git --git-dir="$repo" archive --format="$format" --prefix="$name-$label/" "$commit" |
| 56 | ;; |
| 57 | *) printf "Status: 405 Method Not Allowed\r\n\r\n" ;; |
| 58 | esac |