]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7502-commit.sh
config: be strict on core.commentChar
[thirdparty/git.git] / t / t7502-commit.sh
CommitLineData
b468f0ce
JH
1#!/bin/sh
2
3test_description='git commit porcelain-ish'
4
5. ./test-lib.sh
6
51fb3a3d
RT
7commit_msg_is () {
8 expect=commit_msg_is.expect
9 actual=commit_msg_is.actual
10
11 printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
12 printf "%s" "$1" >$expect &&
13 test_i18ncmp $expect $actual
14}
15
cee9f2b3 16# Arguments: [<prefix] [<commit message>] [<commit options>]
fc6fa0d0
TRC
17check_summary_oneline() {
18 test_tick &&
cee9f2b3 19 git commit ${3+"$3"} -m "$2" | head -1 > act &&
fc6fa0d0
TRC
20
21 # branch name
22 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
23
24 # append the "special" prefix, like "root-commit", "detached HEAD"
25 if test -n "$1"
26 then
27 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
28 fi
29
30 # abbrev SHA-1
31 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
32 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
33
f79ce8db 34 test_i18ncmp exp act
fc6fa0d0
TRC
35}
36
37test_expect_success 'output summary format' '
38
39 echo new >file1 &&
40 git add file1 &&
41 check_summary_oneline "root-commit" "initial" &&
42
43 echo change >>file1 &&
7f5673d7
ÆAB
44 git add file1
45'
46
47test_expect_success 'output summary format: root-commit' '
fc6fa0d0
TRC
48 check_summary_oneline "" "a change"
49'
50
a45e1a87 51test_expect_success 'output summary format for commit with an empty diff' '
cee9f2b3
TRC
52
53 check_summary_oneline "" "empty" "--allow-empty"
54'
55
a45e1a87 56test_expect_success 'output summary format for merges' '
cee9f2b3
TRC
57
58 git checkout -b recursive-base &&
59 test_commit base file1 &&
60
61 git checkout -b recursive-a recursive-base &&
62 test_commit commit-a file1 &&
63
64 git checkout -b recursive-b recursive-base &&
65 test_commit commit-b file1 &&
66
67 # conflict
68 git checkout recursive-a &&
69 test_must_fail git merge recursive-b &&
70 # resolve the conflict
71 echo commit-a > file1 &&
72 git add file1 &&
73 check_summary_oneline "" "Merge"
74'
75
fc6fa0d0
TRC
76output_tests_cleanup() {
77 # this is needed for "do not fire editor in the presence of conflicts"
78 git checkout master &&
79
80 # this is needed for the "partial removal" test to pass
81 git rm file1 &&
82 git commit -m "cleanup"
83}
84
b468f0ce
JH
85test_expect_success 'the basics' '
86
fc6fa0d0
TRC
87 output_tests_cleanup &&
88
b468f0ce
JH
89 echo doing partial >"commit is" &&
90 mkdir not &&
91 echo very much encouraged but we should >not/forbid &&
92 git add "commit is" not &&
93 echo update added "commit is" file >"commit is" &&
94 echo also update another >not/forbid &&
95 test_tick &&
96 git commit -a -m "initial with -a" &&
97
98 git cat-file blob HEAD:"commit is" >current.1 &&
99 git cat-file blob HEAD:not/forbid >current.2 &&
100
101 cmp current.1 "commit is" &&
102 cmp current.2 not/forbid
103
104'
105
106test_expect_success 'partial' '
107
108 echo another >"commit is" &&
109 echo another >not/forbid &&
110 test_tick &&
111 git commit -m "partial commit to handle a file" "commit is" &&
112
113 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
114 test "$changed" = "commit is"
115
116'
117
e8f30160 118test_expect_success 'partial modification in a subdirectory' '
b468f0ce
JH
119
120 test_tick &&
121 git commit -m "partial commit to subdirectory" not &&
122
123 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
124 test "$changed" = "not/forbid"
125
126'
127
128test_expect_success 'partial removal' '
129
130 git rm not/forbid &&
131 git commit -m "partial commit to remove not/forbid" not &&
132
133 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
134 test "$changed" = "not/forbid" &&
135 remain=$(git ls-tree -r --name-only HEAD) &&
136 test "$remain" = "commit is"
137
138'
139
140test_expect_success 'sign off' '
141
142 >positive &&
143 git add positive &&
144 git commit -s -m "thank you" &&
145 actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
146 expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
147 test "z$actual" = "z$expected"
148
149'
150
151test_expect_success 'multiple -m' '
152
153 >negative &&
154 git add negative &&
155 git commit -m "one" -m "two" -m "three" &&
156 actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
157 expected=$(echo one; echo; echo two; echo; echo three) &&
158 test "z$actual" = "z$expected"
159
160'
161
162test_expect_success 'verbose' '
163
164 echo minus >negative &&
165 git add negative &&
166 git status -v | sed -ne "/^diff --git /p" >actual &&
167 echo "diff --git a/negative b/negative" >expect &&
82ebb0b6 168 test_cmp expect actual
b468f0ce
JH
169
170'
171
4f672ad6
JK
172test_expect_success 'verbose respects diff config' '
173
464be630 174 test_config color.diff always &&
4f672ad6 175 git status -v >actual &&
464be630 176 grep "\[1mdiff --git" actual
4f672ad6
JK
177'
178
5b012c80
BC
179mesg_with_comment_and_newlines='
180# text
181
182'
183
184test_expect_success 'prepare file with comment line and trailing newlines' '
185 printf "%s" "$mesg_with_comment_and_newlines" >expect
186'
187
51fb3a3d 188test_expect_success 'cleanup commit messages (verbatim option,-t)' '
5f065737
AR
189
190 echo >>negative &&
67dabab0
BC
191 git commit --cleanup=verbatim --no-status -t expect -a &&
192 git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
82ebb0b6 193 test_cmp expect actual
5f065737
AR
194
195'
196
51fb3a3d 197test_expect_success 'cleanup commit messages (verbatim option,-F)' '
5f065737
AR
198
199 echo >>negative &&
200 git commit --cleanup=verbatim -F expect -a &&
201 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
82ebb0b6 202 test_cmp expect actual
5f065737
AR
203
204'
205
a24a41ea 206test_expect_success 'cleanup commit messages (verbatim option,-m)' '
5f065737
AR
207
208 echo >>negative &&
5b012c80 209 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
5f065737 210 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
82ebb0b6 211 test_cmp expect actual
5f065737
AR
212
213'
214
51fb3a3d 215test_expect_success 'cleanup commit messages (whitespace option,-F)' '
5f065737
AR
216
217 echo >>negative &&
218 { echo;echo "# text";echo; } >text &&
219 echo "# text" >expect &&
220 git commit --cleanup=whitespace -F text -a &&
221 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
82ebb0b6 222 test_cmp expect actual
5f065737
AR
223
224'
225
75df1f43
NTND
226test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
227
228 echo >>negative &&
229 cat >text <<EOF &&
230
231# to be kept
232# ------------------------ >8 ------------------------
233to be removed
234EOF
235 echo "# to be kept" >expect &&
236 git commit --cleanup=scissors -e -F text -a &&
237 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
238 test_cmp expect actual
239
240'
241
51fb3a3d 242test_expect_success 'cleanup commit messages (strip option,-F)' '
5f065737
AR
243
244 echo >>negative &&
245 { echo;echo "# text";echo sample;echo; } >text &&
246 echo sample >expect &&
247 git commit --cleanup=strip -F text -a &&
248 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
82ebb0b6 249 test_cmp expect actual
5f065737
AR
250
251'
252
51fb3a3d 253test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
5f065737
AR
254
255 echo >>negative &&
256 { echo;echo sample;echo; } >text &&
257 git commit -e -F text -a &&
0b430a17
ÆAB
258 head -n 4 .git/COMMIT_EDITMSG >actual
259'
260
261echo "sample
5f065737 262
0b430a17
ÆAB
263# Please enter the commit message for your changes. Lines starting
264# with '#' will be ignored, and an empty message aborts the commit." >expect
265
51fb3a3d 266test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
f79ce8db 267 test_i18ncmp expect actual
5f065737
AR
268'
269
51fb3a3d
RT
270test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
271 test_must_fail git commit --cleanup=non-existent
272'
273
274test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
275 test_must_fail git -c commit.cleanup=non-existent commit
276'
277
278test_expect_success 'cleanup commit message (no config and no option uses default)' '
279 echo content >>file &&
280 git add file &&
24e099f4
BC
281 (
282 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
283 git commit --no-status
284 ) &&
51fb3a3d
RT
285 commit_msg_is "commit message"
286'
287
288test_expect_success 'cleanup commit message (option overrides default)' '
289 echo content >>file &&
290 git add file &&
24e099f4
BC
291 (
292 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
293 git commit --cleanup=whitespace --no-status
294 ) &&
51fb3a3d
RT
295 commit_msg_is "commit message # comment"
296'
297
298test_expect_success 'cleanup commit message (config overrides default)' '
299 echo content >>file &&
300 git add file &&
24e099f4
BC
301 (
302 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
303 git -c commit.cleanup=whitespace commit --no-status
304 ) &&
51fb3a3d
RT
305 commit_msg_is "commit message # comment"
306'
307
308test_expect_success 'cleanup commit message (option overrides config)' '
309 echo content >>file &&
310 git add file &&
24e099f4
BC
311 (
312 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
313 git -c commit.cleanup=whitespace commit --cleanup=default
314 ) &&
51fb3a3d
RT
315 commit_msg_is "commit message"
316'
317
318test_expect_success 'cleanup commit message (default, -m)' '
319 echo content >>file &&
320 git add file &&
321 git commit -m "message #comment " &&
322 commit_msg_is "message #comment"
323'
324
325test_expect_success 'cleanup commit message (whitespace option, -m)' '
326 echo content >>file &&
327 git add file &&
328 git commit --cleanup=whitespace --no-status -m "message #comment " &&
329 commit_msg_is "message #comment"
330'
331
332test_expect_success 'cleanup commit message (whitespace config, -m)' '
333 echo content >>file &&
334 git add file &&
335 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
336 commit_msg_is "message #comment"
337'
338
1f4bf345 339test_expect_success 'message shows author when it is not equal to committer' '
e83dbe80 340 echo >>negative &&
ceacd91a 341 git commit -e -m "sample" -a &&
1f4bf345
JK
342 test_i18ngrep \
343 "^# Author: *A U Thor <author@example.com>\$" \
344 .git/COMMIT_EDITMSG
e83dbe80
SB
345'
346
1d7dc264 347test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
bb1ae3f6
SB
348
349 echo >>negative &&
7845944c 350 (
00648ba0
EN
351 sane_unset GIT_COMMITTER_EMAIL &&
352 sane_unset GIT_COMMITTER_NAME &&
1d7dc264 353 git commit -e -m "sample" -a
7845944c 354 ) &&
1f4bf345
JK
355 # the ident is calculated from the system, so we cannot
356 # check the actual value, only that it is there
357 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
bb1ae3f6
SB
358'
359
a9ebc43b
JK
360write_script .git/FAKE_EDITOR <<EOF
361echo editor started > "$(pwd)/.git/result"
ec84bd00
PB
362exit 0
363EOF
ec84bd00 364
09feffb6 365test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
8c8b3bc3
JK
366 >.git/result
367 >expect &&
368
369 echo >>negative &&
370 (
371 sane_unset GIT_COMMITTER_EMAIL &&
372 sane_unset GIT_COMMITTER_NAME &&
373 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
374 export GIT_EDITOR &&
375 test_must_fail git commit -e -m sample -a
376 ) &&
377 test_cmp expect .git/result
378'
379
25206778
RS
380test_expect_success 'do not fire editor if -m <msg> was given' '
381 echo tick >file &&
382 git add file &&
383 echo "editor not started" >.git/result &&
384 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
385 test "$(cat .git/result)" = "editor not started"
386'
387
388test_expect_success 'do not fire editor if -m "" was given' '
389 echo tock >file &&
390 git add file &&
391 echo "editor not started" >.git/result &&
392 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
393 git commit -m "" --allow-empty-message) &&
394 test "$(cat .git/result)" = "editor not started"
395'
396
ec84bd00
PB
397test_expect_success 'do not fire editor in the presence of conflicts' '
398
a3c91e08
JH
399 git clean -f &&
400 echo f >g &&
401 git add g &&
402 git commit -m "add g" &&
403 git branch second &&
404 echo master >g &&
405 echo g >h &&
406 git add g h &&
407 git commit -m "modify g and add h" &&
408 git checkout second &&
409 echo second >g &&
410 git add g &&
411 git commit -m second &&
412 # Must fail due to conflict
413 test_must_fail git cherry-pick -n master &&
414 echo "editor not started" >.git/result &&
e2007832 415 (
34565f27 416 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
e2007832
BC
417 export GIT_EDITOR &&
418 test_must_fail git commit
419 ) &&
a3c91e08 420 test "$(cat .git/result)" = "editor not started"
ec84bd00
PB
421'
422
a9ebc43b 423write_script .git/FAKE_EDITOR <<EOF
ad5fa3cc
PB
424# kill -TERM command added below.
425EOF
426
fb9a2bea 427test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
ad5fa3cc 428 echo >>negative &&
09b78bc1 429 ! "$SHELL_PATH" -c '\''
ad5fa3cc 430 echo kill -TERM $$ >> .git/FAKE_EDITOR
09b78bc1
BC
431 GIT_EDITOR=.git/FAKE_EDITOR
432 export GIT_EDITOR
433 exec git commit -a'\'' &&
434 test ! -f .git/index.lock
ad5fa3cc
PB
435'
436
67bfc030
JH
437rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
438git reset -q --hard
439
440test_expect_success 'Hand committing of a redundant merge removes dups' '
441
442 git rev-parse second master >expect &&
443 test_must_fail git merge second master &&
444 git checkout master g &&
445 EDITOR=: git commit -a &&
446 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
447 test_cmp expect actual
448
449'
450
e5138436
JH
451test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
452
453 git reset --hard &&
454 git commit -s -m "hello: kitty" --allow-empty &&
455 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
3fb0459b 456 test_line_count = 3 actual
e5138436
JH
457
458'
459
8c613fd5
BC
460test_expect_success 'commit -s places sob on third line after two empty lines' '
461 git commit -s --allow-empty --allow-empty-message &&
462 cat <<-EOF >expect &&
463
464
465 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
466
467 EOF
468 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
469 test_cmp expect actual
470'
471
a9ebc43b
JK
472write_script .git/FAKE_EDITOR <<\EOF
473mv "$1" "$1.orig"
f9c01817
JH
474(
475 echo message
a9ebc43b
JK
476 cat "$1.orig"
477) >"$1"
f9c01817
JH
478EOF
479
480echo '## Custom template' >template
481
f9c01817
JH
482try_commit () {
483 git reset --hard &&
484 echo >>negative &&
485 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
486 case "$use_template" in
487 '')
f79ce8db 488 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
f9c01817 489 *)
f79ce8db 490 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
f9c01817
JH
491 esac
492}
493
494try_commit_status_combo () {
495
f79ce8db 496 test_expect_success 'commit' '
f9c01817 497 try_commit "" &&
f79ce8db 498 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
499 '
500
f79ce8db 501 test_expect_success 'commit' '
f9c01817 502 try_commit "" &&
f79ce8db 503 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
504 '
505
f79ce8db 506 test_expect_success 'commit --status' '
f9c01817 507 try_commit --status &&
f79ce8db 508 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
509 '
510
f79ce8db 511 test_expect_success 'commit --no-status' '
2dec68cf 512 try_commit --no-status &&
f79ce8db 513 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
514 '
515
f79ce8db 516 test_expect_success 'commit with commit.status = yes' '
e023a31d 517 test_config commit.status yes &&
f9c01817 518 try_commit "" &&
f79ce8db 519 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
520 '
521
f79ce8db 522 test_expect_success 'commit with commit.status = no' '
e023a31d 523 test_config commit.status no &&
f9c01817 524 try_commit "" &&
f79ce8db 525 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
526 '
527
f79ce8db 528 test_expect_success 'commit --status with commit.status = yes' '
e023a31d 529 test_config commit.status yes &&
f9c01817 530 try_commit --status &&
f79ce8db 531 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
532 '
533
f79ce8db 534 test_expect_success 'commit --no-status with commit.status = yes' '
e023a31d 535 test_config commit.status yes &&
f9c01817 536 try_commit --no-status &&
f79ce8db 537 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
538 '
539
f79ce8db 540 test_expect_success 'commit --status with commit.status = no' '
e023a31d 541 test_config commit.status no &&
f9c01817 542 try_commit --status &&
f79ce8db 543 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
544 '
545
f79ce8db 546 test_expect_success 'commit --no-status with commit.status = no' '
e023a31d 547 test_config commit.status no &&
f9c01817 548 try_commit --no-status &&
f79ce8db 549 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
550 '
551
552}
553
554try_commit_status_combo
555
556use_template="-t template"
557
558try_commit_status_combo
559
eff80a9f 560test_expect_success 'commit --status with custom comment character' '
464be630 561 test_config core.commentchar ";" &&
eff80a9f
JH
562 try_commit --status &&
563 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
564'
565
b468f0ce 566test_done