t / t7501-commit.shon commit Merge branch 'as/maint-shortlog-cleanup' (d451b50)
   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         git status | grep 'Initial commit'"
  19
  20test_expect_success \
  21        "fail initial amend" \
  22        "test_must_fail git commit --amend"
  23
  24test_expect_success \
  25        "initial commit" \
  26        "git commit -m initial"
  27
  28test_expect_success \
  29        "invalid options 1" \
  30        "test_must_fail git commit -m foo -m bar -F file"
  31
  32test_expect_success \
  33        "invalid options 2" \
  34        "test_must_fail git commit -C HEAD -m illegal"
  35
  36test_expect_success \
  37        "using paths with -a" \
  38        "echo King of the bongo >file &&
  39        test_must_fail git commit -m foo -a file"
  40
  41test_expect_success \
  42        "using paths with --interactive" \
  43        "echo bong-o-bong >file &&
  44        ! (echo 7 | git commit -m foo --interactive file)"
  45
  46test_expect_success \
  47        "using invalid commit with -C" \
  48        "test_must_fail git commit -C bogus"
  49
  50test_expect_success \
  51        "testing nothing to commit" \
  52        "test_must_fail git commit -m initial"
  53
  54test_expect_success \
  55        "next commit" \
  56        "echo 'bongo bongo bongo' >file \
  57         git commit -m next -a"
  58
  59test_expect_success \
  60        "commit message from non-existing file" \
  61        "echo 'more bongo: bongo bongo bongo bongo' >file && \
  62         test_must_fail git commit -F gah -a"
  63
  64# Empty except stray tabs and spaces on a few lines.
  65sed -e 's/@$//' >msg <<EOF
  66                @
  67
  68  @
  69Signed-off-by: hula
  70EOF
  71test_expect_success \
  72        "empty commit message" \
  73        "test_must_fail git commit -F msg -a"
  74
  75test_expect_success \
  76        "commit message from file" \
  77        "echo 'this is the commit message, coming from a file' >msg && \
  78         git commit -F msg -a"
  79
  80cat >editor <<\EOF
  81#!/bin/sh
  82sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
  83mv "$1-" "$1"
  84EOF
  85chmod 755 editor
  86
  87test_expect_success \
  88        "amend commit" \
  89        "VISUAL=./editor git commit --amend"
  90
  91test_expect_success \
  92        "passing -m and -F" \
  93        "echo 'enough with the bongos' >file && \
  94         test_must_fail git commit -F msg -m amending ."
  95
  96test_expect_success \
  97        "using message from other commit" \
  98        "git commit -C HEAD^ ."
  99
 100cat >editor <<\EOF
 101#!/bin/sh
 102sed -e "s/amend/older/g"  < "$1" > "$1-"
 103mv "$1-" "$1"
 104EOF
 105chmod 755 editor
 106
 107test_expect_success \
 108        "editing message from other commit" \
 109        "echo 'hula hula' >file && \
 110         VISUAL=./editor git commit -c HEAD^ -a"
 111
 112test_expect_success \
 113        "message from stdin" \
 114        "echo 'silly new contents' >file && \
 115         echo commit message from stdin | git commit -F - -a"
 116
 117test_expect_success \
 118        "overriding author from command line" \
 119        "echo 'gak' >file && \
 120         git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a"
 121
 122test_expect_success \
 123        "interactive add" \
 124        "echo 7 | git commit --interactive | grep 'What now'"
 125
 126test_expect_success \
 127        "showing committed revisions" \
 128        "git rev-list HEAD >current"
 129
 130cat >editor <<\EOF
 131#!/bin/sh
 132sed -e "s/good/bad/g" < "$1" > "$1-"
 133mv "$1-" "$1"
 134EOF
 135chmod 755 editor
 136
 137cat >msg <<EOF
 138A good commit message.
 139EOF
 140
 141test_expect_success \
 142        'editor not invoked if -F is given' '
 143         echo "moo" >file &&
 144         VISUAL=./editor git commit -a -F msg &&
 145         git show -s --pretty=format:"%s" | grep -q good &&
 146         echo "quack" >file &&
 147         echo "Another good message." | VISUAL=./editor git commit -a -F - &&
 148         git show -s --pretty=format:"%s" | grep -q good
 149         '
 150# We could just check the head sha1, but checking each commit makes it
 151# easier to isolate bugs.
 152
 153cat >expected <<\EOF
 15472c0dc9855b0c9dadcbfd5a31cab072e0cb774ca
 1559b88fc14ce6b32e3d9ee021531a54f18a5cf38a2
 1563536bbb352c3a1ef9a420f5b4242d48578b92aa7
 157d381ac431806e53f3dd7ac2f1ae0534f36d738b9
 1584fd44095ad6334f3ef72e4c5ec8ddf108174b54a
 159402702b49136e7587daa9280e91e4bb7cb2179f7
 160EOF
 161
 162test_expect_success \
 163    'validate git rev-list output.' \
 164    'test_cmp expected current'
 165
 166test_expect_success 'partial commit that involves removal (1)' '
 167
 168        git rm --cached file &&
 169        mv file elif &&
 170        git add elif &&
 171        git commit -m "Partial: add elif" elif &&
 172        git diff-tree --name-status HEAD^ HEAD >current &&
 173        echo "A elif" >expected &&
 174        test_cmp expected current
 175
 176'
 177
 178test_expect_success 'partial commit that involves removal (2)' '
 179
 180        git commit -m "Partial: remove file" file &&
 181        git diff-tree --name-status HEAD^ HEAD >current &&
 182        echo "D file" >expected &&
 183        test_cmp expected current
 184
 185'
 186
 187test_expect_success 'partial commit that involves removal (3)' '
 188
 189        git rm --cached elif &&
 190        echo elif >elif &&
 191        git commit -m "Partial: modify elif" elif &&
 192        git diff-tree --name-status HEAD^ HEAD >current &&
 193        echo "M elif" >expected &&
 194        test_cmp expected current
 195
 196'
 197
 198author="The Real Author <someguy@his.email.org>"
 199test_expect_success 'amend commit to fix author' '
 200
 201        oldtick=$GIT_AUTHOR_DATE &&
 202        test_tick &&
 203        git reset --hard &&
 204        git cat-file -p HEAD |
 205        sed -e "s/author.*/author $author $oldtick/" \
 206                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 207                expected &&
 208        git commit --amend --author="$author" &&
 209        git cat-file -p HEAD > current &&
 210        test_cmp expected current
 211
 212'
 213
 214test_expect_success 'sign off (1)' '
 215
 216        echo 1 >positive &&
 217        git add positive &&
 218        git commit -s -m "thank you" &&
 219        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 220        (
 221                echo thank you
 222                echo
 223                git var GIT_COMMITTER_IDENT |
 224                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 225        ) >expected &&
 226        test_cmp expected actual
 227
 228'
 229
 230test_expect_success 'sign off (2)' '
 231
 232        echo 2 >positive &&
 233        git add positive &&
 234        existing="Signed-off-by: Watch This <watchthis@example.com>" &&
 235        git commit -s -m "thank you
 236
 237$existing" &&
 238        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 239        (
 240                echo thank you
 241                echo
 242                echo $existing
 243                git var GIT_COMMITTER_IDENT |
 244                sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
 245        ) >expected &&
 246        test_cmp expected actual
 247
 248'
 249
 250test_expect_success 'multiple -m' '
 251
 252        >negative &&
 253        git add negative &&
 254        git commit -m "one" -m "two" -m "three" &&
 255        git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
 256        (
 257                echo one
 258                echo
 259                echo two
 260                echo
 261                echo three
 262        ) >expected &&
 263        test_cmp expected actual
 264
 265'
 266
 267author="The Real Author <someguy@his.email.org>"
 268test_expect_success 'amend commit to fix author' '
 269
 270        oldtick=$GIT_AUTHOR_DATE &&
 271        test_tick &&
 272        git reset --hard &&
 273        git cat-file -p HEAD |
 274        sed -e "s/author.*/author $author $oldtick/" \
 275                -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
 276                expected &&
 277        git commit --amend --author="$author" &&
 278        git cat-file -p HEAD > current &&
 279        test_cmp expected current
 280
 281'
 282
 283test_expect_success 'git commit <file> with dirty index' '
 284        echo tacocat > elif &&
 285        echo tehlulz > chz &&
 286        git add chz &&
 287        git commit elif -m "tacocat is a palindrome" &&
 288        git show --stat | grep elif &&
 289        git diff --cached | grep chz
 290'
 291
 292test_expect_success 'same tree (single parent)' '
 293
 294        git reset --hard
 295
 296        if git commit -m empty
 297        then
 298                echo oops -- should have complained
 299                false
 300        else
 301                : happy
 302        fi
 303
 304'
 305
 306test_expect_success 'same tree (single parent) --allow-empty' '
 307
 308        git commit --allow-empty -m "forced empty" &&
 309        git cat-file commit HEAD | grep forced
 310
 311'
 312
 313test_expect_success 'same tree (merge and amend merge)' '
 314
 315        git checkout -b side HEAD^ &&
 316        echo zero >zero &&
 317        git add zero &&
 318        git commit -m "add zero" &&
 319        git checkout master &&
 320
 321        git merge -s ours side -m "empty ok" &&
 322        git diff HEAD^ HEAD >actual &&
 323        : >expected &&
 324        test_cmp expected actual &&
 325
 326        git commit --amend -m "empty really ok" &&
 327        git diff HEAD^ HEAD >actual &&
 328        : >expected &&
 329        test_cmp expected actual
 330
 331'
 332
 333test_expect_success 'amend using the message from another commit' '
 334
 335        git reset --hard &&
 336        test_tick &&
 337        git commit --allow-empty -m "old commit" &&
 338        old=$(git rev-parse --verify HEAD) &&
 339        test_tick &&
 340        git commit --allow-empty -m "new commit" &&
 341        new=$(git rev-parse --verify HEAD) &&
 342        test_tick &&
 343        git commit --allow-empty --amend -C "$old" &&
 344        git show --pretty="format:%ad %s" "$old" >expected &&
 345        git show --pretty="format:%ad %s" HEAD >actual &&
 346        test_cmp expected actual
 347
 348'
 349
 350test_expect_success 'amend using the message from a commit named with tag' '
 351
 352        git reset --hard &&
 353        test_tick &&
 354        git commit --allow-empty -m "old commit" &&
 355        old=$(git rev-parse --verify HEAD) &&
 356        git tag -a -m "tag on old" tagged-old HEAD &&
 357        test_tick &&
 358        git commit --allow-empty -m "new commit" &&
 359        new=$(git rev-parse --verify HEAD) &&
 360        test_tick &&
 361        git commit --allow-empty --amend -C tagged-old &&
 362        git show --pretty="format:%ad %s" "$old" >expected &&
 363        git show --pretty="format:%ad %s" HEAD >actual &&
 364        test_cmp expected actual
 365
 366'
 367
 368test_done