]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5401-update-hooks.sh
Merge branch 'wb/fsmonitor-bitmap-fix'
[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 &&
6b3fa7e7
SP
20 git clone --bare ./. victim.git &&
21 GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
5be60078
JH
22 git update-ref refs/heads/master $commit1 &&
23 git update-ref refs/heads/tofail $commit0
27086d0f
SP
24'
25
6b3fa7e7 26cat >victim.git/hooks/pre-receive <<'EOF'
05ef58ec 27#!/bin/sh
733f1815 28printf %s "$@" >>$GIT_DIR/pre-receive.args
f43cd49f 29cat - >$GIT_DIR/pre-receive.stdin
05ef58ec
SP
30echo STDOUT pre-receive
31echo STDERR pre-receive >&2
32EOF
6b3fa7e7 33chmod u+x victim.git/hooks/pre-receive
05ef58ec 34
6b3fa7e7 35cat >victim.git/hooks/update <<'EOF'
27086d0f 36#!/bin/sh
05ef58ec 37echo "$@" >>$GIT_DIR/update.args
733f1815 38read x; printf %s "$x" >$GIT_DIR/update.stdin
05ef58ec
SP
39echo STDOUT update $1
40echo STDERR update $1 >&2
41test "$1" = refs/heads/master || exit
27086d0f 42EOF
6b3fa7e7 43chmod u+x victim.git/hooks/update
27086d0f 44
6b3fa7e7 45cat >victim.git/hooks/post-receive <<'EOF'
05ef58ec 46#!/bin/sh
733f1815 47printf %s "$@" >>$GIT_DIR/post-receive.args
f43cd49f 48cat - >$GIT_DIR/post-receive.stdin
05ef58ec
SP
49echo STDOUT post-receive
50echo STDERR post-receive >&2
51EOF
6b3fa7e7 52chmod u+x victim.git/hooks/post-receive
05ef58ec 53
6b3fa7e7 54cat >victim.git/hooks/post-update <<'EOF'
27086d0f 55#!/bin/sh
05ef58ec 56echo "$@" >>$GIT_DIR/post-update.args
733f1815 57read x; printf %s "$x" >$GIT_DIR/post-update.stdin
27086d0f
SP
58echo STDOUT post-update
59echo STDERR post-update >&2
60EOF
6b3fa7e7 61chmod u+x victim.git/hooks/post-update
27086d0f 62
41ac414e 63test_expect_success push '
6b3fa7e7 64 test_must_fail git send-pack --force ./victim.git \
d492b31c 65 master tofail >send.out 2>send.err
05ef58ec
SP
66'
67
68test_expect_success 'updated as expected' '
6b3fa7e7
SP
69 test $(GIT_DIR=victim.git git rev-parse master) = $commit1 &&
70 test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
27086d0f
SP
71'
72
73test_expect_success 'hooks ran' '
6b3fa7e7
SP
74 test -f victim.git/pre-receive.args &&
75 test -f victim.git/pre-receive.stdin &&
76 test -f victim.git/update.args &&
77 test -f victim.git/update.stdin &&
78 test -f victim.git/post-receive.args &&
79 test -f victim.git/post-receive.stdin &&
80 test -f victim.git/post-update.args &&
81 test -f victim.git/post-update.stdin
27086d0f
SP
82'
83
f43cd49f 84test_expect_success 'pre-receive hook input' '
51b85471 85 (echo $commit0 $commit1 refs/heads/master &&
f43cd49f 86 echo $commit1 $commit0 refs/heads/tofail
6b3fa7e7 87 ) | test_cmp - victim.git/pre-receive.stdin
05ef58ec
SP
88'
89
27086d0f 90test_expect_success 'update hook arguments' '
51b85471 91 (echo refs/heads/master $commit0 $commit1 &&
05ef58ec 92 echo refs/heads/tofail $commit1 $commit0
6b3fa7e7 93 ) | test_cmp - victim.git/update.args
05ef58ec
SP
94'
95
f43cd49f
SP
96test_expect_success 'post-receive hook input' '
97 echo $commit0 $commit1 refs/heads/master |
6b3fa7e7 98 test_cmp - victim.git/post-receive.stdin
27086d0f
SP
99'
100
101test_expect_success 'post-update hook arguments' '
102 echo refs/heads/master |
6b3fa7e7 103 test_cmp - victim.git/post-update.args
27086d0f
SP
104'
105
05ef58ec 106test_expect_success 'all hook stdin is /dev/null' '
ec10b018
SG
107 test_must_be_empty victim.git/update.stdin &&
108 test_must_be_empty victim.git/post-update.stdin
27086d0f
SP
109'
110
f43cd49f 111test_expect_success 'all *-receive hook args are empty' '
ec10b018
SG
112 test_must_be_empty victim.git/pre-receive.args &&
113 test_must_be_empty victim.git/post-receive.args
f43cd49f
SP
114'
115
41ac414e 116test_expect_success 'send-pack produced no output' '
ec10b018 117 test_must_be_empty send.out
27086d0f
SP
118'
119
05ef58ec 120cat <<EOF >expect
6d525d38
SP
121remote: STDOUT pre-receive
122remote: STDERR pre-receive
123remote: STDOUT update refs/heads/master
124remote: STDERR update refs/heads/master
125remote: STDOUT update refs/heads/tofail
126remote: STDERR update refs/heads/tofail
466dbc42 127remote: error: hook declined to update refs/heads/tofail
6d525d38
SP
128remote: STDOUT post-receive
129remote: STDERR post-receive
130remote: STDOUT post-update
131remote: STDERR post-update
05ef58ec 132EOF
27086d0f 133test_expect_success 'send-pack stderr contains hook messages' '
6d525d38 134 grep ^remote: send.err | sed "s/ *\$//" >actual &&
466dbc42 135 test_cmp expect actual
27086d0f
SP
136'
137
ec7dbd14
JH
138test_expect_success 'pre-receive hook that forgets to read its input' '
139 write_script victim.git/hooks/pre-receive <<-\EOF &&
140 exit 0
141 EOF
142 rm -f victim.git/hooks/update victim.git/hooks/post-update &&
143
144 for v in $(test_seq 100 999)
145 do
146 git branch branch_$v master || return
147 done &&
148 git push ./victim.git "+refs/heads/*:refs/heads/*"
149'
150
27086d0f 151test_done