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