]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5571-pre-push-hook.sh
hook tests: test for exact "pre-push" hook input
[thirdparty/git.git] / t / t5571-pre-push-hook.sh
CommitLineData
ec55559f
AS
1#!/bin/sh
2
3test_description='check pre-push hooks'
028cb644 4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
ec55559f
AS
7. ./test-lib.sh
8
9# Setup hook that always succeeds
10HOOKDIR="$(git rev-parse --git-dir)/hooks"
11HOOK="$HOOKDIR/pre-push"
12mkdir -p "$HOOKDIR"
13write_script "$HOOK" <<EOF
bbd837f0 14cat >actual
ec55559f
AS
15exit 0
16EOF
17
18test_expect_success 'setup' '
19 git config push.default upstream &&
20 git init --bare repo1 &&
21 git remote add parent1 repo1 &&
22 test_commit one &&
bbd837f0
ÆAB
23 cat >expect <<-EOF &&
24 HEAD $(git rev-parse HEAD) refs/heads/foreign $(test_oid zero)
25 EOF
26
27 test_when_finished "rm actual" &&
28 git push parent1 HEAD:foreign &&
29 test_cmp expect actual
ec55559f
AS
30'
31write_script "$HOOK" <<EOF
bbd837f0 32cat >actual
ec55559f
AS
33exit 1
34EOF
35
36COMMIT1="$(git rev-parse HEAD)"
37export COMMIT1
38
39test_expect_success 'push with failing hook' '
40 test_commit two &&
bbd837f0
ÆAB
41 cat >expect <<-EOF &&
42 HEAD $(git rev-parse HEAD) refs/heads/main $(test_oid zero)
43 EOF
44
45 test_when_finished "rm actual" &&
46 test_must_fail git push parent1 HEAD &&
47 test_cmp expect actual
ec55559f
AS
48'
49
50test_expect_success '--no-verify bypasses hook' '
bbd837f0
ÆAB
51 git push --no-verify parent1 HEAD &&
52 test_path_is_missing actual
ec55559f
AS
53'
54
55COMMIT2="$(git rev-parse HEAD)"
56export COMMIT2
57
58write_script "$HOOK" <<'EOF'
59echo "$1" >actual
60echo "$2" >>actual
61cat >>actual
62EOF
63
64cat >expected <<EOF
65parent1
66repo1
028cb644 67refs/heads/main $COMMIT2 refs/heads/foreign $COMMIT1
ec55559f
AS
68EOF
69
70test_expect_success 'push with hook' '
028cb644 71 git push parent1 main:foreign &&
ec55559f
AS
72 diff expected actual
73'
74
75test_expect_success 'add a branch' '
76 git checkout -b other parent1/foreign &&
77 test_commit three
78'
79
80COMMIT3="$(git rev-parse HEAD)"
81export COMMIT3
82
83cat >expected <<EOF
84parent1
85repo1
86refs/heads/other $COMMIT3 refs/heads/foreign $COMMIT2
87EOF
88
89test_expect_success 'push to default' '
90 git push &&
91 diff expected actual
92'
93
94cat >expected <<EOF
95parent1
96repo1
8125a58b 97refs/tags/one $COMMIT1 refs/tags/tag1 $ZERO_OID
98HEAD~ $COMMIT2 refs/heads/prev $ZERO_OID
ec55559f
AS
99EOF
100
101test_expect_success 'push non-branches' '
102 git push parent1 one:tag1 HEAD~:refs/heads/prev &&
103 diff expected actual
104'
105
106cat >expected <<EOF
107parent1
108repo1
8125a58b 109(delete) $ZERO_OID refs/heads/prev $COMMIT2
ec55559f
AS
110EOF
111
112test_expect_success 'push delete' '
113 git push parent1 :prev &&
114 diff expected actual
115'
116
117cat >expected <<EOF
118repo1
119repo1
8125a58b 120HEAD $COMMIT3 refs/heads/other $ZERO_OID
ec55559f
AS
121EOF
122
123test_expect_success 'push to URL' '
124 git push repo1 HEAD &&
125 diff expected actual
126'
127
af65f68c
CB
128test_expect_success 'set up many-ref tests' '
129 {
7abcbcb7 130 nr=1000 &&
af65f68c
CB
131 while test $nr -lt 2000
132 do
74d2f569 133 nr=$(( $nr + 1 )) &&
d0fd9931 134 echo "create refs/heads/b/$nr $COMMIT3" || return 1
af65f68c
CB
135 done
136 } | git update-ref --stdin
137'
138
139test_expect_success 'sigpipe does not cause pre-push hook failure' '
140 echo "exit 0" | write_script "$HOOK" &&
141 git push parent1 "refs/heads/b/*:refs/heads/b/*"
142'
ec55559f
AS
143
144test_done