commit: error out for missing commit message template
[git/mingw.git] / t / t7500-commit.sh
blob1590877c425c4ae4b884770ca812de7f1a5f359f
1 #!/bin/sh
3 # Copyright (c) 2007 Steven Grimm
6 test_description='git commit
8 Tests for selected commit options.'
10 . ./test-lib.sh
12 commit_msg_is () {
13 test "`git log --pretty=format:%s%b -1`" = "$1"
16 # A sanity check to see if commit is working at all.
17 test_expect_success 'a basic commit in an empty tree should succeed' '
18 echo content > foo &&
19 git add foo &&
20 git commit -m "initial commit"
23 test_expect_success 'nonexistent template file should return error' '
24 echo changes >> foo &&
25 git add foo &&
27 GIT_EDITOR="echo hello >\"\$1\"" &&
28 export GIT_EDITOR &&
29 test_must_fail git commit --template "$PWD"/notexist
33 test_expect_success 'nonexistent template file in config should return error' '
34 git config commit.template "$PWD"/notexist &&
35 test_when_finished "git config --unset commit.template" &&
37 GIT_EDITOR="echo hello >\"\$1\"" &&
38 export GIT_EDITOR &&
39 test_must_fail git commit
43 # From now on we'll use a template file that exists.
44 TEMPLATE="$PWD"/template
46 test_expect_success 'unedited template should not commit' '
47 echo "template line" > "$TEMPLATE" &&
48 test_must_fail git commit --template "$TEMPLATE"
51 test_expect_success 'unedited template with comments should not commit' '
52 echo "# comment in template" >> "$TEMPLATE" &&
53 test_must_fail git commit --template "$TEMPLATE"
56 test_expect_success 'a Signed-off-by line by itself should not commit' '
58 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
59 test_must_fail git commit --template "$TEMPLATE"
63 test_expect_success 'adding comments to a template should not commit' '
65 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
66 test_must_fail git commit --template "$TEMPLATE"
70 test_expect_success 'adding real content to a template should commit' '
72 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
73 git commit --template "$TEMPLATE"
74 ) &&
75 commit_msg_is "template linecommit message"
78 test_expect_success '-t option should be short for --template' '
79 echo "short template" > "$TEMPLATE" &&
80 echo "new content" >> foo &&
81 git add foo &&
83 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
84 git commit -t "$TEMPLATE"
85 ) &&
86 commit_msg_is "short templatecommit message"
89 test_expect_success 'config-specified template should commit' '
90 echo "new template" > "$TEMPLATE" &&
91 git config commit.template "$TEMPLATE" &&
92 echo "more content" >> foo &&
93 git add foo &&
95 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
96 git commit
97 ) &&
98 git config --unset commit.template &&
99 commit_msg_is "new templatecommit message"
102 test_expect_success 'explicit commit message should override template' '
103 echo "still more content" >> foo &&
104 git add foo &&
105 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
106 -m "command line msg" &&
107 commit_msg_is "command line msg"
110 test_expect_success 'commit message from file should override template' '
111 echo "content galore" >> foo &&
112 git add foo &&
113 echo "standard input msg" |
115 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
116 git commit --template "$TEMPLATE" --file -
117 ) &&
118 commit_msg_is "standard input msg"
121 test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
123 cp .git/index saved-index &&
125 echo some new content >file &&
126 GIT_INDEX_FILE=.git/another_index &&
127 export GIT_INDEX_FILE &&
128 git add file &&
129 git commit -m "commit using another index" &&
130 git diff-index --exit-code HEAD &&
131 git diff-files --exit-code
132 ) &&
133 cmp .git/index saved-index >/dev/null
137 test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
139 cp .git/index saved-index &&
141 rm -f .git/no-such-index &&
142 GIT_INDEX_FILE=.git/no-such-index &&
143 export GIT_INDEX_FILE &&
144 git commit -m "commit using nonexistent index" &&
145 test -z "$(git ls-files)" &&
146 test -z "$(git ls-tree HEAD)"
148 ) &&
149 cmp .git/index saved-index >/dev/null
152 cat > expect << EOF
153 zort
155 Signed-off-by: C O Mitter <committer@example.com>
158 test_expect_success '--signoff' '
159 echo "yet another content *narf*" >> foo &&
160 echo "zort" | git commit -s -F - foo &&
161 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
162 test_cmp expect output
165 test_expect_success 'commit message from file (1)' '
166 mkdir subdir &&
167 echo "Log in top directory" >log &&
168 echo "Log in sub directory" >subdir/log &&
170 cd subdir &&
171 git commit --allow-empty -F log
172 ) &&
173 commit_msg_is "Log in sub directory"
176 test_expect_success 'commit message from file (2)' '
177 rm -f log &&
178 echo "Log in sub directory" >subdir/log &&
180 cd subdir &&
181 git commit --allow-empty -F log
182 ) &&
183 commit_msg_is "Log in sub directory"
186 test_expect_success 'commit message from stdin' '
188 cd subdir &&
189 echo "Log with foo word" | git commit --allow-empty -F -
190 ) &&
191 commit_msg_is "Log with foo word"
194 test_expect_success 'commit -F overrides -t' '
196 cd subdir &&
197 echo "-F log" > f.log &&
198 echo "-t template" > t.template &&
199 git commit --allow-empty -F f.log -t t.template
200 ) &&
201 commit_msg_is "-F log"
204 test_expect_success 'Commit without message is allowed with --allow-empty-message' '
205 echo "more content" >>foo &&
206 git add foo &&
207 >empty &&
208 git commit --allow-empty-message <empty &&
209 commit_msg_is ""
212 test_expect_success 'Commit without message is no-no without --allow-empty-message' '
213 echo "more content" >>foo &&
214 git add foo &&
215 >empty &&
216 test_must_fail git commit <empty
219 test_expect_success 'Commit a message with --allow-empty-message' '
220 echo "even more content" >>foo &&
221 git add foo &&
222 git commit --allow-empty-message -m"hello there" &&
223 commit_msg_is "hello there"
226 test_done