3 # Copyright (c) 2006 Shawn O. Pearce
6 test_description
='Test the update hook infrastructure.'
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success setup
'
12 echo This is a test. >a &&
13 git update-index --add a &&
14 tree0=$(git write-tree) &&
15 commit0=$(echo setup | git commit-tree $tree0) &&
16 echo We hope it works. >a &&
18 tree1=$(git write-tree) &&
19 commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
20 git update-ref refs/heads/main $commit0 &&
21 git update-ref refs/heads/tofail $commit1 &&
22 git clone --bare ./. victim.git &&
23 GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
24 git update-ref refs/heads/main $commit1 &&
25 git update-ref refs/heads/tofail $commit0 &&
27 test_hook --setup -C victim.git pre-receive <<-\EOF &&
28 printf %s "$@" >>$GIT_DIR/pre-receive.args
29 cat - >$GIT_DIR/pre-receive.stdin
30 echo STDOUT pre-receive
31 echo STDERR pre-receive >&2
34 test_hook --setup -C victim.git update <<-\EOF &&
35 echo "$@" >>$GIT_DIR/update.args
36 read x; printf %s "$x" >$GIT_DIR/update.stdin
38 echo STDERR update $1 >&2
39 test "$1" = refs/heads/main || exit
42 test_hook --setup -C victim.git post-receive <<-\EOF &&
43 printf %s "$@" >>$GIT_DIR/post-receive.args
44 cat - >$GIT_DIR/post-receive.stdin
45 echo STDOUT post-receive
46 echo STDERR post-receive >&2
49 test_hook --setup -C victim.git post-update <<-\EOF
50 echo "$@" >>$GIT_DIR/post-update.args
51 read x; printf %s "$x" >$GIT_DIR/post-update.stdin
52 echo STDOUT post-update
53 echo STDERR post-update >&2
57 test_expect_success push
'
58 test_must_fail git send-pack --force ./victim.git \
59 main tofail >send.out 2>send.err
62 test_expect_success
'updated as expected' '
63 test $(GIT_DIR=victim.git git rev-parse main) = $commit1 &&
64 test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
67 test_expect_success
'hooks ran' '
68 test -f victim.git/pre-receive.args &&
69 test -f victim.git/pre-receive.stdin &&
70 test -f victim.git/update.args &&
71 test -f victim.git/update.stdin &&
72 test -f victim.git/post-receive.args &&
73 test -f victim.git/post-receive.stdin &&
74 test -f victim.git/post-update.args &&
75 test -f victim.git/post-update.stdin
78 test_expect_success
'pre-receive hook input' '
79 (echo $commit0 $commit1 refs/heads/main &&
80 echo $commit1 $commit0 refs/heads/tofail
81 ) | test_cmp - victim.git/pre-receive.stdin
84 test_expect_success
'update hook arguments' '
85 (echo refs/heads/main $commit0 $commit1 &&
86 echo refs/heads/tofail $commit1 $commit0
87 ) | test_cmp - victim.git/update.args
90 test_expect_success
'post-receive hook input' '
91 echo $commit0 $commit1 refs/heads/main |
92 test_cmp - victim.git/post-receive.stdin
95 test_expect_success
'post-update hook arguments' '
96 echo refs/heads/main |
97 test_cmp - victim.git/post-update.args
100 test_expect_success
'all hook stdin is /dev/null' '
101 test_must_be_empty victim.git/update.stdin &&
102 test_must_be_empty victim.git/post-update.stdin
105 test_expect_success
'all *-receive hook args are empty' '
106 test_must_be_empty victim.git/pre-receive.args &&
107 test_must_be_empty victim.git/post-receive.args
110 test_expect_success
'send-pack produced no output' '
111 test_must_be_empty send.out
115 remote: STDOUT pre-receive
116 remote: STDERR pre-receive
117 remote: STDOUT update refs/heads/main
118 remote: STDERR update refs/heads/main
119 remote: STDOUT update refs/heads/tofail
120 remote: STDERR update refs/heads/tofail
121 remote: error: hook declined to update refs/heads/tofail
122 remote: STDOUT post-receive
123 remote: STDERR post-receive
124 remote: STDOUT post-update
125 remote: STDERR post-update
127 test_expect_success
'send-pack stderr contains hook messages' '
128 sed -n "/^remote:/s/ *\$//p" send.err >actual &&
129 test_cmp expect actual
132 test_expect_success
'pre-receive hook that forgets to read its input' '
133 test_hook --clobber -C victim.git pre-receive <<-\EOF &&
136 rm -f victim.git/hooks/update victim.git/hooks/post-update &&
138 printf "create refs/heads/branch_%d main\n" $(test_seq 100 999) >input &&
139 git update-ref --stdin <input &&
140 git push ./victim.git "+refs/heads/*:refs/heads/*"