File: git-hook-pre-push

package info (click to toggle)
cockpit 354-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308,956 kB
  • sloc: javascript: 775,606; python: 40,351; ansic: 35,655; cpp: 11,117; sh: 3,511; makefile: 580; xml: 261
file content (50 lines) | stat: -rwxr-xr-x 1,476 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh

set -eu

if [ -t 2 ]; then
    red="\e[1;31m"
    green="\e[1;32m"
    blue="\e[1;34m"
    grey="\e[0m"
else
    red=""
    green=""
    blue=""
    grey=""
fi

post_commit_hook="$(realpath -m "$0"/../git-hook-post-commit)"
fail=''

# cf. man 5 githooks
# <local ref> SP <local object name> SP <remote ref> SP <remote object name> LF
while read local_ref local_object_name remote_ref remote_object_name; do
    # never check commits already on origin
    opts='^origin'

    # if we have the commit that the remote branch pointed to before, exclude it as well
    opts="${opts} $(git rev-parse --not --quiet --verify "${remote_object_name}^{commit}" || true)"

    # check the object being pushed (if it's not null)
    opts="${opts} $(git rev-parse --quiet --verify "${local_object_name}^{commit}" || true)"

    # compute the list of commits to check
    commits="$(git rev-list ${opts})"
    [ -z "${commits}" ] && continue

    printf "${blue}Performing pre-push checks for %s:${grey}\n" "${remote_ref}" >&2
    for commit in ${commits}; do
        subject="$(git show --no-patch --format='%h %s' "${commit}" --)"

        if output="$("${post_commit_hook}" "${commit}")" && test -z "${output}"; then
            printf " - ${green}%s${grey}\n" "${subject}"
        else
            printf " - ${red}%s${grey}${output:+:}\n" "${subject}"
            printf "%s" "${output}" | sed 's/^/     /;$a\\'
            fail=1
        fi
    done
done

exit "${fail:-0}"