Git fork
at reftables-rust 588 lines 16 kB view raw
1#!/bin/sh 2# 3# Copyright (c) 2007 Steven Grimm 4# 5 6test_description='git commit 7 8Tests for template, signoff, squash and -F functions.' 9 10. ./test-lib.sh 11 12. "$TEST_DIRECTORY"/lib-rebase.sh 13 14commit_msg_is () { 15 expect=commit_msg_is.expect 16 actual=commit_msg_is.actual 17 18 printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" && 19 printf "%s" "$1" >"$expect" && 20 test_cmp "$expect" "$actual" 21} 22 23# A sanity check to see if commit is working at all. 24test_expect_success 'a basic commit in an empty tree should succeed' ' 25 echo content > foo && 26 git add foo && 27 git commit -m "initial commit" 28' 29 30test_expect_success 'nonexistent template file should return error' ' 31 echo changes >> foo && 32 git add foo && 33 ( 34 GIT_EDITOR="echo hello >" && 35 export GIT_EDITOR && 36 test_must_fail git commit --template "$(pwd)"/notexist 37 ) 38' 39 40test_expect_success 'nonexistent optional template file on command line' ' 41 echo changes >> foo && 42 git add foo && 43 ( 44 GIT_EDITOR="echo hello >\"\$1\"" && 45 export GIT_EDITOR && 46 git commit --template ":(optional)$(pwd)/notexist" 47 ) 48' 49 50test_expect_success 'nonexistent template file in config should return error' ' 51 test_config commit.template "$(pwd)"/notexist && 52 ( 53 GIT_EDITOR="echo hello >" && 54 export GIT_EDITOR && 55 test_must_fail git commit --allow-empty 56 ) 57' 58 59test_expect_success 'nonexistent optional template file in config' ' 60 test_config commit.template ":(optional)$(pwd)"/notexist && 61 GIT_EDITOR="echo hello >" git commit --allow-empty && 62 git cat-file commit HEAD | sed -e "1,/^$/d" >actual && 63 echo hello >expect && 64 test_cmp expect actual 65' 66 67# From now on we'll use a template file that exists. 68TEMPLATE="$(pwd)"/template 69 70test_expect_success 'unedited template should not commit' ' 71 echo "template line" >"$TEMPLATE" && 72 test_must_fail git commit --allow-empty --template "$TEMPLATE" 73' 74 75test_expect_success 'unedited template with comments should not commit' ' 76 echo "# comment in template" >>"$TEMPLATE" && 77 test_must_fail git commit --allow-empty --template "$TEMPLATE" 78' 79 80test_expect_success 'a Signed-off-by line by itself should not commit' ' 81 ( 82 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off && 83 test_must_fail git commit --allow-empty --template "$TEMPLATE" 84 ) 85' 86 87test_expect_success 'adding comments to a template should not commit' ' 88 ( 89 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments && 90 test_must_fail git commit --allow-empty --template "$TEMPLATE" 91 ) 92' 93 94test_expect_success 'adding real content to a template should commit' ' 95 ( 96 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 97 git commit --allow-empty --template "$TEMPLATE" 98 ) && 99 commit_msg_is "template linecommit message" 100' 101 102test_expect_success 'existent template marked optional should commit' ' 103 echo "existent template" >"$TEMPLATE" && 104 ( 105 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 106 git commit --allow-empty --template ":(optional)$TEMPLATE" 107 ) && 108 commit_msg_is "existent templatecommit message" 109' 110 111test_expect_success '-t option should be short for --template' ' 112 echo "short template" > "$TEMPLATE" && 113 echo "new content" >> foo && 114 git add foo && 115 ( 116 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 117 git commit -t "$TEMPLATE" 118 ) && 119 commit_msg_is "short templatecommit message" 120' 121 122test_expect_success 'config-specified template should commit' ' 123 echo "new template" > "$TEMPLATE" && 124 test_config commit.template "$TEMPLATE" && 125 echo "more content" >> foo && 126 git add foo && 127 ( 128 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 129 git commit 130 ) && 131 commit_msg_is "new templatecommit message" 132' 133 134test_expect_success 'explicit commit message should override template' ' 135 echo "still more content" >> foo && 136 git add foo && 137 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \ 138 -m "command line msg" && 139 commit_msg_is "command line msg" 140' 141 142test_expect_success 'commit message from file should override template' ' 143 echo "content galore" >> foo && 144 git add foo && 145 echo "standard input msg" | 146 ( 147 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 148 git commit --template "$TEMPLATE" --file - 149 ) && 150 commit_msg_is "standard input msg" 151' 152 153cat >"$TEMPLATE" <<\EOF 154 155 156### template 157 158EOF 159test_expect_success 'commit message from template with whitespace issue' ' 160 echo "content galore" >>foo && 161 git add foo && 162 GIT_EDITOR=\""$TEST_DIRECTORY"\"/t7500/add-whitespaced-content \ 163 git commit --template "$TEMPLATE" && 164 commit_msg_is "commit message" 165' 166 167test_expect_success 'using alternate GIT_INDEX_FILE (1)' ' 168 169 cp .git/index saved-index && 170 ( 171 echo some new content >file && 172 GIT_INDEX_FILE=.git/another_index && 173 export GIT_INDEX_FILE && 174 git add file && 175 git commit -m "commit using another index" && 176 git diff-index --exit-code HEAD && 177 git diff-files --exit-code 178 ) && 179 cmp .git/index saved-index >/dev/null 180 181' 182 183test_expect_success 'using alternate GIT_INDEX_FILE (2)' ' 184 185 cp .git/index saved-index && 186 ( 187 rm -f .git/no-such-index && 188 GIT_INDEX_FILE=.git/no-such-index && 189 export GIT_INDEX_FILE && 190 git commit -m "commit using nonexistent index" && 191 test -z "$(git ls-files)" && 192 test -z "$(git ls-tree HEAD)" 193 194 ) && 195 cmp .git/index saved-index >/dev/null 196' 197 198cat > expect << EOF 199zort 200 201Signed-off-by: C O Mitter <committer@example.com> 202EOF 203 204test_expect_success '--signoff' ' 205 echo "yet another content *narf*" >> foo && 206 echo "zort" | git commit -s -F - foo && 207 git cat-file commit HEAD | sed "1,/^\$/d" > output && 208 test_cmp expect output 209' 210 211test_expect_success 'commit message from file (1)' ' 212 mkdir subdir && 213 echo "Log in top directory" >log && 214 echo "Log in sub directory" >subdir/log && 215 ( 216 cd subdir && 217 git commit --allow-empty -F log 218 ) && 219 commit_msg_is "Log in sub directory" 220' 221 222test_expect_success 'commit message from file (2)' ' 223 rm -f log && 224 echo "Log in sub directory" >subdir/log && 225 ( 226 cd subdir && 227 git commit --allow-empty -F log 228 ) && 229 commit_msg_is "Log in sub directory" 230' 231 232test_expect_success 'commit message from stdin' ' 233 ( 234 cd subdir && 235 echo "Log with foo word" | git commit --allow-empty -F - 236 ) && 237 commit_msg_is "Log with foo word" 238' 239 240test_expect_success 'commit -F overrides -t' ' 241 ( 242 cd subdir && 243 echo "-F log" > f.log && 244 echo "-t template" > t.template && 245 git commit --allow-empty -F f.log -t t.template 246 ) && 247 commit_msg_is "-F log" 248' 249 250test_expect_success 'Commit without message is allowed with --allow-empty-message' ' 251 echo "more content" >>foo && 252 git add foo && 253 >empty && 254 git commit --allow-empty-message <empty && 255 commit_msg_is "" && 256 git tag empty-message-commit 257' 258 259test_expect_success 'Commit without message is no-no without --allow-empty-message' ' 260 echo "more content" >>foo && 261 git add foo && 262 >empty && 263 test_must_fail git commit <empty 264' 265 266test_expect_success 'Commit a message with --allow-empty-message' ' 267 echo "even more content" >>foo && 268 git add foo && 269 git commit --allow-empty-message -m"hello there" && 270 commit_msg_is "hello there" 271' 272 273test_expect_success 'commit -C empty respects --allow-empty-message' ' 274 echo more >>foo && 275 git add foo && 276 test_must_fail git commit -C empty-message-commit && 277 git commit -C empty-message-commit --allow-empty-message && 278 commit_msg_is "" 279' 280 281commit_for_rebase_autosquash_setup () { 282 echo "first content line" >>foo && 283 git add foo && 284 cat >log <<EOF && 285target message subject line 286 287target message body line 1 288target message body line 2 289EOF 290 git commit -F log && 291 echo "second content line" >>foo && 292 git add foo && 293 git commit -m "intermediate commit" && 294 echo "third content line" >>foo && 295 git add foo 296} 297 298test_expect_success 'commit --fixup provides correct one-line commit message' ' 299 commit_for_rebase_autosquash_setup && 300 EDITOR="echo ignored >>" git commit --fixup HEAD~1 && 301 commit_msg_is "fixup! target message subject line" 302' 303 304test_expect_success 'commit --fixup -m"something" -m"extra"' ' 305 commit_for_rebase_autosquash_setup && 306 git commit --fixup HEAD~1 -m"something" -m"extra" && 307 commit_msg_is "fixup! target message subject linesomething 308 309extra" 310' 311test_expect_success 'commit --fixup --edit' ' 312 commit_for_rebase_autosquash_setup && 313 EDITOR="printf \"something\nextra\" >>" git commit --fixup HEAD~1 --edit && 314 commit_msg_is "fixup! target message subject linesomething 315extra" 316' 317 318get_commit_msg () { 319 rev="$1" && 320 git log -1 --pretty=format:"%B" "$rev" 321} 322 323test_expect_success 'commit --fixup=amend: creates amend! commit' ' 324 commit_for_rebase_autosquash_setup && 325 cat >expected <<-EOF && 326 amend! $(git log -1 --format=%s HEAD~) 327 328 $(get_commit_msg HEAD~) 329 330 edited 331 EOF 332 ( 333 set_fake_editor && 334 FAKE_COMMIT_AMEND="edited" \ 335 git commit --fixup=amend:HEAD~ 336 ) && 337 get_commit_msg HEAD >actual && 338 test_cmp expected actual 339' 340 341test_expect_success '--fixup=amend: --only ignores staged changes' ' 342 commit_for_rebase_autosquash_setup && 343 cat >expected <<-EOF && 344 amend! $(git log -1 --format=%s HEAD~) 345 346 $(get_commit_msg HEAD~) 347 348 edited 349 EOF 350 ( 351 set_fake_editor && 352 FAKE_COMMIT_AMEND="edited" \ 353 git commit --fixup=amend:HEAD~ --only 354 ) && 355 get_commit_msg HEAD >actual && 356 test_cmp expected actual && 357 test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} && 358 test_cmp_rev HEAD@{1} HEAD^ && 359 test_expect_code 1 git diff --cached --exit-code && 360 git cat-file blob :foo >actual && 361 test_cmp foo actual 362' 363 364test_expect_success '--fixup=reword: ignores staged changes' ' 365 commit_for_rebase_autosquash_setup && 366 cat >expected <<-EOF && 367 amend! $(git log -1 --format=%s HEAD~) 368 369 $(get_commit_msg HEAD~) 370 371 edited 372 EOF 373 ( 374 set_fake_editor && 375 FAKE_COMMIT_AMEND="edited" \ 376 git commit --fixup=reword:HEAD~ 377 ) && 378 get_commit_msg HEAD >actual && 379 test_cmp expected actual && 380 test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} && 381 test_cmp_rev HEAD@{1} HEAD^ && 382 test_expect_code 1 git diff --cached --exit-code && 383 git cat-file blob :foo >actual && 384 test_cmp foo actual 385' 386 387test_expect_success '--fixup=reword: error out with -m option' ' 388 commit_for_rebase_autosquash_setup && 389 echo "fatal: options '\''-m'\'' and '\''--fixup:reword'\'' cannot be used together" >expect && 390 test_must_fail git commit --fixup=reword:HEAD~ -m "reword commit message" 2>actual && 391 test_cmp expect actual 392' 393 394test_expect_success '--fixup=amend: error out with -m option' ' 395 commit_for_rebase_autosquash_setup && 396 echo "fatal: options '\''-m'\'' and '\''--fixup:amend'\'' cannot be used together" >expect && 397 test_must_fail git commit --fixup=amend:HEAD~ -m "amend commit message" 2>actual && 398 test_cmp expect actual 399' 400 401test_expect_success 'consecutive amend! commits remove amend! line from commit msg body' ' 402 commit_for_rebase_autosquash_setup && 403 cat >expected <<-EOF && 404 amend! amend! $(git log -1 --format=%s HEAD~) 405 406 $(get_commit_msg HEAD~) 407 408 edited 1 409 410 edited 2 411 EOF 412 echo "reword new commit message" >actual && 413 ( 414 set_fake_editor && 415 FAKE_COMMIT_AMEND="edited 1" \ 416 git commit --fixup=reword:HEAD~ && 417 FAKE_COMMIT_AMEND="edited 2" \ 418 git commit --fixup=reword:HEAD 419 ) && 420 get_commit_msg HEAD >actual && 421 test_cmp expected actual 422' 423 424test_expect_success 'deny to create amend! commit if its commit msg body is empty' ' 425 commit_for_rebase_autosquash_setup && 426 echo "Aborting commit due to empty commit message body." >expected && 427 ( 428 set_fake_editor && 429 test_must_fail env FAKE_COMMIT_MESSAGE="amend! target message subject line" \ 430 git commit --fixup=amend:HEAD~ 2>actual 431 ) && 432 test_cmp expected actual 433' 434 435test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' ' 436 commit_for_rebase_autosquash_setup && 437 cat >expected <<-EOF && 438 amend! $(git log -1 --format=%s HEAD~) 439 EOF 440 ( 441 set_fake_editor && 442 FAKE_COMMIT_MESSAGE="amend! target message subject line" \ 443 git commit --fixup=amend:HEAD~ --allow-empty-message && 444 get_commit_msg HEAD >actual 445 ) && 446 test_cmp expected actual 447' 448 449test_fixup_reword_opt () { 450 test_expect_success "--fixup=reword: incompatible with $1" " 451 echo 'fatal: reword option of '\''--fixup'\'' and' \ 452 ''\''--patch/--interactive/--all/--include/--only'\' \ 453 'cannot be used together' >expect && 454 test_must_fail git commit --fixup=reword:HEAD~ $1 2>actual && 455 test_cmp expect actual 456 " 457} 458 459for opt in --all --include --only --interactive --patch 460do 461 test_fixup_reword_opt $opt 462done 463 464test_expect_success '--fixup=reword: give error with pathsec' ' 465 commit_for_rebase_autosquash_setup && 466 echo "fatal: reword option of '\''--fixup'\'' and path '\''foo'\'' cannot be used together" >expect && 467 test_must_fail git commit --fixup=reword:HEAD~ -- foo 2>actual && 468 test_cmp expect actual 469' 470 471test_expect_success '--fixup=reword: -F give error message' ' 472 echo "fatal: options '\''-F'\'' and '\''--fixup'\'' cannot be used together" >expect && 473 test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual && 474 test_cmp expect actual 475' 476 477test_expect_success 'commit --squash works with -F' ' 478 commit_for_rebase_autosquash_setup && 479 echo "log message from file" >msgfile && 480 git commit --squash HEAD~1 -F msgfile && 481 commit_msg_is "squash! target message subject linelog message from file" 482' 483 484test_expect_success 'commit --squash works with -m' ' 485 commit_for_rebase_autosquash_setup && 486 git commit --squash HEAD~1 -m "foo bar\nbaz" && 487 commit_msg_is "squash! target message subject linefoo bar\nbaz" 488' 489 490test_expect_success 'commit --squash works with -C' ' 491 commit_for_rebase_autosquash_setup && 492 git commit --squash HEAD~1 -C HEAD && 493 commit_msg_is "squash! target message subject lineintermediate commit" 494' 495 496test_expect_success 'commit --squash works with -c' ' 497 commit_for_rebase_autosquash_setup && 498 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && 499 git commit --squash HEAD~1 -c HEAD && 500 commit_msg_is "squash! target message subject lineedited commit" 501' 502 503test_expect_success 'commit --squash works with -C for same commit' ' 504 commit_for_rebase_autosquash_setup && 505 git commit --squash HEAD -C HEAD && 506 commit_msg_is "squash! intermediate commit" 507' 508 509test_expect_success 'commit --squash works with -c for same commit' ' 510 commit_for_rebase_autosquash_setup && 511 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && 512 git commit --squash HEAD -c HEAD && 513 commit_msg_is "squash! edited commit" 514' 515 516test_expect_success 'commit --squash works with editor' ' 517 commit_for_rebase_autosquash_setup && 518 test_set_editor "$TEST_DIRECTORY"/t7500/add-content && 519 git commit --squash HEAD~1 && 520 commit_msg_is "squash! target message subject linecommit message" 521' 522 523test_expect_success 'invalid message options when using --fixup' ' 524 echo changes >>foo && 525 echo "message" >log && 526 git add foo && 527 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 && 528 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 && 529 test_must_fail git commit --fixup HEAD~1 -c HEAD~2 && 530 test_must_fail git commit --fixup HEAD~1 -F log 531' 532 533cat >expected-template <<EOF 534 535# Please enter the commit message for your changes. Lines starting 536# with '#' will be ignored. 537# 538# Author: A U Thor <author@example.com> 539# 540# On branch commit-template-check 541# Changes to be committed: 542# new file: commit-template-check 543# 544# Untracked files not listed 545EOF 546 547test_expect_success 'new line found before status message in commit template' ' 548 git checkout -b commit-template-check && 549 git reset --hard HEAD && 550 touch commit-template-check && 551 git add commit-template-check && 552 GIT_EDITOR="cat >editor-input" git commit --untracked-files=no --allow-empty-message && 553 test_cmp expected-template editor-input 554' 555 556test_expect_success 'setup empty commit with unstaged rename and copy' ' 557 test_create_repo unstaged_rename_and_copy && 558 ( 559 cd unstaged_rename_and_copy && 560 561 echo content >orig && 562 git add orig && 563 test_commit orig && 564 565 cp orig new_copy && 566 mv orig new_rename && 567 git add -N new_copy new_rename 568 ) 569' 570 571test_expect_success 'check commit with unstaged rename and copy' ' 572 ( 573 cd unstaged_rename_and_copy && 574 575 test_must_fail git -c diff.renames=copy commit 576 ) 577' 578 579test_expect_success 'commit without staging files fails and displays hints' ' 580 echo "initial" >file && 581 git add file && 582 git commit -m initial && 583 echo "changes" >>file && 584 test_must_fail git commit -m update >actual && 585 test_grep "no changes added to commit (use \"git add\" and/or \"git commit -a\")" actual 586' 587 588test_done