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