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