t / t7500-commit.shon commit Documentation: "git reset <tree-ish> <pathspec>" takes a tree-ish, not tree-sh (debecc5)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Steven Grimm
   4#
   5
   6test_description='git commit
   7
   8Tests for selected commit options.'
   9
  10. ./test-lib.sh
  11
  12commit_msg_is () {
  13        expect=commit_msg_is.expect
  14        actual=commit_msg_is.actual
  15
  16        printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" &&
  17        printf "%s" "$1" >"$expect" &&
  18        test_i18ncmp "$expect" "$actual"
  19}
  20
  21# A sanity check to see if commit is working at all.
  22test_expect_success 'a basic commit in an empty tree should succeed' '
  23        echo content > foo &&
  24        git add foo &&
  25        git commit -m "initial commit"
  26'
  27
  28test_expect_success 'nonexistent template file should return error' '
  29        echo changes >> foo &&
  30        git add foo &&
  31        (
  32                GIT_EDITOR="echo hello >\"\$1\"" &&
  33                export GIT_EDITOR &&
  34                test_must_fail git commit --template "$PWD"/notexist
  35        )
  36'
  37
  38test_expect_success 'nonexistent template file in config should return error' '
  39        test_config commit.template "$PWD"/notexist &&
  40        (
  41                GIT_EDITOR="echo hello >\"\$1\"" &&
  42                export GIT_EDITOR &&
  43                test_must_fail git commit
  44        )
  45'
  46
  47# From now on we'll use a template file that exists.
  48TEMPLATE="$PWD"/template
  49
  50test_expect_success 'unedited template should not commit' '
  51        echo "template line" > "$TEMPLATE" &&
  52        test_must_fail git commit --template "$TEMPLATE"
  53'
  54
  55test_expect_success 'unedited template with comments should not commit' '
  56        echo "# comment in template" >> "$TEMPLATE" &&
  57        test_must_fail git commit --template "$TEMPLATE"
  58'
  59
  60test_expect_success 'a Signed-off-by line by itself should not commit' '
  61        (
  62                test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
  63                test_must_fail git commit --template "$TEMPLATE"
  64        )
  65'
  66
  67test_expect_success 'adding comments to a template should not commit' '
  68        (
  69                test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
  70                test_must_fail git commit --template "$TEMPLATE"
  71        )
  72'
  73
  74test_expect_success 'adding real content to a template should commit' '
  75        (
  76                test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
  77                git commit --template "$TEMPLATE"
  78        ) &&
  79        commit_msg_is "template linecommit message"
  80'
  81
  82test_expect_success '-t option should be short for --template' '
  83        echo "short template" > "$TEMPLATE" &&
  84        echo "new content" >> foo &&
  85        git add foo &&
  86        (
  87                test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
  88                git commit -t "$TEMPLATE"
  89        ) &&
  90        commit_msg_is "short templatecommit message"
  91'
  92
  93test_expect_success 'config-specified template should commit' '
  94        echo "new template" > "$TEMPLATE" &&
  95        test_config commit.template "$TEMPLATE" &&
  96        echo "more content" >> foo &&
  97        git add foo &&
  98        (
  99                test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
 100                git commit
 101        ) &&
 102        commit_msg_is "new templatecommit message"
 103'
 104
 105test_expect_success 'explicit commit message should override template' '
 106        echo "still more content" >> foo &&
 107        git add foo &&
 108        GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
 109                -m "command line msg" &&
 110        commit_msg_is "command line msg"
 111'
 112
 113test_expect_success 'commit message from file should override template' '
 114        echo "content galore" >> foo &&
 115        git add foo &&
 116        echo "standard input msg" |
 117        (
 118                test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
 119                git commit --template "$TEMPLATE" --file -
 120        ) &&
 121        commit_msg_is "standard input msg"
 122'
 123
 124cat >"$TEMPLATE" <<\EOF
 125
 126
 127### template
 128
 129EOF
 130test_expect_success 'commit message from template with whitespace issue' '
 131        echo "content galore" >>foo &&
 132        git add foo &&
 133        GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-whitespaced-content git commit \
 134                --template "$TEMPLATE" &&
 135        commit_msg_is "commit message"
 136'
 137
 138test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
 139
 140        cp .git/index saved-index &&
 141        (
 142                echo some new content >file &&
 143                GIT_INDEX_FILE=.git/another_index &&
 144                export GIT_INDEX_FILE &&
 145                git add file &&
 146                git commit -m "commit using another index" &&
 147                git diff-index --exit-code HEAD &&
 148                git diff-files --exit-code
 149        ) &&
 150        cmp .git/index saved-index >/dev/null
 151
 152'
 153
 154test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
 155
 156        cp .git/index saved-index &&
 157        (
 158                rm -f .git/no-such-index &&
 159                GIT_INDEX_FILE=.git/no-such-index &&
 160                export GIT_INDEX_FILE &&
 161                git commit -m "commit using nonexistent index" &&
 162                test -z "$(git ls-files)" &&
 163                test -z "$(git ls-tree HEAD)"
 164
 165        ) &&
 166        cmp .git/index saved-index >/dev/null
 167'
 168
 169cat > expect << EOF
 170zort
 171
 172Signed-off-by: C O Mitter <committer@example.com>
 173EOF
 174
 175test_expect_success '--signoff' '
 176        echo "yet another content *narf*" >> foo &&
 177        echo "zort" | git commit -s -F - foo &&
 178        git cat-file commit HEAD | sed "1,/^\$/d" > output &&
 179        test_cmp expect output
 180'
 181
 182test_expect_success 'commit message from file (1)' '
 183        mkdir subdir &&
 184        echo "Log in top directory" >log &&
 185        echo "Log in sub directory" >subdir/log &&
 186        (
 187                cd subdir &&
 188                git commit --allow-empty -F log
 189        ) &&
 190        commit_msg_is "Log in sub directory"
 191'
 192
 193test_expect_success 'commit message from file (2)' '
 194        rm -f log &&
 195        echo "Log in sub directory" >subdir/log &&
 196        (
 197                cd subdir &&
 198                git commit --allow-empty -F log
 199        ) &&
 200        commit_msg_is "Log in sub directory"
 201'
 202
 203test_expect_success 'commit message from stdin' '
 204        (
 205                cd subdir &&
 206                echo "Log with foo word" | git commit --allow-empty -F -
 207        ) &&
 208        commit_msg_is "Log with foo word"
 209'
 210
 211test_expect_success 'commit -F overrides -t' '
 212        (
 213                cd subdir &&
 214                echo "-F log" > f.log &&
 215                echo "-t template" > t.template &&
 216                git commit --allow-empty -F f.log -t t.template
 217        ) &&
 218        commit_msg_is "-F log"
 219'
 220
 221test_expect_success 'Commit without message is allowed with --allow-empty-message' '
 222        echo "more content" >>foo &&
 223        git add foo &&
 224        >empty &&
 225        git commit --allow-empty-message <empty &&
 226        commit_msg_is ""
 227'
 228
 229test_expect_success 'Commit without message is no-no without --allow-empty-message' '
 230        echo "more content" >>foo &&
 231        git add foo &&
 232        >empty &&
 233        test_must_fail git commit <empty
 234'
 235
 236test_expect_success 'Commit a message with --allow-empty-message' '
 237        echo "even more content" >>foo &&
 238        git add foo &&
 239        git commit --allow-empty-message -m"hello there" &&
 240        commit_msg_is "hello there"
 241'
 242
 243commit_for_rebase_autosquash_setup () {
 244        echo "first content line" >>foo &&
 245        git add foo &&
 246        cat >log <<EOF &&
 247target message subject line
 248
 249target message body line 1
 250target message body line 2
 251EOF
 252        git commit -F log &&
 253        echo "second content line" >>foo &&
 254        git add foo &&
 255        git commit -m "intermediate commit" &&
 256        echo "third content line" >>foo &&
 257        git add foo
 258}
 259
 260test_expect_success 'commit --fixup provides correct one-line commit message' '
 261        commit_for_rebase_autosquash_setup &&
 262        git commit --fixup HEAD~1 &&
 263        commit_msg_is "fixup! target message subject line"
 264'
 265
 266test_expect_success 'commit --squash works with -F' '
 267        commit_for_rebase_autosquash_setup &&
 268        echo "log message from file" >msgfile &&
 269        git commit --squash HEAD~1 -F msgfile  &&
 270        commit_msg_is "squash! target message subject linelog message from file"
 271'
 272
 273test_expect_success 'commit --squash works with -m' '
 274        commit_for_rebase_autosquash_setup &&
 275        git commit --squash HEAD~1 -m "foo bar\nbaz" &&
 276        commit_msg_is "squash! target message subject linefoo bar\nbaz"
 277'
 278
 279test_expect_success 'commit --squash works with -C' '
 280        commit_for_rebase_autosquash_setup &&
 281        git commit --squash HEAD~1 -C HEAD &&
 282        commit_msg_is "squash! target message subject lineintermediate commit"
 283'
 284
 285test_expect_success 'commit --squash works with -c' '
 286        commit_for_rebase_autosquash_setup &&
 287        test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
 288        git commit --squash HEAD~1 -c HEAD &&
 289        commit_msg_is "squash! target message subject lineedited commit"
 290'
 291
 292test_expect_success 'commit --squash works with -C for same commit' '
 293        commit_for_rebase_autosquash_setup &&
 294        git commit --squash HEAD -C HEAD &&
 295        commit_msg_is "squash! intermediate commit"
 296'
 297
 298test_expect_success 'commit --squash works with -c for same commit' '
 299        commit_for_rebase_autosquash_setup &&
 300        test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
 301        git commit --squash HEAD -c HEAD &&
 302        commit_msg_is "squash! edited commit"
 303'
 304
 305test_expect_success 'commit --squash works with editor' '
 306        commit_for_rebase_autosquash_setup &&
 307        test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
 308        git commit --squash HEAD~1 &&
 309        commit_msg_is "squash! target message subject linecommit message"
 310'
 311
 312test_expect_success 'invalid message options when using --fixup' '
 313        echo changes >>foo &&
 314        echo "message" >log &&
 315        git add foo &&
 316        test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
 317        test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
 318        test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
 319        test_must_fail git commit --fixup HEAD~1 -m "cmdline message" &&
 320        test_must_fail git commit --fixup HEAD~1 -F log
 321'
 322
 323test_done