t7500: rename commit tests script to comply with naming convention
[git/git.git] / t / t7500-commit-template-squash-signoff.sh
CommitLineData
d1cc130a
SG
1#!/bin/sh
2#
3# Copyright (c) 2007 Steven Grimm
4#
5
47a528ad 6test_description='git commit
d1cc130a 7
ea6cff84 8Tests for template, signoff, squash and -F functions.'
d1cc130a
SG
9
10. ./test-lib.sh
11
12commit_msg_is () {
d5a719e4
ÆAB
13 expect=commit_msg_is.expect
14 actual=commit_msg_is.actual
15
f66d000b
AP
16 printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" &&
17 printf "%s" "$1" >"$expect" &&
18 test_i18ncmp "$expect" "$actual"
d1cc130a
SG
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 &&
2140b140
JN
31 (
32 GIT_EDITOR="echo hello >\"\$1\"" &&
33 export GIT_EDITOR &&
34 test_must_fail git commit --template "$PWD"/notexist
35 )
d1cc130a
SG
36'
37
38test_expect_success 'nonexistent template file in config should return error' '
22179b30 39 test_config commit.template "$PWD"/notexist &&
2140b140
JN
40 (
41 GIT_EDITOR="echo hello >\"\$1\"" &&
42 export GIT_EDITOR &&
43 test_must_fail git commit
44 )
d1cc130a
SG
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" &&
d492b31c 52 test_must_fail git commit --template "$TEMPLATE"
d1cc130a
SG
53'
54
55test_expect_success 'unedited template with comments should not commit' '
56 echo "# comment in template" >> "$TEMPLATE" &&
d492b31c 57 test_must_fail git commit --template "$TEMPLATE"
d1cc130a
SG
58'
59
60test_expect_success 'a Signed-off-by line by itself should not commit' '
bfdbee98
JH
61 (
62 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
63 test_must_fail git commit --template "$TEMPLATE"
64 )
d1cc130a
SG
65'
66
67test_expect_success 'adding comments to a template should not commit' '
bfdbee98
JH
68 (
69 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
70 test_must_fail git commit --template "$TEMPLATE"
71 )
d1cc130a
SG
72'
73
29853c82 74test_expect_success 'adding real content to a template should commit' '
bfdbee98
JH
75 (
76 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
77 git commit --template "$TEMPLATE"
78 ) &&
d1cc130a
SG
79 commit_msg_is "template linecommit message"
80'
81
29853c82 82test_expect_success '-t option should be short for --template' '
d1cc130a
SG
83 echo "short template" > "$TEMPLATE" &&
84 echo "new content" >> foo &&
85 git add foo &&
bfdbee98
JH
86 (
87 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
88 git commit -t "$TEMPLATE"
89 ) &&
d1cc130a
SG
90 commit_msg_is "short templatecommit message"
91'
92
29853c82 93test_expect_success 'config-specified template should commit' '
d1cc130a 94 echo "new template" > "$TEMPLATE" &&
22179b30 95 test_config commit.template "$TEMPLATE" &&
d1cc130a
SG
96 echo "more content" >> foo &&
97 git add foo &&
bfdbee98
JH
98 (
99 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
100 git commit
101 ) &&
d1cc130a
SG
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 &&
bfdbee98 108 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
d1cc130a 109 -m "command line msg" &&
55246aac 110 commit_msg_is "command line msg"
d1cc130a
SG
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" |
bfdbee98
JH
117 (
118 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
119 git commit --template "$TEMPLATE" --file -
120 ) &&
55246aac 121 commit_msg_is "standard input msg"
d1cc130a
SG
122'
123
8b1ae678
BF
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 &&
567c53d0
JS
133 GIT_EDITOR=\""$TEST_DIRECTORY"\"/t7500/add-whitespaced-content \
134 git commit --template "$TEMPLATE" &&
8b1ae678
BF
135 commit_msg_is "commit message"
136'
137
859a4dbc
RV
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
13208572 167'
859a4dbc 168
13208572
JS
169cat > expect << EOF
170zort
2150554b 171
13208572
JS
172Signed-off-by: C O Mitter <committer@example.com>
173EOF
174
175test_expect_success '--signoff' '
176 echo "yet another content *narf*" >> foo &&
7eb5bbdb 177 echo "zort" | git commit -s -F - foo &&
9524cf29 178 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
bfdbee98 179 test_cmp expect output
859a4dbc
RV
180'
181
dbd0f5c7
JH
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
aae94ffb
SB
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
c9b5fde7
ÆAB
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 &&
076cbd63
JK
226 commit_msg_is "" &&
227 git tag empty-message-commit
c9b5fde7
ÆAB
228'
229
230test_expect_success 'Commit without message is no-no without --allow-empty-message' '
231 echo "more content" >>foo &&
232 git add foo &&
233 >empty &&
234 test_must_fail git commit <empty
235'
236
237test_expect_success 'Commit a message with --allow-empty-message' '
238 echo "even more content" >>foo &&
239 git add foo &&
240 git commit --allow-empty-message -m"hello there" &&
241 commit_msg_is "hello there"
242'
243
076cbd63
JK
244test_expect_success 'commit -C empty respects --allow-empty-message' '
245 echo more >>foo &&
246 git add foo &&
247 test_must_fail git commit -C empty-message-commit &&
248 git commit -C empty-message-commit --allow-empty-message &&
249 commit_msg_is ""
250'
251
b1a6c0a9
PN
252commit_for_rebase_autosquash_setup () {
253 echo "first content line" >>foo &&
254 git add foo &&
255 cat >log <<EOF &&
256target message subject line
257
258target message body line 1
259target message body line 2
260EOF
261 git commit -F log &&
262 echo "second content line" >>foo &&
263 git add foo &&
264 git commit -m "intermediate commit" &&
265 echo "third content line" >>foo &&
266 git add foo
267}
268
269test_expect_success 'commit --fixup provides correct one-line commit message' '
270 commit_for_rebase_autosquash_setup &&
271 git commit --fixup HEAD~1 &&
272 commit_msg_is "fixup! target message subject line"
273'
274
30884c9a
ÆAB
275test_expect_success 'commit --fixup -m"something" -m"extra"' '
276 commit_for_rebase_autosquash_setup &&
277 git commit --fixup HEAD~1 -m"something" -m"extra" &&
278 commit_msg_is "fixup! target message subject linesomething
279
280extra"
281'
282
7951bd30
PN
283test_expect_success 'commit --squash works with -F' '
284 commit_for_rebase_autosquash_setup &&
285 echo "log message from file" >msgfile &&
286 git commit --squash HEAD~1 -F msgfile &&
287 commit_msg_is "squash! target message subject linelog message from file"
288'
289
290test_expect_success 'commit --squash works with -m' '
291 commit_for_rebase_autosquash_setup &&
292 git commit --squash HEAD~1 -m "foo bar\nbaz" &&
293 commit_msg_is "squash! target message subject linefoo bar\nbaz"
294'
295
296test_expect_success 'commit --squash works with -C' '
297 commit_for_rebase_autosquash_setup &&
298 git commit --squash HEAD~1 -C HEAD &&
299 commit_msg_is "squash! target message subject lineintermediate commit"
300'
301
302test_expect_success 'commit --squash works with -c' '
303 commit_for_rebase_autosquash_setup &&
304 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
305 git commit --squash HEAD~1 -c HEAD &&
306 commit_msg_is "squash! target message subject lineedited commit"
307'
308
309test_expect_success 'commit --squash works with -C for same commit' '
310 commit_for_rebase_autosquash_setup &&
311 git commit --squash HEAD -C HEAD &&
312 commit_msg_is "squash! intermediate commit"
313'
314
315test_expect_success 'commit --squash works with -c for same commit' '
316 commit_for_rebase_autosquash_setup &&
317 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
318 git commit --squash HEAD -c HEAD &&
319 commit_msg_is "squash! edited commit"
320'
321
29853c82 322test_expect_success 'commit --squash works with editor' '
7951bd30
PN
323 commit_for_rebase_autosquash_setup &&
324 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
325 git commit --squash HEAD~1 &&
326 commit_msg_is "squash! target message subject linecommit message"
327'
328
b1a6c0a9
PN
329test_expect_success 'invalid message options when using --fixup' '
330 echo changes >>foo &&
331 echo "message" >log &&
332 git add foo &&
7951bd30 333 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
b1a6c0a9
PN
334 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
335 test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
b1a6c0a9
PN
336 test_must_fail git commit --fixup HEAD~1 -F log
337'
338
b3cf1b77
KS
339cat >expected-template <<EOF
340
341# Please enter the commit message for your changes. Lines starting
342# with '#' will be ignored, and an empty message aborts the commit.
343#
344# Author: A U Thor <author@example.com>
345#
346# On branch commit-template-check
347# Changes to be committed:
348# new file: commit-template-check
349#
350# Untracked files not listed
351EOF
352
353test_expect_success 'new line found before status message in commit template' '
354 git checkout -b commit-template-check &&
355 git reset --hard HEAD &&
356 touch commit-template-check &&
357 git add commit-template-check &&
358 GIT_EDITOR="cat >editor-input" git commit --untracked-files=no --allow-empty-message &&
359 test_i18ncmp expected-template editor-input
360'
361
3e73cc62
EN
362test_expect_success 'setup empty commit with unstaged rename and copy' '
363 test_create_repo unstaged_rename_and_copy &&
364 (
365 cd unstaged_rename_and_copy &&
366
367 echo content >orig &&
368 git add orig &&
369 test_commit orig &&
370
371 cp orig new_copy &&
372 mv orig new_rename &&
373 git add -N new_copy new_rename
374 )
375'
376
377test_expect_success 'check commit with unstaged rename and copy' '
378 (
379 cd unstaged_rename_and_copy &&
380
381 test_must_fail git -c diff.renames=copy commit
382 )
383'
384
d1cc130a 385test_done