Git fork
at reftables-rust 796 lines 20 kB view raw
1#!/bin/sh 2# 3# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com> 4# 5 6# FIXME: Test the various index usages, test reflog 7 8test_description='git commit' 9GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 10export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 11 12. ./test-lib.sh 13. "$TEST_DIRECTORY/lib-diff.sh" 14 15author='The Real Author <someguy@his.email.org>' 16 17test_tick 18 19test_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 26test_expect_success 'fail initial amend' ' 27 test_must_fail git commit --amend 28' 29 30test_expect_success 'setup: initial commit' ' 31 git commit -m initial 32' 33 34test_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 39test_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 44test_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 49test_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 57test_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 69test_expect_success 'using invalid commit with -C' ' 70 test_must_fail git commit --allow-empty -C bogus 71' 72 73test_expect_success 'nothing to commit' ' 74 git reset --hard && 75 test_must_fail git commit -m initial 76' 77 78test_expect_success '--dry-run fails with nothing to commit' ' 79 test_must_fail git commit -m initial --dry-run 80' 81 82test_expect_success '--short fails with nothing to commit' ' 83 test_must_fail git commit -m initial --short 84' 85 86test_expect_success '--porcelain fails with nothing to commit' ' 87 test_must_fail git commit -m initial --porcelain 88' 89 90test_expect_success '--long fails with nothing to commit' ' 91 test_must_fail git commit -m initial --long 92' 93 94test_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 108test_expect_success 'setup: non-initial commit' ' 109 echo bongo bongo bongo >file && 110 git commit -m next -a 111' 112 113test_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 118test_expect_success '--short with stuff to commit returns ok' ' 119 echo bongo bongo bongo >>file && 120 git commit -m next -a --short 121' 122 123test_expect_success '--porcelain with stuff to commit returns ok' ' 124 echo bongo bongo bongo >>file && 125 git commit -m next -a --porcelain 126' 127 128test_expect_success '--long with stuff to commit returns ok' ' 129 echo bongo bongo bongo >>file && 130 git commit -m next -a --long 131' 132 133for opt in "" "-o" "--only" 134do 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 ' 152done 153 154test_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 170test_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 178test_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 183test_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 194test_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 199test_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 205test_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 211test_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 221test_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 231test_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 240test_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 249test_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 260test_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 270test_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 285test_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 295test_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 305test_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 315test_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 320test_expect_success 'using message from other commit' ' 321 git commit -C HEAD^ . 322' 323 324test_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 335test_expect_success 'message from stdin' ' 336 echo silly new contents >file && 337 echo commit message from stdin | 338 git commit -F - -a 339' 340 341test_expect_success 'overriding author from command line' ' 342 echo gak >file && 343 git commit -m author \ 344 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 && 345 grep Rubber.Duck output 346' 347 348test_expect_success 'interactive add' ' 349 echo 7 | test_must_fail git commit --interactive >out && 350 grep "What now" out 351' 352 353test_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 366test_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 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 grep -q good subject 386' 387 388test_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 400test_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 409test_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 420test_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 435test_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 450test_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 461test_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 472test_expect_success 'commit mentions forced date in output' ' 473 git commit --amend --date=2010-01-02T03:04:05 >output && 474 grep "Date: *Sat Jan 2 03:04:05 2010" output 475' 476 477test_expect_success 'commit complains about completely bogus dates' ' 478 test_must_fail git commit --amend --date=seventeen 479' 480 481test_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 489test_expect_success 'sign off (1)' ' 490 491 echo 1 >positive && 492 git add positive && 493 git commit -s -m "thank you" && 494 git cat-file commit HEAD >commit && 495 sed -e "1,/^\$/d" commit >actual && 496 ( 497 echo thank you && 498 echo && 499 git var GIT_COMMITTER_IDENT >ident && 500 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 501 ) >expected && 502 test_cmp expected actual 503 504' 505 506test_expect_success 'sign off (2)' ' 507 508 echo 2 >positive && 509 git add positive && 510 existing="Signed-off-by: Watch This <watchthis@example.com>" && 511 git commit -s -m "thank you 512 513$existing" && 514 git cat-file commit HEAD >commit && 515 sed -e "1,/^\$/d" commit >actual && 516 ( 517 echo thank you && 518 echo && 519 echo $existing && 520 git var GIT_COMMITTER_IDENT >ident && 521 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 522 ) >expected && 523 test_cmp expected actual 524 525' 526 527test_expect_success 'signoff gap' ' 528 529 echo 3 >positive && 530 git add positive && 531 alt="Alt-RFC-822-Header: Value" && 532 git commit -s -m "welcome 533 534$alt" && 535 git cat-file commit HEAD >commit && 536 sed -e "1,/^\$/d" commit >actual && 537 ( 538 echo welcome && 539 echo && 540 echo $alt && 541 git var GIT_COMMITTER_IDENT >ident && 542 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 543 ) >expected && 544 test_cmp expected actual 545' 546 547test_expect_success 'signoff gap 2' ' 548 549 echo 4 >positive && 550 git add positive && 551 alt="fixed: 34" && 552 git commit -s -m "welcome 553 554We have now 555$alt" && 556 git cat-file commit HEAD >commit && 557 sed -e "1,/^\$/d" commit >actual && 558 ( 559 echo welcome && 560 echo && 561 echo We have now && 562 echo $alt && 563 echo && 564 git var GIT_COMMITTER_IDENT >ident && 565 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident 566 ) >expected && 567 test_cmp expected actual 568' 569 570test_expect_success 'signoff respects trailer config' ' 571 572 echo 5 >positive && 573 git add positive && 574 git commit -s -m "subject 575 576non-trailer line 577Myfooter: x" && 578 git cat-file commit HEAD >commit && 579 sed -e "1,/^\$/d" commit >actual && 580 ( 581 echo subject && 582 echo && 583 echo non-trailer line && 584 echo Myfooter: x && 585 echo && 586 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" 587 ) >expected && 588 test_cmp expected actual && 589 590 echo 6 >positive && 591 git add positive && 592 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject 593 594non-trailer line 595Myfooter: x" && 596 git cat-file commit HEAD >commit && 597 sed -e "1,/^\$/d" commit >actual && 598 ( 599 echo subject && 600 echo && 601 echo non-trailer line && 602 echo Myfooter: x && 603 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" 604 ) >expected && 605 test_cmp expected actual 606' 607 608test_expect_success 'signoff not confused by ---' ' 609 cat >expected <<-EOF && 610 subject 611 612 body 613 --- 614 these dashes confuse the parser! 615 616 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 617 EOF 618 # should be a noop, since we already signed 619 git commit --allow-empty --signoff -F expected && 620 git log -1 --pretty=format:%B >actual && 621 test_cmp expected actual 622' 623 624test_expect_success 'multiple -m' ' 625 626 >negative && 627 git add negative && 628 git commit -m "one" -m "two" -m "three" && 629 git cat-file commit HEAD >commit && 630 sed -e "1,/^\$/d" commit >actual && 631 ( 632 echo one && 633 echo && 634 echo two && 635 echo && 636 echo three 637 ) >expected && 638 test_cmp expected actual 639 640' 641 642test_expect_success 'amend commit to fix author' ' 643 644 oldtick=$GIT_AUTHOR_DATE && 645 test_tick && 646 git reset --hard && 647 git cat-file -p HEAD >commit && 648 sed -e "s/author.*/author $author $oldtick/" \ 649 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \ 650 commit >expected && 651 git commit --amend --author="$author" && 652 git cat-file -p HEAD >current && 653 test_cmp expected current 654 655' 656 657test_expect_success 'git commit <file> with dirty index' ' 658 echo tacocat >elif && 659 echo tehlulz >chz && 660 git add chz && 661 git commit elif -m "tacocat is a palindrome" && 662 git show --stat >stat && 663 grep elif stat && 664 git diff --cached >diff && 665 grep chz diff 666' 667 668test_expect_success 'same tree (single parent)' ' 669 670 git reset --hard && 671 test_must_fail git commit -m empty 672 673' 674 675test_expect_success 'same tree (single parent) --allow-empty' ' 676 677 git commit --allow-empty -m "forced empty" && 678 git cat-file commit HEAD >commit && 679 grep forced commit 680 681' 682 683test_expect_success 'same tree (merge and amend merge)' ' 684 685 git checkout -b side HEAD^ && 686 echo zero >zero && 687 git add zero && 688 git commit -m "add zero" && 689 git checkout main && 690 691 git merge -s ours side -m "empty ok" && 692 git diff HEAD^ HEAD >actual && 693 test_must_be_empty actual && 694 695 git commit --amend -m "empty really ok" && 696 git diff HEAD^ HEAD >actual && 697 test_must_be_empty actual 698 699' 700 701test_expect_success 'amend using the message from another commit' ' 702 703 git reset --hard && 704 test_tick && 705 git commit --allow-empty -m "old commit" && 706 old=$(git rev-parse --verify HEAD) && 707 test_tick && 708 git commit --allow-empty -m "new commit" && 709 new=$(git rev-parse --verify HEAD) && 710 test_tick && 711 git commit --allow-empty --amend -C "$old" && 712 git show --pretty="format:%ad %s" "$old" >expected && 713 git show --pretty="format:%ad %s" HEAD >actual && 714 test_cmp expected actual 715 716' 717 718test_expect_success 'amend using the message from a commit named with tag' ' 719 720 git reset --hard && 721 test_tick && 722 git commit --allow-empty -m "old commit" && 723 old=$(git rev-parse --verify HEAD) && 724 git tag -a -m "tag on old" tagged-old HEAD && 725 test_tick && 726 git commit --allow-empty -m "new commit" && 727 new=$(git rev-parse --verify HEAD) && 728 test_tick && 729 git commit --allow-empty --amend -C tagged-old && 730 git show --pretty="format:%ad %s" "$old" >expected && 731 git show --pretty="format:%ad %s" HEAD >actual && 732 test_cmp expected actual 733 734' 735 736test_expect_success 'amend can copy notes' ' 737 738 git config notes.rewrite.amend true && 739 git config notes.rewriteRef "refs/notes/*" && 740 test_commit foo && 741 git notes add -m"a note" && 742 test_tick && 743 git commit --amend -m"new foo" && 744 test "$(git notes show)" = "a note" 745 746' 747 748test_expect_success 'commit a file whose name is a dash' ' 749 git reset --hard && 750 test_write_lines 1 2 3 4 5 >./- && 751 git add ./- && 752 test_tick && 753 git commit -m "add dash" >output </dev/null && 754 test_grep " changed, 5 insertions" output 755' 756 757test_expect_success '--only works on to-be-born branch' ' 758 # This test relies on having something in the index, as it 759 # would not otherwise actually prove much. So check this. 760 test -n "$(git ls-files)" && 761 git checkout --orphan orphan && 762 echo foo >newfile && 763 git add newfile && 764 git commit --only newfile -m"--only on unborn branch" && 765 echo newfile >expected && 766 git ls-tree -r --name-only HEAD >actual && 767 test_cmp expected actual 768' 769 770test_expect_success '--dry-run with conflicts fixed from a merge' ' 771 # setup two branches with conflicting information 772 # in the same file, resolve the conflict, 773 # call commit with --dry-run 774 echo "Initial contents, unimportant" >test-file && 775 git add test-file && 776 git commit -m "Initial commit" && 777 echo "commit-1-state" >test-file && 778 git commit -m "commit 1" -i test-file && 779 git tag commit-1 && 780 git checkout -b branch-2 HEAD^1 && 781 echo "commit-2-state" >test-file && 782 git commit -m "commit 2" -i test-file && 783 test_must_fail git merge --no-commit commit-1 && 784 echo "commit-2-state" >test-file && 785 git add test-file && 786 git commit --dry-run && 787 git commit -m "conflicts fixed from merge." 788' 789 790test_expect_success '--dry-run --short' ' 791 >test-file && 792 git add test-file && 793 git commit --dry-run --short 794' 795 796test_done