Merge branch 'ar/submodule-add-config'
[git/debian.git] / t / t7501-commit-basic-functionality.sh
blob512ae2781fe2c9b02a5a37f5230ec14992114ae7
1 #!/bin/sh
3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, -i and -o, test reflog,
7 # signoff
9 test_description='git commit'
10 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
11 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13 . ./test-lib.sh
14 . "$TEST_DIRECTORY/lib-diff.sh"
16 author='The Real Author <someguy@his.email.org>'
18 test_tick
20 test_expect_success 'initial status' '
21 echo bongo bongo >file &&
22 git add file &&
23 git status >actual &&
24 test_i18ngrep "No commits yet" actual
27 test_expect_success 'fail initial amend' '
28 test_must_fail git commit --amend
31 test_expect_success 'setup: initial commit' '
32 git commit -m initial
35 test_expect_success '-m and -F do not mix' '
36 git checkout HEAD file && echo >>file && git add file &&
37 test_must_fail git commit -m foo -m bar -F file
40 test_expect_success '-m and -C do not mix' '
41 git checkout HEAD file && echo >>file && git add file &&
42 test_must_fail git commit -C HEAD -m illegal
45 test_expect_success 'paths and -a do not mix' '
46 echo King of the bongo >file &&
47 test_must_fail git commit -m foo -a file
50 test_expect_success PERL 'can use paths with --interactive' '
51 echo bong-o-bong >file &&
52 # 2: update, 1:st path, that is all, 7: quit
53 test_write_lines 2 1 "" 7 |
54 git commit -m foo --interactive file &&
55 git reset --hard HEAD^
58 test_expect_success 'removed files and relative paths' '
59 test_when_finished "rm -rf foo" &&
60 git init foo &&
61 >foo/foo.txt &&
62 git -C foo add foo.txt &&
63 git -C foo commit -m first &&
64 git -C foo rm foo.txt &&
66 mkdir -p foo/bar &&
67 git -C foo/bar commit -m second ../foo.txt
70 test_expect_success 'using invalid commit with -C' '
71 test_must_fail git commit --allow-empty -C bogus
74 test_expect_success 'nothing to commit' '
75 git reset --hard &&
76 test_must_fail git commit -m initial
79 test_expect_success '--dry-run fails with nothing to commit' '
80 test_must_fail git commit -m initial --dry-run
83 test_expect_success '--short fails with nothing to commit' '
84 test_must_fail git commit -m initial --short
87 test_expect_success '--porcelain fails with nothing to commit' '
88 test_must_fail git commit -m initial --porcelain
91 test_expect_success '--long fails with nothing to commit' '
92 test_must_fail git commit -m initial --long
95 test_expect_success 'setup: non-initial commit' '
96 echo bongo bongo bongo >file &&
97 git commit -m next -a
100 test_expect_success '--dry-run with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --dry-run
105 test_expect_success '--short with stuff to commit returns ok' '
106 echo bongo bongo bongo >>file &&
107 git commit -m next -a --short
110 test_expect_success '--porcelain with stuff to commit returns ok' '
111 echo bongo bongo bongo >>file &&
112 git commit -m next -a --porcelain
115 test_expect_success '--long with stuff to commit returns ok' '
116 echo bongo bongo bongo >>file &&
117 git commit -m next -a --long
120 test_expect_success 'commit message from non-existing file' '
121 echo more bongo: bongo bongo bongo bongo >file &&
122 test_must_fail git commit -F gah -a
125 test_expect_success 'empty commit message' '
126 # Empty except stray tabs and spaces on a few lines.
127 sed -e "s/@//g" >msg <<-\EOF &&
131 @Signed-off-by: hula@
133 test_must_fail git commit -F msg -a
136 test_expect_success 'template "emptyness" check does not kick in with -F' '
137 git checkout HEAD file && echo >>file && git add file &&
138 git commit -t file -F file
141 test_expect_success 'template "emptyness" check' '
142 git checkout HEAD file && echo >>file && git add file &&
143 test_must_fail git commit -t file 2>err &&
144 test_i18ngrep "did not edit" err
147 test_expect_success 'setup: commit message from file' '
148 git checkout HEAD file && echo >>file && git add file &&
149 echo this is the commit message, coming from a file >msg &&
150 git commit -F msg -a
153 test_expect_success 'amend commit' '
154 cat >editor <<-\EOF &&
155 #!/bin/sh
156 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
157 mv "$1-" "$1"
159 chmod 755 editor &&
160 EDITOR=./editor git commit --amend
163 test_expect_success 'amend --only ignores staged contents' '
164 cp file file.expect &&
165 echo changed >file &&
166 git add file &&
167 git commit --no-edit --amend --only &&
168 git cat-file blob HEAD:file >file.actual &&
169 test_cmp file.expect file.actual &&
170 git diff --exit-code
173 test_expect_success 'allow-empty --only ignores staged contents' '
174 echo changed-again >file &&
175 git add file &&
176 git commit --allow-empty --only -m "empty" &&
177 git cat-file blob HEAD:file >file.actual &&
178 test_cmp file.expect file.actual &&
179 git diff --exit-code
182 test_expect_success 'set up editor' '
183 cat >editor <<-\EOF &&
184 #!/bin/sh
185 sed -e "s/unamended/amended/g" <"$1" >"$1-"
186 mv "$1-" "$1"
188 chmod 755 editor
191 test_expect_success 'amend without launching editor' '
192 echo unamended >expect &&
193 git commit --allow-empty -m "unamended" &&
194 echo needs more bongo >file &&
195 git add file &&
196 EDITOR=./editor git commit --no-edit --amend &&
197 git diff --exit-code HEAD -- file &&
198 git diff-tree -s --format=%s HEAD >msg &&
199 test_cmp expect msg
202 test_expect_success '--amend --edit' '
203 echo amended >expect &&
204 git commit --allow-empty -m "unamended" &&
205 echo bongo again >file &&
206 git add file &&
207 EDITOR=./editor git commit --edit --amend &&
208 git diff-tree -s --format=%s HEAD >msg &&
209 test_cmp expect msg
212 test_expect_success '--amend --edit of empty message' '
213 cat >replace <<-\EOF &&
214 #!/bin/sh
215 echo "amended" >"$1"
217 chmod 755 replace &&
218 git commit --allow-empty --allow-empty-message -m "" &&
219 echo more bongo >file &&
220 git add file &&
221 EDITOR=./replace git commit --edit --amend &&
222 git diff-tree -s --format=%s HEAD >msg &&
223 ./replace expect &&
224 test_cmp expect msg
227 test_expect_success '--amend to set message to empty' '
228 echo bata >file &&
229 git add file &&
230 git commit -m "unamended" &&
231 git commit --amend --allow-empty-message -m "" &&
232 git diff-tree -s --format=%s HEAD >msg &&
233 echo "" >expect &&
234 test_cmp expect msg
237 test_expect_success '--amend to set empty message needs --allow-empty-message' '
238 echo conga >file &&
239 git add file &&
240 git commit -m "unamended" &&
241 test_must_fail git commit --amend -m "" &&
242 git diff-tree -s --format=%s HEAD >msg &&
243 echo "unamended" >expect &&
244 test_cmp expect msg
247 test_expect_success '-m --edit' '
248 echo amended >expect &&
249 git commit --allow-empty -m buffer &&
250 echo bongo bongo >file &&
251 git add file &&
252 EDITOR=./editor git commit -m unamended --edit &&
253 git diff-tree -s --format=%s HEAD >msg &&
254 test_cmp expect msg
257 test_expect_success '-m and -F do not mix' '
258 echo enough with the bongos >file &&
259 test_must_fail git commit -F msg -m amending .
262 test_expect_success 'using message from other commit' '
263 git commit -C HEAD^ .
266 test_expect_success 'editing message from other commit' '
267 cat >editor <<-\EOF &&
268 #!/bin/sh
269 sed -e "s/amend/older/g" <"$1" >"$1-"
270 mv "$1-" "$1"
272 chmod 755 editor &&
273 echo hula hula >file &&
274 EDITOR=./editor git commit -c HEAD^ -a
277 test_expect_success 'message from stdin' '
278 echo silly new contents >file &&
279 echo commit message from stdin |
280 git commit -F - -a
283 test_expect_success 'overriding author from command line' '
284 echo gak >file &&
285 git commit -m author \
286 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
287 grep Rubber.Duck output
290 test_expect_success PERL 'interactive add' '
291 echo 7 | test_must_fail git commit --interactive >out &&
292 grep "What now" out
295 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
296 echo zoo >file &&
297 test_must_fail git diff --exit-code >diff1 &&
298 test_write_lines u "*" q |
300 EDITOR=: &&
301 export EDITOR &&
302 test_must_fail git commit --interactive
303 ) &&
304 git diff >diff2 &&
305 compare_diff_patch diff1 diff2
308 test_expect_success 'editor not invoked if -F is given' '
309 cat >editor <<-\EOF &&
310 #!/bin/sh
311 sed -e s/good/bad/g <"$1" >"$1-"
312 mv "$1-" "$1"
314 chmod 755 editor &&
316 echo A good commit message. >msg &&
317 echo moo >file &&
319 EDITOR=./editor git commit -a -F msg &&
320 git show -s --pretty=format:%s >subject &&
321 grep -q good subject &&
323 echo quack >file &&
324 echo Another good message. |
325 EDITOR=./editor git commit -a -F - &&
326 git show -s --pretty=format:%s >subject &&
327 grep -q good subject
330 test_expect_success 'partial commit that involves removal (1)' '
332 git rm --cached file &&
333 mv file elif &&
334 git add elif &&
335 git commit -m "Partial: add elif" elif &&
336 git diff-tree --name-status HEAD^ HEAD >current &&
337 echo "A elif" >expected &&
338 test_cmp expected current
342 test_expect_success 'partial commit that involves removal (2)' '
344 git commit -m "Partial: remove file" file &&
345 git diff-tree --name-status HEAD^ HEAD >current &&
346 echo "D file" >expected &&
347 test_cmp expected current
351 test_expect_success 'partial commit that involves removal (3)' '
353 git rm --cached elif &&
354 echo elif >elif &&
355 git commit -m "Partial: modify elif" elif &&
356 git diff-tree --name-status HEAD^ HEAD >current &&
357 echo "M elif" >expected &&
358 test_cmp expected current
362 test_expect_success 'amend commit to fix author' '
364 oldtick=$GIT_AUTHOR_DATE &&
365 test_tick &&
366 git reset --hard &&
367 git cat-file -p HEAD >commit &&
368 sed -e "s/author.*/author $author $oldtick/" \
369 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
370 commit >expected &&
371 git commit --amend --author="$author" &&
372 git cat-file -p HEAD >current &&
373 test_cmp expected current
377 test_expect_success 'amend commit to fix date' '
379 test_tick &&
380 newtick=$GIT_AUTHOR_DATE &&
381 git reset --hard &&
382 git cat-file -p HEAD >commit &&
383 sed -e "s/author.*/author $author $newtick/" \
384 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
385 commit >expected &&
386 git commit --amend --date="$newtick" &&
387 git cat-file -p HEAD >current &&
388 test_cmp expected current
392 test_expect_success 'commit mentions forced date in output' '
393 git commit --amend --date=2010-01-02T03:04:05 >output &&
394 grep "Date: *Sat Jan 2 03:04:05 2010" output
397 test_expect_success 'commit complains about completely bogus dates' '
398 test_must_fail git commit --amend --date=seventeen
401 test_expect_success 'commit --date allows approxidate' '
402 git commit --amend \
403 --date="midnight the 12th of october, anno domini 1979" &&
404 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
405 git log -1 --format=%ad >actual &&
406 test_cmp expect actual
409 test_expect_success 'sign off (1)' '
411 echo 1 >positive &&
412 git add positive &&
413 git commit -s -m "thank you" &&
414 git cat-file commit HEAD >commit &&
415 sed -e "1,/^\$/d" commit >actual &&
417 echo thank you &&
418 echo &&
419 git var GIT_COMMITTER_IDENT >ident &&
420 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
421 ) >expected &&
422 test_cmp expected actual
426 test_expect_success 'sign off (2)' '
428 echo 2 >positive &&
429 git add positive &&
430 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
431 git commit -s -m "thank you
433 $existing" &&
434 git cat-file commit HEAD >commit &&
435 sed -e "1,/^\$/d" commit >actual &&
437 echo thank you &&
438 echo &&
439 echo $existing &&
440 git var GIT_COMMITTER_IDENT >ident &&
441 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
442 ) >expected &&
443 test_cmp expected actual
447 test_expect_success 'signoff gap' '
449 echo 3 >positive &&
450 git add positive &&
451 alt="Alt-RFC-822-Header: Value" &&
452 git commit -s -m "welcome
454 $alt" &&
455 git cat-file commit HEAD >commit &&
456 sed -e "1,/^\$/d" commit >actual &&
458 echo welcome &&
459 echo &&
460 echo $alt &&
461 git var GIT_COMMITTER_IDENT >ident &&
462 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
463 ) >expected &&
464 test_cmp expected actual
467 test_expect_success 'signoff gap 2' '
469 echo 4 >positive &&
470 git add positive &&
471 alt="fixed: 34" &&
472 git commit -s -m "welcome
474 We have now
475 $alt" &&
476 git cat-file commit HEAD >commit &&
477 sed -e "1,/^\$/d" commit >actual &&
479 echo welcome &&
480 echo &&
481 echo We have now &&
482 echo $alt &&
483 echo &&
484 git var GIT_COMMITTER_IDENT >ident &&
485 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
486 ) >expected &&
487 test_cmp expected actual
490 test_expect_success 'signoff respects trailer config' '
492 echo 5 >positive &&
493 git add positive &&
494 git commit -s -m "subject
496 non-trailer line
497 Myfooter: x" &&
498 git cat-file commit HEAD >commit &&
499 sed -e "1,/^\$/d" commit >actual &&
501 echo subject &&
502 echo &&
503 echo non-trailer line &&
504 echo Myfooter: x &&
505 echo &&
506 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
507 ) >expected &&
508 test_cmp expected actual &&
510 echo 6 >positive &&
511 git add positive &&
512 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
514 non-trailer line
515 Myfooter: x" &&
516 git cat-file commit HEAD >commit &&
517 sed -e "1,/^\$/d" commit >actual &&
519 echo subject &&
520 echo &&
521 echo non-trailer line &&
522 echo Myfooter: x &&
523 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
524 ) >expected &&
525 test_cmp expected actual
528 test_expect_success 'signoff not confused by ---' '
529 cat >expected <<-EOF &&
530 subject
532 body
534 these dashes confuse the parser!
536 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
538 # should be a noop, since we already signed
539 git commit --allow-empty --signoff -F expected &&
540 git log -1 --pretty=format:%B >actual &&
541 test_cmp expected actual
544 test_expect_success 'multiple -m' '
546 >negative &&
547 git add negative &&
548 git commit -m "one" -m "two" -m "three" &&
549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
552 echo one &&
553 echo &&
554 echo two &&
555 echo &&
556 echo three
557 ) >expected &&
558 test_cmp expected actual
562 test_expect_success 'amend commit to fix author' '
564 oldtick=$GIT_AUTHOR_DATE &&
565 test_tick &&
566 git reset --hard &&
567 git cat-file -p HEAD >commit &&
568 sed -e "s/author.*/author $author $oldtick/" \
569 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
570 commit >expected &&
571 git commit --amend --author="$author" &&
572 git cat-file -p HEAD >current &&
573 test_cmp expected current
577 test_expect_success 'git commit <file> with dirty index' '
578 echo tacocat >elif &&
579 echo tehlulz >chz &&
580 git add chz &&
581 git commit elif -m "tacocat is a palindrome" &&
582 git show --stat >stat &&
583 grep elif stat &&
584 git diff --cached >diff &&
585 grep chz diff
588 test_expect_success 'same tree (single parent)' '
590 git reset --hard &&
591 test_must_fail git commit -m empty
595 test_expect_success 'same tree (single parent) --allow-empty' '
597 git commit --allow-empty -m "forced empty" &&
598 git cat-file commit HEAD >commit &&
599 grep forced commit
603 test_expect_success 'same tree (merge and amend merge)' '
605 git checkout -b side HEAD^ &&
606 echo zero >zero &&
607 git add zero &&
608 git commit -m "add zero" &&
609 git checkout main &&
611 git merge -s ours side -m "empty ok" &&
612 git diff HEAD^ HEAD >actual &&
613 test_must_be_empty actual &&
615 git commit --amend -m "empty really ok" &&
616 git diff HEAD^ HEAD >actual &&
617 test_must_be_empty actual
621 test_expect_success 'amend using the message from another commit' '
623 git reset --hard &&
624 test_tick &&
625 git commit --allow-empty -m "old commit" &&
626 old=$(git rev-parse --verify HEAD) &&
627 test_tick &&
628 git commit --allow-empty -m "new commit" &&
629 new=$(git rev-parse --verify HEAD) &&
630 test_tick &&
631 git commit --allow-empty --amend -C "$old" &&
632 git show --pretty="format:%ad %s" "$old" >expected &&
633 git show --pretty="format:%ad %s" HEAD >actual &&
634 test_cmp expected actual
638 test_expect_success 'amend using the message from a commit named with tag' '
640 git reset --hard &&
641 test_tick &&
642 git commit --allow-empty -m "old commit" &&
643 old=$(git rev-parse --verify HEAD) &&
644 git tag -a -m "tag on old" tagged-old HEAD &&
645 test_tick &&
646 git commit --allow-empty -m "new commit" &&
647 new=$(git rev-parse --verify HEAD) &&
648 test_tick &&
649 git commit --allow-empty --amend -C tagged-old &&
650 git show --pretty="format:%ad %s" "$old" >expected &&
651 git show --pretty="format:%ad %s" HEAD >actual &&
652 test_cmp expected actual
656 test_expect_success 'amend can copy notes' '
658 git config notes.rewrite.amend true &&
659 git config notes.rewriteRef "refs/notes/*" &&
660 test_commit foo &&
661 git notes add -m"a note" &&
662 test_tick &&
663 git commit --amend -m"new foo" &&
664 test "$(git notes show)" = "a note"
668 test_expect_success 'commit a file whose name is a dash' '
669 git reset --hard &&
670 for i in 1 2 3 4 5
672 echo $i
673 done >./- &&
674 git add ./- &&
675 test_tick &&
676 git commit -m "add dash" >output </dev/null &&
677 test_i18ngrep " changed, 5 insertions" output
680 test_expect_success '--only works on to-be-born branch' '
681 # This test relies on having something in the index, as it
682 # would not otherwise actually prove much. So check this.
683 test -n "$(git ls-files)" &&
684 git checkout --orphan orphan &&
685 echo foo >newfile &&
686 git add newfile &&
687 git commit --only newfile -m"--only on unborn branch" &&
688 echo newfile >expected &&
689 git ls-tree -r --name-only HEAD >actual &&
690 test_cmp expected actual
693 test_expect_success '--dry-run with conflicts fixed from a merge' '
694 # setup two branches with conflicting information
695 # in the same file, resolve the conflict,
696 # call commit with --dry-run
697 echo "Initial contents, unimportant" >test-file &&
698 git add test-file &&
699 git commit -m "Initial commit" &&
700 echo "commit-1-state" >test-file &&
701 git commit -m "commit 1" -i test-file &&
702 git tag commit-1 &&
703 git checkout -b branch-2 HEAD^1 &&
704 echo "commit-2-state" >test-file &&
705 git commit -m "commit 2" -i test-file &&
706 test_must_fail git merge --no-commit commit-1 &&
707 echo "commit-2-state" >test-file &&
708 git add test-file &&
709 git commit --dry-run &&
710 git commit -m "conflicts fixed from merge."
713 test_expect_success '--dry-run --short' '
714 >test-file &&
715 git add test-file &&
716 git commit --dry-run --short
719 test_done