t / t7501-commit.shon commit glossary: clarify description of HEAD (67393c5)
   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
  12test_tick
  13
  14test_expect_success \
  15        "initial status" \
  16        "echo 'bongo bongo' >file &&
  17         git add file"
  18
  19test_expect_success "Constructing initial commit" '
  20        git status >actual &&
  21        test_i18ngrep "Initial commit" actual
  22'
  23
  24test_expect_success \
  25        "fail initial amend" \
  26        "test_must_fail git commit --amend"
  27
  28test_expect_success \
  29        "initial commit" \
  30        "git commit -m initial"
  31
  32test_expect_success \
  33        "invalid options 1" \
  34        "test_must_fail git commit -m foo -m bar -F file"
  35
  36test_expect_success \
  37        "invalid options 2" \
  38        "test_must_fail git commit -C HEAD -m illegal"
  39
  40test_expect_success \
  41        "using paths with -a" \
  42        "echo King of the bongo >file &&
  43        test_must_fail git commit -m foo -a file"
  44
  45test_expect_success PERL \
  46        "using paths with --interactive" \
  47        "echo bong-o-bong >file &&
  48        ! (echo 7 | git commit -m foo --interactive file)"
  49
  50test_expect_success \
  51        "using invalid commit with -C" \
  52        "test_must_fail git commit -C bogus"
  53
  54test_expect_success \
  55        "testing nothing to commit" \
  56        "test_must_fail git commit -m initial"
  57
  58test_expect_success \
  59        "next commit" \
  60        "echo 'bongo bongo bongo' >file \
  61         git commit -m next -a"
  62
  63test_expect_success \
  64        "commit message from non-existing file" \
  65        "echo 'more bongo: bongo bongo bongo bongo' >file && \
  66         test_must_fail git commit -F gah -a"
  67
  68# Empty except stray tabs and spaces on a few lines.
  69sed -e 's/@$//' >msg <<EOF
  70                @
  71
  72  @
  73Signed-off-by: hula
  74EOF
  75test_expect_success \
  76        "empty commit message" \
  77        "test_must_fail git commit -F msg -a"
  78
  79test_expect_success \
  80        "commit message from file" \
  81        "echo 'this is the commit message, coming from a file' >msg && \
  82         git commit -F msg -a"
  83
  84cat >editor <<\EOF
  85#!/bin/sh
  86sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
  87mv "$1-" "$1"
  88EOF
  89chmod 755 editor
  90
  91test_expect_success \
  92        "amend commit" \
  93        "EDITOR=./editor git commit --amend"
  94
  95test_expect_success \
  96        "passing -m and -F" \
  97        "echo 'enough with the bongos' >file && \
  98         test_must_fail git commit -F msg -m amending ."
  99
 100test_expect_success \
 101        "using message from other commit" \
 102        "git commit -C HEAD^ ."
 103
 104cat >editor <<\EOF
 105#!/bin/sh
 106sed -e "s/amend/older/g"  < "$1" > "$1-"
 107mv "$1-" "$1"
 108EOF
 109chmod 755 editor
 110
 111test_expect_success \
 112        "editing message from other commit" \
 113        "echo 'hula hula' >file && \
 114         EDITOR=./editor git commit -c HEAD^ -a"
 115
 116test_expect_success \
 117        "message from stdin" \
 118        "echo 'silly new contents' >file && \
 119         echo commit message from stdin | git commit -F - -a"
 120
 121test_expect_success \
 122        "overriding author from command line" \
 123        "echo 'gak' >file && \
 124         git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a >output 2>&1"
 125
 126test_expect_success \
 127        "commit --author output mentions author" \
 128        "grep Rubber.Duck output"
 129
 130test_expect_success PERL \
 131        "interactive add" \
 132        "echo 7 | git commit --interactive | grep 'What now'"
 133
 134test_expect_success \
 135        "showing committed revisions" \
 136        "git rev-list HEAD >current"
 137
 138cat >editor <<\EOF
 139#!/bin/sh
 140sed -e "s/good/bad/g" < "$1" > "$1-"
 141mv "$1-" "$1"
 142EOF
 143chmod 755 editor
 144
 145cat >msg <<EOF
 146A good commit message.
 147EOF
 148
 149test_expect_success \
 150        'editor not invoked if -F is given' '
 151         echo "moo" >file &&
 152         EDITOR=./editor git commit -a -F msg &&
 153         git show -s --pretty=format:"%s" | grep -q good &&
 154         echo "quack" >file &&
 155         echo "Another good message." | EDITOR=./editor git commit -a -F - &&
 156         git show -s --pretty=format:"%s" | grep -q good
 157         '
 158# We could just check the head sha1, but checking each commit makes it
 159# easier to isolate bugs.
 160
 161cat >expected <<\EOF
 16272c0dc9855b0c9dadcbfd5a31cab072e0cb774ca
 1639b88fc14ce6b32e3d9ee021531a54f18a5cf38a2
 1643536bbb352c3a1ef9a420f5b4242d48578b92aa7
 165d381ac431806e53f3dd7ac2f1ae0534f36d738b9
 1664fd44095ad6334f3ef72e4c5ec8ddf108174b54a
 167402702b49136e7587daa9280e91e4bb7cb2179f7
 168EOF
 169
 170test_expect_success \
 171    'validate git rev-list output.' \
 172    'test_cmp expected current'
 173
 174test_expect_success 'partial commit that involves removal (1)' '
 175
 176        git rm --cached file &&
 177        mv file elif &&
 178        git add elif &&
 179        git commit -m "Partial: add elif" elif &&
 180        git diff-tree --name-status HEAD^ HEAD >current &&
 181        echo "A elif" >expected &&
 182        test_cmp expected current
 183
 184'
 185
 186test_expect_success 'partial commit that involves removal (2)' '
 187
 188        git commit -m "Partial: remove file" file &&
 189        git diff-tree --name-status HEAD^ HEAD >current &&
 190        echo "D file" >expected &&
 191        test_cmp expected current
 192
 193'
 194
 195test_expect_success 'partial commit that involves removal (3)' '
 196
 197        git rm --cached elif &&
 198        echo elif >elif &&
 199        git commit -m "Partial: modify elif" elif &&
 200        git diff-tree --name-status HEAD^ HEAD >current &&
 201        echo "M elif" >expected &&
 202        test_cmp expected current
 203
 204'
 205
 206author="The Real Author <someguy@his.email.org>"
 207test_expect_success 'amend commit to fix author' '
 208
 209        oldtick=$GIT_AUTHOR_DATE &&
 210        test_tick &&
 211        git reset --hard &&
 212        git cat-file -p HEAD |
 213        sed -e "s/author.*/author $author $oldtick/" \
 214                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 215                expected &&
 216        git commit --amend --author="$author" &&
 217        git cat-file -p HEAD > current &&
 218        test_cmp expected current
 219
 220'
 221
 222test_expect_success 'amend commit to fix date' '
 223
 224        test_tick &&
 225        newtick=$GIT_AUTHOR_DATE &&
 226        git reset --hard &&
 227        git cat-file -p HEAD |
 228        sed -e "s/author.*/author $author $newtick/" \
 229                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 230                expected &&
 231        git commit --amend --date="$newtick" &&
 232        git cat-file -p HEAD > current &&
 233        test_cmp expected current
 234
 235'
 236
 237test_expect_success 'commit complains about bogus date' '
 238        test_must_fail git commit --amend --date=10.11.2010
 239'
 240
 241test_expect_success 'sign off (1)' '
 242
 243        echo 1 >positive &&
 244        git add positive &&
 245        git commit -s -m "thank you" &&
 246        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 247        (
 248                echo thank you
 249                echo
 250                git var GIT_COMMITTER_IDENT |
 251                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 252        ) >expected &&
 253        test_cmp expected actual
 254
 255'
 256
 257test_expect_success 'sign off (2)' '
 258
 259        echo 2 >positive &&
 260        git add positive &&
 261        existing="Signed-off-by: Watch This <watchthis@example.com>" &&
 262        git commit -s -m "thank you
 263
 264$existing" &&
 265        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 266        (
 267                echo thank you
 268                echo
 269                echo $existing
 270                git var GIT_COMMITTER_IDENT |
 271                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 272        ) >expected &&
 273        test_cmp expected actual
 274
 275'
 276
 277test_expect_success 'signoff gap' '
 278
 279        echo 3 >positive &&
 280        git add positive &&
 281        alt="Alt-RFC-822-Header: Value" &&
 282        git commit -s -m "welcome
 283
 284$alt" &&
 285        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 286        (
 287                echo welcome
 288                echo
 289                echo $alt
 290                git var GIT_COMMITTER_IDENT |
 291                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 292        ) >expected &&
 293        test_cmp expected actual
 294'
 295
 296test_expect_success 'signoff gap 2' '
 297
 298        echo 4 >positive &&
 299        git add positive &&
 300        alt="fixed: 34" &&
 301        git commit -s -m "welcome
 302
 303We have now
 304$alt" &&
 305        git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
 306        (
 307                echo welcome
 308                echo
 309                echo We have now
 310                echo $alt
 311                echo
 312                git var GIT_COMMITTER_IDENT |
 313                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 314        ) >expected &&
 315        test_cmp expected actual
 316'
 317
 318test_expect_success 'multiple -m' '
 319
 320        >negative &&
 321        git add negative &&
 322        git commit -m "one" -m "two" -m "three" &&
 323        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 324        (
 325                echo one
 326                echo
 327                echo two
 328                echo
 329                echo three
 330        ) >expected &&
 331        test_cmp expected actual
 332
 333'
 334
 335author="The Real Author <someguy@his.email.org>"
 336test_expect_success 'amend commit to fix author' '
 337
 338        oldtick=$GIT_AUTHOR_DATE &&
 339        test_tick &&
 340        git reset --hard &&
 341        git cat-file -p HEAD |
 342        sed -e "s/author.*/author $author $oldtick/" \
 343                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 344                expected &&
 345        git commit --amend --author="$author" &&
 346        git cat-file -p HEAD > current &&
 347        test_cmp expected current
 348
 349'
 350
 351test_expect_success 'git commit <file> with dirty index' '
 352        echo tacocat > elif &&
 353        echo tehlulz > chz &&
 354        git add chz &&
 355        git commit elif -m "tacocat is a palindrome" &&
 356        git show --stat | grep elif &&
 357        git diff --cached | grep chz
 358'
 359
 360test_expect_success 'same tree (single parent)' '
 361
 362        git reset --hard
 363
 364        if git commit -m empty
 365        then
 366                echo oops -- should have complained
 367                false
 368        else
 369                : happy
 370        fi
 371
 372'
 373
 374test_expect_success 'same tree (single parent) --allow-empty' '
 375
 376        git commit --allow-empty -m "forced empty" &&
 377        git cat-file commit HEAD | grep forced
 378
 379'
 380
 381test_expect_success 'same tree (merge and amend merge)' '
 382
 383        git checkout -b side HEAD^ &&
 384        echo zero >zero &&
 385        git add zero &&
 386        git commit -m "add zero" &&
 387        git checkout master &&
 388
 389        git merge -s ours side -m "empty ok" &&
 390        git diff HEAD^ HEAD >actual &&
 391        : >expected &&
 392        test_cmp expected actual &&
 393
 394        git commit --amend -m "empty really ok" &&
 395        git diff HEAD^ HEAD >actual &&
 396        : >expected &&
 397        test_cmp expected actual
 398
 399'
 400
 401test_expect_success 'amend using the message from another commit' '
 402
 403        git reset --hard &&
 404        test_tick &&
 405        git commit --allow-empty -m "old commit" &&
 406        old=$(git rev-parse --verify HEAD) &&
 407        test_tick &&
 408        git commit --allow-empty -m "new commit" &&
 409        new=$(git rev-parse --verify HEAD) &&
 410        test_tick &&
 411        git commit --allow-empty --amend -C "$old" &&
 412        git show --pretty="format:%ad %s" "$old" >expected &&
 413        git show --pretty="format:%ad %s" HEAD >actual &&
 414        test_cmp expected actual
 415
 416'
 417
 418test_expect_success 'amend using the message from a commit named with tag' '
 419
 420        git reset --hard &&
 421        test_tick &&
 422        git commit --allow-empty -m "old commit" &&
 423        old=$(git rev-parse --verify HEAD) &&
 424        git tag -a -m "tag on old" tagged-old HEAD &&
 425        test_tick &&
 426        git commit --allow-empty -m "new commit" &&
 427        new=$(git rev-parse --verify HEAD) &&
 428        test_tick &&
 429        git commit --allow-empty --amend -C tagged-old &&
 430        git show --pretty="format:%ad %s" "$old" >expected &&
 431        git show --pretty="format:%ad %s" HEAD >actual &&
 432        test_cmp expected actual
 433
 434'
 435
 436test_expect_success 'amend can copy notes' '
 437
 438        git config notes.rewrite.amend true &&
 439        git config notes.rewriteRef "refs/notes/*" &&
 440        test_commit foo &&
 441        git notes add -m"a note" &&
 442        test_tick &&
 443        git commit --amend -m"new foo" &&
 444        test "$(git notes show)" = "a note"
 445
 446'
 447
 448test_done