Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Josh England |
| 4 | # |
| 5 | |
| 6 | test_description='Test the post-checkout hook.' |
| 7 | . ./test-lib.sh |
| 8 | |
| 9 | test_expect_success setup ' |
Matthieu Moy | 6e8f993 | 2009-12-30 15:45:31 +0100 | [diff] [blame] | 10 | echo Data for commit0. >a && |
| 11 | echo Data for commit0. >b && |
| 12 | git update-index --add a && |
| 13 | git update-index --add b && |
| 14 | tree0=$(git write-tree) && |
| 15 | commit0=$(echo setup | git commit-tree $tree0) && |
| 16 | git update-ref refs/heads/master $commit0 && |
| 17 | git clone ./. clone1 && |
| 18 | git clone ./. clone2 && |
| 19 | GIT_DIR=clone2/.git git branch new2 && |
| 20 | echo Data for commit1. >clone2/b && |
| 21 | GIT_DIR=clone2/.git git add clone2/b && |
| 22 | GIT_DIR=clone2/.git git commit -m new2 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 23 | ' |
| 24 | |
| 25 | for clone in 1 2; do |
| 26 | cat >clone${clone}/.git/hooks/post-checkout <<'EOF' |
| 27 | #!/bin/sh |
| 28 | echo $@ > $GIT_DIR/post-checkout.args |
| 29 | EOF |
| 30 | chmod u+x clone${clone}/.git/hooks/post-checkout |
| 31 | done |
| 32 | |
| 33 | test_expect_success 'post-checkout runs as expected ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 34 | GIT_DIR=clone1/.git git checkout master && |
| 35 | test -e clone1/.git/post-checkout.args |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 36 | ' |
| 37 | |
| 38 | test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 39 | old=$(awk "{print \$1}" clone1/.git/post-checkout.args) && |
| 40 | new=$(awk "{print \$2}" clone1/.git/post-checkout.args) && |
| 41 | flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 42 | test $old = $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 43 | ' |
| 44 | |
| 45 | test_expect_success 'post-checkout runs as expected ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 46 | GIT_DIR=clone1/.git git checkout master && |
| 47 | test -e clone1/.git/post-checkout.args |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 48 | ' |
| 49 | |
| 50 | test_expect_success 'post-checkout args are correct with git checkout -b ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 51 | GIT_DIR=clone1/.git git checkout -b new1 && |
| 52 | old=$(awk "{print \$1}" clone1/.git/post-checkout.args) && |
| 53 | new=$(awk "{print \$2}" clone1/.git/post-checkout.args) && |
| 54 | flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 55 | test $old = $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 56 | ' |
| 57 | |
| 58 | test_expect_success 'post-checkout receives the right args with HEAD changed ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 59 | GIT_DIR=clone2/.git git checkout new2 && |
| 60 | old=$(awk "{print \$1}" clone2/.git/post-checkout.args) && |
| 61 | new=$(awk "{print \$2}" clone2/.git/post-checkout.args) && |
| 62 | flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 63 | test $old != $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 64 | ' |
| 65 | |
| 66 | test_expect_success 'post-checkout receives the right args when not switching branches ' ' |
Nguyễn Thái Ngọc Duy | a604572 | 2011-10-12 20:35:04 +1100 | [diff] [blame] | 67 | GIT_DIR=clone2/.git git checkout master b && |
| 68 | old=$(awk "{print \$1}" clone2/.git/post-checkout.args) && |
| 69 | new=$(awk "{print \$2}" clone2/.git/post-checkout.args) && |
| 70 | flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 71 | test $old = $new && test $flag = 0 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 72 | ' |
| 73 | |
Alex Riesen | d42ec12 | 2009-03-17 17:22:53 +0100 | [diff] [blame] | 74 | if test "$(git config --bool core.filemode)" = true; then |
Jeff King | dfa7a6c | 2009-03-03 00:37:51 -0500 | [diff] [blame] | 75 | mkdir -p templates/hooks |
| 76 | cat >templates/hooks/post-checkout <<'EOF' |
| 77 | #!/bin/sh |
| 78 | echo $@ > $GIT_DIR/post-checkout.args |
| 79 | EOF |
| 80 | chmod +x templates/hooks/post-checkout |
| 81 | |
| 82 | test_expect_success 'post-checkout hook is triggered by clone' ' |
| 83 | git clone --template=templates . clone3 && |
| 84 | test -f clone3/.git/post-checkout.args |
| 85 | ' |
Alex Riesen | d42ec12 | 2009-03-17 17:22:53 +0100 | [diff] [blame] | 86 | fi |
Jeff King | dfa7a6c | 2009-03-03 00:37:51 -0500 | [diff] [blame] | 87 | |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 88 | test_done |