Merge branch 'master' of github.com:Softcatala/git-po
[git/debian.git] / t / t7502-commit-porcelain.sh
blob6396897cc81804be174db6e37ccc7a8af8d648f1
1 #!/bin/sh
3 test_description='git commit porcelain-ish'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 commit_msg_is () {
11 expect=commit_msg_is.expect
12 actual=commit_msg_is.actual
14 printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
15 printf "%s" "$1" >$expect &&
16 test_cmp $expect $actual
19 # Arguments: [<prefix] [<commit message>] [<commit options>]
20 check_summary_oneline() {
21 test_tick &&
22 git commit ${3+"$3"} -m "$2" >raw &&
23 head -n 1 raw >act &&
25 # branch name
26 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
28 # append the "special" prefix, like "root-commit", "detached HEAD"
29 if test -n "$1"
30 then
31 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
34 # abbrev SHA-1
35 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
36 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
38 test_cmp exp act
41 test_expect_success 'output summary format' '
43 echo new >file1 &&
44 git add file1 &&
45 check_summary_oneline "root-commit" "initial" &&
47 echo change >>file1 &&
48 git add file1
51 test_expect_success 'output summary format: root-commit' '
52 check_summary_oneline "" "a change"
55 test_expect_success 'output summary format for commit with an empty diff' '
57 check_summary_oneline "" "empty" "--allow-empty"
60 test_expect_success 'output summary format for merges' '
62 git checkout -b recursive-base &&
63 test_commit base file1 &&
65 git checkout -b recursive-a recursive-base &&
66 test_commit commit-a file1 &&
68 git checkout -b recursive-b recursive-base &&
69 test_commit commit-b file1 &&
71 # conflict
72 git checkout recursive-a &&
73 test_must_fail git merge recursive-b &&
74 # resolve the conflict
75 echo commit-a >file1 &&
76 git add file1 &&
77 check_summary_oneline "" "Merge"
80 output_tests_cleanup() {
81 # this is needed for "do not fire editor in the presence of conflicts"
82 git checkout main &&
84 # this is needed for the "partial removal" test to pass
85 git rm file1 &&
86 git commit -m "cleanup"
89 test_expect_success 'the basics' '
91 output_tests_cleanup &&
93 echo doing partial >"commit is" &&
94 mkdir not &&
95 echo very much encouraged but we should >not/forbid &&
96 git add "commit is" not &&
97 echo update added "commit is" file >"commit is" &&
98 echo also update another >not/forbid &&
99 test_tick &&
100 git commit -a -m "initial with -a" &&
102 git cat-file blob HEAD:"commit is" >current.1 &&
103 git cat-file blob HEAD:not/forbid >current.2 &&
105 cmp current.1 "commit is" &&
106 cmp current.2 not/forbid
110 test_expect_success 'partial' '
112 echo another >"commit is" &&
113 echo another >not/forbid &&
114 test_tick &&
115 git commit -m "partial commit to handle a file" "commit is" &&
117 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
118 test "$changed" = "commit is"
122 test_expect_success 'partial modification in a subdirectory' '
124 test_tick &&
125 git commit -m "partial commit to subdirectory" not &&
127 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
128 test "$changed" = "not/forbid"
132 test_expect_success 'partial removal' '
134 git rm not/forbid &&
135 git commit -m "partial commit to remove not/forbid" not &&
137 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
138 test "$changed" = "not/forbid" &&
139 remain=$(git ls-tree -r --name-only HEAD) &&
140 test "$remain" = "commit is"
144 test_expect_success 'sign off' '
146 >positive &&
147 git add positive &&
148 git commit -s -m "thank you" &&
149 git cat-file commit HEAD >commit.msg &&
150 sed -ne "s/Signed-off-by: //p" commit.msg >actual &&
151 git var GIT_COMMITTER_IDENT >ident &&
152 sed -e "s/>.*/>/" ident >expected &&
153 test_cmp expected actual
157 test_expect_success 'multiple -m' '
159 >negative &&
160 git add negative &&
161 git commit -m "one" -m "two" -m "three" &&
162 actual=$(git cat-file commit HEAD >tmp && sed -e "1,/^\$/d" tmp && rm tmp) &&
163 expected=$(test_write_lines "one" "" "two" "" "three") &&
164 test "z$actual" = "z$expected"
168 test_expect_success 'verbose' '
170 echo minus >negative &&
171 git add negative &&
172 git status -v >raw &&
173 sed -ne "/^diff --git /p" raw >actual &&
174 echo "diff --git a/negative b/negative" >expect &&
175 test_cmp expect actual
179 test_expect_success 'verbose respects diff config' '
181 test_config diff.noprefix true &&
182 git status -v >actual &&
183 grep "diff --git negative negative" actual
186 mesg_with_comment_and_newlines='
187 # text
191 test_expect_success 'prepare file with comment line and trailing newlines' '
192 printf "%s" "$mesg_with_comment_and_newlines" >expect
195 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
197 echo >>negative &&
198 git commit --cleanup=verbatim --no-status -t expect -a &&
199 git cat-file -p HEAD >raw &&
200 sed -e "1,/^\$/d" raw >actual &&
201 test_cmp expect actual
205 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
207 echo >>negative &&
208 git commit --cleanup=verbatim -F expect -a &&
209 git cat-file -p HEAD >raw &&
210 sed -e "1,/^\$/d" raw >actual &&
211 test_cmp expect actual
215 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
217 echo >>negative &&
218 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
219 git cat-file -p HEAD >raw &&
220 sed -e "1,/^\$/d" raw >actual &&
221 test_cmp expect actual
225 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
227 echo >>negative &&
228 test_write_lines "" "# text" "" >text &&
229 echo "# text" >expect &&
230 git commit --cleanup=whitespace -F text -a &&
231 git cat-file -p HEAD >raw &&
232 sed -e "1,/^\$/d" raw >actual &&
233 test_cmp expect actual
237 test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
239 echo >>negative &&
240 cat >text <<-\EOF &&
242 # to be kept
244 # ------------------------ >8 ------------------------
245 # to be kept, too
246 # ------------------------ >8 ------------------------
247 to be removed
248 # ------------------------ >8 ------------------------
249 to be removed, too
252 cat >expect <<-\EOF &&
253 # to be kept
255 # ------------------------ >8 ------------------------
256 # to be kept, too
258 git commit --cleanup=scissors -e -F text -a &&
259 git cat-file -p HEAD >raw &&
260 sed -e "1,/^\$/d" raw >actual &&
261 test_cmp expect actual
264 test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
266 echo >>negative &&
267 cat >text <<-\EOF &&
268 # ------------------------ >8 ------------------------
269 to be removed
271 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
272 git cat-file -p HEAD >raw &&
273 sed -e "1,/^\$/d" raw >actual &&
274 test_must_be_empty actual
277 test_expect_success 'cleanup commit messages (strip option,-F)' '
279 echo >>negative &&
280 test_write_lines "" "# text" "sample" "" >text &&
281 echo sample >expect &&
282 git commit --cleanup=strip -F text -a &&
283 git cat-file -p HEAD >raw &&
284 sed -e "1,/^\$/d" raw >actual &&
285 test_cmp expect actual
289 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
291 echo >>negative &&
292 test_write_lines "" "sample" "" >text &&
293 git commit -e -F text -a &&
294 head -n 4 .git/COMMIT_EDITMSG >actual
297 echo "sample
299 # Please enter the commit message for your changes. Lines starting
300 # with '#' will be ignored, and an empty message aborts the commit." >expect
302 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
303 test_cmp expect actual
306 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
307 test_must_fail git commit --cleanup=non-existent
310 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
311 test_must_fail git -c commit.cleanup=non-existent commit
314 test_expect_success 'cleanup commit message (no config and no option uses default)' '
315 echo content >>file &&
316 git add file &&
318 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
319 git commit --no-status
320 ) &&
321 commit_msg_is "commit message"
324 test_expect_success 'cleanup commit message (option overrides default)' '
325 echo content >>file &&
326 git add file &&
328 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
329 git commit --cleanup=whitespace --no-status
330 ) &&
331 commit_msg_is "commit message # comment"
334 test_expect_success 'cleanup commit message (config overrides default)' '
335 echo content >>file &&
336 git add file &&
338 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
339 git -c commit.cleanup=whitespace commit --no-status
340 ) &&
341 commit_msg_is "commit message # comment"
344 test_expect_success 'cleanup commit message (option overrides config)' '
345 echo content >>file &&
346 git add file &&
348 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
349 git -c commit.cleanup=whitespace commit --cleanup=default
350 ) &&
351 commit_msg_is "commit message"
354 test_expect_success 'cleanup commit message (default, -m)' '
355 echo content >>file &&
356 git add file &&
357 git commit -m "message #comment " &&
358 commit_msg_is "message #comment"
361 test_expect_success 'cleanup commit message (whitespace option, -m)' '
362 echo content >>file &&
363 git add file &&
364 git commit --cleanup=whitespace --no-status -m "message #comment " &&
365 commit_msg_is "message #comment"
368 test_expect_success 'cleanup commit message (whitespace config, -m)' '
369 echo content >>file &&
370 git add file &&
371 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
372 commit_msg_is "message #comment"
375 test_expect_success 'message shows author when it is not equal to committer' '
376 echo >>negative &&
377 git commit -e -m "sample" -a &&
378 test_i18ngrep \
379 "^# Author: *A U Thor <author@example.com>\$" \
380 .git/COMMIT_EDITMSG
383 test_expect_success 'message shows date when it is explicitly set' '
384 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
385 test_i18ngrep \
386 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
387 .git/COMMIT_EDITMSG
390 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
392 echo >>negative &&
394 sane_unset GIT_COMMITTER_EMAIL &&
395 sane_unset GIT_COMMITTER_NAME &&
396 git commit -e -m "sample" -a
397 ) &&
398 # the ident is calculated from the system, so we cannot
399 # check the actual value, only that it is there
400 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
403 write_script .git/FAKE_EDITOR <<EOF
404 echo editor started >"$(pwd)/.git/result"
405 exit 0
408 test_expect_success !FAIL_PREREQS,!AUTOIDENT 'do not fire editor when committer is bogus' '
409 >.git/result &&
411 echo >>negative &&
413 sane_unset GIT_COMMITTER_EMAIL &&
414 sane_unset GIT_COMMITTER_NAME &&
415 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
416 export GIT_EDITOR &&
417 test_must_fail git commit -e -m sample -a
418 ) &&
419 test_must_be_empty .git/result
422 test_expect_success 'do not fire editor if -m <msg> was given' '
423 echo tick >file &&
424 git add file &&
425 echo "editor not started" >.git/result &&
426 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
427 test "$(cat .git/result)" = "editor not started"
430 test_expect_success 'do not fire editor if -m "" was given' '
431 echo tock >file &&
432 git add file &&
433 echo "editor not started" >.git/result &&
434 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
435 git commit -m "" --allow-empty-message) &&
436 test "$(cat .git/result)" = "editor not started"
439 test_expect_success 'do not fire editor in the presence of conflicts' '
441 git clean -f &&
442 echo f >g &&
443 git add g &&
444 git commit -m "add g" &&
445 git branch second &&
446 echo main >g &&
447 echo g >h &&
448 git add g h &&
449 git commit -m "modify g and add h" &&
450 git checkout second &&
451 echo second >g &&
452 git add g &&
453 git commit -m second &&
454 # Must fail due to conflict
455 test_must_fail git cherry-pick -n main &&
456 echo "editor not started" >.git/result &&
458 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
459 export GIT_EDITOR &&
460 test_must_fail git commit
461 ) &&
462 test "$(cat .git/result)" = "editor not started"
465 write_script .git/FAKE_EDITOR <<EOF
466 # kill -TERM command added below.
469 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
470 echo >>negative &&
471 ! "$SHELL_PATH" -c '\''
472 echo kill -TERM $$ >>.git/FAKE_EDITOR
473 GIT_EDITOR=.git/FAKE_EDITOR
474 export GIT_EDITOR
475 exec git commit -a'\'' &&
476 test ! -f .git/index.lock
479 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
480 git reset -q --hard
482 test_expect_success 'Hand committing of a redundant merge removes dups' '
484 git rev-parse second main >expect &&
485 test_must_fail git merge second main &&
486 git checkout main g &&
487 EDITOR=: git commit -a &&
488 git cat-file commit HEAD >raw &&
489 sed -n -e "s/^parent //p" -e "/^$/q" raw >actual &&
490 test_cmp expect actual
494 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
496 git reset --hard &&
497 git commit -s -m "hello: kitty" --allow-empty &&
498 git cat-file commit HEAD >raw &&
499 sed -e "1,/^$/d" raw >actual &&
500 test_line_count = 3 actual
504 test_expect_success 'commit -s places sob on third line after two empty lines' '
505 git commit -s --allow-empty --allow-empty-message &&
506 cat <<-EOF >expect &&
509 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
512 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
513 test_cmp expect actual
516 write_script .git/FAKE_EDITOR <<\EOF
517 mv "$1" "$1.orig"
519 echo message
520 cat "$1.orig"
521 ) >"$1"
524 echo '## Custom template' >template
526 try_commit () {
527 git reset --hard &&
528 echo >>negative &&
529 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
530 case "$use_template" in
532 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
534 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
535 esac
538 try_commit_status_combo () {
540 test_expect_success 'commit' '
541 try_commit "" &&
542 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
545 test_expect_success 'commit --status' '
546 try_commit --status &&
547 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
550 test_expect_success 'commit --no-status' '
551 try_commit --no-status &&
552 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
555 test_expect_success 'commit with commit.status = yes' '
556 test_config commit.status yes &&
557 try_commit "" &&
558 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
561 test_expect_success 'commit with commit.status = no' '
562 test_config commit.status no &&
563 try_commit "" &&
564 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
567 test_expect_success 'commit --status with commit.status = yes' '
568 test_config commit.status yes &&
569 try_commit --status &&
570 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
573 test_expect_success 'commit --no-status with commit.status = yes' '
574 test_config commit.status yes &&
575 try_commit --no-status &&
576 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
579 test_expect_success 'commit --status with commit.status = no' '
580 test_config commit.status no &&
581 try_commit --status &&
582 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
585 test_expect_success 'commit --no-status with commit.status = no' '
586 test_config commit.status no &&
587 try_commit --no-status &&
588 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
593 try_commit_status_combo
595 use_template="-t template"
597 try_commit_status_combo
599 test_expect_success 'commit --status with custom comment character' '
600 test_config core.commentchar ";" &&
601 try_commit --status &&
602 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
605 test_expect_success 'switch core.commentchar' '
606 test_commit "#foo" foo &&
607 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
608 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
611 test_expect_success 'switch core.commentchar but out of options' '
612 cat >text <<\EOF &&
622 : 10
624 git commit --amend -F text &&
626 test_set_editor .git/FAKE_EDITOR &&
627 test_must_fail git -c core.commentChar=auto commit --amend
631 test_done