Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Steven Grimm |
| 4 | # |
| 5 | |
Nanako Shiraishi | 47a528a | 2008-09-03 17:59:33 +0900 | [diff] [blame] | 6 | test_description='git commit |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 7 | |
| 8 | Tests for selected commit options.' |
| 9 | |
| 10 | . ./test-lib.sh |
| 11 | |
| 12 | commit_msg_is () { |
Ævar Arnfjörð Bjarmason | d5a719e | 2011-02-19 18:29:09 +0000 | [diff] [blame] | 13 | expect=commit_msg_is.expect |
| 14 | actual=commit_msg_is.actual |
| 15 | |
Andrew Pimlott | f66d000 | 2013-07-01 09:20:36 -0700 | [diff] [blame] | 16 | printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" && |
| 17 | printf "%s" "$1" >"$expect" && |
| 18 | test_i18ncmp "$expect" "$actual" |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | # A sanity check to see if commit is working at all. |
| 22 | test_expect_success 'a basic commit in an empty tree should succeed' ' |
| 23 | echo content > foo && |
| 24 | git add foo && |
| 25 | git commit -m "initial commit" |
| 26 | ' |
| 27 | |
| 28 | test_expect_success 'nonexistent template file should return error' ' |
| 29 | echo changes >> foo && |
| 30 | git add foo && |
Jonathan Nieder | 2140b14 | 2011-02-25 03:07:57 -0600 | [diff] [blame] | 31 | ( |
| 32 | GIT_EDITOR="echo hello >\"\$1\"" && |
| 33 | export GIT_EDITOR && |
| 34 | test_must_fail git commit --template "$PWD"/notexist |
| 35 | ) |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 36 | ' |
| 37 | |
| 38 | test_expect_success 'nonexistent template file in config should return error' ' |
Yann Droneaud | 22179b3 | 2013-03-24 22:06:09 +0100 | [diff] [blame] | 39 | test_config commit.template "$PWD"/notexist && |
Jonathan Nieder | 2140b14 | 2011-02-25 03:07:57 -0600 | [diff] [blame] | 40 | ( |
| 41 | GIT_EDITOR="echo hello >\"\$1\"" && |
| 42 | export GIT_EDITOR && |
| 43 | test_must_fail git commit |
| 44 | ) |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 45 | ' |
| 46 | |
| 47 | # From now on we'll use a template file that exists. |
| 48 | TEMPLATE="$PWD"/template |
| 49 | |
| 50 | test_expect_success 'unedited template should not commit' ' |
| 51 | echo "template line" > "$TEMPLATE" && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 52 | test_must_fail git commit --template "$TEMPLATE" |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 53 | ' |
| 54 | |
| 55 | test_expect_success 'unedited template with comments should not commit' ' |
| 56 | echo "# comment in template" >> "$TEMPLATE" && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 57 | test_must_fail git commit --template "$TEMPLATE" |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 58 | ' |
| 59 | |
| 60 | test_expect_success 'a Signed-off-by line by itself should not commit' ' |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 61 | ( |
| 62 | test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off && |
| 63 | test_must_fail git commit --template "$TEMPLATE" |
| 64 | ) |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 65 | ' |
| 66 | |
| 67 | test_expect_success 'adding comments to a template should not commit' ' |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 68 | ( |
| 69 | test_set_editor "$TEST_DIRECTORY"/t7500/add-comments && |
| 70 | test_must_fail git commit --template "$TEMPLATE" |
| 71 | ) |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 72 | ' |
| 73 | |
Junio C Hamano | 29853c8 | 2011-04-12 16:48:35 -0700 | [diff] [blame] | 74 | test_expect_success 'adding real content to a template should commit' ' |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 75 | ( |
| 76 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && |
| 77 | git commit --template "$TEMPLATE" |
| 78 | ) && |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 79 | commit_msg_is "template linecommit message" |
| 80 | ' |
| 81 | |
Junio C Hamano | 29853c8 | 2011-04-12 16:48:35 -0700 | [diff] [blame] | 82 | test_expect_success '-t option should be short for --template' ' |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 83 | echo "short template" > "$TEMPLATE" && |
| 84 | echo "new content" >> foo && |
| 85 | git add foo && |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 86 | ( |
| 87 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && |
| 88 | git commit -t "$TEMPLATE" |
| 89 | ) && |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 90 | commit_msg_is "short templatecommit message" |
| 91 | ' |
| 92 | |
Junio C Hamano | 29853c8 | 2011-04-12 16:48:35 -0700 | [diff] [blame] | 93 | test_expect_success 'config-specified template should commit' ' |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 94 | echo "new template" > "$TEMPLATE" && |
Yann Droneaud | 22179b3 | 2013-03-24 22:06:09 +0100 | [diff] [blame] | 95 | test_config commit.template "$TEMPLATE" && |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 96 | echo "more content" >> foo && |
| 97 | git add foo && |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 98 | ( |
| 99 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && |
| 100 | git commit |
| 101 | ) && |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 102 | commit_msg_is "new templatecommit message" |
| 103 | ' |
| 104 | |
| 105 | test_expect_success 'explicit commit message should override template' ' |
| 106 | echo "still more content" >> foo && |
| 107 | git add foo && |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 108 | GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \ |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 109 | -m "command line msg" && |
Michal Vitecek | 55246aa | 2007-09-25 16:38:46 +0200 | [diff] [blame] | 110 | commit_msg_is "command line msg" |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 111 | ' |
| 112 | |
| 113 | test_expect_success 'commit message from file should override template' ' |
| 114 | echo "content galore" >> foo && |
| 115 | git add foo && |
| 116 | echo "standard input msg" | |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 117 | ( |
| 118 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && |
| 119 | git commit --template "$TEMPLATE" --file - |
| 120 | ) && |
Michal Vitecek | 55246aa | 2007-09-25 16:38:46 +0200 | [diff] [blame] | 121 | commit_msg_is "standard input msg" |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 122 | ' |
| 123 | |
Boris Faure | 8b1ae67 | 2011-05-08 12:31:02 +0200 | [diff] [blame] | 124 | cat >"$TEMPLATE" <<\EOF |
| 125 | |
| 126 | |
| 127 | ### template |
| 128 | |
| 129 | EOF |
| 130 | test_expect_success 'commit message from template with whitespace issue' ' |
| 131 | echo "content galore" >>foo && |
| 132 | git add foo && |
| 133 | GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-whitespaced-content git commit \ |
| 134 | --template "$TEMPLATE" && |
| 135 | commit_msg_is "commit message" |
| 136 | ' |
| 137 | |
Rémi Vanicat | 859a4db | 2007-11-11 13:28:08 +0100 | [diff] [blame] | 138 | test_expect_success 'using alternate GIT_INDEX_FILE (1)' ' |
| 139 | |
| 140 | cp .git/index saved-index && |
| 141 | ( |
| 142 | echo some new content >file && |
| 143 | GIT_INDEX_FILE=.git/another_index && |
| 144 | export GIT_INDEX_FILE && |
| 145 | git add file && |
| 146 | git commit -m "commit using another index" && |
| 147 | git diff-index --exit-code HEAD && |
| 148 | git diff-files --exit-code |
| 149 | ) && |
| 150 | cmp .git/index saved-index >/dev/null |
| 151 | |
| 152 | ' |
| 153 | |
| 154 | test_expect_success 'using alternate GIT_INDEX_FILE (2)' ' |
| 155 | |
| 156 | cp .git/index saved-index && |
| 157 | ( |
| 158 | rm -f .git/no-such-index && |
| 159 | GIT_INDEX_FILE=.git/no-such-index && |
| 160 | export GIT_INDEX_FILE && |
| 161 | git commit -m "commit using nonexistent index" && |
| 162 | test -z "$(git ls-files)" && |
| 163 | test -z "$(git ls-tree HEAD)" |
| 164 | |
| 165 | ) && |
| 166 | cmp .git/index saved-index >/dev/null |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 167 | ' |
Rémi Vanicat | 859a4db | 2007-11-11 13:28:08 +0100 | [diff] [blame] | 168 | |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 169 | cat > expect << EOF |
| 170 | zort |
Johannes Schindelin | 2150554 | 2007-11-11 17:36:27 +0000 | [diff] [blame] | 171 | |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 172 | Signed-off-by: C O Mitter <committer@example.com> |
| 173 | EOF |
| 174 | |
| 175 | test_expect_success '--signoff' ' |
| 176 | echo "yet another content *narf*" >> foo && |
Adeodato Simó | 7eb5bbd | 2009-01-09 18:30:05 +0100 | [diff] [blame] | 177 | echo "zort" | git commit -s -F - foo && |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 178 | git cat-file commit HEAD | sed "1,/^\$/d" > output && |
Junio C Hamano | bfdbee9 | 2008-08-08 02:26:28 -0700 | [diff] [blame] | 179 | test_cmp expect output |
Rémi Vanicat | 859a4db | 2007-11-11 13:28:08 +0100 | [diff] [blame] | 180 | ' |
| 181 | |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 182 | test_expect_success 'commit message from file (1)' ' |
| 183 | mkdir subdir && |
| 184 | echo "Log in top directory" >log && |
| 185 | echo "Log in sub directory" >subdir/log && |
| 186 | ( |
| 187 | cd subdir && |
| 188 | git commit --allow-empty -F log |
| 189 | ) && |
| 190 | commit_msg_is "Log in sub directory" |
| 191 | ' |
| 192 | |
| 193 | test_expect_success 'commit message from file (2)' ' |
| 194 | rm -f log && |
| 195 | echo "Log in sub directory" >subdir/log && |
| 196 | ( |
| 197 | cd subdir && |
| 198 | git commit --allow-empty -F log |
| 199 | ) && |
| 200 | commit_msg_is "Log in sub directory" |
| 201 | ' |
| 202 | |
| 203 | test_expect_success 'commit message from stdin' ' |
| 204 | ( |
| 205 | cd subdir && |
| 206 | echo "Log with foo word" | git commit --allow-empty -F - |
| 207 | ) && |
| 208 | commit_msg_is "Log with foo word" |
| 209 | ' |
| 210 | |
Stephen Boyd | aae94ff | 2009-05-23 11:53:10 -0700 | [diff] [blame] | 211 | test_expect_success 'commit -F overrides -t' ' |
| 212 | ( |
| 213 | cd subdir && |
| 214 | echo "-F log" > f.log && |
| 215 | echo "-t template" > t.template && |
| 216 | git commit --allow-empty -F f.log -t t.template |
| 217 | ) && |
| 218 | commit_msg_is "-F log" |
| 219 | ' |
| 220 | |
Ævar Arnfjörð Bjarmason | c9b5fde | 2010-04-06 08:40:35 +0000 | [diff] [blame] | 221 | test_expect_success 'Commit without message is allowed with --allow-empty-message' ' |
| 222 | echo "more content" >>foo && |
| 223 | git add foo && |
| 224 | >empty && |
| 225 | git commit --allow-empty-message <empty && |
Jeff King | 076cbd6 | 2014-04-25 19:11:15 -0400 | [diff] [blame] | 226 | commit_msg_is "" && |
| 227 | git tag empty-message-commit |
Ævar Arnfjörð Bjarmason | c9b5fde | 2010-04-06 08:40:35 +0000 | [diff] [blame] | 228 | ' |
| 229 | |
| 230 | test_expect_success 'Commit without message is no-no without --allow-empty-message' ' |
| 231 | echo "more content" >>foo && |
| 232 | git add foo && |
| 233 | >empty && |
| 234 | test_must_fail git commit <empty |
| 235 | ' |
| 236 | |
| 237 | test_expect_success 'Commit a message with --allow-empty-message' ' |
| 238 | echo "even more content" >>foo && |
| 239 | git add foo && |
| 240 | git commit --allow-empty-message -m"hello there" && |
| 241 | commit_msg_is "hello there" |
| 242 | ' |
| 243 | |
Jeff King | 076cbd6 | 2014-04-25 19:11:15 -0400 | [diff] [blame] | 244 | test_expect_success 'commit -C empty respects --allow-empty-message' ' |
| 245 | echo more >>foo && |
| 246 | git add foo && |
| 247 | test_must_fail git commit -C empty-message-commit && |
| 248 | git commit -C empty-message-commit --allow-empty-message && |
| 249 | commit_msg_is "" |
| 250 | ' |
| 251 | |
Pat Notz | b1a6c0a | 2010-11-02 13:59:10 -0600 | [diff] [blame] | 252 | commit_for_rebase_autosquash_setup () { |
| 253 | echo "first content line" >>foo && |
| 254 | git add foo && |
| 255 | cat >log <<EOF && |
| 256 | target message subject line |
| 257 | |
| 258 | target message body line 1 |
| 259 | target message body line 2 |
| 260 | EOF |
| 261 | git commit -F log && |
| 262 | echo "second content line" >>foo && |
| 263 | git add foo && |
| 264 | git commit -m "intermediate commit" && |
| 265 | echo "third content line" >>foo && |
| 266 | git add foo |
| 267 | } |
| 268 | |
| 269 | test_expect_success 'commit --fixup provides correct one-line commit message' ' |
| 270 | commit_for_rebase_autosquash_setup && |
| 271 | git commit --fixup HEAD~1 && |
| 272 | commit_msg_is "fixup! target message subject line" |
| 273 | ' |
| 274 | |
Pat Notz | 7951bd3 | 2010-11-02 13:59:12 -0600 | [diff] [blame] | 275 | test_expect_success 'commit --squash works with -F' ' |
| 276 | commit_for_rebase_autosquash_setup && |
| 277 | echo "log message from file" >msgfile && |
| 278 | git commit --squash HEAD~1 -F msgfile && |
| 279 | commit_msg_is "squash! target message subject linelog message from file" |
| 280 | ' |
| 281 | |
| 282 | test_expect_success 'commit --squash works with -m' ' |
| 283 | commit_for_rebase_autosquash_setup && |
| 284 | git commit --squash HEAD~1 -m "foo bar\nbaz" && |
| 285 | commit_msg_is "squash! target message subject linefoo bar\nbaz" |
| 286 | ' |
| 287 | |
| 288 | test_expect_success 'commit --squash works with -C' ' |
| 289 | commit_for_rebase_autosquash_setup && |
| 290 | git commit --squash HEAD~1 -C HEAD && |
| 291 | commit_msg_is "squash! target message subject lineintermediate commit" |
| 292 | ' |
| 293 | |
| 294 | test_expect_success 'commit --squash works with -c' ' |
| 295 | commit_for_rebase_autosquash_setup && |
| 296 | test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && |
| 297 | git commit --squash HEAD~1 -c HEAD && |
| 298 | commit_msg_is "squash! target message subject lineedited commit" |
| 299 | ' |
| 300 | |
| 301 | test_expect_success 'commit --squash works with -C for same commit' ' |
| 302 | commit_for_rebase_autosquash_setup && |
| 303 | git commit --squash HEAD -C HEAD && |
| 304 | commit_msg_is "squash! intermediate commit" |
| 305 | ' |
| 306 | |
| 307 | test_expect_success 'commit --squash works with -c for same commit' ' |
| 308 | commit_for_rebase_autosquash_setup && |
| 309 | test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && |
| 310 | git commit --squash HEAD -c HEAD && |
| 311 | commit_msg_is "squash! edited commit" |
| 312 | ' |
| 313 | |
Junio C Hamano | 29853c8 | 2011-04-12 16:48:35 -0700 | [diff] [blame] | 314 | test_expect_success 'commit --squash works with editor' ' |
Pat Notz | 7951bd3 | 2010-11-02 13:59:12 -0600 | [diff] [blame] | 315 | commit_for_rebase_autosquash_setup && |
| 316 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && |
| 317 | git commit --squash HEAD~1 && |
| 318 | commit_msg_is "squash! target message subject linecommit message" |
| 319 | ' |
| 320 | |
Pat Notz | b1a6c0a | 2010-11-02 13:59:10 -0600 | [diff] [blame] | 321 | test_expect_success 'invalid message options when using --fixup' ' |
| 322 | echo changes >>foo && |
| 323 | echo "message" >log && |
| 324 | git add foo && |
Pat Notz | 7951bd3 | 2010-11-02 13:59:12 -0600 | [diff] [blame] | 325 | test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 && |
Pat Notz | b1a6c0a | 2010-11-02 13:59:10 -0600 | [diff] [blame] | 326 | test_must_fail git commit --fixup HEAD~1 -C HEAD~2 && |
| 327 | test_must_fail git commit --fixup HEAD~1 -c HEAD~2 && |
| 328 | test_must_fail git commit --fixup HEAD~1 -m "cmdline message" && |
| 329 | test_must_fail git commit --fixup HEAD~1 -F log |
| 330 | ' |
| 331 | |
Steven Grimm | d1cc130 | 2007-07-22 21:17:42 -0700 | [diff] [blame] | 332 | test_done |