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