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 -C bogus
59 test_expect_success
'nothing to commit' '
60 test_must_fail git commit -m initial
63 test_expect_success
'setup: non-initial commit' '
64 echo bongo bongo bongo >file &&
68 test_expect_success
'commit message from non-existing file' '
69 echo more bongo: bongo bongo bongo bongo >file &&
70 test_must_fail git commit -F gah -a
73 test_expect_success
'empty commit message' '
74 # Empty except stray tabs and spaces on a few lines.
75 sed -e "s/@//g" >msg <<-\EOF &&
81 test_must_fail git commit -F msg -a
84 test_expect_success
'template "emptyness" check does not kick in with -F' '
85 git checkout HEAD file && echo >>file && git add file &&
86 git commit -t file -F file
89 test_expect_success
'template "emptyness" check' '
90 git checkout HEAD file && echo >>file && git add file &&
91 test_must_fail git commit -t file 2>err &&
92 test_i18ngrep "did not edit" err
95 test_expect_success
'setup: commit message from file' '
96 git checkout HEAD file && echo >>file && git add file &&
97 echo this is the commit message, coming from a file >msg &&
101 test_expect_success
'amend commit' '
102 cat >editor <<-\EOF &&
104 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
108 EDITOR=./editor git commit --amend
111 test_expect_success
'set up editor' '
112 cat >editor <<-\EOF &&
114 sed -e "s/unamended/amended/g" <"$1" >"$1-"
120 test_expect_success
'amend without launching editor' '
121 echo unamended >expect &&
122 git commit --allow-empty -m "unamended" &&
123 echo needs more bongo >file &&
125 EDITOR=./editor git commit --no-edit --amend &&
126 git diff --exit-code HEAD -- file &&
127 git diff-tree -s --format=%s HEAD >msg &&
131 test_expect_success
'--amend --edit' '
132 echo amended >expect &&
133 git commit --allow-empty -m "unamended" &&
134 echo bongo again >file &&
136 EDITOR=./editor git commit --edit --amend &&
137 git diff-tree -s --format=%s HEAD >msg &&
141 test_expect_success
'--amend --edit of empty message' '
142 cat >replace <<-\EOF &&
147 git commit --allow-empty --allow-empty-message -m "" &&
148 echo more bongo >file &&
150 EDITOR=./replace git commit --edit --amend &&
151 git diff-tree -s --format=%s HEAD >msg &&
156 test_expect_success
'-m --edit' '
157 echo amended >expect &&
158 git commit --allow-empty -m buffer &&
159 echo bongo bongo >file &&
161 EDITOR=./editor git commit -m unamended --edit &&
162 git diff-tree -s --format=%s HEAD >msg &&
166 test_expect_success
'-m and -F do not mix' '
167 echo enough with the bongos >file &&
168 test_must_fail git commit -F msg -m amending .
171 test_expect_success
'using message from other commit' '
172 git commit -C HEAD^ .
175 test_expect_success
'editing message from other commit' '
176 cat >editor <<-\EOF &&
178 sed -e "s/amend/older/g" < "$1" > "$1-"
182 echo hula hula >file &&
183 EDITOR=./editor git commit -c HEAD^ -a
186 test_expect_success
'message from stdin' '
187 echo silly new contents >file &&
188 echo commit message from stdin |
192 test_expect_success
'overriding author from command line' '
194 git commit -m author \
195 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
196 grep Rubber.Duck output
199 test_expect_success PERL
'interactive add' '
201 git commit --interactive |
205 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
207 test_must_fail git diff --exit-code >diff1 &&
208 (echo u ; echo "*" ; echo q) |
212 test_must_fail git commit --interactive
215 compare_diff_patch diff1 diff2
218 test_expect_success
'editor not invoked if -F is given' '
219 cat >editor <<-\EOF &&
221 sed -e s/good/bad/g <"$1" >"$1-"
226 echo A good commit message. >msg &&
229 EDITOR=./editor git commit -a -F msg &&
230 git show -s --pretty=format:%s >subject &&
231 grep -q good subject &&
234 echo Another good message. |
235 EDITOR=./editor git commit -a -F - &&
236 git show -s --pretty=format:%s >subject &&
240 test_expect_success
'partial commit that involves removal (1)' '
242 git rm --cached file &&
245 git commit -m "Partial: add elif" elif &&
246 git diff-tree --name-status HEAD^ HEAD >current &&
247 echo "A elif" >expected &&
248 test_cmp expected current
252 test_expect_success
'partial commit that involves removal (2)' '
254 git commit -m "Partial: remove file" file &&
255 git diff-tree --name-status HEAD^ HEAD >current &&
256 echo "D file" >expected &&
257 test_cmp expected current
261 test_expect_success
'partial commit that involves removal (3)' '
263 git rm --cached elif &&
265 git commit -m "Partial: modify elif" elif &&
266 git diff-tree --name-status HEAD^ HEAD >current &&
267 echo "M elif" >expected &&
268 test_cmp expected current
272 test_expect_success
'amend commit to fix author' '
274 oldtick=$GIT_AUTHOR_DATE &&
277 git cat-file -p HEAD |
278 sed -e "s/author.*/author $author $oldtick/" \
279 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
281 git commit --amend --author="$author" &&
282 git cat-file -p HEAD > current &&
283 test_cmp expected current
287 test_expect_success
'amend commit to fix date' '
290 newtick=$GIT_AUTHOR_DATE &&
292 git cat-file -p HEAD |
293 sed -e "s/author.*/author $author $newtick/" \
294 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
296 git commit --amend --date="$newtick" &&
297 git cat-file -p HEAD > current &&
298 test_cmp expected current
302 test_expect_success
'commit complains about bogus date' '
303 test_must_fail git commit --amend --date=10.11.2010
306 test_expect_success
'sign off (1)' '
310 git commit -s -m "thank you" &&
311 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
315 git var GIT_COMMITTER_IDENT |
316 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
318 test_cmp expected actual
322 test_expect_success
'sign off (2)' '
326 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
327 git commit -s -m "thank you
330 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
335 git var GIT_COMMITTER_IDENT |
336 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
338 test_cmp expected actual
342 test_expect_success
'signoff gap' '
346 alt="Alt-RFC-822-Header: Value" &&
347 git commit -s -m "welcome
350 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
355 git var GIT_COMMITTER_IDENT |
356 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
358 test_cmp expected actual
361 test_expect_success
'signoff gap 2' '
366 git commit -s -m "welcome
370 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
377 git var GIT_COMMITTER_IDENT |
378 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
380 test_cmp expected actual
383 test_expect_success
'multiple -m' '
387 git commit -m "one" -m "two" -m "three" &&
388 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
396 test_cmp expected actual
400 test_expect_success
'amend commit to fix author' '
402 oldtick=$GIT_AUTHOR_DATE &&
405 git cat-file -p HEAD |
406 sed -e "s/author.*/author $author $oldtick/" \
407 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
409 git commit --amend --author="$author" &&
410 git cat-file -p HEAD > current &&
411 test_cmp expected current
415 test_expect_success
'git commit <file> with dirty index' '
416 echo tacocat > elif &&
417 echo tehlulz > chz &&
419 git commit elif -m "tacocat is a palindrome" &&
420 git show --stat | grep elif &&
421 git diff --cached | grep chz
424 test_expect_success
'same tree (single parent)' '
427 test_must_fail git commit -m empty
431 test_expect_success
'same tree (single parent) --allow-empty' '
433 git commit --allow-empty -m "forced empty" &&
434 git cat-file commit HEAD | grep forced
438 test_expect_success
'same tree (merge and amend merge)' '
440 git checkout -b side HEAD^ &&
443 git commit -m "add zero" &&
444 git checkout master &&
446 git merge -s ours side -m "empty ok" &&
447 git diff HEAD^ HEAD >actual &&
449 test_cmp expected actual &&
451 git commit --amend -m "empty really ok" &&
452 git diff HEAD^ HEAD >actual &&
454 test_cmp expected actual
458 test_expect_success
'amend using the message from another commit' '
462 git commit --allow-empty -m "old commit" &&
463 old=$(git rev-parse --verify HEAD) &&
465 git commit --allow-empty -m "new commit" &&
466 new=$(git rev-parse --verify HEAD) &&
468 git commit --allow-empty --amend -C "$old" &&
469 git show --pretty="format:%ad %s" "$old" >expected &&
470 git show --pretty="format:%ad %s" HEAD >actual &&
471 test_cmp expected actual
475 test_expect_success
'amend using the message from a commit named with tag' '
479 git commit --allow-empty -m "old commit" &&
480 old=$(git rev-parse --verify HEAD) &&
481 git tag -a -m "tag on old" tagged-old HEAD &&
483 git commit --allow-empty -m "new commit" &&
484 new=$(git rev-parse --verify HEAD) &&
486 git commit --allow-empty --amend -C tagged-old &&
487 git show --pretty="format:%ad %s" "$old" >expected &&
488 git show --pretty="format:%ad %s" HEAD >actual &&
489 test_cmp expected actual
493 test_expect_success
'amend can copy notes' '
495 git config notes.rewrite.amend true &&
496 git config notes.rewriteRef "refs/notes/*" &&
498 git notes add -m"a note" &&
500 git commit --amend -m"new foo" &&
501 test "$(git notes show)" = "a note"
505 test_expect_success
'commit a file whose name is a dash' '
513 git commit -m "add dash" >output </dev/null &&
514 test_i18ngrep " changed, 5 insertions" output