blob: 17bcb0b04096eabb29c13a025fd2afe8d1e4623b [file] [log] [blame]
Shawn O. Pearce27086d02006-12-31 02:20:23 -05001#!/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 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070011 git update-index --add a &&
12 tree0=$(git write-tree) &&
13 commit0=$(echo setup | git commit-tree $tree0) &&
Shawn O. Pearce27086d02006-12-31 02:20:23 -050014 echo We hope it works. >a &&
Junio C Hamano5be60072007-07-02 22:52:14 -070015 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 &&
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080020 git clone --bare ./. victim.git &&
21 GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070022 git update-ref refs/heads/master $commit1 &&
23 git update-ref refs/heads/tofail $commit0
Shawn O. Pearce27086d02006-12-31 02:20:23 -050024'
25
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080026cat >victim.git/hooks/pre-receive <<'EOF'
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050027#!/bin/sh
Shawn O. Pearce733f1812008-01-30 01:21:56 -050028printf %s "$@" >>$GIT_DIR/pre-receive.args
Shawn O. Pearcef43cd492007-03-10 03:28:16 -050029cat - >$GIT_DIR/pre-receive.stdin
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050030echo STDOUT pre-receive
31echo STDERR pre-receive >&2
32EOF
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080033chmod u+x victim.git/hooks/pre-receive
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050034
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080035cat >victim.git/hooks/update <<'EOF'
Shawn O. Pearce27086d02006-12-31 02:20:23 -050036#!/bin/sh
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050037echo "$@" >>$GIT_DIR/update.args
Shawn O. Pearce733f1812008-01-30 01:21:56 -050038read x; printf %s "$x" >$GIT_DIR/update.stdin
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050039echo STDOUT update $1
40echo STDERR update $1 >&2
41test "$1" = refs/heads/master || exit
Shawn O. Pearce27086d02006-12-31 02:20:23 -050042EOF
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080043chmod u+x victim.git/hooks/update
Shawn O. Pearce27086d02006-12-31 02:20:23 -050044
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080045cat >victim.git/hooks/post-receive <<'EOF'
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050046#!/bin/sh
Shawn O. Pearce733f1812008-01-30 01:21:56 -050047printf %s "$@" >>$GIT_DIR/post-receive.args
Shawn O. Pearcef43cd492007-03-10 03:28:16 -050048cat - >$GIT_DIR/post-receive.stdin
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050049echo STDOUT post-receive
50echo STDERR post-receive >&2
51EOF
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080052chmod u+x victim.git/hooks/post-receive
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050053
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080054cat >victim.git/hooks/post-update <<'EOF'
Shawn O. Pearce27086d02006-12-31 02:20:23 -050055#!/bin/sh
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050056echo "$@" >>$GIT_DIR/post-update.args
Shawn O. Pearce733f1812008-01-30 01:21:56 -050057read x; printf %s "$x" >$GIT_DIR/post-update.stdin
Shawn O. Pearce27086d02006-12-31 02:20:23 -050058echo STDOUT post-update
59echo STDERR post-update >&2
60EOF
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080061chmod u+x victim.git/hooks/post-update
Shawn O. Pearce27086d02006-12-31 02:20:23 -050062
Junio C Hamano41ac4142008-02-01 01:50:53 -080063test_expect_success push '
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080064 test_must_fail git send-pack --force ./victim.git \
Stephan Beyerd492b312008-07-12 17:47:52 +020065 master tofail >send.out 2>send.err
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050066'
67
68test_expect_success 'updated as expected' '
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080069 test $(GIT_DIR=victim.git git rev-parse master) = $commit1 &&
70 test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
Shawn O. Pearce27086d02006-12-31 02:20:23 -050071'
72
73test_expect_success 'hooks ran' '
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080074 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
Shawn O. Pearce27086d02006-12-31 02:20:23 -050082'
83
Shawn O. Pearcef43cd492007-03-10 03:28:16 -050084test_expect_success 'pre-receive hook input' '
85 (echo $commit0 $commit1 refs/heads/master;
86 echo $commit1 $commit0 refs/heads/tofail
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080087 ) | test_cmp - victim.git/pre-receive.stdin
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050088'
89
Shawn O. Pearce27086d02006-12-31 02:20:23 -050090test_expect_success 'update hook arguments' '
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050091 (echo refs/heads/master $commit0 $commit1;
92 echo refs/heads/tofail $commit1 $commit0
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080093 ) | test_cmp - victim.git/update.args
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -050094'
95
Shawn O. Pearcef43cd492007-03-10 03:28:16 -050096test_expect_success 'post-receive hook input' '
97 echo $commit0 $commit1 refs/heads/master |
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -080098 test_cmp - victim.git/post-receive.stdin
Shawn O. Pearce27086d02006-12-31 02:20:23 -050099'
100
101test_expect_success 'post-update hook arguments' '
102 echo refs/heads/master |
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -0800103 test_cmp - victim.git/post-update.args
Shawn O. Pearce27086d02006-12-31 02:20:23 -0500104'
105
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -0500106test_expect_success 'all hook stdin is /dev/null' '
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -0800107 ! test -s victim.git/update.stdin &&
108 ! test -s victim.git/post-update.stdin
Shawn O. Pearce27086d02006-12-31 02:20:23 -0500109'
110
Shawn O. Pearcef43cd492007-03-10 03:28:16 -0500111test_expect_success 'all *-receive hook args are empty' '
Shawn O. Pearce6b3fa7e2010-02-09 18:01:29 -0800112 ! test -s victim.git/pre-receive.args &&
113 ! test -s victim.git/post-receive.args
Shawn O. Pearcef43cd492007-03-10 03:28:16 -0500114'
115
Junio C Hamano41ac4142008-02-01 01:50:53 -0800116test_expect_success 'send-pack produced no output' '
117 ! test -s send.out
Shawn O. Pearce27086d02006-12-31 02:20:23 -0500118'
119
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -0500120cat <<EOF >expect
Shawn O. Pearce6d525d32010-02-05 12:57:42 -0800121remote: 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
Shawn O. Pearce466dbc42010-02-10 09:34:12 -0800127remote: error: hook declined to update refs/heads/tofail
Shawn O. Pearce6d525d32010-02-05 12:57:42 -0800128remote: STDOUT post-receive
129remote: STDERR post-receive
130remote: STDOUT post-update
131remote: STDERR post-update
Shawn O. Pearce05ef58e2007-03-07 16:52:05 -0500132EOF
Shawn O. Pearce27086d02006-12-31 02:20:23 -0500133test_expect_success 'send-pack stderr contains hook messages' '
Shawn O. Pearce6d525d32010-02-05 12:57:42 -0800134 grep ^remote: send.err | sed "s/ *\$//" >actual &&
Shawn O. Pearce466dbc42010-02-10 09:34:12 -0800135 test_cmp expect actual
Shawn O. Pearce27086d02006-12-31 02:20:23 -0500136'
137
138test_done