| 1 | #!/bin/sh |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | if [ -t 2 ]; then |
| 6 | red="\e[1;31m" |
| 7 | green="\e[1;32m" |
| 8 | blue="\e[1;34m" |
| 9 | grey="\e[0m" |
| 10 | else |
| 11 | red="" |
| 12 | green="" |
| 13 | blue="" |
| 14 | grey="" |
| 15 | fi |
| 16 | |
| 17 | post_commit_hook="$(realpath -m "$0"/../git-hook-post-commit)" |
| 18 | fail='' |
| 19 | |
| 20 | # cf. man 5 githooks |
| 21 | # <local ref> SP <local object name> SP <remote ref> SP <remote object name> LF |
| 22 | while read local_ref local_object_name remote_ref remote_object_name; do |
| 23 | # never check commits already on origin |
| 24 | opts='^origin' |
| 25 | |
| 26 | # if we have the commit that the remote branch pointed to before, exclude it as well |
| 27 | opts="${opts} $(git rev-parse --not --quiet --verify "${remote_object_name}^{commit}" || true)" |
| 28 | |
| 29 | # check the object being pushed (if it's not null) |
| 30 | opts="${opts} $(git rev-parse --quiet --verify "${local_object_name}^{commit}" || true)" |
| 31 | |
| 32 | # compute the list of commits to check |
| 33 | commits="$(git rev-list ${opts})" |
| 34 | [ -z "${commits}" ] && continue |
| 35 | |
| 36 | printf "${blue}Performing pre-push checks for %s:${grey}\n" "${remote_ref}" >&2 |
| 37 | for commit in ${commits}; do |
| 38 | subject="$(git show --no-patch --format='%h %s' "${commit}" --)" |
| 39 | |
| 40 | if output="$("${post_commit_hook}" "${commit}")" && test -z "${output}"; then |
| 41 | printf " - ${green}%s${grey}\n" "${subject}" |
| 42 | else |
| 43 | printf " - ${red}%s${grey}${output:+:}\n" "${subject}" |
| 44 | printf "%s" "${output}" | sed 's/^/ /;$a\\' |
| 45 | fail=1 |
| 46 | fi |
| 47 | done |
| 48 | done |
| 49 | |
| 50 | exit "${fail:-0}" |