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