Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 3 | test_description='pre-commit and pre-merge-commit hooks' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 4 | |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 8 | . ./test-lib.sh |
| 9 | |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 10 | HOOKDIR="$(git rev-parse --git-dir)/hooks" |
| 11 | PRECOMMIT="$HOOKDIR/pre-commit" |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 12 | PREMERGE="$HOOKDIR/pre-merge-commit" |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 13 | |
| 14 | # Prepare sample scripts that write their $0 to actual_hooks |
| 15 | test_expect_success 'sample script setup' ' |
| 16 | mkdir -p "$HOOKDIR" && |
| 17 | write_script "$HOOKDIR/success.sample" <<-\EOF && |
| 18 | echo $0 >>actual_hooks |
| 19 | exit 0 |
| 20 | EOF |
| 21 | write_script "$HOOKDIR/fail.sample" <<-\EOF && |
| 22 | echo $0 >>actual_hooks |
| 23 | exit 1 |
| 24 | EOF |
| 25 | write_script "$HOOKDIR/non-exec.sample" <<-\EOF && |
| 26 | echo $0 >>actual_hooks |
| 27 | exit 1 |
| 28 | EOF |
| 29 | chmod -x "$HOOKDIR/non-exec.sample" && |
| 30 | write_script "$HOOKDIR/require-prefix.sample" <<-\EOF && |
| 31 | echo $0 >>actual_hooks |
| 32 | test $GIT_PREFIX = "success/" |
| 33 | EOF |
| 34 | write_script "$HOOKDIR/check-author.sample" <<-\EOF |
| 35 | echo $0 >>actual_hooks |
| 36 | test "$GIT_AUTHOR_NAME" = "New Author" && |
| 37 | test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com" |
| 38 | EOF |
| 39 | ' |
| 40 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 41 | test_expect_success 'root commit' ' |
| 42 | echo "root" >file && |
| 43 | git add file && |
| 44 | git commit -m "zeroth" && |
| 45 | git checkout -b side && |
| 46 | echo "foo" >foo && |
| 47 | git add foo && |
| 48 | git commit -m "make it non-ff" && |
| 49 | git branch side-orig side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 50 | git checkout main |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 51 | ' |
| 52 | |
| 53 | test_expect_success 'setup conflicting branches' ' |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 54 | test_when_finished "git checkout main" && |
| 55 | git checkout -b conflicting-a main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 56 | echo a >conflicting && |
| 57 | git add conflicting && |
| 58 | git commit -m conflicting-a && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 59 | git checkout -b conflicting-b main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 60 | echo b >conflicting && |
| 61 | git add conflicting && |
| 62 | git commit -m conflicting-b |
| 63 | ' |
| 64 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 65 | test_expect_success 'with no hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 66 | test_when_finished "rm -f actual_hooks" && |
| 67 | echo "foo" >file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 68 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 69 | git commit -m "first" && |
| 70 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 71 | ' |
| 72 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 73 | test_expect_success 'with no hook (merge)' ' |
| 74 | test_when_finished "rm -f actual_hooks" && |
| 75 | git branch -f side side-orig && |
| 76 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 77 | git merge -m "merge main" main && |
| 78 | git checkout main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 79 | test_path_is_missing actual_hooks |
| 80 | ' |
| 81 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 82 | test_expect_success '--no-verify with no hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 83 | test_when_finished "rm -f actual_hooks" && |
| 84 | echo "bar" >file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 85 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 86 | git commit --no-verify -m "bar" && |
| 87 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 88 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 89 | |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 90 | test_expect_success '--no-verify with no hook (merge)' ' |
| 91 | test_when_finished "rm -f actual_hooks" && |
| 92 | git branch -f side side-orig && |
| 93 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 94 | git merge --no-verify -m "merge main" main && |
| 95 | git checkout main && |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 96 | test_path_is_missing actual_hooks |
| 97 | ' |
| 98 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 99 | test_expect_success 'with succeeding hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 100 | test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && |
| 101 | cp "$HOOKDIR/success.sample" "$PRECOMMIT" && |
| 102 | echo "$PRECOMMIT" >expected_hooks && |
| 103 | echo "more" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 104 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 105 | git commit -m "more" && |
| 106 | test_cmp expected_hooks actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 107 | ' |
| 108 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 109 | test_expect_success 'with succeeding hook (merge)' ' |
| 110 | test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && |
| 111 | cp "$HOOKDIR/success.sample" "$PREMERGE" && |
| 112 | echo "$PREMERGE" >expected_hooks && |
| 113 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 114 | git merge -m "merge main" main && |
| 115 | git checkout main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 116 | test_cmp expected_hooks actual_hooks |
| 117 | ' |
| 118 | |
| 119 | test_expect_success 'automatic merge fails; both hooks are available' ' |
| 120 | test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" && |
| 121 | test_when_finished "rm -f expected_hooks actual_hooks" && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 122 | test_when_finished "git checkout main" && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 123 | cp "$HOOKDIR/success.sample" "$PREMERGE" && |
| 124 | cp "$HOOKDIR/success.sample" "$PRECOMMIT" && |
| 125 | |
| 126 | git checkout conflicting-a && |
| 127 | test_must_fail git merge -m "merge conflicting-b" conflicting-b && |
| 128 | test_path_is_missing actual_hooks && |
| 129 | |
| 130 | echo "$PRECOMMIT" >expected_hooks && |
| 131 | echo a+b >conflicting && |
| 132 | git add conflicting && |
| 133 | git commit -m "resolve conflict" && |
| 134 | test_cmp expected_hooks actual_hooks |
| 135 | ' |
| 136 | |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 137 | test_expect_success '--no-verify with succeeding hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 138 | test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && |
| 139 | cp "$HOOKDIR/success.sample" "$PRECOMMIT" && |
| 140 | echo "even more" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 141 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 142 | git commit --no-verify -m "even more" && |
| 143 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 144 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 145 | |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 146 | test_expect_success '--no-verify with succeeding hook (merge)' ' |
| 147 | test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && |
| 148 | cp "$HOOKDIR/success.sample" "$PREMERGE" && |
| 149 | git branch -f side side-orig && |
| 150 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 151 | git merge --no-verify -m "merge main" main && |
| 152 | git checkout main && |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 153 | test_path_is_missing actual_hooks |
| 154 | ' |
| 155 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 156 | test_expect_success 'with failing hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 157 | test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && |
| 158 | cp "$HOOKDIR/fail.sample" "$PRECOMMIT" && |
| 159 | echo "$PRECOMMIT" >expected_hooks && |
| 160 | echo "another" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 161 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 162 | test_must_fail git commit -m "another" && |
| 163 | test_cmp expected_hooks actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 164 | ' |
| 165 | |
| 166 | test_expect_success '--no-verify with failing hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 167 | test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && |
| 168 | cp "$HOOKDIR/fail.sample" "$PRECOMMIT" && |
| 169 | echo "stuff" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 170 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 171 | git commit --no-verify -m "stuff" && |
| 172 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 173 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 174 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 175 | test_expect_success 'with failing hook (merge)' ' |
| 176 | test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && |
| 177 | cp "$HOOKDIR/fail.sample" "$PREMERGE" && |
| 178 | echo "$PREMERGE" >expected_hooks && |
| 179 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 180 | test_must_fail git merge -m "merge main" main && |
| 181 | git checkout main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 182 | test_cmp expected_hooks actual_hooks |
| 183 | ' |
| 184 | |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 185 | test_expect_success '--no-verify with failing hook (merge)' ' |
| 186 | test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && |
| 187 | cp "$HOOKDIR/fail.sample" "$PREMERGE" && |
| 188 | git branch -f side side-orig && |
| 189 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 190 | git merge --no-verify -m "merge main" main && |
| 191 | git checkout main && |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 192 | test_path_is_missing actual_hooks |
| 193 | ' |
| 194 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 195 | test_expect_success POSIXPERM 'with non-executable hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 196 | test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && |
| 197 | cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" && |
| 198 | echo "content" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 199 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 200 | git commit -m "content" && |
| 201 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 202 | ' |
| 203 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 204 | test_expect_success POSIXPERM '--no-verify with non-executable hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 205 | test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && |
| 206 | cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" && |
| 207 | echo "more content" >>file && |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 208 | git add file && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 209 | git commit --no-verify -m "more content" && |
| 210 | test_path_is_missing actual_hooks |
Wincent Colaiuta | cf7e147 | 2007-12-10 08:42:45 +0100 | [diff] [blame] | 211 | ' |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 212 | |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 213 | test_expect_success POSIXPERM 'with non-executable hook (merge)' ' |
| 214 | test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && |
| 215 | cp "$HOOKDIR/non-exec.sample" "$PREMERGE" && |
| 216 | git branch -f side side-orig && |
| 217 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 218 | git merge -m "merge main" main && |
| 219 | git checkout main && |
Michael J Gruber | 6098817 | 2019-08-07 11:57:07 -0700 | [diff] [blame] | 220 | test_path_is_missing actual_hooks |
| 221 | ' |
| 222 | |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 223 | test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' ' |
| 224 | test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && |
| 225 | cp "$HOOKDIR/non-exec.sample" "$PREMERGE" && |
| 226 | git branch -f side side-orig && |
| 227 | git checkout side && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 228 | git merge --no-verify -m "merge main" main && |
| 229 | git checkout main && |
Michael J Gruber | bc40ce4 | 2019-08-07 11:57:08 -0700 | [diff] [blame] | 230 | test_path_is_missing actual_hooks |
| 231 | ' |
| 232 | |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 233 | test_expect_success 'with hook requiring GIT_PREFIX' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 234 | test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" && |
| 235 | cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" && |
| 236 | echo "$PRECOMMIT" >expected_hooks && |
| 237 | echo "more content" >>file && |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 238 | git add file && |
| 239 | mkdir success && |
| 240 | ( |
| 241 | cd success && |
| 242 | git commit -m "hook requires GIT_PREFIX = success/" |
| 243 | ) && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 244 | test_cmp expected_hooks actual_hooks |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 245 | ' |
| 246 | |
| 247 | test_expect_success 'with failing hook requiring GIT_PREFIX' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 248 | test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" && |
| 249 | cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" && |
| 250 | echo "$PRECOMMIT" >expected_hooks && |
| 251 | echo "more content" >>file && |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 252 | git add file && |
| 253 | mkdir fail && |
| 254 | ( |
| 255 | cd fail && |
| 256 | test_must_fail git commit -m "hook must fail" |
| 257 | ) && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 258 | git checkout -- file && |
| 259 | test_cmp expected_hooks actual_hooks |
David Aguilar | c35ec8c | 2011-06-02 02:26:25 -0700 | [diff] [blame] | 260 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 261 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 262 | test_expect_success 'check the author in hook' ' |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 263 | test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && |
| 264 | cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" && |
| 265 | cat >expected_hooks <<-EOF && |
| 266 | $PRECOMMIT |
| 267 | $PRECOMMIT |
| 268 | $PRECOMMIT |
Junio C Hamano | 0486198 | 2012-03-11 03:51:32 -0700 | [diff] [blame] | 269 | EOF |
| 270 | test_must_fail git commit --allow-empty -m "by a.u.thor" && |
| 271 | ( |
| 272 | GIT_AUTHOR_NAME="New Author" && |
| 273 | GIT_AUTHOR_EMAIL="newauthor@example.com" && |
| 274 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL && |
| 275 | git commit --allow-empty -m "by new.author via env" && |
| 276 | git show -s |
| 277 | ) && |
| 278 | git commit --author="New Author <newauthor@example.com>" \ |
| 279 | --allow-empty -m "by new.author via command line" && |
Josh Steadmon | f78f6c7 | 2019-08-07 11:57:05 -0700 | [diff] [blame] | 280 | git show -s && |
| 281 | test_cmp expected_hooks actual_hooks |
Junio C Hamano | 0486198 | 2012-03-11 03:51:32 -0700 | [diff] [blame] | 282 | ' |
| 283 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 284 | test_done |