Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Shawn O. Pearce |
| 4 | # |
| 5 | |
| 6 | test_description='Test the update hook infrastructure.' |
| 7 | . ./test-lib.sh |
| 8 | |
| 9 | test_expect_success setup ' |
| 10 | echo This is a test. >a && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 11 | git update-index --add a && |
| 12 | tree0=$(git write-tree) && |
| 13 | commit0=$(echo setup | git commit-tree $tree0) && |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 14 | echo We hope it works. >a && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 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 && |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 20 | git clone --bare ./. victim.git && |
| 21 | GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 22 | git update-ref refs/heads/master $commit1 && |
| 23 | git update-ref refs/heads/tofail $commit0 |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 24 | ' |
| 25 | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 26 | cat >victim.git/hooks/pre-receive <<'EOF' |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 27 | #!/bin/sh |
Shawn O. Pearce | 733f181 | 2008-01-30 01:21:56 -0500 | [diff] [blame] | 28 | printf %s "$@" >>$GIT_DIR/pre-receive.args |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 29 | cat - >$GIT_DIR/pre-receive.stdin |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 30 | echo STDOUT pre-receive |
| 31 | echo STDERR pre-receive >&2 |
| 32 | EOF |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 33 | chmod u+x victim.git/hooks/pre-receive |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 34 | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 35 | cat >victim.git/hooks/update <<'EOF' |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 36 | #!/bin/sh |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 37 | echo "$@" >>$GIT_DIR/update.args |
Shawn O. Pearce | 733f181 | 2008-01-30 01:21:56 -0500 | [diff] [blame] | 38 | read x; printf %s "$x" >$GIT_DIR/update.stdin |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 39 | echo STDOUT update $1 |
| 40 | echo STDERR update $1 >&2 |
| 41 | test "$1" = refs/heads/master || exit |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 42 | EOF |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 43 | chmod u+x victim.git/hooks/update |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 44 | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 45 | cat >victim.git/hooks/post-receive <<'EOF' |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 46 | #!/bin/sh |
Shawn O. Pearce | 733f181 | 2008-01-30 01:21:56 -0500 | [diff] [blame] | 47 | printf %s "$@" >>$GIT_DIR/post-receive.args |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 48 | cat - >$GIT_DIR/post-receive.stdin |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 49 | echo STDOUT post-receive |
| 50 | echo STDERR post-receive >&2 |
| 51 | EOF |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 52 | chmod u+x victim.git/hooks/post-receive |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 53 | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 54 | cat >victim.git/hooks/post-update <<'EOF' |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 55 | #!/bin/sh |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 56 | echo "$@" >>$GIT_DIR/post-update.args |
Shawn O. Pearce | 733f181 | 2008-01-30 01:21:56 -0500 | [diff] [blame] | 57 | read x; printf %s "$x" >$GIT_DIR/post-update.stdin |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 58 | echo STDOUT post-update |
| 59 | echo STDERR post-update >&2 |
| 60 | EOF |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 61 | chmod u+x victim.git/hooks/post-update |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 62 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 63 | test_expect_success push ' |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 64 | test_must_fail git send-pack --force ./victim.git \ |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 65 | master tofail >send.out 2>send.err |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 66 | ' |
| 67 | |
| 68 | test_expect_success 'updated as expected' ' |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 69 | test $(GIT_DIR=victim.git git rev-parse master) = $commit1 && |
| 70 | test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1 |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 71 | ' |
| 72 | |
| 73 | test_expect_success 'hooks ran' ' |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 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 |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 82 | ' |
| 83 | |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 84 | test_expect_success 'pre-receive hook input' ' |
| 85 | (echo $commit0 $commit1 refs/heads/master; |
| 86 | echo $commit1 $commit0 refs/heads/tofail |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 87 | ) | test_cmp - victim.git/pre-receive.stdin |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 88 | ' |
| 89 | |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 90 | test_expect_success 'update hook arguments' ' |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 91 | (echo refs/heads/master $commit0 $commit1; |
| 92 | echo refs/heads/tofail $commit1 $commit0 |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 93 | ) | test_cmp - victim.git/update.args |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 94 | ' |
| 95 | |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 96 | test_expect_success 'post-receive hook input' ' |
| 97 | echo $commit0 $commit1 refs/heads/master | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 98 | test_cmp - victim.git/post-receive.stdin |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 99 | ' |
| 100 | |
| 101 | test_expect_success 'post-update hook arguments' ' |
| 102 | echo refs/heads/master | |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 103 | test_cmp - victim.git/post-update.args |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 104 | ' |
| 105 | |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 106 | test_expect_success 'all hook stdin is /dev/null' ' |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 107 | ! test -s victim.git/update.stdin && |
| 108 | ! test -s victim.git/post-update.stdin |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 109 | ' |
| 110 | |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 111 | test_expect_success 'all *-receive hook args are empty' ' |
Shawn O. Pearce | 6b3fa7e | 2010-02-09 18:01:29 -0800 | [diff] [blame] | 112 | ! test -s victim.git/pre-receive.args && |
| 113 | ! test -s victim.git/post-receive.args |
Shawn O. Pearce | f43cd49 | 2007-03-10 03:28:16 -0500 | [diff] [blame] | 114 | ' |
| 115 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 116 | test_expect_success 'send-pack produced no output' ' |
| 117 | ! test -s send.out |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 118 | ' |
| 119 | |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 120 | cat <<EOF >expect |
Shawn O. Pearce | 6d525d3 | 2010-02-05 12:57:42 -0800 | [diff] [blame] | 121 | remote: STDOUT pre-receive |
| 122 | remote: STDERR pre-receive |
| 123 | remote: STDOUT update refs/heads/master |
| 124 | remote: STDERR update refs/heads/master |
| 125 | remote: STDOUT update refs/heads/tofail |
| 126 | remote: STDERR update refs/heads/tofail |
Shawn O. Pearce | 466dbc4 | 2010-02-10 09:34:12 -0800 | [diff] [blame] | 127 | remote: error: hook declined to update refs/heads/tofail |
Shawn O. Pearce | 6d525d3 | 2010-02-05 12:57:42 -0800 | [diff] [blame] | 128 | remote: STDOUT post-receive |
| 129 | remote: STDERR post-receive |
| 130 | remote: STDOUT post-update |
| 131 | remote: STDERR post-update |
Shawn O. Pearce | 05ef58e | 2007-03-07 16:52:05 -0500 | [diff] [blame] | 132 | EOF |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 133 | test_expect_success 'send-pack stderr contains hook messages' ' |
Shawn O. Pearce | 6d525d3 | 2010-02-05 12:57:42 -0800 | [diff] [blame] | 134 | grep ^remote: send.err | sed "s/ *\$//" >actual && |
Shawn O. Pearce | 466dbc4 | 2010-02-10 09:34:12 -0800 | [diff] [blame] | 135 | test_cmp expect actual |
Shawn O. Pearce | 27086d0 | 2006-12-31 02:20:23 -0500 | [diff] [blame] | 136 | ' |
| 137 | |
| 138 | test_done |