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
'amend --only ignores staged contents' '
112 cp file file.expect &&
113 echo changed >file &&
115 git commit --no-edit --amend --only &&
116 git cat-file blob HEAD:file >file.actual &&
117 test_cmp file.expect file.actual &&
121 test_expect_success
'set up editor' '
122 cat >editor <<-\EOF &&
124 sed -e "s/unamended/amended/g" <"$1" >"$1-"
130 test_expect_success
'amend without launching editor' '
131 echo unamended >expect &&
132 git commit --allow-empty -m "unamended" &&
133 echo needs more bongo >file &&
135 EDITOR=./editor git commit --no-edit --amend &&
136 git diff --exit-code HEAD -- file &&
137 git diff-tree -s --format=%s HEAD >msg &&
141 test_expect_success
'--amend --edit' '
142 echo amended >expect &&
143 git commit --allow-empty -m "unamended" &&
144 echo bongo again >file &&
146 EDITOR=./editor git commit --edit --amend &&
147 git diff-tree -s --format=%s HEAD >msg &&
151 test_expect_success
'--amend --edit of empty message' '
152 cat >replace <<-\EOF &&
157 git commit --allow-empty --allow-empty-message -m "" &&
158 echo more bongo >file &&
160 EDITOR=./replace git commit --edit --amend &&
161 git diff-tree -s --format=%s HEAD >msg &&
166 test_expect_success
'-m --edit' '
167 echo amended >expect &&
168 git commit --allow-empty -m buffer &&
169 echo bongo bongo >file &&
171 EDITOR=./editor git commit -m unamended --edit &&
172 git diff-tree -s --format=%s HEAD >msg &&
176 test_expect_success
'-m and -F do not mix' '
177 echo enough with the bongos >file &&
178 test_must_fail git commit -F msg -m amending .
181 test_expect_success
'using message from other commit' '
182 git commit -C HEAD^ .
185 test_expect_success
'editing message from other commit' '
186 cat >editor <<-\EOF &&
188 sed -e "s/amend/older/g" < "$1" > "$1-"
192 echo hula hula >file &&
193 EDITOR=./editor git commit -c HEAD^ -a
196 test_expect_success
'message from stdin' '
197 echo silly new contents >file &&
198 echo commit message from stdin |
202 test_expect_success
'overriding author from command line' '
204 git commit -m author \
205 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
206 grep Rubber.Duck output
209 test_expect_success PERL
'interactive add' '
211 git commit --interactive |
215 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
217 test_must_fail git diff --exit-code >diff1 &&
218 (echo u ; echo "*" ; echo q) |
222 test_must_fail git commit --interactive
225 compare_diff_patch diff1 diff2
228 test_expect_success
'editor not invoked if -F is given' '
229 cat >editor <<-\EOF &&
231 sed -e s/good/bad/g <"$1" >"$1-"
236 echo A good commit message. >msg &&
239 EDITOR=./editor git commit -a -F msg &&
240 git show -s --pretty=format:%s >subject &&
241 grep -q good subject &&
244 echo Another good message. |
245 EDITOR=./editor git commit -a -F - &&
246 git show -s --pretty=format:%s >subject &&
250 test_expect_success
'partial commit that involves removal (1)' '
252 git rm --cached file &&
255 git commit -m "Partial: add elif" elif &&
256 git diff-tree --name-status HEAD^ HEAD >current &&
257 echo "A elif" >expected &&
258 test_cmp expected current
262 test_expect_success
'partial commit that involves removal (2)' '
264 git commit -m "Partial: remove file" file &&
265 git diff-tree --name-status HEAD^ HEAD >current &&
266 echo "D file" >expected &&
267 test_cmp expected current
271 test_expect_success
'partial commit that involves removal (3)' '
273 git rm --cached elif &&
275 git commit -m "Partial: modify elif" elif &&
276 git diff-tree --name-status HEAD^ HEAD >current &&
277 echo "M elif" >expected &&
278 test_cmp expected current
282 test_expect_success
'amend commit to fix author' '
284 oldtick=$GIT_AUTHOR_DATE &&
287 git cat-file -p HEAD |
288 sed -e "s/author.*/author $author $oldtick/" \
289 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
291 git commit --amend --author="$author" &&
292 git cat-file -p HEAD > current &&
293 test_cmp expected current
297 test_expect_success
'amend commit to fix date' '
300 newtick=$GIT_AUTHOR_DATE &&
302 git cat-file -p HEAD |
303 sed -e "s/author.*/author $author $newtick/" \
304 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
306 git commit --amend --date="$newtick" &&
307 git cat-file -p HEAD > current &&
308 test_cmp expected current
312 test_expect_success
'commit complains about bogus date' '
313 test_must_fail git commit --amend --date=10.11.2010
316 test_expect_success
'sign off (1)' '
320 git commit -s -m "thank you" &&
321 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
325 git var GIT_COMMITTER_IDENT |
326 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
328 test_cmp expected actual
332 test_expect_success
'sign off (2)' '
336 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
337 git commit -s -m "thank you
340 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
345 git var GIT_COMMITTER_IDENT |
346 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
348 test_cmp expected actual
352 test_expect_success
'signoff gap' '
356 alt="Alt-RFC-822-Header: Value" &&
357 git commit -s -m "welcome
360 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
365 git var GIT_COMMITTER_IDENT |
366 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
368 test_cmp expected actual
371 test_expect_success
'signoff gap 2' '
376 git commit -s -m "welcome
380 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
387 git var GIT_COMMITTER_IDENT |
388 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
390 test_cmp expected actual
393 test_expect_success
'multiple -m' '
397 git commit -m "one" -m "two" -m "three" &&
398 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
406 test_cmp expected actual
410 test_expect_success
'amend commit to fix author' '
412 oldtick=$GIT_AUTHOR_DATE &&
415 git cat-file -p HEAD |
416 sed -e "s/author.*/author $author $oldtick/" \
417 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
419 git commit --amend --author="$author" &&
420 git cat-file -p HEAD > current &&
421 test_cmp expected current
425 test_expect_success
'git commit <file> with dirty index' '
426 echo tacocat > elif &&
427 echo tehlulz > chz &&
429 git commit elif -m "tacocat is a palindrome" &&
430 git show --stat | grep elif &&
431 git diff --cached | grep chz
434 test_expect_success
'same tree (single parent)' '
437 test_must_fail git commit -m empty
441 test_expect_success
'same tree (single parent) --allow-empty' '
443 git commit --allow-empty -m "forced empty" &&
444 git cat-file commit HEAD | grep forced
448 test_expect_success
'same tree (merge and amend merge)' '
450 git checkout -b side HEAD^ &&
453 git commit -m "add zero" &&
454 git checkout master &&
456 git merge -s ours side -m "empty ok" &&
457 git diff HEAD^ HEAD >actual &&
459 test_cmp expected actual &&
461 git commit --amend -m "empty really ok" &&
462 git diff HEAD^ HEAD >actual &&
464 test_cmp expected actual
468 test_expect_success
'amend using the message from another commit' '
472 git commit --allow-empty -m "old commit" &&
473 old=$(git rev-parse --verify HEAD) &&
475 git commit --allow-empty -m "new commit" &&
476 new=$(git rev-parse --verify HEAD) &&
478 git commit --allow-empty --amend -C "$old" &&
479 git show --pretty="format:%ad %s" "$old" >expected &&
480 git show --pretty="format:%ad %s" HEAD >actual &&
481 test_cmp expected actual
485 test_expect_success
'amend using the message from a commit named with tag' '
489 git commit --allow-empty -m "old commit" &&
490 old=$(git rev-parse --verify HEAD) &&
491 git tag -a -m "tag on old" tagged-old HEAD &&
493 git commit --allow-empty -m "new commit" &&
494 new=$(git rev-parse --verify HEAD) &&
496 git commit --allow-empty --amend -C tagged-old &&
497 git show --pretty="format:%ad %s" "$old" >expected &&
498 git show --pretty="format:%ad %s" HEAD >actual &&
499 test_cmp expected actual
503 test_expect_success
'amend can copy notes' '
505 git config notes.rewrite.amend true &&
506 git config notes.rewriteRef "refs/notes/*" &&
508 git notes add -m"a note" &&
510 git commit --amend -m"new foo" &&
511 test "$(git notes show)" = "a note"
515 test_expect_success
'commit a file whose name is a dash' '
523 git commit -m "add dash" >output </dev/null &&
524 test_i18ngrep " changed, 5 insertions" output