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