]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5401-update-hooks.sh
t/t{3600,3800,5401}: do not use egrep when grep would do
[thirdparty/git.git] / t / t5401-update-hooks.sh
CommitLineData
27086d0f
SP
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 &&
5be60078
JH
11 git update-index --add a &&
12 tree0=$(git write-tree) &&
13 commit0=$(echo setup | git commit-tree $tree0) &&
27086d0f 14 echo We hope it works. >a &&
5be60078
JH
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 &&
05ef58ec 20 git-clone ./. victim &&
5be60078
JH
21 GIT_DIR=victim/.git git update-ref refs/heads/tofail $commit1 &&
22 git update-ref refs/heads/master $commit1 &&
23 git update-ref refs/heads/tofail $commit0
27086d0f
SP
24'
25
05ef58ec
SP
26cat >victim/.git/hooks/pre-receive <<'EOF'
27#!/bin/sh
f43cd49f
SP
28printf "$@" >>$GIT_DIR/pre-receive.args
29cat - >$GIT_DIR/pre-receive.stdin
05ef58ec
SP
30echo STDOUT pre-receive
31echo STDERR pre-receive >&2
32EOF
33chmod u+x victim/.git/hooks/pre-receive
34
27086d0f
SP
35cat >victim/.git/hooks/update <<'EOF'
36#!/bin/sh
05ef58ec 37echo "$@" >>$GIT_DIR/update.args
2aad957a 38read x; printf "$x" >$GIT_DIR/update.stdin
05ef58ec
SP
39echo STDOUT update $1
40echo STDERR update $1 >&2
41test "$1" = refs/heads/master || exit
27086d0f
SP
42EOF
43chmod u+x victim/.git/hooks/update
44
05ef58ec
SP
45cat >victim/.git/hooks/post-receive <<'EOF'
46#!/bin/sh
f43cd49f
SP
47printf "$@" >>$GIT_DIR/post-receive.args
48cat - >$GIT_DIR/post-receive.stdin
05ef58ec
SP
49echo STDOUT post-receive
50echo STDERR post-receive >&2
51EOF
52chmod u+x victim/.git/hooks/post-receive
53
27086d0f
SP
54cat >victim/.git/hooks/post-update <<'EOF'
55#!/bin/sh
05ef58ec 56echo "$@" >>$GIT_DIR/post-update.args
2aad957a 57read x; printf "$x" >$GIT_DIR/post-update.stdin
27086d0f
SP
58echo STDOUT post-update
59echo STDERR post-update >&2
60EOF
61chmod u+x victim/.git/hooks/post-update
62
05ef58ec
SP
63test_expect_failure push '
64 git-send-pack --force ./victim/.git master tofail >send.out 2>send.err
65'
66
67test_expect_success 'updated as expected' '
5be60078
JH
68 test $(GIT_DIR=victim/.git git rev-parse master) = $commit1 &&
69 test $(GIT_DIR=victim/.git git rev-parse tofail) = $commit1
27086d0f
SP
70'
71
72test_expect_success 'hooks ran' '
05ef58ec
SP
73 test -f victim/.git/pre-receive.args &&
74 test -f victim/.git/pre-receive.stdin &&
27086d0f
SP
75 test -f victim/.git/update.args &&
76 test -f victim/.git/update.stdin &&
05ef58ec
SP
77 test -f victim/.git/post-receive.args &&
78 test -f victim/.git/post-receive.stdin &&
27086d0f
SP
79 test -f victim/.git/post-update.args &&
80 test -f victim/.git/post-update.stdin
81'
82
f43cd49f
SP
83test_expect_success 'pre-receive hook input' '
84 (echo $commit0 $commit1 refs/heads/master;
85 echo $commit1 $commit0 refs/heads/tofail
86 ) | git diff - victim/.git/pre-receive.stdin
05ef58ec
SP
87'
88
27086d0f 89test_expect_success 'update hook arguments' '
05ef58ec
SP
90 (echo refs/heads/master $commit0 $commit1;
91 echo refs/heads/tofail $commit1 $commit0
cf6981d4 92 ) | git diff - victim/.git/update.args
05ef58ec
SP
93'
94
f43cd49f
SP
95test_expect_success 'post-receive hook input' '
96 echo $commit0 $commit1 refs/heads/master |
97 git diff - victim/.git/post-receive.stdin
27086d0f
SP
98'
99
100test_expect_success 'post-update hook arguments' '
101 echo refs/heads/master |
5bd74506 102 git diff - victim/.git/post-update.args
27086d0f
SP
103'
104
05ef58ec 105test_expect_success 'all hook stdin is /dev/null' '
05ef58ec 106 ! test -s victim/.git/update.stdin &&
05ef58ec 107 ! test -s victim/.git/post-update.stdin
27086d0f
SP
108'
109
f43cd49f
SP
110test_expect_success 'all *-receive hook args are empty' '
111 ! test -s victim/.git/pre-receive.args &&
112 ! test -s victim/.git/post-receive.args
113'
114
27086d0f
SP
115test_expect_failure 'send-pack produced no output' '
116 test -s send.out
117'
118
05ef58ec
SP
119cat <<EOF >expect
120STDOUT pre-receive
121STDERR pre-receive
122STDOUT update refs/heads/master
123STDERR update refs/heads/master
124STDOUT update refs/heads/tofail
125STDERR update refs/heads/tofail
126STDOUT post-receive
127STDERR post-receive
128STDOUT post-update
129STDERR post-update
130EOF
27086d0f 131test_expect_success 'send-pack stderr contains hook messages' '
0feb4d1c 132 grep ^STD send.err >actual &&
cf6981d4 133 git diff - actual <expect
27086d0f
SP
134'
135
136test_done