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