]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7502-commit-porcelain.sh
Merge branch 'dl/reset-doc-no-wrt-abbrev'
[thirdparty/git.git] / t / t7502-commit-porcelain.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
0fcf760e 174 test_config diff.noprefix true &&
4f672ad6 175 git status -v >actual &&
0fcf760e 176 grep "diff --git negative negative" 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
fbfa0973
SG
232
233 # ------------------------ >8 ------------------------
234# to be kept, too
75df1f43
NTND
235# ------------------------ >8 ------------------------
236to be removed
fbfa0973
SG
237# ------------------------ >8 ------------------------
238to be removed, too
239EOF
240
241 cat >expect <<EOF &&
242# to be kept
243
244 # ------------------------ >8 ------------------------
245# to be kept, too
75df1f43 246EOF
75df1f43
NTND
247 git commit --cleanup=scissors -e -F text -a &&
248 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
249 test_cmp expect actual
fbfa0973 250'
75df1f43 251
fbfa0973
SG
252test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
253
254 echo >>negative &&
255 cat >text <<EOF &&
256# ------------------------ >8 ------------------------
257to be removed
258EOF
259 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
260 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
261 test_must_be_empty actual
75df1f43
NTND
262'
263
51fb3a3d 264test_expect_success 'cleanup commit messages (strip option,-F)' '
5f065737
AR
265
266 echo >>negative &&
267 { echo;echo "# text";echo sample;echo; } >text &&
268 echo sample >expect &&
269 git commit --cleanup=strip -F text -a &&
270 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
82ebb0b6 271 test_cmp expect actual
5f065737
AR
272
273'
274
51fb3a3d 275test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
5f065737
AR
276
277 echo >>negative &&
278 { echo;echo sample;echo; } >text &&
279 git commit -e -F text -a &&
0b430a17
ÆAB
280 head -n 4 .git/COMMIT_EDITMSG >actual
281'
282
283echo "sample
5f065737 284
0b430a17
ÆAB
285# Please enter the commit message for your changes. Lines starting
286# with '#' will be ignored, and an empty message aborts the commit." >expect
287
51fb3a3d 288test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
f79ce8db 289 test_i18ncmp expect actual
5f065737
AR
290'
291
51fb3a3d
RT
292test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
293 test_must_fail git commit --cleanup=non-existent
294'
295
296test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
297 test_must_fail git -c commit.cleanup=non-existent commit
298'
299
300test_expect_success 'cleanup commit message (no config and no option uses default)' '
301 echo content >>file &&
302 git add file &&
24e099f4
BC
303 (
304 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
305 git commit --no-status
306 ) &&
51fb3a3d
RT
307 commit_msg_is "commit message"
308'
309
310test_expect_success 'cleanup commit message (option overrides default)' '
311 echo content >>file &&
312 git add file &&
24e099f4
BC
313 (
314 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
315 git commit --cleanup=whitespace --no-status
316 ) &&
51fb3a3d
RT
317 commit_msg_is "commit message # comment"
318'
319
320test_expect_success 'cleanup commit message (config overrides default)' '
321 echo content >>file &&
322 git add file &&
24e099f4
BC
323 (
324 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
325 git -c commit.cleanup=whitespace commit --no-status
326 ) &&
51fb3a3d
RT
327 commit_msg_is "commit message # comment"
328'
329
330test_expect_success 'cleanup commit message (option overrides config)' '
331 echo content >>file &&
332 git add file &&
24e099f4
BC
333 (
334 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
335 git -c commit.cleanup=whitespace commit --cleanup=default
336 ) &&
51fb3a3d
RT
337 commit_msg_is "commit message"
338'
339
340test_expect_success 'cleanup commit message (default, -m)' '
341 echo content >>file &&
342 git add file &&
343 git commit -m "message #comment " &&
344 commit_msg_is "message #comment"
345'
346
347test_expect_success 'cleanup commit message (whitespace option, -m)' '
348 echo content >>file &&
349 git add file &&
350 git commit --cleanup=whitespace --no-status -m "message #comment " &&
351 commit_msg_is "message #comment"
352'
353
354test_expect_success 'cleanup commit message (whitespace config, -m)' '
355 echo content >>file &&
356 git add file &&
357 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
358 commit_msg_is "message #comment"
359'
360
1f4bf345 361test_expect_success 'message shows author when it is not equal to committer' '
e83dbe80 362 echo >>negative &&
ceacd91a 363 git commit -e -m "sample" -a &&
1f4bf345
JK
364 test_i18ngrep \
365 "^# Author: *A U Thor <author@example.com>\$" \
366 .git/COMMIT_EDITMSG
e83dbe80
SB
367'
368
b7242b8c
JK
369test_expect_success 'message shows date when it is explicitly set' '
370 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
371 test_i18ngrep \
372 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
373 .git/COMMIT_EDITMSG
374'
375
1d7dc264 376test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
bb1ae3f6
SB
377
378 echo >>negative &&
7845944c 379 (
00648ba0
EN
380 sane_unset GIT_COMMITTER_EMAIL &&
381 sane_unset GIT_COMMITTER_NAME &&
1d7dc264 382 git commit -e -m "sample" -a
7845944c 383 ) &&
1f4bf345
JK
384 # the ident is calculated from the system, so we cannot
385 # check the actual value, only that it is there
386 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
bb1ae3f6
SB
387'
388
a9ebc43b
JK
389write_script .git/FAKE_EDITOR <<EOF
390echo editor started > "$(pwd)/.git/result"
ec84bd00
PB
391exit 0
392EOF
ec84bd00 393
09feffb6 394test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
f84df81f 395 >.git/result &&
8c8b3bc3
JK
396
397 echo >>negative &&
398 (
399 sane_unset GIT_COMMITTER_EMAIL &&
400 sane_unset GIT_COMMITTER_NAME &&
401 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
402 export GIT_EDITOR &&
403 test_must_fail git commit -e -m sample -a
404 ) &&
d3c6751b 405 test_must_be_empty .git/result
8c8b3bc3
JK
406'
407
25206778
RS
408test_expect_success 'do not fire editor if -m <msg> was given' '
409 echo tick >file &&
410 git add file &&
411 echo "editor not started" >.git/result &&
412 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
413 test "$(cat .git/result)" = "editor not started"
414'
415
416test_expect_success 'do not fire editor if -m "" was given' '
417 echo tock >file &&
418 git add file &&
419 echo "editor not started" >.git/result &&
420 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
421 git commit -m "" --allow-empty-message) &&
422 test "$(cat .git/result)" = "editor not started"
423'
424
ec84bd00
PB
425test_expect_success 'do not fire editor in the presence of conflicts' '
426
a3c91e08
JH
427 git clean -f &&
428 echo f >g &&
429 git add g &&
430 git commit -m "add g" &&
431 git branch second &&
432 echo master >g &&
433 echo g >h &&
434 git add g h &&
435 git commit -m "modify g and add h" &&
436 git checkout second &&
437 echo second >g &&
438 git add g &&
439 git commit -m second &&
440 # Must fail due to conflict
441 test_must_fail git cherry-pick -n master &&
442 echo "editor not started" >.git/result &&
e2007832 443 (
34565f27 444 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
e2007832
BC
445 export GIT_EDITOR &&
446 test_must_fail git commit
447 ) &&
a3c91e08 448 test "$(cat .git/result)" = "editor not started"
ec84bd00
PB
449'
450
a9ebc43b 451write_script .git/FAKE_EDITOR <<EOF
ad5fa3cc
PB
452# kill -TERM command added below.
453EOF
454
fb9a2bea 455test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
ad5fa3cc 456 echo >>negative &&
09b78bc1 457 ! "$SHELL_PATH" -c '\''
ad5fa3cc 458 echo kill -TERM $$ >> .git/FAKE_EDITOR
09b78bc1
BC
459 GIT_EDITOR=.git/FAKE_EDITOR
460 export GIT_EDITOR
461 exec git commit -a'\'' &&
462 test ! -f .git/index.lock
ad5fa3cc
PB
463'
464
67bfc030
JH
465rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
466git reset -q --hard
467
468test_expect_success 'Hand committing of a redundant merge removes dups' '
469
470 git rev-parse second master >expect &&
471 test_must_fail git merge second master &&
472 git checkout master g &&
473 EDITOR=: git commit -a &&
474 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
475 test_cmp expect actual
476
477'
478
e5138436
JH
479test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
480
481 git reset --hard &&
482 git commit -s -m "hello: kitty" --allow-empty &&
483 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
3fb0459b 484 test_line_count = 3 actual
e5138436
JH
485
486'
487
8c613fd5
BC
488test_expect_success 'commit -s places sob on third line after two empty lines' '
489 git commit -s --allow-empty --allow-empty-message &&
490 cat <<-EOF >expect &&
491
492
493 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
494
495 EOF
496 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
497 test_cmp expect actual
498'
499
a9ebc43b
JK
500write_script .git/FAKE_EDITOR <<\EOF
501mv "$1" "$1.orig"
f9c01817
JH
502(
503 echo message
a9ebc43b
JK
504 cat "$1.orig"
505) >"$1"
f9c01817
JH
506EOF
507
508echo '## Custom template' >template
509
f9c01817
JH
510try_commit () {
511 git reset --hard &&
512 echo >>negative &&
513 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
514 case "$use_template" in
515 '')
f79ce8db 516 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
f9c01817 517 *)
f79ce8db 518 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
f9c01817
JH
519 esac
520}
521
522try_commit_status_combo () {
523
f79ce8db 524 test_expect_success 'commit' '
f9c01817 525 try_commit "" &&
f79ce8db 526 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
527 '
528
f79ce8db 529 test_expect_success 'commit --status' '
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' '
2dec68cf 535 try_commit --no-status &&
f79ce8db 536 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
537 '
538
f79ce8db 539 test_expect_success 'commit with commit.status = yes' '
e023a31d 540 test_config commit.status yes &&
f9c01817 541 try_commit "" &&
f79ce8db 542 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
543 '
544
f79ce8db 545 test_expect_success 'commit with commit.status = no' '
e023a31d 546 test_config commit.status no &&
f9c01817 547 try_commit "" &&
f79ce8db 548 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
549 '
550
f79ce8db 551 test_expect_success 'commit --status with commit.status = yes' '
e023a31d 552 test_config commit.status yes &&
f9c01817 553 try_commit --status &&
f79ce8db 554 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
555 '
556
f79ce8db 557 test_expect_success 'commit --no-status with commit.status = yes' '
e023a31d 558 test_config commit.status yes &&
f9c01817 559 try_commit --no-status &&
f79ce8db 560 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
561 '
562
f79ce8db 563 test_expect_success 'commit --status with commit.status = no' '
e023a31d 564 test_config commit.status no &&
f9c01817 565 try_commit --status &&
f79ce8db 566 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
567 '
568
f79ce8db 569 test_expect_success 'commit --no-status with commit.status = no' '
e023a31d 570 test_config commit.status no &&
f9c01817 571 try_commit --no-status &&
f79ce8db 572 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
f9c01817
JH
573 '
574
575}
576
577try_commit_status_combo
578
579use_template="-t template"
580
581try_commit_status_combo
582
eff80a9f 583test_expect_success 'commit --status with custom comment character' '
464be630 584 test_config core.commentchar ";" &&
eff80a9f
JH
585 try_commit --status &&
586 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
587'
588
84c9dc2c
NTND
589test_expect_success 'switch core.commentchar' '
590 test_commit "#foo" foo &&
591 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
592 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
593'
594
595test_expect_success 'switch core.commentchar but out of options' '
596 cat >text <<\EOF &&
597# 1
598; 2
599@ 3
600! 4
601$ 5
602% 6
603^ 7
604& 8
605| 9
606: 10
607EOF
608 git commit --amend -F text &&
609 (
610 test_set_editor .git/FAKE_EDITOR &&
611 test_must_fail git -c core.commentChar=auto commit --amend
612 )
613'
614
b468f0ce 615test_done