ls-files: convert show_ce_entry to take an index
[git/debian.git] / t / t7502-commit.sh
blob725687d5d5f26d6535b3f26d1fa221a8c567d57c
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
233 # ------------------------ >8 ------------------------
234 # to be kept, too
235 # ------------------------ >8 ------------------------
236 to be removed
237 # ------------------------ >8 ------------------------
238 to be removed, too
241 cat >expect <<EOF &&
242 # to be kept
244 # ------------------------ >8 ------------------------
245 # to be kept, too
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
252 test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
254 echo >>negative &&
255 cat >text <<EOF &&
256 # ------------------------ >8 ------------------------
257 to be removed
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
264 test_expect_success 'cleanup commit messages (strip option,-F)' '
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 &&
271 test_cmp expect actual
275 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
277 echo >>negative &&
278 { echo;echo sample;echo; } >text &&
279 git commit -e -F text -a &&
280 head -n 4 .git/COMMIT_EDITMSG >actual
283 echo "sample
285 # Please enter the commit message for your changes. Lines starting
286 # with '#' will be ignored, and an empty message aborts the commit." >expect
288 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
289 test_i18ncmp expect actual
292 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
293 test_must_fail git commit --cleanup=non-existent
296 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
297 test_must_fail git -c commit.cleanup=non-existent commit
300 test_expect_success 'cleanup commit message (no config and no option uses default)' '
301 echo content >>file &&
302 git add file &&
304 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
305 git commit --no-status
306 ) &&
307 commit_msg_is "commit message"
310 test_expect_success 'cleanup commit message (option overrides default)' '
311 echo content >>file &&
312 git add file &&
314 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
315 git commit --cleanup=whitespace --no-status
316 ) &&
317 commit_msg_is "commit message # comment"
320 test_expect_success 'cleanup commit message (config overrides default)' '
321 echo content >>file &&
322 git add file &&
324 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
325 git -c commit.cleanup=whitespace commit --no-status
326 ) &&
327 commit_msg_is "commit message # comment"
330 test_expect_success 'cleanup commit message (option overrides config)' '
331 echo content >>file &&
332 git add file &&
334 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
335 git -c commit.cleanup=whitespace commit --cleanup=default
336 ) &&
337 commit_msg_is "commit message"
340 test_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"
347 test_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"
354 test_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"
361 test_expect_success 'message shows author when it is not equal to committer' '
362 echo >>negative &&
363 git commit -e -m "sample" -a &&
364 test_i18ngrep \
365 "^# Author: *A U Thor <author@example.com>\$" \
366 .git/COMMIT_EDITMSG
369 test_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
376 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
378 echo >>negative &&
380 sane_unset GIT_COMMITTER_EMAIL &&
381 sane_unset GIT_COMMITTER_NAME &&
382 git commit -e -m "sample" -a
383 ) &&
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
389 write_script .git/FAKE_EDITOR <<EOF
390 echo editor started > "$(pwd)/.git/result"
391 exit 0
394 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
395 >.git/result &&
396 >expect &&
398 echo >>negative &&
400 sane_unset GIT_COMMITTER_EMAIL &&
401 sane_unset GIT_COMMITTER_NAME &&
402 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
403 export GIT_EDITOR &&
404 test_must_fail git commit -e -m sample -a
405 ) &&
406 test_cmp expect .git/result
409 test_expect_success 'do not fire editor if -m <msg> was given' '
410 echo tick >file &&
411 git add file &&
412 echo "editor not started" >.git/result &&
413 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
414 test "$(cat .git/result)" = "editor not started"
417 test_expect_success 'do not fire editor if -m "" was given' '
418 echo tock >file &&
419 git add file &&
420 echo "editor not started" >.git/result &&
421 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
422 git commit -m "" --allow-empty-message) &&
423 test "$(cat .git/result)" = "editor not started"
426 test_expect_success 'do not fire editor in the presence of conflicts' '
428 git clean -f &&
429 echo f >g &&
430 git add g &&
431 git commit -m "add g" &&
432 git branch second &&
433 echo master >g &&
434 echo g >h &&
435 git add g h &&
436 git commit -m "modify g and add h" &&
437 git checkout second &&
438 echo second >g &&
439 git add g &&
440 git commit -m second &&
441 # Must fail due to conflict
442 test_must_fail git cherry-pick -n master &&
443 echo "editor not started" >.git/result &&
445 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
446 export GIT_EDITOR &&
447 test_must_fail git commit
448 ) &&
449 test "$(cat .git/result)" = "editor not started"
452 write_script .git/FAKE_EDITOR <<EOF
453 # kill -TERM command added below.
456 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
457 echo >>negative &&
458 ! "$SHELL_PATH" -c '\''
459 echo kill -TERM $$ >> .git/FAKE_EDITOR
460 GIT_EDITOR=.git/FAKE_EDITOR
461 export GIT_EDITOR
462 exec git commit -a'\'' &&
463 test ! -f .git/index.lock
466 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
467 git reset -q --hard
469 test_expect_success 'Hand committing of a redundant merge removes dups' '
471 git rev-parse second master >expect &&
472 test_must_fail git merge second master &&
473 git checkout master g &&
474 EDITOR=: git commit -a &&
475 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
476 test_cmp expect actual
480 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
482 git reset --hard &&
483 git commit -s -m "hello: kitty" --allow-empty &&
484 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
485 test_line_count = 3 actual
489 test_expect_success 'commit -s places sob on third line after two empty lines' '
490 git commit -s --allow-empty --allow-empty-message &&
491 cat <<-EOF >expect &&
494 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
497 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
498 test_cmp expect actual
501 write_script .git/FAKE_EDITOR <<\EOF
502 mv "$1" "$1.orig"
504 echo message
505 cat "$1.orig"
506 ) >"$1"
509 echo '## Custom template' >template
511 try_commit () {
512 git reset --hard &&
513 echo >>negative &&
514 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
515 case "$use_template" in
517 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
519 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
520 esac
523 try_commit_status_combo () {
525 test_expect_success 'commit' '
526 try_commit "" &&
527 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
530 test_expect_success 'commit --status' '
531 try_commit --status &&
532 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
535 test_expect_success 'commit --no-status' '
536 try_commit --no-status &&
537 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
540 test_expect_success 'commit with commit.status = yes' '
541 test_config commit.status yes &&
542 try_commit "" &&
543 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
546 test_expect_success 'commit with commit.status = no' '
547 test_config commit.status no &&
548 try_commit "" &&
549 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
552 test_expect_success 'commit --status with commit.status = yes' '
553 test_config commit.status yes &&
554 try_commit --status &&
555 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
558 test_expect_success 'commit --no-status with commit.status = yes' '
559 test_config commit.status yes &&
560 try_commit --no-status &&
561 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
564 test_expect_success 'commit --status with commit.status = no' '
565 test_config commit.status no &&
566 try_commit --status &&
567 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
570 test_expect_success 'commit --no-status with commit.status = no' '
571 test_config commit.status no &&
572 try_commit --no-status &&
573 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
578 try_commit_status_combo
580 use_template="-t template"
582 try_commit_status_combo
584 test_expect_success 'commit --status with custom comment character' '
585 test_config core.commentchar ";" &&
586 try_commit --status &&
587 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
590 test_expect_success 'switch core.commentchar' '
591 test_commit "#foo" foo &&
592 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
593 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
596 test_expect_success 'switch core.commentchar but out of options' '
597 cat >text <<\EOF &&
607 : 10
609 git commit --amend -F text &&
611 test_set_editor .git/FAKE_EDITOR &&
612 test_must_fail git -c core.commentChar=auto commit --amend
616 test_done