| Giorgi Lekveishvili | 285ab62 | 2023-11-22 13:50:45 +0400 | [diff] [blame] | 1 | #!/bin/ash |
| 2 | |
| 3 | delete() { |
| 4 | rm -rf /var/gerrit/git/${REPO}.git |
| 5 | |
| 6 | if ! test -f /var/gerrit/git/${REPO}.git; then |
| 7 | STATUS_CODE="204 No Content" |
| 8 | MESSAGE="Repository ${REPO} deleted." |
| 9 | else |
| 10 | MESSAGE="Repository ${REPO} could not be deleted." |
| 11 | fi |
| 12 | } |
| 13 | |
| 14 | new() { |
| 15 | if test -d /var/gerrit/git/${REPO}.git; then |
| 16 | STATUS_CODE="200 OK" |
| 17 | MESSAGE="Repository already available." |
| 18 | else |
| 19 | git init --bare /var/gerrit/git/${REPO}.git > /dev/null |
| 20 | if test -f /var/gerrit/git/${REPO}.git/HEAD; then |
| 21 | STATUS_CODE="201 Created" |
| 22 | MESSAGE="Repository ${REPO} created." |
| 23 | else |
| 24 | MESSAGE="Repository ${REPO} could not be created." |
| 25 | fi |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | update_head(){ |
| 30 | read -n ${CONTENT_LENGTH} POST_STRING |
| 31 | NEW_HEAD=$(echo ${POST_STRING} | jq .ref - | tr -d '"') |
| 32 | |
| 33 | git --git-dir /var/gerrit/git/${REPO}.git symbolic-ref HEAD ${NEW_HEAD} |
| 34 | |
| 35 | if test "ref: ${NEW_HEAD}" == "$(cat /var/gerrit/git/${REPO}.git/HEAD)"; then |
| 36 | STATUS_CODE="200 OK" |
| 37 | MESSAGE="Repository HEAD updated to ${NEW_HEAD}." |
| 38 | else |
| 39 | MESSAGE="Repository HEAD could not be updated to ${NEW_HEAD}." |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | echo "Content-type: text/html" |
| 44 | REPO=${REQUEST_URI##/a/projects/} |
| 45 | REPO="${REPO//%2F//}" |
| 46 | REPO="${REPO%%.git}" |
| 47 | |
| 48 | if test "${REQUEST_METHOD}" == "PUT"; then |
| 49 | if [[ ${REQUEST_URI} == */HEAD ]]; then |
| 50 | REPO=${REPO%"/HEAD"} |
| 51 | update_head |
| 52 | else |
| 53 | new |
| 54 | fi |
| 55 | elif test "${REQUEST_METHOD}" == "DELETE"; then |
| 56 | delete |
| 57 | else |
| 58 | STATUS_CODE="400 Bad Request" |
| 59 | MESSAGE="Unknown method." |
| 60 | fi |
| 61 | |
| 62 | test -z ${STATUS_CODE} && STATUS_CODE="500 Internal Server Error" |
| 63 | |
| 64 | echo "Status: ${STATUS_CODE}" |
| 65 | echo "" |
| 66 | echo "${MESSAGE}" |