summaryrefslogtreecommitdiff
path: root/buildscripts/install-hooks
blob: 5389bc615e195bb2d3d19f228c48892b3b7fd17d (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash

# Create a pre-push hook driver in this repo. The hook will run all
# excutable scripts in ~/.githooks/<repo name>/pre-push

# Find out the repo name and where the .git directory is for this repo
origin="`git config remote.origin.url`"
repo="`basename -s .git $origin`"
tld="`git rev-parse --show-toplevel`"

# Location for the hooks.
# WARNING: if you change this you'll need to change the value of the
# "hooksdir" variable in the heredoc below as well
hooksdir="$HOME/.githooks/$repo/pre-push"

usage() {
    echo "Usage: `basename $0` [-f|-h]"
    echo
    echo "Install a pre-push hook in $tld/.git/hooks"
    echo "  -f force overwriting existing hooks"
    echo "  -h print this help"
    echo
    exit 0
}

# --- Command line options ---
force=0
while getopts fh opt_arg; do
    case $opt_arg in
        f) force=1 ;;
        *) usage   ;;
    esac
done
shift `expr $OPTIND - 1`
# ----------------------------

set -e

# If there's already a pre-push hook installed bail out, we don't want to
# overwrite it (unless -f is passed in the command line)
pre_push_hook="$tld/.git/hooks/pre-push"

if [ -e "$pre_push_hook" -a $force -eq 0 ]; then
    echo "ERROR: found an existing pre-push hook: $pre_push_hook"
    exit 1
fi

echo -n "Installing pre-push hook in $pre_push_hook: "

cat > $pre_push_hook <<'EOF'
#!/bin/bash

# set GITHOOKS_QUIET to anything to anything to make this script quiet
quiet=${GITHOOKS_QUIET:-""}

origin="`git config remote.origin.url`"
repo="`basename -s .git $origin`"

hooksdir="$HOME/.githooks/$repo/pre-push"
if [ ! -d "$hooksdir" ]; then
    echo
    echo "WARNING:"
    echo "  hooks directory doesn't exist: $hooksdir"
    echo "  pushing anyway"
    echo
    sleep 1
    exit 0
fi

hooksrun=0
allhooks="`/bin/ls -1 $hooksdir 2>/dev/null`"

for hook in `echo $allhooks` ; do
    if [ -x "$hooksdir/$hook" ]; then
        [ -z "$quiet" ] && echo -e "\n=== running hook: $hooksdir/$hook"
        $hooksdir/$hook "$@"
        rc=$?
        hooksrun=$((hooksrun + 1))
        if [ $rc -ne 0 ]; then
            echo
            echo "ERROR: hook $hooksdir/$hook returned non-zero status: $rc"
            echo
            exit $rc
        else
            [ -z "$quiet" ] && echo "=== done."
        fi
    fi
done

if [ $hooksrun -eq 0 ]; then
    echo
    echo "WARNING:"
    echo "  couldn't find any pre-push hooks to run in $hooksdir"
    echo "  pushing anyway"
    sleep 1
fi

echo
exit 0
EOF

chmod +x $pre_push_hook

echo "done."

mkdir -p "$hooksdir"
echo "Now add your favorite pre-push scripts to: $hooksdir"