Merge branch 'jk/sha1-file-reduce-useless-warnings' into maint
[git.git] / t / t7502-commit.sh
blob051489ea334c9693c2d19823ad23947703f66d90
1 #!/bin/sh
3 test_description='git commit porcelain-ish'
5 . ./test-lib.sh
7 commit_msg_is () {
8 expect=commit_msg_is.expect
9 actual=commit_msg_is.actual
11 printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
12 printf "%s" "$1" >$expect &&
13 test_i18ncmp $expect $actual
16 # Arguments: [<prefix] [<commit message>] [<commit options>]
17 check_summary_oneline() {
18 test_tick &&
19 git commit ${3+"$3"} -m "$2" | head -1 > act &&
21 # branch name
22 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
24 # append the "special" prefix, like "root-commit", "detached HEAD"
25 if test -n "$1"
26 then
27 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
30 # abbrev SHA-1
31 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
32 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
34 test_i18ncmp exp act
37 test_expect_success 'output summary format' '
39 echo new >file1 &&
40 git add file1 &&
41 check_summary_oneline "root-commit" "initial" &&
43 echo change >>file1 &&
44 git add file1
47 test_expect_success 'output summary format: root-commit' '
48 check_summary_oneline "" "a change"
51 test_expect_success 'output summary format for commit with an empty diff' '
53 check_summary_oneline "" "empty" "--allow-empty"
56 test_expect_success 'output summary format for merges' '
58 git checkout -b recursive-base &&
59 test_commit base file1 &&
61 git checkout -b recursive-a recursive-base &&
62 test_commit commit-a file1 &&
64 git checkout -b recursive-b recursive-base &&
65 test_commit commit-b file1 &&
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"
76 output_tests_cleanup() {
77 # this is needed for "do not fire editor in the presence of conflicts"
78 git checkout master &&
80 # this is needed for the "partial removal" test to pass
81 git rm file1 &&
82 git commit -m "cleanup"
85 test_expect_success 'the basics' '
87 output_tests_cleanup &&
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" &&
98 git cat-file blob HEAD:"commit is" >current.1 &&
99 git cat-file blob HEAD:not/forbid >current.2 &&
101 cmp current.1 "commit is" &&
102 cmp current.2 not/forbid
106 test_expect_success 'partial' '
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" &&
113 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
114 test "$changed" = "commit is"
118 test_expect_success 'partial modification in a subdirectory' '
120 test_tick &&
121 git commit -m "partial commit to subdirectory" not &&
123 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
124 test "$changed" = "not/forbid"
128 test_expect_success 'partial removal' '
130 git rm not/forbid &&
131 git commit -m "partial commit to remove not/forbid" not &&
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"
140 test_expect_success 'sign off' '
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"
151 test_expect_success 'multiple -m' '
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"
162 test_expect_success 'verbose' '
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 &&
168 test_cmp expect actual
172 test_expect_success 'verbose respects diff config' '
174 test_config color.diff always &&
175 git status -v >actual &&
176 grep "\[1mdiff --git" actual
179 mesg_with_comment_and_newlines='
180 # text
184 test_expect_success 'prepare file with comment line and trailing newlines' '
185 printf "%s" "$mesg_with_comment_and_newlines" >expect
188 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
190 echo >>negative &&
191 git commit --cleanup=verbatim --no-status -t expect -a &&
192 git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
193 test_cmp expect actual
197 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
199 echo >>negative &&
200 git commit --cleanup=verbatim -F expect -a &&
201 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
202 test_cmp expect actual
206 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
208 echo >>negative &&
209 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
210 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
211 test_cmp expect actual
215 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
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 &&
222 test_cmp expect actual
226 test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
228 echo >>negative &&
229 cat >text <<EOF &&
231 # to be kept
232 # ------------------------ >8 ------------------------
233 to be removed
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
242 test_expect_success 'cleanup commit messages (strip option,-F)' '
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 &&
249 test_cmp expect actual
253 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
255 echo >>negative &&
256 { echo;echo sample;echo; } >text &&
257 git commit -e -F text -a &&
258 head -n 4 .git/COMMIT_EDITMSG >actual
261 echo "sample
263 # Please enter the commit message for your changes. Lines starting
264 # with '#' will be ignored, and an empty message aborts the commit." >expect
266 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
267 test_i18ncmp expect actual
270 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
271 test_must_fail git commit --cleanup=non-existent
274 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
275 test_must_fail git -c commit.cleanup=non-existent commit
278 test_expect_success 'cleanup commit message (no config and no option uses default)' '
279 echo content >>file &&
280 git add file &&
282 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
283 git commit --no-status
284 ) &&
285 commit_msg_is "commit message"
288 test_expect_success 'cleanup commit message (option overrides default)' '
289 echo content >>file &&
290 git add file &&
292 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
293 git commit --cleanup=whitespace --no-status
294 ) &&
295 commit_msg_is "commit message # comment"
298 test_expect_success 'cleanup commit message (config overrides default)' '
299 echo content >>file &&
300 git add file &&
302 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
303 git -c commit.cleanup=whitespace commit --no-status
304 ) &&
305 commit_msg_is "commit message # comment"
308 test_expect_success 'cleanup commit message (option overrides config)' '
309 echo content >>file &&
310 git add file &&
312 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
313 git -c commit.cleanup=whitespace commit --cleanup=default
314 ) &&
315 commit_msg_is "commit message"
318 test_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"
325 test_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"
332 test_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"
339 test_expect_success 'message shows author when it is not equal to committer' '
340 echo >>negative &&
341 git commit -e -m "sample" -a &&
342 test_i18ngrep \
343 "^# Author: *A U Thor <author@example.com>\$" \
344 .git/COMMIT_EDITMSG
347 test_expect_success 'message shows date when it is explicitly set' '
348 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
349 test_i18ngrep \
350 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
351 .git/COMMIT_EDITMSG
354 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
356 echo >>negative &&
358 sane_unset GIT_COMMITTER_EMAIL &&
359 sane_unset GIT_COMMITTER_NAME &&
360 git commit -e -m "sample" -a
361 ) &&
362 # the ident is calculated from the system, so we cannot
363 # check the actual value, only that it is there
364 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
367 write_script .git/FAKE_EDITOR <<EOF
368 echo editor started > "$(pwd)/.git/result"
369 exit 0
372 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
373 >.git/result
374 >expect &&
376 echo >>negative &&
378 sane_unset GIT_COMMITTER_EMAIL &&
379 sane_unset GIT_COMMITTER_NAME &&
380 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
381 export GIT_EDITOR &&
382 test_must_fail git commit -e -m sample -a
383 ) &&
384 test_cmp expect .git/result
387 test_expect_success 'do not fire editor if -m <msg> was given' '
388 echo tick >file &&
389 git add file &&
390 echo "editor not started" >.git/result &&
391 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
392 test "$(cat .git/result)" = "editor not started"
395 test_expect_success 'do not fire editor if -m "" was given' '
396 echo tock >file &&
397 git add file &&
398 echo "editor not started" >.git/result &&
399 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
400 git commit -m "" --allow-empty-message) &&
401 test "$(cat .git/result)" = "editor not started"
404 test_expect_success 'do not fire editor in the presence of conflicts' '
406 git clean -f &&
407 echo f >g &&
408 git add g &&
409 git commit -m "add g" &&
410 git branch second &&
411 echo master >g &&
412 echo g >h &&
413 git add g h &&
414 git commit -m "modify g and add h" &&
415 git checkout second &&
416 echo second >g &&
417 git add g &&
418 git commit -m second &&
419 # Must fail due to conflict
420 test_must_fail git cherry-pick -n master &&
421 echo "editor not started" >.git/result &&
423 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
424 export GIT_EDITOR &&
425 test_must_fail git commit
426 ) &&
427 test "$(cat .git/result)" = "editor not started"
430 write_script .git/FAKE_EDITOR <<EOF
431 # kill -TERM command added below.
434 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
435 echo >>negative &&
436 ! "$SHELL_PATH" -c '\''
437 echo kill -TERM $$ >> .git/FAKE_EDITOR
438 GIT_EDITOR=.git/FAKE_EDITOR
439 export GIT_EDITOR
440 exec git commit -a'\'' &&
441 test ! -f .git/index.lock
444 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
445 git reset -q --hard
447 test_expect_success 'Hand committing of a redundant merge removes dups' '
449 git rev-parse second master >expect &&
450 test_must_fail git merge second master &&
451 git checkout master g &&
452 EDITOR=: git commit -a &&
453 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
454 test_cmp expect actual
458 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
460 git reset --hard &&
461 git commit -s -m "hello: kitty" --allow-empty &&
462 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
463 test_line_count = 3 actual
467 test_expect_success 'commit -s places sob on third line after two empty lines' '
468 git commit -s --allow-empty --allow-empty-message &&
469 cat <<-EOF >expect &&
472 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
475 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
476 test_cmp expect actual
479 write_script .git/FAKE_EDITOR <<\EOF
480 mv "$1" "$1.orig"
482 echo message
483 cat "$1.orig"
484 ) >"$1"
487 echo '## Custom template' >template
489 try_commit () {
490 git reset --hard &&
491 echo >>negative &&
492 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
493 case "$use_template" in
495 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
497 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
498 esac
501 try_commit_status_combo () {
503 test_expect_success 'commit' '
504 try_commit "" &&
505 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
508 test_expect_success 'commit' '
509 try_commit "" &&
510 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
513 test_expect_success 'commit --status' '
514 try_commit --status &&
515 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
518 test_expect_success 'commit --no-status' '
519 try_commit --no-status &&
520 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
523 test_expect_success 'commit with commit.status = yes' '
524 test_config commit.status yes &&
525 try_commit "" &&
526 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
529 test_expect_success 'commit with commit.status = no' '
530 test_config commit.status no &&
531 try_commit "" &&
532 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
535 test_expect_success 'commit --status with commit.status = yes' '
536 test_config commit.status yes &&
537 try_commit --status &&
538 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
541 test_expect_success 'commit --no-status with commit.status = yes' '
542 test_config commit.status yes &&
543 try_commit --no-status &&
544 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
547 test_expect_success 'commit --status with commit.status = no' '
548 test_config commit.status no &&
549 try_commit --status &&
550 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
553 test_expect_success 'commit --no-status with commit.status = no' '
554 test_config commit.status no &&
555 try_commit --no-status &&
556 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
561 try_commit_status_combo
563 use_template="-t template"
565 try_commit_status_combo
567 test_expect_success 'commit --status with custom comment character' '
568 test_config core.commentchar ";" &&
569 try_commit --status &&
570 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
573 test_expect_success 'switch core.commentchar' '
574 test_commit "#foo" foo &&
575 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
576 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
579 test_expect_success 'switch core.commentchar but out of options' '
580 cat >text <<\EOF &&
590 : 10
592 git commit --amend -F text &&
594 test_set_editor .git/FAKE_EDITOR &&
595 test_must_fail git -c core.commentChar=auto commit --amend
599 test_done