Git 2.45
[git/gitster.git] / t / t5401-update-hooks.sh
blobd8cadeec73310d36ad11a1427ec44cc9fc2d3a66
1 #!/bin/sh
3 # Copyright (c) 2006 Shawn O. Pearce
6 test_description='Test the update hook infrastructure.'
7 . ./test-lib.sh
9 test_expect_success setup '
10 echo This is a test. >a &&
11 git update-index --add a &&
12 tree0=$(git write-tree) &&
13 commit0=$(echo setup | git commit-tree $tree0) &&
14 echo We hope it works. >a &&
15 git update-index a &&
16 tree1=$(git write-tree) &&
17 commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
18 git update-ref refs/heads/main $commit0 &&
19 git update-ref refs/heads/tofail $commit1 &&
20 git clone --bare ./. victim.git &&
21 GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
22 git update-ref refs/heads/main $commit1 &&
23 git update-ref refs/heads/tofail $commit0 &&
25 test_hook --setup -C victim.git pre-receive <<-\EOF &&
26 printf %s "$@" >>$GIT_DIR/pre-receive.args
27 cat - >$GIT_DIR/pre-receive.stdin
28 echo STDOUT pre-receive
29 echo STDERR pre-receive >&2
30 EOF
32 test_hook --setup -C victim.git update <<-\EOF &&
33 echo "$@" >>$GIT_DIR/update.args
34 read x; printf %s "$x" >$GIT_DIR/update.stdin
35 echo STDOUT update $1
36 echo STDERR update $1 >&2
37 test "$1" = refs/heads/main || exit
38 EOF
40 test_hook --setup -C victim.git post-receive <<-\EOF &&
41 printf %s "$@" >>$GIT_DIR/post-receive.args
42 cat - >$GIT_DIR/post-receive.stdin
43 echo STDOUT post-receive
44 echo STDERR post-receive >&2
45 EOF
47 test_hook --setup -C victim.git post-update <<-\EOF
48 echo "$@" >>$GIT_DIR/post-update.args
49 read x; printf %s "$x" >$GIT_DIR/post-update.stdin
50 echo STDOUT post-update
51 echo STDERR post-update >&2
52 EOF
55 test_expect_success push '
56 test_must_fail git send-pack --force ./victim.git \
57 main tofail >send.out 2>send.err
60 test_expect_success 'updated as expected' '
61 test $(GIT_DIR=victim.git git rev-parse main) = $commit1 &&
62 test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
65 test_expect_success 'hooks ran' '
66 test -f victim.git/pre-receive.args &&
67 test -f victim.git/pre-receive.stdin &&
68 test -f victim.git/update.args &&
69 test -f victim.git/update.stdin &&
70 test -f victim.git/post-receive.args &&
71 test -f victim.git/post-receive.stdin &&
72 test -f victim.git/post-update.args &&
73 test -f victim.git/post-update.stdin
76 test_expect_success 'pre-receive hook input' '
77 (echo $commit0 $commit1 refs/heads/main &&
78 echo $commit1 $commit0 refs/heads/tofail
79 ) | test_cmp - victim.git/pre-receive.stdin
82 test_expect_success 'update hook arguments' '
83 (echo refs/heads/main $commit0 $commit1 &&
84 echo refs/heads/tofail $commit1 $commit0
85 ) | test_cmp - victim.git/update.args
88 test_expect_success 'post-receive hook input' '
89 echo $commit0 $commit1 refs/heads/main |
90 test_cmp - victim.git/post-receive.stdin
93 test_expect_success 'post-update hook arguments' '
94 echo refs/heads/main |
95 test_cmp - victim.git/post-update.args
98 test_expect_success 'all hook stdin is /dev/null' '
99 test_must_be_empty victim.git/update.stdin &&
100 test_must_be_empty victim.git/post-update.stdin
103 test_expect_success 'all *-receive hook args are empty' '
104 test_must_be_empty victim.git/pre-receive.args &&
105 test_must_be_empty victim.git/post-receive.args
108 test_expect_success 'send-pack produced no output' '
109 test_must_be_empty send.out
112 cat <<EOF >expect
113 remote: STDOUT pre-receive
114 remote: STDERR pre-receive
115 remote: STDOUT update refs/heads/main
116 remote: STDERR update refs/heads/main
117 remote: STDOUT update refs/heads/tofail
118 remote: STDERR update refs/heads/tofail
119 remote: error: hook declined to update refs/heads/tofail
120 remote: STDOUT post-receive
121 remote: STDERR post-receive
122 remote: STDOUT post-update
123 remote: STDERR post-update
125 test_expect_success 'send-pack stderr contains hook messages' '
126 sed -n "/^remote:/s/ *\$//p" send.err >actual &&
127 test_cmp expect actual
130 test_expect_success 'pre-receive hook that forgets to read its input' '
131 test_hook --clobber -C victim.git pre-receive <<-\EOF &&
132 exit 0
134 rm -f victim.git/hooks/update victim.git/hooks/post-update &&
136 printf "create refs/heads/branch_%d main\n" $(test_seq 100 999) >input &&
137 git update-ref --stdin <input &&
138 git push ./victim.git "+refs/heads/*:refs/heads/*"
141 test_done