1#!/bin/sh
2
3test_description='check pre-push hooks'
4. ./test-lib.sh
5
6# Setup hook that always succeeds
7HOOKDIR="$(git rev-parse --git-dir)/hooks"
8HOOK="$HOOKDIR/pre-push"
9mkdir -p "$HOOKDIR"
10write_script "$HOOK" <<EOF
11cat >/dev/null
12exit 0
13EOF
14
15test_expect_success 'setup' '
16 git config push.default upstream &&
17 git init --bare repo1 &&
18 git remote add parent1 repo1 &&
19 test_commit one &&
20 git push parent1 HEAD:foreign
21'
22write_script "$HOOK" <<EOF
23cat >/dev/null
24exit 1
25EOF
26
27COMMIT1="$(git rev-parse HEAD)"
28export COMMIT1
29
30test_expect_success 'push with failing hook' '
31 test_commit two &&
32 test_must_fail git push parent1 HEAD
33'
34
35test_expect_success '--no-verify bypasses hook' '
36 git push --no-verify parent1 HEAD
37'
38
39COMMIT2="$(git rev-parse HEAD)"
40export COMMIT2
41
42write_script "$HOOK" <<'EOF'
43echo "$1" >actual
44echo "$2" >>actual
45cat >>actual
46EOF
47
48cat >expected <<EOF
49parent1
50repo1
51refs/heads/master $COMMIT2 refs/heads/foreign $COMMIT1
52EOF
53
54test_expect_success 'push with hook' '
55 git push parent1 master:foreign &&
56 diff expected actual
57'
58
59test_expect_success 'add a branch' '
60 git checkout -b other parent1/foreign &&
61 test_commit three
62'
63
64COMMIT3="$(git rev-parse HEAD)"
65export COMMIT3
66
67cat >expected <<EOF
68parent1
69repo1
70refs/heads/other $COMMIT3 refs/heads/foreign $COMMIT2
71EOF
72
73test_expect_success 'push to default' '
74 git push &&
75 diff expected actual
76'
77
78cat >expected <<EOF
79parent1
80repo1
81refs/tags/one $COMMIT1 refs/tags/tag1 $_z40
82HEAD~ $COMMIT2 refs/heads/prev $_z40
83EOF
84
85test_expect_success 'push non-branches' '
86 git push parent1 one:tag1 HEAD~:refs/heads/prev &&
87 diff expected actual
88'
89
90cat >expected <<EOF
91parent1
92repo1
93(delete) $_z40 refs/heads/prev $COMMIT2
94EOF
95
96test_expect_success 'push delete' '
97 git push parent1 :prev &&
98 diff expected actual
99'
100
101cat >expected <<EOF
102repo1
103repo1
104HEAD $COMMIT3 refs/heads/other $_z40
105EOF
106
107test_expect_success 'push to URL' '
108 git push repo1 HEAD &&
109 diff expected actual
110'
111
112test_expect_success 'set up many-ref tests' '
113 {
114 nr=1000
115 while test $nr -lt 2000
116 do
117 nr=$(( $nr + 1 ))
118 echo "create refs/heads/b/$nr $COMMIT3"
119 done
120 } | git update-ref --stdin
121'
122
123test_expect_success 'sigpipe does not cause pre-push hook failure' '
124 echo "exit 0" | write_script "$HOOK" &&
125 git push parent1 "refs/heads/b/*:refs/heads/b/*"
126'
127
128test_done