Git fork
1#!/bin/sh
2#
3# Copyright (c) 2006 Shawn O. Pearce
4#
5
6test_description='Test the update hook infrastructure.'
7
8. ./test-lib.sh
9
10test_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
56test_expect_success push '
57 test_must_fail git send-pack --force ./victim.git \
58 main tofail >send.out 2>send.err
59'
60
61test_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
66test_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
77test_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
83test_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
89test_expect_success 'post-receive hook input' '
90 echo $commit0 $commit1 refs/heads/main |
91 test_cmp - victim.git/post-receive.stdin
92'
93
94test_expect_success 'post-update hook arguments' '
95 echo refs/heads/main |
96 test_cmp - victim.git/post-update.args
97'
98
99test_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
104test_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
109test_expect_success 'send-pack produced no output' '
110 test_must_be_empty send.out
111'
112
113cat <<EOF >expect
114remote: STDOUT pre-receive
115remote: STDERR pre-receive
116remote: STDOUT update refs/heads/main
117remote: STDERR update refs/heads/main
118remote: STDOUT update refs/heads/tofail
119remote: STDERR update refs/heads/tofail
120remote: error: hook declined to update refs/heads/tofail
121remote: STDOUT post-receive
122remote: STDERR post-receive
123remote: STDOUT post-update
124remote: STDERR post-update
125EOF
126test_expect_success 'send-pack stderr contains hook messages' '
127 sed -n "/^remote:/s/ *\$//p" send.err >actual &&
128 test_cmp expect actual
129'
130
131test_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 printf "create refs/heads/branch_%d main\n" $(test_seq 100 999) >input &&
138 git update-ref --stdin <input &&
139 git push ./victim.git "+refs/heads/*:refs/heads/*"
140'
141
142test_done