Sync with maint
[git] / t / t5571-pre-push-hook.sh
1 #!/bin/sh
2
3 test_description='check pre-push hooks'
4 . ./test-lib.sh
5
6 # Setup hook that always succeeds
7 HOOKDIR="$(git rev-parse --git-dir)/hooks"
8 HOOK="$HOOKDIR/pre-push"
9 mkdir -p "$HOOKDIR"
10 write_script "$HOOK" <<EOF
11 cat >/dev/null
12 exit 0
13 EOF
14
15 test_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 '
22 write_script "$HOOK" <<EOF
23 cat >/dev/null
24 exit 1
25 EOF
26
27 COMMIT1="$(git rev-parse HEAD)"
28 export COMMIT1
29
30 test_expect_success 'push with failing hook' '
31         test_commit two &&
32         test_must_fail git push parent1 HEAD
33 '
34
35 test_expect_success '--no-verify bypasses hook' '
36         git push --no-verify parent1 HEAD
37 '
38
39 COMMIT2="$(git rev-parse HEAD)"
40 export COMMIT2
41
42 write_script "$HOOK" <<'EOF'
43 echo "$1" >actual
44 echo "$2" >>actual
45 cat >>actual
46 EOF
47
48 cat >expected <<EOF
49 parent1
50 repo1
51 refs/heads/master $COMMIT2 refs/heads/foreign $COMMIT1
52 EOF
53
54 test_expect_success 'push with hook' '
55         git push parent1 master:foreign &&
56         diff expected actual
57 '
58
59 test_expect_success 'add a branch' '
60         git checkout -b other parent1/foreign &&
61         test_commit three
62 '
63
64 COMMIT3="$(git rev-parse HEAD)"
65 export COMMIT3
66
67 cat >expected <<EOF
68 parent1
69 repo1
70 refs/heads/other $COMMIT3 refs/heads/foreign $COMMIT2
71 EOF
72
73 test_expect_success 'push to default' '
74         git push &&
75         diff expected actual
76 '
77
78 cat >expected <<EOF
79 parent1
80 repo1
81 refs/tags/one $COMMIT1 refs/tags/tag1 $_z40
82 HEAD~ $COMMIT2 refs/heads/prev $_z40
83 EOF
84
85 test_expect_success 'push non-branches' '
86         git push parent1 one:tag1 HEAD~:refs/heads/prev &&
87         diff expected actual
88 '
89
90 cat >expected <<EOF
91 parent1
92 repo1
93 (delete) $_z40 refs/heads/prev $COMMIT2
94 EOF
95
96 test_expect_success 'push delete' '
97         git push parent1 :prev &&
98         diff expected actual
99 '
100
101 cat >expected <<EOF
102 repo1
103 repo1
104 HEAD $COMMIT3 refs/heads/other $_z40
105 EOF
106
107 test_expect_success 'push to URL' '
108         git push repo1 HEAD &&
109         diff expected actual
110 '
111
112 # Test that filling pipe buffers doesn't cause failure
113 # Too slow to leave enabled for general use
114 if false
115 then
116         printf 'parent1\nrepo1\n' >expected
117         nr=1000
118         while test $nr -lt 2000
119         do
120                 nr=$(( $nr + 1 ))
121                 git branch b/$nr $COMMIT3
122                 echo "refs/heads/b/$nr $COMMIT3 refs/heads/b/$nr $_z40" >>expected
123         done
124
125         test_expect_success 'push many refs' '
126                 git push parent1 "refs/heads/b/*:refs/heads/b/*" &&
127                 diff expected actual
128         '
129 fi
130
131 test_done