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 test_must_fail git commit -m foo -m bar -F file
36 test_expect_success
'-m and -C do not mix' '
37 test_must_fail git commit -C HEAD -m illegal
40 test_expect_success
'paths and -a do not mix' '
41 echo King of the bongo >file &&
42 test_must_fail git commit -m foo -a file
45 test_expect_success PERL
'can use paths with --interactive' '
46 echo bong-o-bong >file &&
47 # 2: update, 1:st path, that is all, 7: quit
48 ( echo 2; echo 1; echo; echo 7 ) |
49 git commit -m foo --interactive file &&
50 git reset --hard HEAD^
53 test_expect_success
'using invalid commit with -C' '
54 test_must_fail git commit -C bogus
57 test_expect_success
'nothing to commit' '
58 test_must_fail git commit -m initial
61 test_expect_success
'setup: non-initial commit' '
62 echo bongo bongo bongo >file &&
66 test_expect_success
'commit message from non-existing file' '
67 echo more bongo: bongo bongo bongo bongo >file &&
68 test_must_fail git commit -F gah -a
71 test_expect_success
'empty commit message' '
72 # Empty except stray tabs and spaces on a few lines.
73 sed -e "s/@//g" >msg <<-\EOF &&
79 test_must_fail git commit -F msg -a
82 test_expect_success
'setup: commit message from file' '
83 echo this is the commit message, coming from a file >msg &&
87 test_expect_success
'amend commit' '
88 cat >editor <<-\EOF &&
90 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
94 EDITOR=./editor git commit --amend
97 test_expect_success
'set up editor' '
98 cat >editor <<-\EOF &&
100 sed -e "s/unamended/amended/g" <"$1" >"$1-"
106 test_expect_success
'amend without launching editor' '
107 echo unamended >expect &&
108 git commit --allow-empty -m "unamended" &&
109 echo needs more bongo >file &&
111 EDITOR=./editor git commit --no-edit --amend &&
112 git diff --exit-code HEAD -- file &&
113 git diff-tree -s --format=%s HEAD >msg &&
117 test_expect_success
'--amend --edit' '
118 echo amended >expect &&
119 git commit --allow-empty -m "unamended" &&
120 echo bongo again >file &&
122 EDITOR=./editor git commit --edit --amend &&
123 git diff-tree -s --format=%s HEAD >msg &&
127 test_expect_success
'-m --edit' '
128 echo amended >expect &&
129 git commit --allow-empty -m buffer &&
130 echo bongo bongo >file &&
132 EDITOR=./editor git commit -m unamended --edit &&
133 git diff-tree -s --format=%s HEAD >msg &&
137 test_expect_success
'-m and -F do not mix' '
138 echo enough with the bongos >file &&
139 test_must_fail git commit -F msg -m amending .
142 test_expect_success
'using message from other commit' '
143 git commit -C HEAD^ .
146 test_expect_success
'editing message from other commit' '
147 cat >editor <<-\EOF &&
149 sed -e "s/amend/older/g" < "$1" > "$1-"
153 echo hula hula >file &&
154 EDITOR=./editor git commit -c HEAD^ -a
157 test_expect_success
'message from stdin' '
158 echo silly new contents >file &&
159 echo commit message from stdin |
163 test_expect_success
'overriding author from command line' '
165 git commit -m author \
166 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
167 grep Rubber.Duck output
170 test_expect_success PERL
'interactive add' '
172 git commit --interactive |
176 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
178 test_must_fail git diff --exit-code >diff1 &&
179 (echo u ; echo "*" ; echo q) |
183 test_must_fail git commit --interactive
186 compare_diff_patch diff1 diff2
189 test_expect_success
'editor not invoked if -F is given' '
190 cat >editor <<-\EOF &&
192 sed -e s/good/bad/g <"$1" >"$1-"
197 echo A good commit message. >msg &&
200 EDITOR=./editor git commit -a -F msg &&
201 git show -s --pretty=format:%s >subject &&
202 grep -q good subject &&
205 echo Another good message. |
206 EDITOR=./editor git commit -a -F - &&
207 git show -s --pretty=format:%s >subject &&
211 test_expect_success
'partial commit that involves removal (1)' '
213 git rm --cached file &&
216 git commit -m "Partial: add elif" elif &&
217 git diff-tree --name-status HEAD^ HEAD >current &&
218 echo "A elif" >expected &&
219 test_cmp expected current
223 test_expect_success
'partial commit that involves removal (2)' '
225 git commit -m "Partial: remove file" file &&
226 git diff-tree --name-status HEAD^ HEAD >current &&
227 echo "D file" >expected &&
228 test_cmp expected current
232 test_expect_success
'partial commit that involves removal (3)' '
234 git rm --cached elif &&
236 git commit -m "Partial: modify elif" elif &&
237 git diff-tree --name-status HEAD^ HEAD >current &&
238 echo "M elif" >expected &&
239 test_cmp expected current
243 test_expect_success
'amend commit to fix author' '
245 oldtick=$GIT_AUTHOR_DATE &&
248 git cat-file -p HEAD |
249 sed -e "s/author.*/author $author $oldtick/" \
250 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
252 git commit --amend --author="$author" &&
253 git cat-file -p HEAD > current &&
254 test_cmp expected current
258 test_expect_success
'amend commit to fix date' '
261 newtick=$GIT_AUTHOR_DATE &&
263 git cat-file -p HEAD |
264 sed -e "s/author.*/author $author $newtick/" \
265 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
267 git commit --amend --date="$newtick" &&
268 git cat-file -p HEAD > current &&
269 test_cmp expected current
273 test_expect_success
'commit complains about bogus date' '
274 test_must_fail git commit --amend --date=10.11.2010
277 test_expect_success
'sign off (1)' '
281 git commit -s -m "thank you" &&
282 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
286 git var GIT_COMMITTER_IDENT |
287 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
289 test_cmp expected actual
293 test_expect_success
'sign off (2)' '
297 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
298 git commit -s -m "thank you
301 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
306 git var GIT_COMMITTER_IDENT |
307 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
309 test_cmp expected actual
313 test_expect_success
'signoff gap' '
317 alt="Alt-RFC-822-Header: Value" &&
318 git commit -s -m "welcome
321 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
332 test_expect_success
'signoff gap 2' '
337 git commit -s -m "welcome
341 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
348 git var GIT_COMMITTER_IDENT |
349 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
351 test_cmp expected actual
354 test_expect_success
'multiple -m' '
358 git commit -m "one" -m "two" -m "three" &&
359 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
367 test_cmp expected actual
371 test_expect_success
'amend commit to fix author' '
373 oldtick=$GIT_AUTHOR_DATE &&
376 git cat-file -p HEAD |
377 sed -e "s/author.*/author $author $oldtick/" \
378 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
380 git commit --amend --author="$author" &&
381 git cat-file -p HEAD > current &&
382 test_cmp expected current
386 test_expect_success
'git commit <file> with dirty index' '
387 echo tacocat > elif &&
388 echo tehlulz > chz &&
390 git commit elif -m "tacocat is a palindrome" &&
391 git show --stat | grep elif &&
392 git diff --cached | grep chz
395 test_expect_success
'same tree (single parent)' '
398 test_must_fail git commit -m empty
402 test_expect_success
'same tree (single parent) --allow-empty' '
404 git commit --allow-empty -m "forced empty" &&
405 git cat-file commit HEAD | grep forced
409 test_expect_success
'same tree (merge and amend merge)' '
411 git checkout -b side HEAD^ &&
414 git commit -m "add zero" &&
415 git checkout master &&
417 git merge -s ours side -m "empty ok" &&
418 git diff HEAD^ HEAD >actual &&
420 test_cmp expected actual &&
422 git commit --amend -m "empty really ok" &&
423 git diff HEAD^ HEAD >actual &&
425 test_cmp expected actual
429 test_expect_success
'amend using the message from another commit' '
433 git commit --allow-empty -m "old commit" &&
434 old=$(git rev-parse --verify HEAD) &&
436 git commit --allow-empty -m "new commit" &&
437 new=$(git rev-parse --verify HEAD) &&
439 git commit --allow-empty --amend -C "$old" &&
440 git show --pretty="format:%ad %s" "$old" >expected &&
441 git show --pretty="format:%ad %s" HEAD >actual &&
442 test_cmp expected actual
446 test_expect_success
'amend using the message from a commit named with tag' '
450 git commit --allow-empty -m "old commit" &&
451 old=$(git rev-parse --verify HEAD) &&
452 git tag -a -m "tag on old" tagged-old HEAD &&
454 git commit --allow-empty -m "new commit" &&
455 new=$(git rev-parse --verify HEAD) &&
457 git commit --allow-empty --amend -C tagged-old &&
458 git show --pretty="format:%ad %s" "$old" >expected &&
459 git show --pretty="format:%ad %s" HEAD >actual &&
460 test_cmp expected actual
464 test_expect_success
'amend can copy notes' '
466 git config notes.rewrite.amend true &&
467 git config notes.rewriteRef "refs/notes/*" &&
469 git notes add -m"a note" &&
471 git commit --amend -m"new foo" &&
472 test "$(git notes show)" = "a note"