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 'setup: non-initial commit' '
65 echo bongo bongo bongo >file &&
69 test_expect_success 'commit message from non-existing file' '
70 echo more bongo: bongo bongo bongo bongo >file &&
71 test_must_fail git commit -F gah -a
74 test_expect_success 'empty commit message' '
75 # Empty except stray tabs and spaces on a few lines.
76 sed -e "s/@//g" >msg <<-\EOF &&
82 test_must_fail git commit -F msg -a
85 test_expect_success 'template "emptyness" check does not kick in with -F' '
86 git checkout HEAD file && echo >>file && git add file &&
87 git commit -t file -F file
90 test_expect_success 'template "emptyness" check' '
91 git checkout HEAD file && echo >>file && git add file &&
92 test_must_fail git commit -t file 2>err &&
93 test_i18ngrep "did not edit" err
96 test_expect_success 'setup: commit message from file' '
97 git checkout HEAD file && echo >>file && git add file &&
98 echo this is the commit message, coming from a file >msg &&
102 test_expect_success 'amend commit' '
103 cat >editor <<-\EOF &&
105 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
109 EDITOR=./editor git commit --amend
112 test_expect_success 'amend --only ignores staged contents' '
113 cp file file.expect &&
114 echo changed >file &&
116 git commit --no-edit --amend --only &&
117 git cat-file blob HEAD:file >file.actual &&
118 test_cmp file.expect file.actual &&
122 test_expect_success 'set up editor' '
123 cat >editor <<-\EOF &&
125 sed -e "s/unamended/amended/g" <"$1" >"$1-"
131 test_expect_success 'amend without launching editor' '
132 echo unamended >expect &&
133 git commit --allow-empty -m "unamended" &&
134 echo needs more bongo >file &&
136 EDITOR=./editor git commit --no-edit --amend &&
137 git diff --exit-code HEAD -- file &&
138 git diff-tree -s --format=%s HEAD >msg &&
142 test_expect_success '--amend --edit' '
143 echo amended >expect &&
144 git commit --allow-empty -m "unamended" &&
145 echo bongo again >file &&
147 EDITOR=./editor git commit --edit --amend &&
148 git diff-tree -s --format=%s HEAD >msg &&
152 test_expect_success '--amend --edit of empty message' '
153 cat >replace <<-\EOF &&
158 git commit --allow-empty --allow-empty-message -m "" &&
159 echo more bongo >file &&
161 EDITOR=./replace git commit --edit --amend &&
162 git diff-tree -s --format=%s HEAD >msg &&
167 test_expect_success '-m --edit' '
168 echo amended >expect &&
169 git commit --allow-empty -m buffer &&
170 echo bongo bongo >file &&
172 EDITOR=./editor git commit -m unamended --edit &&
173 git diff-tree -s --format=%s HEAD >msg &&
177 test_expect_success '-m and -F do not mix' '
178 echo enough with the bongos >file &&
179 test_must_fail git commit -F msg -m amending .
182 test_expect_success 'using message from other commit' '
183 git commit -C HEAD^ .
186 test_expect_success 'editing message from other commit' '
187 cat >editor <<-\EOF &&
189 sed -e "s/amend/older/g" < "$1" > "$1-"
193 echo hula hula >file &&
194 EDITOR=./editor git commit -c HEAD^ -a
197 test_expect_success 'message from stdin' '
198 echo silly new contents >file &&
199 echo commit message from stdin |
203 test_expect_success 'overriding author from command line' '
205 git commit -m author \
206 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
207 grep Rubber.Duck output
210 test_expect_success PERL 'interactive add' '
212 git commit --interactive |
216 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
218 test_must_fail git diff --exit-code >diff1 &&
219 (echo u ; echo "*" ; echo q) |
223 test_must_fail git commit --interactive
226 compare_diff_patch diff1 diff2
229 test_expect_success 'editor not invoked if -F is given' '
230 cat >editor <<-\EOF &&
232 sed -e s/good/bad/g <"$1" >"$1-"
237 echo A good commit message. >msg &&
240 EDITOR=./editor git commit -a -F msg &&
241 git show -s --pretty=format:%s >subject &&
242 grep -q good subject &&
245 echo Another good message. |
246 EDITOR=./editor git commit -a -F - &&
247 git show -s --pretty=format:%s >subject &&
251 test_expect_success 'partial commit that involves removal (1)' '
253 git rm --cached file &&
256 git commit -m "Partial: add elif" elif &&
257 git diff-tree --name-status HEAD^ HEAD >current &&
258 echo "A elif" >expected &&
259 test_cmp expected current
263 test_expect_success 'partial commit that involves removal (2)' '
265 git commit -m "Partial: remove file" file &&
266 git diff-tree --name-status HEAD^ HEAD >current &&
267 echo "D file" >expected &&
268 test_cmp expected current
272 test_expect_success 'partial commit that involves removal (3)' '
274 git rm --cached elif &&
276 git commit -m "Partial: modify elif" elif &&
277 git diff-tree --name-status HEAD^ HEAD >current &&
278 echo "M elif" >expected &&
279 test_cmp expected current
283 test_expect_success 'amend commit to fix author' '
285 oldtick=$GIT_AUTHOR_DATE &&
288 git cat-file -p HEAD |
289 sed -e "s/author.*/author $author $oldtick/" \
290 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
292 git commit --amend --author="$author" &&
293 git cat-file -p HEAD > current &&
294 test_cmp expected current
298 test_expect_success 'amend commit to fix date' '
301 newtick=$GIT_AUTHOR_DATE &&
303 git cat-file -p HEAD |
304 sed -e "s/author.*/author $author $newtick/" \
305 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
307 git commit --amend --date="$newtick" &&
308 git cat-file -p HEAD > current &&
309 test_cmp expected current
313 test_expect_success 'commit complains about bogus date' '
314 test_must_fail git commit --amend --date=10.11.2010
317 test_expect_success 'sign off (1)' '
321 git commit -s -m "thank you" &&
322 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
326 git var GIT_COMMITTER_IDENT |
327 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
329 test_cmp expected actual
333 test_expect_success 'sign off (2)' '
337 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
338 git commit -s -m "thank you
341 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
346 git var GIT_COMMITTER_IDENT |
347 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
349 test_cmp expected actual
353 test_expect_success 'signoff gap' '
357 alt="Alt-RFC-822-Header: Value" &&
358 git commit -s -m "welcome
361 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
366 git var GIT_COMMITTER_IDENT |
367 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
369 test_cmp expected actual
372 test_expect_success 'signoff gap 2' '
377 git commit -s -m "welcome
381 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
388 git var GIT_COMMITTER_IDENT |
389 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
391 test_cmp expected actual
394 test_expect_success 'multiple -m' '
398 git commit -m "one" -m "two" -m "three" &&
399 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
407 test_cmp expected actual
411 test_expect_success 'amend commit to fix author' '
413 oldtick=$GIT_AUTHOR_DATE &&
416 git cat-file -p HEAD |
417 sed -e "s/author.*/author $author $oldtick/" \
418 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
420 git commit --amend --author="$author" &&
421 git cat-file -p HEAD > current &&
422 test_cmp expected current
426 test_expect_success 'git commit <file> with dirty index' '
427 echo tacocat > elif &&
428 echo tehlulz > chz &&
430 git commit elif -m "tacocat is a palindrome" &&
431 git show --stat | grep elif &&
432 git diff --cached | grep chz
435 test_expect_success 'same tree (single parent)' '
438 test_must_fail git commit -m empty
442 test_expect_success 'same tree (single parent) --allow-empty' '
444 git commit --allow-empty -m "forced empty" &&
445 git cat-file commit HEAD | grep forced
449 test_expect_success 'same tree (merge and amend merge)' '
451 git checkout -b side HEAD^ &&
454 git commit -m "add zero" &&
455 git checkout master &&
457 git merge -s ours side -m "empty ok" &&
458 git diff HEAD^ HEAD >actual &&
460 test_cmp expected actual &&
462 git commit --amend -m "empty really ok" &&
463 git diff HEAD^ HEAD >actual &&
465 test_cmp expected actual
469 test_expect_success 'amend using the message from another commit' '
473 git commit --allow-empty -m "old commit" &&
474 old=$(git rev-parse --verify HEAD) &&
476 git commit --allow-empty -m "new commit" &&
477 new=$(git rev-parse --verify HEAD) &&
479 git commit --allow-empty --amend -C "$old" &&
480 git show --pretty="format:%ad %s" "$old" >expected &&
481 git show --pretty="format:%ad %s" HEAD >actual &&
482 test_cmp expected actual
486 test_expect_success 'amend using the message from a commit named with tag' '
490 git commit --allow-empty -m "old commit" &&
491 old=$(git rev-parse --verify HEAD) &&
492 git tag -a -m "tag on old" tagged-old HEAD &&
494 git commit --allow-empty -m "new commit" &&
495 new=$(git rev-parse --verify HEAD) &&
497 git commit --allow-empty --amend -C tagged-old &&
498 git show --pretty="format:%ad %s" "$old" >expected &&
499 git show --pretty="format:%ad %s" HEAD >actual &&
500 test_cmp expected actual
504 test_expect_success 'amend can copy notes' '
506 git config notes.rewrite.amend true &&
507 git config notes.rewriteRef "refs/notes/*" &&
509 git notes add -m"a note" &&
511 git commit --amend -m"new foo" &&
512 test "$(git notes show)" = "a note"
516 test_expect_success 'commit a file whose name is a dash' '
524 git commit -m "add dash" >output </dev/null &&
525 test_i18ngrep " changed, 5 insertions" output
528 test_expect_success '--only works on to-be-born branch' '
529 # This test relies on having something in the index, as it
530 # would not otherwise actually prove much. So check this.
531 test -n "$(git ls-files)" &&
532 git checkout --orphan orphan &&
535 git commit --only newfile -m"--only on unborn branch" &&
536 echo newfile >expected &&
537 git ls-tree -r --name-only HEAD >actual &&
538 test_cmp expected actual