t / t7501-commit.shon commit Merge branch 'fc/git-complete-helper' into fc/git-prompt-script (423b5a1)
   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 'set up editor' '
 112        cat >editor <<-\EOF &&
 113        #!/bin/sh
 114        sed -e "s/unamended/amended/g" <"$1" >"$1-"
 115        mv "$1-" "$1"
 116        EOF
 117        chmod 755 editor
 118'
 119
 120test_expect_success 'amend without launching editor' '
 121        echo unamended >expect &&
 122        git commit --allow-empty -m "unamended" &&
 123        echo needs more bongo >file &&
 124        git add file &&
 125        EDITOR=./editor git commit --no-edit --amend &&
 126        git diff --exit-code HEAD -- file &&
 127        git diff-tree -s --format=%s HEAD >msg &&
 128        test_cmp expect msg
 129'
 130
 131test_expect_success '--amend --edit' '
 132        echo amended >expect &&
 133        git commit --allow-empty -m "unamended" &&
 134        echo bongo again >file &&
 135        git add file &&
 136        EDITOR=./editor git commit --edit --amend &&
 137        git diff-tree -s --format=%s HEAD >msg &&
 138        test_cmp expect msg
 139'
 140
 141test_expect_success '-m --edit' '
 142        echo amended >expect &&
 143        git commit --allow-empty -m buffer &&
 144        echo bongo bongo >file &&
 145        git add file &&
 146        EDITOR=./editor git commit -m unamended --edit &&
 147        git diff-tree -s  --format=%s HEAD >msg &&
 148        test_cmp expect msg
 149'
 150
 151test_expect_success '-m and -F do not mix' '
 152        echo enough with the bongos >file &&
 153        test_must_fail git commit -F msg -m amending .
 154'
 155
 156test_expect_success 'using message from other commit' '
 157        git commit -C HEAD^ .
 158'
 159
 160test_expect_success 'editing message from other commit' '
 161        cat >editor <<-\EOF &&
 162        #!/bin/sh
 163        sed -e "s/amend/older/g"  < "$1" > "$1-"
 164        mv "$1-" "$1"
 165        EOF
 166        chmod 755 editor &&
 167        echo hula hula >file &&
 168        EDITOR=./editor git commit -c HEAD^ -a
 169'
 170
 171test_expect_success 'message from stdin' '
 172        echo silly new contents >file &&
 173        echo commit message from stdin |
 174        git commit -F - -a
 175'
 176
 177test_expect_success 'overriding author from command line' '
 178        echo gak >file &&
 179        git commit -m author \
 180                --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
 181        grep Rubber.Duck output
 182'
 183
 184test_expect_success PERL 'interactive add' '
 185        echo 7 |
 186        git commit --interactive |
 187        grep "What now"
 188'
 189
 190test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
 191        echo zoo >file &&
 192        test_must_fail git diff --exit-code >diff1 &&
 193        (echo u ; echo "*" ; echo q) |
 194        (
 195                EDITOR=: &&
 196                export EDITOR &&
 197                test_must_fail git commit --interactive
 198        ) &&
 199        git diff >diff2 &&
 200        compare_diff_patch diff1 diff2
 201'
 202
 203test_expect_success 'editor not invoked if -F is given' '
 204        cat >editor <<-\EOF &&
 205        #!/bin/sh
 206        sed -e s/good/bad/g <"$1" >"$1-"
 207        mv "$1-" "$1"
 208        EOF
 209        chmod 755 editor &&
 210
 211        echo A good commit message. >msg &&
 212        echo moo >file &&
 213
 214        EDITOR=./editor git commit -a -F msg &&
 215        git show -s --pretty=format:%s >subject &&
 216        grep -q good subject &&
 217
 218        echo quack >file &&
 219        echo Another good message. |
 220        EDITOR=./editor git commit -a -F - &&
 221        git show -s --pretty=format:%s >subject &&
 222        grep -q good subject
 223'
 224
 225test_expect_success 'partial commit that involves removal (1)' '
 226
 227        git rm --cached file &&
 228        mv file elif &&
 229        git add elif &&
 230        git commit -m "Partial: add elif" elif &&
 231        git diff-tree --name-status HEAD^ HEAD >current &&
 232        echo "A elif" >expected &&
 233        test_cmp expected current
 234
 235'
 236
 237test_expect_success 'partial commit that involves removal (2)' '
 238
 239        git commit -m "Partial: remove file" file &&
 240        git diff-tree --name-status HEAD^ HEAD >current &&
 241        echo "D file" >expected &&
 242        test_cmp expected current
 243
 244'
 245
 246test_expect_success 'partial commit that involves removal (3)' '
 247
 248        git rm --cached elif &&
 249        echo elif >elif &&
 250        git commit -m "Partial: modify elif" elif &&
 251        git diff-tree --name-status HEAD^ HEAD >current &&
 252        echo "M elif" >expected &&
 253        test_cmp expected current
 254
 255'
 256
 257test_expect_success 'amend commit to fix author' '
 258
 259        oldtick=$GIT_AUTHOR_DATE &&
 260        test_tick &&
 261        git reset --hard &&
 262        git cat-file -p HEAD |
 263        sed -e "s/author.*/author $author $oldtick/" \
 264                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 265                expected &&
 266        git commit --amend --author="$author" &&
 267        git cat-file -p HEAD > current &&
 268        test_cmp expected current
 269
 270'
 271
 272test_expect_success 'amend commit to fix date' '
 273
 274        test_tick &&
 275        newtick=$GIT_AUTHOR_DATE &&
 276        git reset --hard &&
 277        git cat-file -p HEAD |
 278        sed -e "s/author.*/author $author $newtick/" \
 279                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 280                expected &&
 281        git commit --amend --date="$newtick" &&
 282        git cat-file -p HEAD > current &&
 283        test_cmp expected current
 284
 285'
 286
 287test_expect_success 'commit complains about bogus date' '
 288        test_must_fail git commit --amend --date=10.11.2010
 289'
 290
 291test_expect_success 'sign off (1)' '
 292
 293        echo 1 >positive &&
 294        git add positive &&
 295        git commit -s -m "thank you" &&
 296        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 297        (
 298                echo thank you
 299                echo
 300                git var GIT_COMMITTER_IDENT |
 301                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 302        ) >expected &&
 303        test_cmp expected actual
 304
 305'
 306
 307test_expect_success 'sign off (2)' '
 308
 309        echo 2 >positive &&
 310        git add positive &&
 311        existing="Signed-off-by: Watch This <watchthis@example.com>" &&
 312        git commit -s -m "thank you
 313
 314$existing" &&
 315        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 316        (
 317                echo thank you
 318                echo
 319                echo $existing
 320                git var GIT_COMMITTER_IDENT |
 321                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 322        ) >expected &&
 323        test_cmp expected actual
 324
 325'
 326
 327test_expect_success 'signoff gap' '
 328
 329        echo 3 >positive &&
 330        git add positive &&
 331        alt="Alt-RFC-822-Header: Value" &&
 332        git commit -s -m "welcome
 333
 334$alt" &&
 335        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 336        (
 337                echo welcome
 338                echo
 339                echo $alt
 340                git var GIT_COMMITTER_IDENT |
 341                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 342        ) >expected &&
 343        test_cmp expected actual
 344'
 345
 346test_expect_success 'signoff gap 2' '
 347
 348        echo 4 >positive &&
 349        git add positive &&
 350        alt="fixed: 34" &&
 351        git commit -s -m "welcome
 352
 353We have now
 354$alt" &&
 355        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 356        (
 357                echo welcome
 358                echo
 359                echo We have now
 360                echo $alt
 361                echo
 362                git var GIT_COMMITTER_IDENT |
 363                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 364        ) >expected &&
 365        test_cmp expected actual
 366'
 367
 368test_expect_success 'multiple -m' '
 369
 370        >negative &&
 371        git add negative &&
 372        git commit -m "one" -m "two" -m "three" &&
 373        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 374        (
 375                echo one
 376                echo
 377                echo two
 378                echo
 379                echo three
 380        ) >expected &&
 381        test_cmp expected actual
 382
 383'
 384
 385test_expect_success 'amend commit to fix author' '
 386
 387        oldtick=$GIT_AUTHOR_DATE &&
 388        test_tick &&
 389        git reset --hard &&
 390        git cat-file -p HEAD |
 391        sed -e "s/author.*/author $author $oldtick/" \
 392                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 393                expected &&
 394        git commit --amend --author="$author" &&
 395        git cat-file -p HEAD > current &&
 396        test_cmp expected current
 397
 398'
 399
 400test_expect_success 'git commit <file> with dirty index' '
 401        echo tacocat > elif &&
 402        echo tehlulz > chz &&
 403        git add chz &&
 404        git commit elif -m "tacocat is a palindrome" &&
 405        git show --stat | grep elif &&
 406        git diff --cached | grep chz
 407'
 408
 409test_expect_success 'same tree (single parent)' '
 410
 411        git reset --hard &&
 412        test_must_fail git commit -m empty
 413
 414'
 415
 416test_expect_success 'same tree (single parent) --allow-empty' '
 417
 418        git commit --allow-empty -m "forced empty" &&
 419        git cat-file commit HEAD | grep forced
 420
 421'
 422
 423test_expect_success 'same tree (merge and amend merge)' '
 424
 425        git checkout -b side HEAD^ &&
 426        echo zero >zero &&
 427        git add zero &&
 428        git commit -m "add zero" &&
 429        git checkout master &&
 430
 431        git merge -s ours side -m "empty ok" &&
 432        git diff HEAD^ HEAD >actual &&
 433        : >expected &&
 434        test_cmp expected actual &&
 435
 436        git commit --amend -m "empty really ok" &&
 437        git diff HEAD^ HEAD >actual &&
 438        : >expected &&
 439        test_cmp expected actual
 440
 441'
 442
 443test_expect_success 'amend using the message from another commit' '
 444
 445        git reset --hard &&
 446        test_tick &&
 447        git commit --allow-empty -m "old commit" &&
 448        old=$(git rev-parse --verify HEAD) &&
 449        test_tick &&
 450        git commit --allow-empty -m "new commit" &&
 451        new=$(git rev-parse --verify HEAD) &&
 452        test_tick &&
 453        git commit --allow-empty --amend -C "$old" &&
 454        git show --pretty="format:%ad %s" "$old" >expected &&
 455        git show --pretty="format:%ad %s" HEAD >actual &&
 456        test_cmp expected actual
 457
 458'
 459
 460test_expect_success 'amend using the message from a commit named with tag' '
 461
 462        git reset --hard &&
 463        test_tick &&
 464        git commit --allow-empty -m "old commit" &&
 465        old=$(git rev-parse --verify HEAD) &&
 466        git tag -a -m "tag on old" tagged-old HEAD &&
 467        test_tick &&
 468        git commit --allow-empty -m "new commit" &&
 469        new=$(git rev-parse --verify HEAD) &&
 470        test_tick &&
 471        git commit --allow-empty --amend -C tagged-old &&
 472        git show --pretty="format:%ad %s" "$old" >expected &&
 473        git show --pretty="format:%ad %s" HEAD >actual &&
 474        test_cmp expected actual
 475
 476'
 477
 478test_expect_success 'amend can copy notes' '
 479
 480        git config notes.rewrite.amend true &&
 481        git config notes.rewriteRef "refs/notes/*" &&
 482        test_commit foo &&
 483        git notes add -m"a note" &&
 484        test_tick &&
 485        git commit --amend -m"new foo" &&
 486        test "$(git notes show)" = "a note"
 487
 488'
 489
 490test_done