3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, -i and -o, test reflog,
9 test_description
='git commit'
11 .
"$TEST_DIRECTORY/diff-lib.sh"
13 author
='The Real Author <someguy@his.email.org>'
17 test_expect_success
'initial status' '
18 echo bongo bongo >file &&
21 test_i18ngrep "Initial commit" actual
24 test_expect_success
'fail initial amend' '
25 test_must_fail git commit --amend
28 test_expect_success
'setup: initial commit' '
32 test_expect_success
'-m and -F do not mix' '
33 git checkout HEAD file && echo >>file && git add file &&
34 test_must_fail git commit -m foo -m bar -F file
37 test_expect_success
'-m and -C do not mix' '
38 git checkout HEAD file && echo >>file && git add file &&
39 test_must_fail git commit -C HEAD -m illegal
42 test_expect_success
'paths and -a do not mix' '
43 echo King of the bongo >file &&
44 test_must_fail git commit -m foo -a file
47 test_expect_success PERL
'can use paths with --interactive' '
48 echo bong-o-bong >file &&
49 # 2: update, 1:st path, that is all, 7: quit
50 ( echo 2; echo 1; echo; echo 7 ) |
51 git commit -m foo --interactive file &&
52 git reset --hard HEAD^
55 test_expect_success
'using invalid commit with -C' '
56 test_must_fail git commit --allow-empty -C bogus
59 test_expect_success
'nothing to commit' '
61 test_must_fail git commit -m initial
64 test_expect_success
'--dry-run fails with nothing to commit' '
65 test_must_fail git commit -m initial --dry-run
68 test_expect_success
'--short fails with nothing to commit' '
69 test_must_fail git commit -m initial --short
72 test_expect_success
'--porcelain fails with nothing to commit' '
73 test_must_fail git commit -m initial --porcelain
76 test_expect_success
'--long fails with nothing to commit' '
77 test_must_fail git commit -m initial --long
80 test_expect_success
'setup: non-initial commit' '
81 echo bongo bongo bongo >file &&
85 test_expect_success
'--dry-run with stuff to commit returns ok' '
86 echo bongo bongo bongo >>file &&
87 git commit -m next -a --dry-run
90 test_expect_failure
'--short with stuff to commit returns ok' '
91 echo bongo bongo bongo >>file &&
92 git commit -m next -a --short
95 test_expect_failure
'--porcelain with stuff to commit returns ok' '
96 echo bongo bongo bongo >>file &&
97 git commit -m next -a --porcelain
100 test_expect_success
'--long with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --long
105 test_expect_success
'commit message from non-existing file' '
106 echo more bongo: bongo bongo bongo bongo >file &&
107 test_must_fail git commit -F gah -a
110 test_expect_success
'empty commit message' '
111 # Empty except stray tabs and spaces on a few lines.
112 sed -e "s/@//g" >msg <<-\EOF &&
116 @Signed-off-by: hula@
118 test_must_fail git commit -F msg -a
121 test_expect_success
'template "emptyness" check does not kick in with -F' '
122 git checkout HEAD file && echo >>file && git add file &&
123 git commit -t file -F file
126 test_expect_success
'template "emptyness" check' '
127 git checkout HEAD file && echo >>file && git add file &&
128 test_must_fail git commit -t file 2>err &&
129 test_i18ngrep "did not edit" err
132 test_expect_success
'setup: commit message from file' '
133 git checkout HEAD file && echo >>file && git add file &&
134 echo this is the commit message, coming from a file >msg &&
138 test_expect_success
'amend commit' '
139 cat >editor <<-\EOF &&
141 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
145 EDITOR=./editor git commit --amend
148 test_expect_success
'amend --only ignores staged contents' '
149 cp file file.expect &&
150 echo changed >file &&
152 git commit --no-edit --amend --only &&
153 git cat-file blob HEAD:file >file.actual &&
154 test_cmp file.expect file.actual &&
158 test_expect_success
'set up editor' '
159 cat >editor <<-\EOF &&
161 sed -e "s/unamended/amended/g" <"$1" >"$1-"
167 test_expect_success
'amend without launching editor' '
168 echo unamended >expect &&
169 git commit --allow-empty -m "unamended" &&
170 echo needs more bongo >file &&
172 EDITOR=./editor git commit --no-edit --amend &&
173 git diff --exit-code HEAD -- file &&
174 git diff-tree -s --format=%s HEAD >msg &&
178 test_expect_success
'--amend --edit' '
179 echo amended >expect &&
180 git commit --allow-empty -m "unamended" &&
181 echo bongo again >file &&
183 EDITOR=./editor git commit --edit --amend &&
184 git diff-tree -s --format=%s HEAD >msg &&
188 test_expect_success
'--amend --edit of empty message' '
189 cat >replace <<-\EOF &&
194 git commit --allow-empty --allow-empty-message -m "" &&
195 echo more bongo >file &&
197 EDITOR=./replace git commit --edit --amend &&
198 git diff-tree -s --format=%s HEAD >msg &&
203 test_expect_success
'--amend to set message to empty' '
206 git commit -m "unamended" &&
207 git commit --amend --allow-empty-message -m "" &&
208 git diff-tree -s --format=%s HEAD >msg &&
213 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
216 git commit -m "unamended" &&
217 test_must_fail git commit --amend -m "" &&
218 git diff-tree -s --format=%s HEAD >msg &&
219 echo "unamended" >expect &&
223 test_expect_success
'-m --edit' '
224 echo amended >expect &&
225 git commit --allow-empty -m buffer &&
226 echo bongo bongo >file &&
228 EDITOR=./editor git commit -m unamended --edit &&
229 git diff-tree -s --format=%s HEAD >msg &&
233 test_expect_success
'-m and -F do not mix' '
234 echo enough with the bongos >file &&
235 test_must_fail git commit -F msg -m amending .
238 test_expect_success
'using message from other commit' '
239 git commit -C HEAD^ .
242 test_expect_success
'editing message from other commit' '
243 cat >editor <<-\EOF &&
245 sed -e "s/amend/older/g" < "$1" > "$1-"
249 echo hula hula >file &&
250 EDITOR=./editor git commit -c HEAD^ -a
253 test_expect_success
'message from stdin' '
254 echo silly new contents >file &&
255 echo commit message from stdin |
259 test_expect_success
'overriding author from command line' '
261 git commit -m author \
262 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
263 grep Rubber.Duck output
266 test_expect_success PERL
'interactive add' '
268 git commit --interactive |
272 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
274 test_must_fail git diff --exit-code >diff1 &&
275 (echo u ; echo "*" ; echo q) |
279 test_must_fail git commit --interactive
282 compare_diff_patch diff1 diff2
285 test_expect_success
'editor not invoked if -F is given' '
286 cat >editor <<-\EOF &&
288 sed -e s/good/bad/g <"$1" >"$1-"
293 echo A good commit message. >msg &&
296 EDITOR=./editor git commit -a -F msg &&
297 git show -s --pretty=format:%s >subject &&
298 grep -q good subject &&
301 echo Another good message. |
302 EDITOR=./editor git commit -a -F - &&
303 git show -s --pretty=format:%s >subject &&
307 test_expect_success
'partial commit that involves removal (1)' '
309 git rm --cached file &&
312 git commit -m "Partial: add elif" elif &&
313 git diff-tree --name-status HEAD^ HEAD >current &&
314 echo "A elif" >expected &&
315 test_cmp expected current
319 test_expect_success
'partial commit that involves removal (2)' '
321 git commit -m "Partial: remove file" file &&
322 git diff-tree --name-status HEAD^ HEAD >current &&
323 echo "D file" >expected &&
324 test_cmp expected current
328 test_expect_success
'partial commit that involves removal (3)' '
330 git rm --cached elif &&
332 git commit -m "Partial: modify elif" elif &&
333 git diff-tree --name-status HEAD^ HEAD >current &&
334 echo "M elif" >expected &&
335 test_cmp expected current
339 test_expect_success
'amend commit to fix author' '
341 oldtick=$GIT_AUTHOR_DATE &&
344 git cat-file -p HEAD |
345 sed -e "s/author.*/author $author $oldtick/" \
346 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
348 git commit --amend --author="$author" &&
349 git cat-file -p HEAD > current &&
350 test_cmp expected current
354 test_expect_success
'amend commit to fix date' '
357 newtick=$GIT_AUTHOR_DATE &&
359 git cat-file -p HEAD |
360 sed -e "s/author.*/author $author $newtick/" \
361 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
363 git commit --amend --date="$newtick" &&
364 git cat-file -p HEAD > current &&
365 test_cmp expected current
369 test_expect_success
'commit mentions forced date in output' '
370 git commit --amend --date=2010-01-02T03:04:05 >output &&
371 grep "Date: *Sat Jan 2 03:04:05 2010" output
374 test_expect_success
'commit complains about completely bogus dates' '
375 test_must_fail git commit --amend --date=seventeen
378 test_expect_success
'commit --date allows approxidate' '
380 --date="midnight the 12th of october, anno domini 1979" &&
381 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
382 git log -1 --format=%ad >actual &&
383 test_cmp expect actual
386 test_expect_success
'sign off (1)' '
390 git commit -s -m "thank you" &&
391 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
395 git var GIT_COMMITTER_IDENT |
396 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
398 test_cmp expected actual
402 test_expect_success
'sign off (2)' '
406 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
407 git commit -s -m "thank you
410 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
415 git var GIT_COMMITTER_IDENT |
416 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
418 test_cmp expected actual
422 test_expect_success
'signoff gap' '
426 alt="Alt-RFC-822-Header: Value" &&
427 git commit -s -m "welcome
430 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
435 git var GIT_COMMITTER_IDENT |
436 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
438 test_cmp expected actual
441 test_expect_success
'signoff gap 2' '
446 git commit -s -m "welcome
450 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
457 git var GIT_COMMITTER_IDENT |
458 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
460 test_cmp expected actual
463 test_expect_success
'multiple -m' '
467 git commit -m "one" -m "two" -m "three" &&
468 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
476 test_cmp expected actual
480 test_expect_success
'amend commit to fix author' '
482 oldtick=$GIT_AUTHOR_DATE &&
485 git cat-file -p HEAD |
486 sed -e "s/author.*/author $author $oldtick/" \
487 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
489 git commit --amend --author="$author" &&
490 git cat-file -p HEAD > current &&
491 test_cmp expected current
495 test_expect_success
'git commit <file> with dirty index' '
496 echo tacocat > elif &&
497 echo tehlulz > chz &&
499 git commit elif -m "tacocat is a palindrome" &&
500 git show --stat | grep elif &&
501 git diff --cached | grep chz
504 test_expect_success
'same tree (single parent)' '
507 test_must_fail git commit -m empty
511 test_expect_success
'same tree (single parent) --allow-empty' '
513 git commit --allow-empty -m "forced empty" &&
514 git cat-file commit HEAD | grep forced
518 test_expect_success
'same tree (merge and amend merge)' '
520 git checkout -b side HEAD^ &&
523 git commit -m "add zero" &&
524 git checkout master &&
526 git merge -s ours side -m "empty ok" &&
527 git diff HEAD^ HEAD >actual &&
529 test_cmp expected actual &&
531 git commit --amend -m "empty really ok" &&
532 git diff HEAD^ HEAD >actual &&
534 test_cmp expected actual
538 test_expect_success
'amend using the message from another commit' '
542 git commit --allow-empty -m "old commit" &&
543 old=$(git rev-parse --verify HEAD) &&
545 git commit --allow-empty -m "new commit" &&
546 new=$(git rev-parse --verify HEAD) &&
548 git commit --allow-empty --amend -C "$old" &&
549 git show --pretty="format:%ad %s" "$old" >expected &&
550 git show --pretty="format:%ad %s" HEAD >actual &&
551 test_cmp expected actual
555 test_expect_success
'amend using the message from a commit named with tag' '
559 git commit --allow-empty -m "old commit" &&
560 old=$(git rev-parse --verify HEAD) &&
561 git tag -a -m "tag on old" tagged-old HEAD &&
563 git commit --allow-empty -m "new commit" &&
564 new=$(git rev-parse --verify HEAD) &&
566 git commit --allow-empty --amend -C tagged-old &&
567 git show --pretty="format:%ad %s" "$old" >expected &&
568 git show --pretty="format:%ad %s" HEAD >actual &&
569 test_cmp expected actual
573 test_expect_success
'amend can copy notes' '
575 git config notes.rewrite.amend true &&
576 git config notes.rewriteRef "refs/notes/*" &&
578 git notes add -m"a note" &&
580 git commit --amend -m"new foo" &&
581 test "$(git notes show)" = "a note"
585 test_expect_success
'commit a file whose name is a dash' '
593 git commit -m "add dash" >output </dev/null &&
594 test_i18ngrep " changed, 5 insertions" output
597 test_expect_success
'--only works on to-be-born branch' '
598 # This test relies on having something in the index, as it
599 # would not otherwise actually prove much. So check this.
600 test -n "$(git ls-files)" &&
601 git checkout --orphan orphan &&
604 git commit --only newfile -m"--only on unborn branch" &&
605 echo newfile >expected &&
606 git ls-tree -r --name-only HEAD >actual &&
607 test_cmp expected actual