t / t7501-commit.shon commit Merge branch 'wk/doc-pre-rebase' (f944ec9)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
   4#
   5
   6# FIXME: Test the various index usages, -i and -o, test reflog,
   7# signoff
   8
   9test_description='git commit'
  10. ./test-lib.sh
  11. "$TEST_DIRECTORY/diff-lib.sh"
  12
  13author='The Real Author <someguy@his.email.org>'
  14
  15test_tick
  16
  17test_expect_success 'initial status' '
  18        echo bongo bongo >file &&
  19        git add file &&
  20        git status >actual &&
  21        test_i18ngrep "Initial commit" actual
  22'
  23
  24test_expect_success 'fail initial amend' '
  25        test_must_fail git commit --amend
  26'
  27
  28test_expect_success 'setup: initial commit' '
  29        git commit -m initial
  30'
  31
  32test_expect_success '-m and -F do not mix' '
  33        git checkout HEAD file && echo >>file && git add file &&
  34        test_must_fail git commit -m foo -m bar -F file
  35'
  36
  37test_expect_success '-m and -C do not mix' '
  38        git checkout HEAD file && echo >>file && git add file &&
  39        test_must_fail git commit -C HEAD -m illegal
  40'
  41
  42test_expect_success 'paths and -a do not mix' '
  43        echo King of the bongo >file &&
  44        test_must_fail git commit -m foo -a file
  45'
  46
  47test_expect_success PERL 'can use paths with --interactive' '
  48        echo bong-o-bong >file &&
  49        # 2: update, 1:st path, that is all, 7: quit
  50        ( echo 2; echo 1; echo; echo 7 ) |
  51        git commit -m foo --interactive file &&
  52        git reset --hard HEAD^
  53'
  54
  55test_expect_success 'using invalid commit with -C' '
  56        test_must_fail git commit -C bogus
  57'
  58
  59test_expect_success 'nothing to commit' '
  60        test_must_fail git commit -m initial
  61'
  62
  63test_expect_success 'setup: non-initial commit' '
  64        echo bongo bongo bongo >file &&
  65        git commit -m next -a
  66'
  67
  68test_expect_success 'commit message from non-existing file' '
  69        echo more bongo: bongo bongo bongo bongo >file &&
  70        test_must_fail git commit -F gah -a
  71'
  72
  73test_expect_success 'empty commit message' '
  74        # Empty except stray tabs and spaces on a few lines.
  75        sed -e "s/@//g" >msg <<-\EOF &&
  76                @               @
  77                @@
  78                @  @
  79                @Signed-off-by: hula@
  80        EOF
  81        test_must_fail git commit -F msg -a
  82'
  83
  84test_expect_success 'template "emptyness" check does not kick in with -F' '
  85        git checkout HEAD file && echo >>file && git add file &&
  86        git commit -t file -F file
  87'
  88
  89test_expect_success 'template "emptyness" check' '
  90        git checkout HEAD file && echo >>file && git add file &&
  91        test_must_fail git commit -t file 2>err &&
  92        test_i18ngrep "did not edit" err
  93'
  94
  95test_expect_success 'setup: commit message from file' '
  96        git checkout HEAD file && echo >>file && git add file &&
  97        echo this is the commit message, coming from a file >msg &&
  98        git commit -F msg -a
  99'
 100
 101test_expect_success 'amend commit' '
 102        cat >editor <<-\EOF &&
 103        #!/bin/sh
 104        sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
 105        mv "$1-" "$1"
 106        EOF
 107        chmod 755 editor &&
 108        EDITOR=./editor git commit --amend
 109'
 110
 111test_expect_success 'amend --only ignores staged contents' '
 112        cp file file.expect &&
 113        echo changed >file &&
 114        git add file &&
 115        git commit --no-edit --amend --only &&
 116        git cat-file blob HEAD:file >file.actual &&
 117        test_cmp file.expect file.actual &&
 118        git diff --exit-code
 119'
 120
 121test_expect_success 'set up editor' '
 122        cat >editor <<-\EOF &&
 123        #!/bin/sh
 124        sed -e "s/unamended/amended/g" <"$1" >"$1-"
 125        mv "$1-" "$1"
 126        EOF
 127        chmod 755 editor
 128'
 129
 130test_expect_success 'amend without launching editor' '
 131        echo unamended >expect &&
 132        git commit --allow-empty -m "unamended" &&
 133        echo needs more bongo >file &&
 134        git add file &&
 135        EDITOR=./editor git commit --no-edit --amend &&
 136        git diff --exit-code HEAD -- file &&
 137        git diff-tree -s --format=%s HEAD >msg &&
 138        test_cmp expect msg
 139'
 140
 141test_expect_success '--amend --edit' '
 142        echo amended >expect &&
 143        git commit --allow-empty -m "unamended" &&
 144        echo bongo again >file &&
 145        git add file &&
 146        EDITOR=./editor git commit --edit --amend &&
 147        git diff-tree -s --format=%s HEAD >msg &&
 148        test_cmp expect msg
 149'
 150
 151test_expect_success '--amend --edit of empty message' '
 152        cat >replace <<-\EOF &&
 153        #!/bin/sh
 154        echo "amended" >"$1"
 155        EOF
 156        chmod 755 replace &&
 157        git commit --allow-empty --allow-empty-message -m "" &&
 158        echo more bongo >file &&
 159        git add file &&
 160        EDITOR=./replace git commit --edit --amend &&
 161        git diff-tree -s --format=%s HEAD >msg &&
 162        ./replace expect &&
 163        test_cmp expect msg
 164'
 165
 166test_expect_success '-m --edit' '
 167        echo amended >expect &&
 168        git commit --allow-empty -m buffer &&
 169        echo bongo bongo >file &&
 170        git add file &&
 171        EDITOR=./editor git commit -m unamended --edit &&
 172        git diff-tree -s  --format=%s HEAD >msg &&
 173        test_cmp expect msg
 174'
 175
 176test_expect_success '-m and -F do not mix' '
 177        echo enough with the bongos >file &&
 178        test_must_fail git commit -F msg -m amending .
 179'
 180
 181test_expect_success 'using message from other commit' '
 182        git commit -C HEAD^ .
 183'
 184
 185test_expect_success 'editing message from other commit' '
 186        cat >editor <<-\EOF &&
 187        #!/bin/sh
 188        sed -e "s/amend/older/g"  < "$1" > "$1-"
 189        mv "$1-" "$1"
 190        EOF
 191        chmod 755 editor &&
 192        echo hula hula >file &&
 193        EDITOR=./editor git commit -c HEAD^ -a
 194'
 195
 196test_expect_success 'message from stdin' '
 197        echo silly new contents >file &&
 198        echo commit message from stdin |
 199        git commit -F - -a
 200'
 201
 202test_expect_success 'overriding author from command line' '
 203        echo gak >file &&
 204        git commit -m author \
 205                --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
 206        grep Rubber.Duck output
 207'
 208
 209test_expect_success PERL 'interactive add' '
 210        echo 7 |
 211        git commit --interactive |
 212        grep "What now"
 213'
 214
 215test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
 216        echo zoo >file &&
 217        test_must_fail git diff --exit-code >diff1 &&
 218        (echo u ; echo "*" ; echo q) |
 219        (
 220                EDITOR=: &&
 221                export EDITOR &&
 222                test_must_fail git commit --interactive
 223        ) &&
 224        git diff >diff2 &&
 225        compare_diff_patch diff1 diff2
 226'
 227
 228test_expect_success 'editor not invoked if -F is given' '
 229        cat >editor <<-\EOF &&
 230        #!/bin/sh
 231        sed -e s/good/bad/g <"$1" >"$1-"
 232        mv "$1-" "$1"
 233        EOF
 234        chmod 755 editor &&
 235
 236        echo A good commit message. >msg &&
 237        echo moo >file &&
 238
 239        EDITOR=./editor git commit -a -F msg &&
 240        git show -s --pretty=format:%s >subject &&
 241        grep -q good subject &&
 242
 243        echo quack >file &&
 244        echo Another good message. |
 245        EDITOR=./editor git commit -a -F - &&
 246        git show -s --pretty=format:%s >subject &&
 247        grep -q good subject
 248'
 249
 250test_expect_success 'partial commit that involves removal (1)' '
 251
 252        git rm --cached file &&
 253        mv file elif &&
 254        git add elif &&
 255        git commit -m "Partial: add elif" elif &&
 256        git diff-tree --name-status HEAD^ HEAD >current &&
 257        echo "A elif" >expected &&
 258        test_cmp expected current
 259
 260'
 261
 262test_expect_success 'partial commit that involves removal (2)' '
 263
 264        git commit -m "Partial: remove file" file &&
 265        git diff-tree --name-status HEAD^ HEAD >current &&
 266        echo "D file" >expected &&
 267        test_cmp expected current
 268
 269'
 270
 271test_expect_success 'partial commit that involves removal (3)' '
 272
 273        git rm --cached elif &&
 274        echo elif >elif &&
 275        git commit -m "Partial: modify elif" elif &&
 276        git diff-tree --name-status HEAD^ HEAD >current &&
 277        echo "M elif" >expected &&
 278        test_cmp expected current
 279
 280'
 281
 282test_expect_success 'amend commit to fix author' '
 283
 284        oldtick=$GIT_AUTHOR_DATE &&
 285        test_tick &&
 286        git reset --hard &&
 287        git cat-file -p HEAD |
 288        sed -e "s/author.*/author $author $oldtick/" \
 289                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 290                expected &&
 291        git commit --amend --author="$author" &&
 292        git cat-file -p HEAD > current &&
 293        test_cmp expected current
 294
 295'
 296
 297test_expect_success 'amend commit to fix date' '
 298
 299        test_tick &&
 300        newtick=$GIT_AUTHOR_DATE &&
 301        git reset --hard &&
 302        git cat-file -p HEAD |
 303        sed -e "s/author.*/author $author $newtick/" \
 304                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 305                expected &&
 306        git commit --amend --date="$newtick" &&
 307        git cat-file -p HEAD > current &&
 308        test_cmp expected current
 309
 310'
 311
 312test_expect_success 'commit complains about bogus date' '
 313        test_must_fail git commit --amend --date=10.11.2010
 314'
 315
 316test_expect_success 'sign off (1)' '
 317
 318        echo 1 >positive &&
 319        git add positive &&
 320        git commit -s -m "thank you" &&
 321        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 322        (
 323                echo thank you
 324                echo
 325                git var GIT_COMMITTER_IDENT |
 326                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 327        ) >expected &&
 328        test_cmp expected actual
 329
 330'
 331
 332test_expect_success 'sign off (2)' '
 333
 334        echo 2 >positive &&
 335        git add positive &&
 336        existing="Signed-off-by: Watch This <watchthis@example.com>" &&
 337        git commit -s -m "thank you
 338
 339$existing" &&
 340        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 341        (
 342                echo thank you
 343                echo
 344                echo $existing
 345                git var GIT_COMMITTER_IDENT |
 346                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 347        ) >expected &&
 348        test_cmp expected actual
 349
 350'
 351
 352test_expect_success 'signoff gap' '
 353
 354        echo 3 >positive &&
 355        git add positive &&
 356        alt="Alt-RFC-822-Header: Value" &&
 357        git commit -s -m "welcome
 358
 359$alt" &&
 360        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 361        (
 362                echo welcome
 363                echo
 364                echo $alt
 365                git var GIT_COMMITTER_IDENT |
 366                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 367        ) >expected &&
 368        test_cmp expected actual
 369'
 370
 371test_expect_success 'signoff gap 2' '
 372
 373        echo 4 >positive &&
 374        git add positive &&
 375        alt="fixed: 34" &&
 376        git commit -s -m "welcome
 377
 378We have now
 379$alt" &&
 380        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 381        (
 382                echo welcome
 383                echo
 384                echo We have now
 385                echo $alt
 386                echo
 387                git var GIT_COMMITTER_IDENT |
 388                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 389        ) >expected &&
 390        test_cmp expected actual
 391'
 392
 393test_expect_success 'multiple -m' '
 394
 395        >negative &&
 396        git add negative &&
 397        git commit -m "one" -m "two" -m "three" &&
 398        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 399        (
 400                echo one
 401                echo
 402                echo two
 403                echo
 404                echo three
 405        ) >expected &&
 406        test_cmp expected actual
 407
 408'
 409
 410test_expect_success 'amend commit to fix author' '
 411
 412        oldtick=$GIT_AUTHOR_DATE &&
 413        test_tick &&
 414        git reset --hard &&
 415        git cat-file -p HEAD |
 416        sed -e "s/author.*/author $author $oldtick/" \
 417                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 418                expected &&
 419        git commit --amend --author="$author" &&
 420        git cat-file -p HEAD > current &&
 421        test_cmp expected current
 422
 423'
 424
 425test_expect_success 'git commit <file> with dirty index' '
 426        echo tacocat > elif &&
 427        echo tehlulz > chz &&
 428        git add chz &&
 429        git commit elif -m "tacocat is a palindrome" &&
 430        git show --stat | grep elif &&
 431        git diff --cached | grep chz
 432'
 433
 434test_expect_success 'same tree (single parent)' '
 435
 436        git reset --hard &&
 437        test_must_fail git commit -m empty
 438
 439'
 440
 441test_expect_success 'same tree (single parent) --allow-empty' '
 442
 443        git commit --allow-empty -m "forced empty" &&
 444        git cat-file commit HEAD | grep forced
 445
 446'
 447
 448test_expect_success 'same tree (merge and amend merge)' '
 449
 450        git checkout -b side HEAD^ &&
 451        echo zero >zero &&
 452        git add zero &&
 453        git commit -m "add zero" &&
 454        git checkout master &&
 455
 456        git merge -s ours side -m "empty ok" &&
 457        git diff HEAD^ HEAD >actual &&
 458        : >expected &&
 459        test_cmp expected actual &&
 460
 461        git commit --amend -m "empty really ok" &&
 462        git diff HEAD^ HEAD >actual &&
 463        : >expected &&
 464        test_cmp expected actual
 465
 466'
 467
 468test_expect_success 'amend using the message from another commit' '
 469
 470        git reset --hard &&
 471        test_tick &&
 472        git commit --allow-empty -m "old commit" &&
 473        old=$(git rev-parse --verify HEAD) &&
 474        test_tick &&
 475        git commit --allow-empty -m "new commit" &&
 476        new=$(git rev-parse --verify HEAD) &&
 477        test_tick &&
 478        git commit --allow-empty --amend -C "$old" &&
 479        git show --pretty="format:%ad %s" "$old" >expected &&
 480        git show --pretty="format:%ad %s" HEAD >actual &&
 481        test_cmp expected actual
 482
 483'
 484
 485test_expect_success 'amend using the message from a commit named with tag' '
 486
 487        git reset --hard &&
 488        test_tick &&
 489        git commit --allow-empty -m "old commit" &&
 490        old=$(git rev-parse --verify HEAD) &&
 491        git tag -a -m "tag on old" tagged-old HEAD &&
 492        test_tick &&
 493        git commit --allow-empty -m "new commit" &&
 494        new=$(git rev-parse --verify HEAD) &&
 495        test_tick &&
 496        git commit --allow-empty --amend -C tagged-old &&
 497        git show --pretty="format:%ad %s" "$old" >expected &&
 498        git show --pretty="format:%ad %s" HEAD >actual &&
 499        test_cmp expected actual
 500
 501'
 502
 503test_expect_success 'amend can copy notes' '
 504
 505        git config notes.rewrite.amend true &&
 506        git config notes.rewriteRef "refs/notes/*" &&
 507        test_commit foo &&
 508        git notes add -m"a note" &&
 509        test_tick &&
 510        git commit --amend -m"new foo" &&
 511        test "$(git notes show)" = "a note"
 512
 513'
 514
 515test_expect_success 'commit a file whose name is a dash' '
 516        git reset --hard &&
 517        for i in 1 2 3 4 5
 518        do
 519                echo $i
 520        done >./- &&
 521        git add ./- &&
 522        test_tick &&
 523        git commit -m "add dash" >output </dev/null &&
 524        test_i18ngrep " changed, 5 insertions" output
 525'
 526
 527test_done