Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='pre-commit hook' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 7 | test_expect_success 'with no hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 8 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 9 | echo "foo" > file && |
| 10 | git add file && |
| 11 | git commit -m "first" |
| 12 | |
| 13 | ' |
| 14 | |
| 15 | test_expect_success '--no-verify with no hook' ' |
| 16 | |
| 17 | echo "bar" > file && |
| 18 | git add file && |
| 19 | git commit --no-verify -m "bar" |
| 20 | |
| 21 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 22 | |
| 23 | # now install hook that always succeeds |
| 24 | HOOKDIR="$(git rev-parse --git-dir)/hooks" |
| 25 | HOOK="$HOOKDIR/pre-commit" |
| 26 | mkdir -p "$HOOKDIR" |
| 27 | cat > "$HOOK" <<EOF |
| 28 | #!/bin/sh |
| 29 | exit 0 |
| 30 | EOF |
| 31 | chmod +x "$HOOK" |
| 32 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 33 | test_expect_success 'with succeeding hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 34 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 35 | echo "more" >> file && |
| 36 | git add file && |
| 37 | git commit -m "more" |
| 38 | |
| 39 | ' |
| 40 | |
| 41 | test_expect_success '--no-verify with succeeding hook' ' |
| 42 | |
| 43 | echo "even more" >> file && |
| 44 | git add file && |
| 45 | git commit --no-verify -m "even more" |
| 46 | |
| 47 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 48 | |
| 49 | # now a hook that fails |
| 50 | cat > "$HOOK" <<EOF |
| 51 | #!/bin/sh |
| 52 | exit 1 |
| 53 | EOF |
| 54 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 55 | test_expect_success 'with failing hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 56 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 57 | echo "another" >> file && |
| 58 | git add file && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 59 | test_must_fail git commit -m "another" |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 60 | |
| 61 | ' |
| 62 | |
| 63 | test_expect_success '--no-verify with failing hook' ' |
| 64 | |
| 65 | echo "stuff" >> file && |
| 66 | git add file && |
| 67 | git commit --no-verify -m "stuff" |
| 68 | |
| 69 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 70 | |
| 71 | chmod -x "$HOOK" |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 72 | test_expect_success POSIXPERM 'with non-executable hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 73 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 74 | echo "content" >> file && |
| 75 | git add file && |
| 76 | git commit -m "content" |
| 77 | |
| 78 | ' |
| 79 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 80 | test_expect_success POSIXPERM '--no-verify with non-executable hook' ' |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 81 | |
| 82 | echo "more content" >> file && |
| 83 | git add file && |
| 84 | git commit --no-verify -m "more content" |
| 85 | |
| 86 | ' |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 87 | chmod +x "$HOOK" |
| 88 | |
| 89 | # a hook that checks $GIT_PREFIX and succeeds inside the |
| 90 | # success/ subdirectory only |
| 91 | cat > "$HOOK" <<EOF |
| 92 | #!/bin/sh |
| 93 | test \$GIT_PREFIX = success/ |
| 94 | EOF |
| 95 | |
| 96 | test_expect_success 'with hook requiring GIT_PREFIX' ' |
| 97 | |
| 98 | echo "more content" >> file && |
| 99 | git add file && |
| 100 | mkdir success && |
| 101 | ( |
| 102 | cd success && |
| 103 | git commit -m "hook requires GIT_PREFIX = success/" |
| 104 | ) && |
| 105 | rmdir success |
| 106 | ' |
| 107 | |
| 108 | test_expect_success 'with failing hook requiring GIT_PREFIX' ' |
| 109 | |
| 110 | echo "more content" >> file && |
| 111 | git add file && |
| 112 | mkdir fail && |
| 113 | ( |
| 114 | cd fail && |
| 115 | test_must_fail git commit -m "hook must fail" |
| 116 | ) && |
| 117 | rmdir fail && |
| 118 | git checkout -- file |
| 119 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 120 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 121 | test_expect_success 'check the author in hook' ' |
Junio C Hamano | 0486198 | 2012-03-11 03:51:32 -0700 | [diff] [blame] | 122 | write_script "$HOOK" <<-\EOF && |
| 123 | test "$GIT_AUTHOR_NAME" = "New Author" && |
| 124 | test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com" |
| 125 | EOF |
| 126 | test_must_fail git commit --allow-empty -m "by a.u.thor" && |
| 127 | ( |
| 128 | GIT_AUTHOR_NAME="New Author" && |
| 129 | GIT_AUTHOR_EMAIL="newauthor@example.com" && |
| 130 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL && |
| 131 | git commit --allow-empty -m "by new.author via env" && |
| 132 | git show -s |
| 133 | ) && |
| 134 | git commit --author="New Author <newauthor@example.com>" \ |
| 135 | --allow-empty -m "by new.author via command line" && |
| 136 | git show -s |
| 137 | ' |
| 138 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 139 | test_done |