remote-hg: fix new branch creation
[git.git] / t / t7502-commit.sh
bloba4938b1e4549d5082362ec3f5513ffe91f210d39
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 (strip option,-F)' '
228 echo >>negative &&
229 { echo;echo "# text";echo sample;echo; } >text &&
230 echo sample >expect &&
231 git commit --cleanup=strip -F text -a &&
232 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
233 test_cmp expect actual
237 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
239 echo >>negative &&
240 { echo;echo sample;echo; } >text &&
241 git commit -e -F text -a &&
242 head -n 4 .git/COMMIT_EDITMSG >actual
245 echo "sample
247 # Please enter the commit message for your changes. Lines starting
248 # with '#' will be ignored, and an empty message aborts the commit." >expect
250 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
251 test_i18ncmp expect actual
254 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
255 test_must_fail git commit --cleanup=non-existent
258 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
259 test_must_fail git -c commit.cleanup=non-existent commit
262 test_expect_success 'cleanup commit message (no config and no option uses default)' '
263 echo content >>file &&
264 git add file &&
266 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
267 git commit --no-status
268 ) &&
269 commit_msg_is "commit message"
272 test_expect_success 'cleanup commit message (option overrides default)' '
273 echo content >>file &&
274 git add file &&
276 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
277 git commit --cleanup=whitespace --no-status
278 ) &&
279 commit_msg_is "commit message # comment"
282 test_expect_success 'cleanup commit message (config overrides default)' '
283 echo content >>file &&
284 git add file &&
286 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
287 git -c commit.cleanup=whitespace commit --no-status
288 ) &&
289 commit_msg_is "commit message # comment"
292 test_expect_success 'cleanup commit message (option overrides config)' '
293 echo content >>file &&
294 git add file &&
296 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
297 git -c commit.cleanup=whitespace commit --cleanup=default
298 ) &&
299 commit_msg_is "commit message"
302 test_expect_success 'cleanup commit message (default, -m)' '
303 echo content >>file &&
304 git add file &&
305 git commit -m "message #comment " &&
306 commit_msg_is "message #comment"
309 test_expect_success 'cleanup commit message (whitespace option, -m)' '
310 echo content >>file &&
311 git add file &&
312 git commit --cleanup=whitespace --no-status -m "message #comment " &&
313 commit_msg_is "message #comment"
316 test_expect_success 'cleanup commit message (whitespace config, -m)' '
317 echo content >>file &&
318 git add file &&
319 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
320 commit_msg_is "message #comment"
323 test_expect_success 'message shows author when it is not equal to committer' '
324 echo >>negative &&
325 git commit -e -m "sample" -a &&
326 test_i18ngrep \
327 "^# Author: *A U Thor <author@example.com>\$" \
328 .git/COMMIT_EDITMSG
331 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
333 echo >>negative &&
335 sane_unset GIT_COMMITTER_EMAIL &&
336 sane_unset GIT_COMMITTER_NAME &&
337 git commit -e -m "sample" -a
338 ) &&
339 # the ident is calculated from the system, so we cannot
340 # check the actual value, only that it is there
341 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
344 write_script .git/FAKE_EDITOR <<EOF
345 echo editor started > "$(pwd)/.git/result"
346 exit 0
349 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
350 >.git/result
351 >expect &&
353 echo >>negative &&
355 sane_unset GIT_COMMITTER_EMAIL &&
356 sane_unset GIT_COMMITTER_NAME &&
357 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
358 export GIT_EDITOR &&
359 test_must_fail git commit -e -m sample -a
360 ) &&
361 test_cmp expect .git/result
364 test_expect_success 'do not fire editor in the presence of conflicts' '
366 git clean -f &&
367 echo f >g &&
368 git add g &&
369 git commit -m "add g" &&
370 git branch second &&
371 echo master >g &&
372 echo g >h &&
373 git add g h &&
374 git commit -m "modify g and add h" &&
375 git checkout second &&
376 echo second >g &&
377 git add g &&
378 git commit -m second &&
379 # Must fail due to conflict
380 test_must_fail git cherry-pick -n master &&
381 echo "editor not started" >.git/result &&
383 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
384 export GIT_EDITOR &&
385 test_must_fail git commit
386 ) &&
387 test "$(cat .git/result)" = "editor not started"
390 write_script .git/FAKE_EDITOR <<EOF
391 # kill -TERM command added below.
394 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
395 echo >>negative &&
396 ! "$SHELL_PATH" -c '\''
397 echo kill -TERM $$ >> .git/FAKE_EDITOR
398 GIT_EDITOR=.git/FAKE_EDITOR
399 export GIT_EDITOR
400 exec git commit -a'\'' &&
401 test ! -f .git/index.lock
404 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
405 git reset -q --hard
407 test_expect_success 'Hand committing of a redundant merge removes dups' '
409 git rev-parse second master >expect &&
410 test_must_fail git merge second master &&
411 git checkout master g &&
412 EDITOR=: git commit -a &&
413 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
414 test_cmp expect actual
418 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
420 git reset --hard &&
421 git commit -s -m "hello: kitty" --allow-empty &&
422 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
423 test_line_count = 3 actual
427 test_expect_success 'commit -s places sob on third line after two empty lines' '
428 git commit -s --allow-empty --allow-empty-message &&
429 cat <<-EOF >expect &&
432 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
435 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
436 test_cmp expect actual
439 write_script .git/FAKE_EDITOR <<\EOF
440 mv "$1" "$1.orig"
442 echo message
443 cat "$1.orig"
444 ) >"$1"
447 echo '## Custom template' >template
449 try_commit () {
450 git reset --hard &&
451 echo >>negative &&
452 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
453 case "$use_template" in
455 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
457 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
458 esac
461 try_commit_status_combo () {
463 test_expect_success 'commit' '
464 try_commit "" &&
465 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
468 test_expect_success 'commit' '
469 try_commit "" &&
470 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
473 test_expect_success 'commit --status' '
474 try_commit --status &&
475 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
478 test_expect_success 'commit --no-status' '
479 try_commit --no-status &&
480 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
483 test_expect_success 'commit with commit.status = yes' '
484 test_config commit.status yes &&
485 try_commit "" &&
486 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
489 test_expect_success 'commit with commit.status = no' '
490 test_config commit.status no &&
491 try_commit "" &&
492 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
495 test_expect_success 'commit --status with commit.status = yes' '
496 test_config commit.status yes &&
497 try_commit --status &&
498 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
501 test_expect_success 'commit --no-status with commit.status = yes' '
502 test_config commit.status yes &&
503 try_commit --no-status &&
504 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
507 test_expect_success 'commit --status with commit.status = no' '
508 test_config commit.status no &&
509 try_commit --status &&
510 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
513 test_expect_success 'commit --no-status with commit.status = no' '
514 test_config commit.status no &&
515 try_commit --no-status &&
516 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
521 try_commit_status_combo
523 use_template="-t template"
525 try_commit_status_combo
527 test_expect_success 'commit --status with custom comment character' '
528 test_config core.commentchar ";" &&
529 try_commit --status &&
530 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
533 test_done