t / t7502-commit.shon commit Merge branch 'master' of git://git.bogomips.org/git-svn (ccf2337)
   1#!/bin/sh
   2
   3test_description='git commit porcelain-ish'
   4
   5. ./test-lib.sh
   6
   7commit_msg_is () {
   8        expect=commit_msg_is.expect
   9        actual=commit_msg_is.actual
  10
  11        printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
  12        printf "%s" "$1" >$expect &&
  13        test_i18ncmp $expect $actual
  14}
  15
  16# Arguments: [<prefix] [<commit message>] [<commit options>]
  17check_summary_oneline() {
  18        test_tick &&
  19        git commit ${3+"$3"} -m "$2" | head -1 > act &&
  20
  21        # branch name
  22        SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
  23
  24        # append the "special" prefix, like "root-commit", "detached HEAD"
  25        if test -n "$1"
  26        then
  27                SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
  28        fi
  29
  30        # abbrev SHA-1
  31        SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
  32        echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
  33
  34        test_i18ncmp exp act
  35}
  36
  37test_expect_success 'output summary format' '
  38
  39        echo new >file1 &&
  40        git add file1 &&
  41        check_summary_oneline "root-commit" "initial" &&
  42
  43        echo change >>file1 &&
  44        git add file1
  45'
  46
  47test_expect_success 'output summary format: root-commit' '
  48        check_summary_oneline "" "a change"
  49'
  50
  51test_expect_success 'output summary format for commit with an empty diff' '
  52
  53        check_summary_oneline "" "empty" "--allow-empty"
  54'
  55
  56test_expect_success 'output summary format for merges' '
  57
  58        git checkout -b recursive-base &&
  59        test_commit base file1 &&
  60
  61        git checkout -b recursive-a recursive-base &&
  62        test_commit commit-a file1 &&
  63
  64        git checkout -b recursive-b recursive-base &&
  65        test_commit commit-b file1 &&
  66
  67        # conflict
  68        git checkout recursive-a &&
  69        test_must_fail git merge recursive-b &&
  70        # resolve the conflict
  71        echo commit-a > file1 &&
  72        git add file1 &&
  73        check_summary_oneline "" "Merge"
  74'
  75
  76output_tests_cleanup() {
  77        # this is needed for "do not fire editor in the presence of conflicts"
  78        git checkout master &&
  79
  80        # this is needed for the "partial removal" test to pass
  81        git rm file1 &&
  82        git commit -m "cleanup"
  83}
  84
  85test_expect_success 'the basics' '
  86
  87        output_tests_cleanup &&
  88
  89        echo doing partial >"commit is" &&
  90        mkdir not &&
  91        echo very much encouraged but we should >not/forbid &&
  92        git add "commit is" not &&
  93        echo update added "commit is" file >"commit is" &&
  94        echo also update another >not/forbid &&
  95        test_tick &&
  96        git commit -a -m "initial with -a" &&
  97
  98        git cat-file blob HEAD:"commit is" >current.1 &&
  99        git cat-file blob HEAD:not/forbid >current.2 &&
 100
 101        cmp current.1 "commit is" &&
 102        cmp current.2 not/forbid
 103
 104'
 105
 106test_expect_success 'partial' '
 107
 108        echo another >"commit is" &&
 109        echo another >not/forbid &&
 110        test_tick &&
 111        git commit -m "partial commit to handle a file" "commit is" &&
 112
 113        changed=$(git diff-tree --name-only HEAD^ HEAD) &&
 114        test "$changed" = "commit is"
 115
 116'
 117
 118test_expect_success 'partial modification in a subdirectory' '
 119
 120        test_tick &&
 121        git commit -m "partial commit to subdirectory" not &&
 122
 123        changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
 124        test "$changed" = "not/forbid"
 125
 126'
 127
 128test_expect_success 'partial removal' '
 129
 130        git rm not/forbid &&
 131        git commit -m "partial commit to remove not/forbid" not &&
 132
 133        changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
 134        test "$changed" = "not/forbid" &&
 135        remain=$(git ls-tree -r --name-only HEAD) &&
 136        test "$remain" = "commit is"
 137
 138'
 139
 140test_expect_success 'sign off' '
 141
 142        >positive &&
 143        git add positive &&
 144        git commit -s -m "thank you" &&
 145        actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
 146        expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
 147        test "z$actual" = "z$expected"
 148
 149'
 150
 151test_expect_success 'multiple -m' '
 152
 153        >negative &&
 154        git add negative &&
 155        git commit -m "one" -m "two" -m "three" &&
 156        actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
 157        expected=$(echo one; echo; echo two; echo; echo three) &&
 158        test "z$actual" = "z$expected"
 159
 160'
 161
 162test_expect_success 'verbose' '
 163
 164        echo minus >negative &&
 165        git add negative &&
 166        git status -v | sed -ne "/^diff --git /p" >actual &&
 167        echo "diff --git a/negative b/negative" >expect &&
 168        test_cmp expect actual
 169
 170'
 171
 172test_expect_success 'verbose respects diff config' '
 173
 174        git config color.diff always &&
 175        git status -v >actual &&
 176        grep "\[1mdiff --git" actual &&
 177        git config --unset color.diff
 178'
 179
 180mesg_with_comment_and_newlines='
 181# text
 182
 183'
 184
 185test_expect_success 'prepare file with comment line and trailing newlines'  '
 186        printf "%s" "$mesg_with_comment_and_newlines" >expect
 187'
 188
 189test_expect_success 'cleanup commit messages (verbatim option,-t)' '
 190
 191        echo >>negative &&
 192        git commit --cleanup=verbatim --no-status -t expect -a &&
 193        git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
 194        test_cmp expect actual
 195
 196'
 197
 198test_expect_success 'cleanup commit messages (verbatim option,-F)' '
 199
 200        echo >>negative &&
 201        git commit --cleanup=verbatim -F expect -a &&
 202        git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
 203        test_cmp expect actual
 204
 205'
 206
 207test_expect_success 'cleanup commit messages (verbatim option,-m)' '
 208
 209        echo >>negative &&
 210        git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
 211        git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
 212        test_cmp expect actual
 213
 214'
 215
 216test_expect_success 'cleanup commit messages (whitespace option,-F)' '
 217
 218        echo >>negative &&
 219        { echo;echo "# text";echo; } >text &&
 220        echo "# text" >expect &&
 221        git commit --cleanup=whitespace -F text -a &&
 222        git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
 223        test_cmp expect actual
 224
 225'
 226
 227test_expect_success 'cleanup commit messages (strip option,-F)' '
 228
 229        echo >>negative &&
 230        { echo;echo "# text";echo sample;echo; } >text &&
 231        echo sample >expect &&
 232        git commit --cleanup=strip -F text -a &&
 233        git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
 234        test_cmp expect actual
 235
 236'
 237
 238test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
 239
 240        echo >>negative &&
 241        { echo;echo sample;echo; } >text &&
 242        git commit -e -F text -a &&
 243        head -n 4 .git/COMMIT_EDITMSG >actual
 244'
 245
 246echo "sample
 247
 248# Please enter the commit message for your changes. Lines starting
 249# with '#' will be ignored, and an empty message aborts the commit." >expect
 250
 251test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
 252        test_i18ncmp expect actual
 253'
 254
 255test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
 256        test_must_fail git commit --cleanup=non-existent
 257'
 258
 259test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
 260        test_must_fail git -c commit.cleanup=non-existent commit
 261'
 262
 263test_expect_success 'cleanup commit message (no config and no option uses default)' '
 264        echo content >>file &&
 265        git add file &&
 266        (
 267          test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 268          git commit --no-status
 269        ) &&
 270        commit_msg_is "commit message"
 271'
 272
 273test_expect_success 'cleanup commit message (option overrides default)' '
 274        echo content >>file &&
 275        git add file &&
 276        (
 277          test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 278          git commit --cleanup=whitespace --no-status
 279        ) &&
 280        commit_msg_is "commit message # comment"
 281'
 282
 283test_expect_success 'cleanup commit message (config overrides default)' '
 284        echo content >>file &&
 285        git add file &&
 286        (
 287          test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 288          git -c commit.cleanup=whitespace commit --no-status
 289        ) &&
 290        commit_msg_is "commit message # comment"
 291'
 292
 293test_expect_success 'cleanup commit message (option overrides config)' '
 294        echo content >>file &&
 295        git add file &&
 296        (
 297          test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 298          git -c commit.cleanup=whitespace commit --cleanup=default
 299        ) &&
 300        commit_msg_is "commit message"
 301'
 302
 303test_expect_success 'cleanup commit message (default, -m)' '
 304        echo content >>file &&
 305        git add file &&
 306        git commit -m "message #comment " &&
 307        commit_msg_is "message #comment"
 308'
 309
 310test_expect_success 'cleanup commit message (whitespace option, -m)' '
 311        echo content >>file &&
 312        git add file &&
 313        git commit --cleanup=whitespace --no-status -m "message #comment " &&
 314        commit_msg_is "message #comment"
 315'
 316
 317test_expect_success 'cleanup commit message (whitespace config, -m)' '
 318        echo content >>file &&
 319        git add file &&
 320        git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
 321        commit_msg_is "message #comment"
 322'
 323
 324test_expect_success 'message shows author when it is not equal to committer' '
 325        echo >>negative &&
 326        git commit -e -m "sample" -a &&
 327        test_i18ngrep \
 328          "^# Author: *A U Thor <author@example.com>\$" \
 329          .git/COMMIT_EDITMSG
 330'
 331
 332test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
 333
 334        echo >>negative &&
 335        (
 336                sane_unset GIT_COMMITTER_EMAIL &&
 337                sane_unset GIT_COMMITTER_NAME &&
 338                git commit -e -m "sample" -a
 339        ) &&
 340        # the ident is calculated from the system, so we cannot
 341        # check the actual value, only that it is there
 342        test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
 343'
 344
 345write_script .git/FAKE_EDITOR <<EOF
 346echo editor started > "$(pwd)/.git/result"
 347exit 0
 348EOF
 349
 350test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
 351        >.git/result
 352        >expect &&
 353
 354        echo >>negative &&
 355        (
 356                sane_unset GIT_COMMITTER_EMAIL &&
 357                sane_unset GIT_COMMITTER_NAME &&
 358                GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
 359                export GIT_EDITOR &&
 360                test_must_fail git commit -e -m sample -a
 361        ) &&
 362        test_cmp expect .git/result
 363'
 364
 365test_expect_success 'do not fire editor in the presence of conflicts' '
 366
 367        git clean -f &&
 368        echo f >g &&
 369        git add g &&
 370        git commit -m "add g" &&
 371        git branch second &&
 372        echo master >g &&
 373        echo g >h &&
 374        git add g h &&
 375        git commit -m "modify g and add h" &&
 376        git checkout second &&
 377        echo second >g &&
 378        git add g &&
 379        git commit -m second &&
 380        # Must fail due to conflict
 381        test_must_fail git cherry-pick -n master &&
 382        echo "editor not started" >.git/result &&
 383        (
 384                GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
 385                export GIT_EDITOR &&
 386                test_must_fail git commit
 387        ) &&
 388        test "$(cat .git/result)" = "editor not started"
 389'
 390
 391write_script .git/FAKE_EDITOR <<EOF
 392# kill -TERM command added below.
 393EOF
 394
 395test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
 396        echo >>negative &&
 397        ! "$SHELL_PATH" -c '\''
 398          echo kill -TERM $$ >> .git/FAKE_EDITOR
 399          GIT_EDITOR=.git/FAKE_EDITOR
 400          export GIT_EDITOR
 401          exec git commit -a'\'' &&
 402        test ! -f .git/index.lock
 403'
 404
 405rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
 406git reset -q --hard
 407
 408test_expect_success 'Hand committing of a redundant merge removes dups' '
 409
 410        git rev-parse second master >expect &&
 411        test_must_fail git merge second master &&
 412        git checkout master g &&
 413        EDITOR=: git commit -a &&
 414        git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
 415        test_cmp expect actual
 416
 417'
 418
 419test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
 420
 421        git reset --hard &&
 422        git commit -s -m "hello: kitty" --allow-empty &&
 423        git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
 424        test_line_count = 3 actual
 425
 426'
 427
 428write_script .git/FAKE_EDITOR <<\EOF
 429mv "$1" "$1.orig"
 430(
 431        echo message
 432        cat "$1.orig"
 433) >"$1"
 434EOF
 435
 436echo '## Custom template' >template
 437
 438clear_config () {
 439        (
 440                git config --unset-all "$1"
 441                case $? in
 442                0|5)    exit 0 ;;
 443                *)      exit 1 ;;
 444                esac
 445        )
 446}
 447
 448try_commit () {
 449        git reset --hard &&
 450        echo >>negative &&
 451        GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
 452        case "$use_template" in
 453        '')
 454                test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
 455        *)
 456                test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
 457        esac
 458}
 459
 460try_commit_status_combo () {
 461
 462        test_expect_success 'commit' '
 463                clear_config commit.status &&
 464                try_commit "" &&
 465                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 466        '
 467
 468        test_expect_success 'commit' '
 469                clear_config commit.status &&
 470                try_commit "" &&
 471                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 472        '
 473
 474        test_expect_success 'commit --status' '
 475                clear_config commit.status &&
 476                try_commit --status &&
 477                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 478        '
 479
 480        test_expect_success 'commit --no-status' '
 481                clear_config commit.status &&
 482                try_commit --no-status &&
 483                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
 484        '
 485
 486        test_expect_success 'commit with commit.status = yes' '
 487                clear_config commit.status &&
 488                git config commit.status yes &&
 489                try_commit "" &&
 490                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 491        '
 492
 493        test_expect_success 'commit with commit.status = no' '
 494                clear_config commit.status &&
 495                git config commit.status no &&
 496                try_commit "" &&
 497                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
 498        '
 499
 500        test_expect_success 'commit --status with commit.status = yes' '
 501                clear_config commit.status &&
 502                git config commit.status yes &&
 503                try_commit --status &&
 504                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 505        '
 506
 507        test_expect_success 'commit --no-status with commit.status = yes' '
 508                clear_config commit.status &&
 509                git config commit.status yes &&
 510                try_commit --no-status &&
 511                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
 512        '
 513
 514        test_expect_success 'commit --status with commit.status = no' '
 515                clear_config commit.status &&
 516                git config commit.status no &&
 517                try_commit --status &&
 518                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
 519        '
 520
 521        test_expect_success 'commit --no-status with commit.status = no' '
 522                clear_config commit.status &&
 523                git config commit.status no &&
 524                try_commit --no-status &&
 525                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
 526        '
 527
 528}
 529
 530try_commit_status_combo
 531
 532use_template="-t template"
 533
 534try_commit_status_combo
 535
 536test_expect_success 'commit --status with custom comment character' '
 537        test_when_finished "git config --unset core.commentchar" &&
 538        git config core.commentchar ";" &&
 539        try_commit --status &&
 540        test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
 541'
 542
 543test_done