Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t7501-commit-basic-functionality.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t7501-commit-basic-functionality.sh` 789 lines · 19470 bytes Raw Blame History 1 #!/bin/sh 2 # 3 # Copyright (c) 2007 Kristian Høgsberg 4 # 5 6 # FIXME: Test the various index usages, test reflog 7 8 test_description='git commit' 9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 11 12 . ./test-lib.sh 13 . "$TEST_DIRECTORY/lib-diff.sh" 14 15 author='The Real Author ' 16 17 test_tick 18 19 test_expect_success 'initial status' ' 20 echo bongo bongo >file && 21 git add file && 22 git status >actual && 23 test_grep "No commits yet" actual 24 ' 25 26 test_expect_success 'fail initial amend' ' 27 test_must_fail git commit --amend 28 ' 29 30 test_expect_success 'setup: initial commit' ' 31 git commit -m initial 32 ' 33 34 test_expect_success '-m and -F do not mix' ' 35 git checkout HEAD file && echo >>file && git add file && 36 test_must_fail git commit -m foo -m bar -F file 37 ' 38 39 test_expect_success '-m and -C do not mix' ' 40 git checkout HEAD file && echo >>file && git add file && 41 test_must_fail git commit -C HEAD -m illegal 42 ' 43 44 test_expect_success 'paths and -a do not mix' ' 45 echo King of the bongo >file && 46 test_must_fail git commit -m foo -a file 47 ' 48 49 test_expect_success 'can use paths with --interactive' ' 50 echo bong-o-bong >file && 51 # 2: update, 1:st path, that is all, 7: quit 52 test_write_lines 2 1 "" 7 | 53 git commit -m foo --interactive file && 54 git reset --hard HEAD^ 55 ' 56 57 test_expect_success 'removed files and relative paths' ' 58 test_when_finished "rm -rf foo" && 59 git init foo && 60 >foo/foo.txt && 61 git -C foo add foo.txt && 62 git -C foo commit -m first && 63 git -C foo rm foo.txt && 64 65 mkdir -p foo/bar && 66 git -C foo/bar commit -m second ../foo.txt 67 ' 68 69 test_expect_success 'using invalid commit with -C' ' 70 test_must_fail git commit --allow-empty -C bogus 71 ' 72 73 test_expect_success 'nothing to commit' ' 74 git reset --hard && 75 test_must_fail git commit -m initial 76 ' 77 78 test_expect_success '--dry-run fails with nothing to commit' ' 79 test_must_fail git commit -m initial --dry-run 80 ' 81 82 test_expect_success '--short fails with nothing to commit' ' 83 test_must_fail git commit -m initial --short 84 ' 85 86 test_expect_success '--porcelain fails with nothing to commit' ' 87 test_must_fail git commit -m initial --porcelain 88 ' 89 90 test_expect_success '--long fails with nothing to commit' ' 91 test_must_fail git commit -m initial --long 92 ' 93 94 test_expect_success 'fail to commit untracked file (even with --include/--only)' ' 95 echo content >baz && 96 error="error: pathspec .baz. did not match any file(s) known to git" && 97 98 test_must_fail git commit -m "baz" baz 2>err && 99 test_grep -e "$error" err && 100 101 test_must_fail git commit --only -m "baz" baz 2>err && 102 test_grep -e "$error" err && 103 104 test_must_fail git commit --include -m "baz" baz 2>err && 105 test_grep -e "$error" err 106 ' 107 108 test_expect_success 'setup: non-initial commit' ' 109 echo bongo bongo bongo >file && 110 git commit -m next -a 111 ' 112 113 test_expect_success '--dry-run with stuff to commit returns ok' ' 114 echo bongo bongo bongo >>file && 115 git commit -m next -a --dry-run 116 ' 117 118 test_expect_success '--short with stuff to commit returns ok' ' 119 echo bongo bongo bongo >>file && 120 git commit -m next -a --short 121 ' 122 123 test_expect_success '--porcelain with stuff to commit returns ok' ' 124 echo bongo bongo bongo >>file && 125 git commit -m next -a --porcelain 126 ' 127 128 test_expect_success '--long with stuff to commit returns ok' ' 129 echo bongo bongo bongo >>file && 130 git commit -m next -a --long 131 ' 132 133 for opt in "" "-o" "--only" 134 do 135 test_expect_success 'exclude additional staged changes when given pathspec' ' 136 echo content >>file && 137 echo content >>baz && 138 git add baz && 139 git commit $opt -m "file" file && 140 141 git diff --name-only >actual && 142 test_must_be_empty actual && 143 144 test_write_lines baz >expect && 145 git diff --name-only --cached >actual && 146 test_cmp expect actual && 147 148 test_write_lines file >expect && 149 git diff --name-only HEAD^ HEAD >actual && 150 test_cmp expect actual 151 ' 152 done 153 154 test_expect_success '-i/--include includes staged changes' ' 155 echo content >>file && 156 echo content >>baz && 157 git add file && 158 159 # baz is in the index, therefore, it will be committed 160 git commit --include -m "file and baz" baz && 161 162 git diff --name-only HEAD >remaining && 163 test_must_be_empty remaining && 164 165 test_write_lines baz file >expect && 166 git diff --name-only HEAD^ HEAD >actual && 167 test_cmp expect actual 168 ' 169 170 test_expect_success '--include and --only do not mix' ' 171 test_when_finished "git reset --hard" && 172 echo content >>file && 173 echo content >>baz && 174 test_must_fail git commit --include --only -m "file baz" file baz 2>actual && 175 test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual 176 ' 177 178 test_expect_success 'commit message from non-existing file' ' 179 echo more bongo: bongo bongo bongo bongo >file && 180 test_must_fail git commit -F gah -a 181 ' 182 183 test_expect_success 'empty commit message' ' 184 # Empty except stray tabs and spaces on a few lines. 185 sed -e "s/@//g" >msg <<-\EOF && 186 @ @ 187 @@ 188 @ @ 189 @Signed-off-by: hula@ 190 EOF 191 test_must_fail git commit -F msg -a 192 ' 193 194 test_expect_success 'template "emptyness" check does not kick in with -F' ' 195 git checkout HEAD file && echo >>file && git add file && 196 git commit -t file -F file 197 ' 198 199 test_expect_success 'template "emptyness" check' ' 200 git checkout HEAD file && echo >>file && git add file && 201 test_must_fail git commit -t file 2>err && 202 test_grep "did not edit" err 203 ' 204 205 test_expect_success 'setup: commit message from file' ' 206 git checkout HEAD file && echo >>file && git add file && 207 echo this is the commit message, coming from a file >msg && 208 git commit -F msg -a 209 ' 210 211 test_expect_success 'amend commit' ' 212 cat >editor <<-\EOF && 213 #!/bin/sh 214 sed -e "s/a file/an amend commit/g" <"$1" >"$1-" 215 mv "$1-" "$1" 216 EOF 217 chmod 755 editor && 218 EDITOR=./editor git commit --amend 219 ' 220 221 test_expect_success 'amend --only ignores staged contents' ' 222 cp file file.expect && 223 echo changed >file && 224 git add file && 225 git commit --no-edit --amend --only && 226 git cat-file blob HEAD:file >file.actual && 227 test_cmp file.expect file.actual && 228 git diff --exit-code 229 ' 230 231 test_expect_success 'allow-empty --only ignores staged contents' ' 232 echo changed-again >file && 233 git add file && 234 git commit --allow-empty --only -m "empty" && 235 git cat-file blob HEAD:file >file.actual && 236 test_cmp file.expect file.actual && 237 git diff --exit-code 238 ' 239 240 test_expect_success 'set up editor' ' 241 cat >editor <<-\EOF && 242 #!/bin/sh 243 sed -e "s/unamended/amended/g" <"$1" >"$1-" 244 mv "$1-" "$1" 245 EOF 246 chmod 755 editor 247 ' 248 249 test_expect_success 'amend without launching editor' ' 250 echo unamended >expect && 251 git commit --allow-empty -m "unamended" && 252 echo needs more bongo >file && 253 git add file && 254 EDITOR=./editor git commit --no-edit --amend && 255 git diff --exit-code HEAD -- file && 256 git diff-tree -s --format=%s HEAD >msg && 257 test_cmp expect msg 258 ' 259 260 test_expect_success '--amend --edit' ' 261 echo amended >expect && 262 git commit --allow-empty -m "unamended" && 263 echo bongo again >file && 264 git add file && 265 EDITOR=./editor git commit --edit --amend && 266 git diff-tree -s --format=%s HEAD >msg && 267 test_cmp expect msg 268 ' 269 270 test_expect_success '--amend --edit of empty message' ' 271 cat >replace <<-\EOF && 272 #!/bin/sh 273 echo "amended" >"$1" 274 EOF 275 chmod 755 replace && 276 git commit --allow-empty --allow-empty-message -m "" && 277 echo more bongo >file && 278 git add file && 279 EDITOR=./replace git commit --edit --amend && 280 git diff-tree -s --format=%s HEAD >msg && 281 ./replace expect && 282 test_cmp expect msg 283 ' 284 285 test_expect_success '--amend to set message to empty' ' 286 echo bata >file && 287 git add file && 288 git commit -m "unamended" && 289 git commit --amend --allow-empty-message -m "" && 290 git diff-tree -s --format=%s HEAD >msg && 291 echo "" >expect && 292 test_cmp expect msg 293 ' 294 295 test_expect_success '--amend to set empty message needs --allow-empty-message' ' 296 echo conga >file && 297 git add file && 298 git commit -m "unamended" && 299 test_must_fail git commit --amend -m "" && 300 git diff-tree -s --format=%s HEAD >msg && 301 echo "unamended" >expect && 302 test_cmp expect msg 303 ' 304 305 test_expect_success '-m --edit' ' 306 echo amended >expect && 307 git commit --allow-empty -m buffer && 308 echo bongo bongo >file && 309 git add file && 310 EDITOR=./editor git commit -m unamended --edit && 311 git diff-tree -s --format=%s HEAD >msg && 312 test_cmp expect msg 313 ' 314 315 test_expect_success '-m and -F do not mix' ' 316 echo enough with the bongos >file && 317 test_must_fail git commit -F msg -m amending . 318 ' 319 320 test_expect_success 'using message from other commit' ' 321 git commit -C HEAD^ . 322 ' 323 324 test_expect_success 'editing message from other commit' ' 325 cat >editor <<-\EOF && 326 #!/bin/sh 327 sed -e "s/amend/older/g" <"$1" >"$1-" 328 mv "$1-" "$1" 329 EOF 330 chmod 755 editor && 331 echo hula hula >file && 332 EDITOR=./editor git commit -c HEAD^ -a 333 ' 334 335 test_expect_success 'message from stdin' ' 336 echo silly new contents >file && 337 echo commit message from stdin | 338 git commit -F - -a 339 ' 340 341 test_expect_success 'overriding author from command line' ' 342 echo gak >file && 343 git commit -m author \ 344 --author "Rubber Duck " -a >output 2>&1 && 345 test_grep Rubber.Duck output 346 ' 347 348 test_expect_success 'interactive add' ' 349 echo 7 | test_must_fail git commit --interactive >out && 350 test_grep "What now" out 351 ' 352 353 test_expect_success "commit --interactive doesn't change index if editor aborts" ' 354 echo zoo >file && 355 test_must_fail git diff --exit-code >diff1 && 356 test_write_lines u "*" q | 357 ( 358 EDITOR=: && 359 export EDITOR && 360 test_must_fail git commit --interactive 361 ) && 362 git diff >diff2 && 363 compare_diff_patch diff1 diff2 364 ' 365 366 test_expect_success 'editor not invoked if -F is given' ' 367 cat >editor <<-\EOF && 368 #!/bin/sh 369 sed -e s/good/bad/g <"$1" >"$1-" 370 mv "$1-" "$1" 371 EOF 372 chmod 755 editor && 373 374 echo A good commit message. >msg && 375 echo moo >file && 376 377 EDITOR=./editor git commit -a -F msg && 378 git show -s --pretty=format:%s >subject && 379 test_grep -q good subject && 380 381 echo quack >file && 382 echo Another good message. | 383 EDITOR=./editor git commit -a -F - && 384 git show -s --pretty=format:%s >subject && 385 test_grep -q good subject 386 ' 387 388 test_expect_success 'partial commit that involves removal (1)' ' 389 390 git rm --cached file && 391 mv file elif && 392 git add elif && 393 git commit -m "Partial: add elif" elif && 394 git diff-tree --name-status HEAD^ HEAD >current && 395 echo "A elif" >expected && 396 test_cmp expected current 397 398 ' 399 400 test_expect_success 'partial commit that involves removal (2)' ' 401 402 git commit -m "Partial: remove file" file && 403 git diff-tree --name-status HEAD^ HEAD >current && 404 echo "D file" >expected && 405 test_cmp expected current 406 407 ' 408 409 test_expect_success 'partial commit that involves removal (3)' ' 410 411 git rm --cached elif && 412 echo elif >elif && 413 git commit -m "Partial: modify elif" elif && 414 git diff-tree --name-status HEAD^ HEAD >current && 415 echo "M elif" >expected && 416 test_cmp expected current 417 418 ' 419 420 test_expect_success 'amend commit to fix author' ' 421 422 oldtick=$GIT_AUTHOR_DATE && 423 test_tick && 424 git reset --hard && 425 git cat-file -p HEAD >commit && 426 sed -e "s/author.*/author $author $oldtick/" \ 427 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \ 428 commit >expected && 429 git commit --amend --author="$author" && 430 git cat-file -p HEAD >current && 431 test_cmp expected current 432 433 ' 434 435 test_expect_success 'amend commit to fix date' ' 436 437 test_tick && 438 newtick=$GIT_AUTHOR_DATE && 439 git reset --hard && 440 git cat-file -p HEAD >commit && 441 sed -e "s/author.*/author $author $newtick/" \ 442 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \ 443 commit >expected && 444 git commit --amend --date="$newtick" && 445 git cat-file -p HEAD >current && 446 test_cmp expected current 447 448 ' 449 450 test_expect_success 'amend commit to add signoff' ' 451 452 test_commit "msg" file content && 453 git commit --amend --signoff && 454 test_commit_message HEAD <<-EOF 455 msg 456 457 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 458 EOF 459 ' 460 461 test_expect_success 'amend does not add signoff if it already exists' ' 462 463 test_commit --signoff "tenor" file newcontent && 464 git commit --amend --signoff && 465 test_commit_message HEAD <<-EOF 466 tenor 467 468 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 469 EOF 470 ' 471 472 test_expect_success 'commit mentions forced date in output' ' 473 git commit --amend --date=2010-01-02T03:04:05 >output && 474 test_grep "Date: *Sat Jan 2 03:04:05 2010" output 475 ' 476 477 test_expect_success 'commit complains about completely bogus dates' ' 478 test_must_fail git commit --amend --date=seventeen 479 ' 480 481 test_expect_success 'commit --date allows approxidate' ' 482 git commit --amend \ 483 --date="midnight the 12th of october, anno domini 1979" && 484 echo "Fri Oct 12 00:00:00 1979 +0000" >expect && 485 git log -1 --format=%ad >actual && 486 test_cmp expect actual 487 ' 488 489 test_expect_success 'sign off (1)' ' 490 491 echo 1 >positive && 492 git add positive && 493 git commit -s -m "thank you" && 494 commit_body HEAD >actual && 495 ( 496 echo thank you && 497 echo && 498 git var GIT_COMMITTER_IDENT >ident && 499 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 500 ) >expected && 501 test_cmp expected actual 502 503 ' 504 505 test_expect_success 'sign off (2)' ' 506 507 echo 2 >positive && 508 git add positive && 509 existing="Signed-off-by: Watch This " && 510 git commit -s -m "thank you 511 512 $existing" && 513 commit_body HEAD >actual && 514 ( 515 echo thank you && 516 echo && 517 echo $existing && 518 git var GIT_COMMITTER_IDENT >ident && 519 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 520 ) >expected && 521 test_cmp expected actual 522 523 ' 524 525 test_expect_success 'signoff gap' ' 526 527 echo 3 >positive && 528 git add positive && 529 alt="Alt-RFC-822-Header: Value" && 530 git commit -s -m "welcome 531 532 $alt" && 533 commit_body HEAD >actual && 534 ( 535 echo welcome && 536 echo && 537 echo $alt && 538 git var GIT_COMMITTER_IDENT >ident && 539 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 540 ) >expected && 541 test_cmp expected actual 542 ' 543 544 test_expect_success 'signoff gap 2' ' 545 546 echo 4 >positive && 547 git add positive && 548 alt="fixed: 34" && 549 git commit -s -m "welcome 550 551 We have now 552 $alt" && 553 commit_body HEAD >actual && 554 ( 555 echo welcome && 556 echo && 557 echo We have now && 558 echo $alt && 559 echo && 560 git var GIT_COMMITTER_IDENT >ident && 561 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 562 ) >expected && 563 test_cmp expected actual 564 ' 565 566 test_expect_success 'signoff respects trailer config' ' 567 568 echo 5 >positive && 569 git add positive && 570 git commit -s -m "subject 571 572 non-trailer line 573 Myfooter: x" && 574 commit_body HEAD >actual && 575 ( 576 echo subject && 577 echo && 578 echo non-trailer line && 579 echo Myfooter: x && 580 echo && 581 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" 582 ) >expected && 583 test_cmp expected actual && 584 585 echo 6 >positive && 586 git add positive && 587 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject 588 589 non-trailer line 590 Myfooter: x" && 591 commit_body HEAD >actual && 592 ( 593 echo subject && 594 echo && 595 echo non-trailer line && 596 echo Myfooter: x && 597 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" 598 ) >expected && 599 test_cmp expected actual 600 ' 601 602 test_expect_success 'signoff not confused by ---' ' 603 cat >expected <<-EOF && 604 subject 605 606 body 607 --- 608 these dashes confuse the parser! 609 610 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 611 EOF 612 # should be a noop, since we already signed 613 git commit --allow-empty --signoff -F expected && 614 git log -1 --pretty=format:%B >actual && 615 test_cmp expected actual 616 ' 617 618 test_expect_success 'multiple -m' ' 619 620 >negative && 621 git add negative && 622 git commit -m "one" -m "two" -m "three" && 623 commit_body HEAD >actual && 624 ( 625 echo one && 626 echo && 627 echo two && 628 echo && 629 echo three 630 ) >expected && 631 test_cmp expected actual 632 633 ' 634 635 test_expect_success 'amend commit to fix author' ' 636 637 oldtick=$GIT_AUTHOR_DATE && 638 test_tick && 639 git reset --hard && 640 git cat-file -p HEAD >commit && 641 sed -e "s/author.*/author $author $oldtick/" \ 642 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \ 643 commit >expected && 644 git commit --amend --author="$author" && 645 git cat-file -p HEAD >current && 646 test_cmp expected current 647 648 ' 649 650 test_expect_success 'git commit with dirty index' ' 651 echo tacocat >elif && 652 echo tehlulz >chz && 653 git add chz && 654 git commit elif -m "tacocat is a palindrome" && 655 git show --stat >stat && 656 test_grep elif stat && 657 git diff --cached >diff && 658 test_grep chz diff 659 ' 660 661 test_expect_success 'same tree (single parent)' ' 662 663 git reset --hard && 664 test_must_fail git commit -m empty 665 666 ' 667 668 test_expect_success 'same tree (single parent) --allow-empty' ' 669 670 git commit --allow-empty -m "forced empty" && 671 git cat-file commit HEAD >commit && 672 test_grep forced commit 673 674 ' 675 676 test_expect_success 'same tree (merge and amend merge)' ' 677 678 git checkout -b side HEAD^ && 679 echo zero >zero && 680 git add zero && 681 git commit -m "add zero" && 682 git checkout main && 683 684 git merge -s ours side -m "empty ok" && 685 git diff HEAD^ HEAD >actual && 686 test_must_be_empty actual && 687 688 git commit --amend -m "empty really ok" && 689 git diff HEAD^ HEAD >actual && 690 test_must_be_empty actual 691 692 ' 693 694 test_expect_success 'amend using the message from another commit' ' 695 696 git reset --hard && 697 test_tick && 698 git commit --allow-empty -m "old commit" && 699 old=$(git rev-parse --verify HEAD) && 700 test_tick && 701 git commit --allow-empty -m "new commit" && 702 new=$(git rev-parse --verify HEAD) && 703 test_tick && 704 git commit --allow-empty --amend -C "$old" && 705 git show --pretty="format:%ad %s" "$old" >expected && 706 git show --pretty="format:%ad %s" HEAD >actual && 707 test_cmp expected actual 708 709 ' 710 711 test_expect_success 'amend using the message from a commit named with tag' ' 712 713 git reset --hard && 714 test_tick && 715 git commit --allow-empty -m "old commit" && 716 old=$(git rev-parse --verify HEAD) && 717 git tag -a -m "tag on old" tagged-old HEAD && 718 test_tick && 719 git commit --allow-empty -m "new commit" && 720 new=$(git rev-parse --verify HEAD) && 721 test_tick && 722 git commit --allow-empty --amend -C tagged-old && 723 git show --pretty="format:%ad %s" "$old" >expected && 724 git show --pretty="format:%ad %s" HEAD >actual && 725 test_cmp expected actual 726 727 ' 728 729 test_expect_success 'amend can copy notes' ' 730 731 git config notes.rewrite.amend true && 732 git config notes.rewriteRef "refs/notes/*" && 733 test_commit foo && 734 git notes add -m"a note" && 735 test_tick && 736 git commit --amend -m"new foo" && 737 test "$(git notes show)" = "a note" 738 739 ' 740 741 test_expect_success 'commit a file whose name is a dash' ' 742 git reset --hard && 743 test_write_lines 1 2 3 4 5 >./- && 744 git add ./- && 745 test_tick && 746 git commit -m "add dash" >output newfile && 756 git add newfile && 757 git commit --only newfile -m"--only on unborn branch" && 758 echo newfile >expected && 759 git ls-tree -r --name-only HEAD >actual && 760 test_cmp expected actual 761 ' 762 763 test_expect_success '--dry-run with conflicts fixed from a merge' ' 764 # setup two branches with conflicting information 765 # in the same file, resolve the conflict, 766 # call commit with --dry-run 767 echo "Initial contents, unimportant" >test-file && 768 git add test-file && 769 git commit -m "Initial commit" && 770 echo "commit-1-state" >test-file && 771 git commit -m "commit 1" -i test-file && 772 git tag commit-1 && 773 git checkout -b branch-2 HEAD^1 && 774 echo "commit-2-state" >test-file && 775 git commit -m "commit 2" -i test-file && 776 test_must_fail git merge --no-commit commit-1 && 777 echo "commit-2-state" >test-file && 778 git add test-file && 779 git commit --dry-run && 780 git commit -m "conflicts fixed from merge." 781 ' 782 783 test_expect_success '--dry-run --short' ' 784 >test-file && 785 git add test-file && 786 git commit --dry-run --short 787 ' 788 789 test_done