1#!/bin/sh
2#
3# Copyright (c) 2006 Shawn O. Pearce
4#
5
6test_description='Test the update hook infrastructure.'
7. ./test-lib.sh
8
9test_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/master $commit0 &&
19 git update-ref refs/heads/tofail $commit1 &&
20 git clone ./. victim &&
21 GIT_DIR=victim/.git git config receive.denyCurrentBranch warn &&
22 GIT_DIR=victim/.git git update-ref refs/heads/tofail $commit1 &&
23 git update-ref refs/heads/master $commit1 &&
24 git update-ref refs/heads/tofail $commit0
25'
26
27cat >victim/.git/hooks/pre-receive <<'EOF'
28#!/bin/sh
29printf %s "$@" >>$GIT_DIR/pre-receive.args
30cat - >$GIT_DIR/pre-receive.stdin
31echo STDOUT pre-receive
32echo STDERR pre-receive >&2
33EOF
34chmod u+x victim/.git/hooks/pre-receive
35
36cat >victim/.git/hooks/update <<'EOF'
37#!/bin/sh
38echo "$@" >>$GIT_DIR/update.args
39read x; printf %s "$x" >$GIT_DIR/update.stdin
40echo STDOUT update $1
41echo STDERR update $1 >&2
42test "$1" = refs/heads/master || exit
43EOF
44chmod u+x victim/.git/hooks/update
45
46cat >victim/.git/hooks/post-receive <<'EOF'
47#!/bin/sh
48printf %s "$@" >>$GIT_DIR/post-receive.args
49cat - >$GIT_DIR/post-receive.stdin
50echo STDOUT post-receive
51echo STDERR post-receive >&2
52EOF
53chmod u+x victim/.git/hooks/post-receive
54
55cat >victim/.git/hooks/post-update <<'EOF'
56#!/bin/sh
57echo "$@" >>$GIT_DIR/post-update.args
58read x; printf %s "$x" >$GIT_DIR/post-update.stdin
59echo STDOUT post-update
60echo STDERR post-update >&2
61EOF
62chmod u+x victim/.git/hooks/post-update
63
64test_expect_success push '
65 test_must_fail git send-pack --force ./victim/.git \
66 master tofail >send.out 2>send.err
67'
68
69test_expect_success 'updated as expected' '
70 test $(GIT_DIR=victim/.git git rev-parse master) = $commit1 &&
71 test $(GIT_DIR=victim/.git git rev-parse tofail) = $commit1
72'
73
74test_expect_success 'hooks ran' '
75 test -f victim/.git/pre-receive.args &&
76 test -f victim/.git/pre-receive.stdin &&
77 test -f victim/.git/update.args &&
78 test -f victim/.git/update.stdin &&
79 test -f victim/.git/post-receive.args &&
80 test -f victim/.git/post-receive.stdin &&
81 test -f victim/.git/post-update.args &&
82 test -f victim/.git/post-update.stdin
83'
84
85test_expect_success 'pre-receive hook input' '
86 (echo $commit0 $commit1 refs/heads/master;
87 echo $commit1 $commit0 refs/heads/tofail
88 ) | test_cmp - victim/.git/pre-receive.stdin
89'
90
91test_expect_success 'update hook arguments' '
92 (echo refs/heads/master $commit0 $commit1;
93 echo refs/heads/tofail $commit1 $commit0
94 ) | test_cmp - victim/.git/update.args
95'
96
97test_expect_success 'post-receive hook input' '
98 echo $commit0 $commit1 refs/heads/master |
99 test_cmp - victim/.git/post-receive.stdin
100'
101
102test_expect_success 'post-update hook arguments' '
103 echo refs/heads/master |
104 test_cmp - victim/.git/post-update.args
105'
106
107test_expect_success 'all hook stdin is /dev/null' '
108 ! test -s victim/.git/update.stdin &&
109 ! test -s victim/.git/post-update.stdin
110'
111
112test_expect_success 'all *-receive hook args are empty' '
113 ! test -s victim/.git/pre-receive.args &&
114 ! test -s victim/.git/post-receive.args
115'
116
117test_expect_success 'send-pack produced no output' '
118 ! test -s send.out
119'
120
121cat <<EOF >expect
122STDOUT pre-receive
123STDERR pre-receive
124STDOUT update refs/heads/master
125STDERR update refs/heads/master
126STDOUT update refs/heads/tofail
127STDERR update refs/heads/tofail
128STDOUT post-receive
129STDERR post-receive
130STDOUT post-update
131STDERR post-update
132EOF
133test_expect_success 'send-pack stderr contains hook messages' '
134 grep ^STD send.err >actual &&
135 test_cmp - actual <expect
136'
137
138test_done