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
'setup: commit message from file' '
85 echo this is the commit message, coming from a file >msg &&
89 test_expect_success
'amend commit' '
90 cat >editor <<-\EOF &&
92 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
96 EDITOR=./editor git commit --amend
99 test_expect_success
'set up editor' '
100 cat >editor <<-\EOF &&
102 sed -e "s/unamended/amended/g" <"$1" >"$1-"
108 test_expect_success
'amend without launching editor' '
109 echo unamended >expect &&
110 git commit --allow-empty -m "unamended" &&
111 echo needs more bongo >file &&
113 EDITOR=./editor git commit --no-edit --amend &&
114 git diff --exit-code HEAD -- file &&
115 git diff-tree -s --format=%s HEAD >msg &&
119 test_expect_success
'--amend --edit' '
120 echo amended >expect &&
121 git commit --allow-empty -m "unamended" &&
122 echo bongo again >file &&
124 EDITOR=./editor git commit --edit --amend &&
125 git diff-tree -s --format=%s HEAD >msg &&
129 test_expect_success
'-m --edit' '
130 echo amended >expect &&
131 git commit --allow-empty -m buffer &&
132 echo bongo bongo >file &&
134 EDITOR=./editor git commit -m unamended --edit &&
135 git diff-tree -s --format=%s HEAD >msg &&
139 test_expect_success
'-m and -F do not mix' '
140 echo enough with the bongos >file &&
141 test_must_fail git commit -F msg -m amending .
144 test_expect_success
'using message from other commit' '
145 git commit -C HEAD^ .
148 test_expect_success
'editing message from other commit' '
149 cat >editor <<-\EOF &&
151 sed -e "s/amend/older/g" < "$1" > "$1-"
155 echo hula hula >file &&
156 EDITOR=./editor git commit -c HEAD^ -a
159 test_expect_success
'message from stdin' '
160 echo silly new contents >file &&
161 echo commit message from stdin |
165 test_expect_success
'overriding author from command line' '
167 git commit -m author \
168 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
169 grep Rubber.Duck output
172 test_expect_success PERL
'interactive add' '
174 git commit --interactive |
178 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
180 test_must_fail git diff --exit-code >diff1 &&
181 (echo u ; echo "*" ; echo q) |
185 test_must_fail git commit --interactive
188 compare_diff_patch diff1 diff2
191 test_expect_success
'editor not invoked if -F is given' '
192 cat >editor <<-\EOF &&
194 sed -e s/good/bad/g <"$1" >"$1-"
199 echo A good commit message. >msg &&
202 EDITOR=./editor git commit -a -F msg &&
203 git show -s --pretty=format:%s >subject &&
204 grep -q good subject &&
207 echo Another good message. |
208 EDITOR=./editor git commit -a -F - &&
209 git show -s --pretty=format:%s >subject &&
213 test_expect_success
'partial commit that involves removal (1)' '
215 git rm --cached file &&
218 git commit -m "Partial: add elif" elif &&
219 git diff-tree --name-status HEAD^ HEAD >current &&
220 echo "A elif" >expected &&
221 test_cmp expected current
225 test_expect_success
'partial commit that involves removal (2)' '
227 git commit -m "Partial: remove file" file &&
228 git diff-tree --name-status HEAD^ HEAD >current &&
229 echo "D file" >expected &&
230 test_cmp expected current
234 test_expect_success
'partial commit that involves removal (3)' '
236 git rm --cached elif &&
238 git commit -m "Partial: modify elif" elif &&
239 git diff-tree --name-status HEAD^ HEAD >current &&
240 echo "M elif" >expected &&
241 test_cmp expected current
245 test_expect_success
'amend commit to fix author' '
247 oldtick=$GIT_AUTHOR_DATE &&
250 git cat-file -p HEAD |
251 sed -e "s/author.*/author $author $oldtick/" \
252 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
254 git commit --amend --author="$author" &&
255 git cat-file -p HEAD > current &&
256 test_cmp expected current
260 test_expect_success
'amend commit to fix date' '
263 newtick=$GIT_AUTHOR_DATE &&
265 git cat-file -p HEAD |
266 sed -e "s/author.*/author $author $newtick/" \
267 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
269 git commit --amend --date="$newtick" &&
270 git cat-file -p HEAD > current &&
271 test_cmp expected current
275 test_expect_success
'commit complains about bogus date' '
276 test_must_fail git commit --amend --date=10.11.2010
279 test_expect_success
'sign off (1)' '
283 git commit -s -m "thank you" &&
284 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
288 git var GIT_COMMITTER_IDENT |
289 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
291 test_cmp expected actual
295 test_expect_success
'sign off (2)' '
299 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
300 git commit -s -m "thank you
303 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
308 git var GIT_COMMITTER_IDENT |
309 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
311 test_cmp expected actual
315 test_expect_success
'signoff gap' '
319 alt="Alt-RFC-822-Header: Value" &&
320 git commit -s -m "welcome
323 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
328 git var GIT_COMMITTER_IDENT |
329 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
331 test_cmp expected actual
334 test_expect_success
'signoff gap 2' '
339 git commit -s -m "welcome
343 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
350 git var GIT_COMMITTER_IDENT |
351 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
353 test_cmp expected actual
356 test_expect_success
'multiple -m' '
360 git commit -m "one" -m "two" -m "three" &&
361 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
369 test_cmp expected actual
373 test_expect_success
'amend commit to fix author' '
375 oldtick=$GIT_AUTHOR_DATE &&
378 git cat-file -p HEAD |
379 sed -e "s/author.*/author $author $oldtick/" \
380 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
382 git commit --amend --author="$author" &&
383 git cat-file -p HEAD > current &&
384 test_cmp expected current
388 test_expect_success
'git commit <file> with dirty index' '
389 echo tacocat > elif &&
390 echo tehlulz > chz &&
392 git commit elif -m "tacocat is a palindrome" &&
393 git show --stat | grep elif &&
394 git diff --cached | grep chz
397 test_expect_success
'same tree (single parent)' '
400 test_must_fail git commit -m empty
404 test_expect_success
'same tree (single parent) --allow-empty' '
406 git commit --allow-empty -m "forced empty" &&
407 git cat-file commit HEAD | grep forced
411 test_expect_success
'same tree (merge and amend merge)' '
413 git checkout -b side HEAD^ &&
416 git commit -m "add zero" &&
417 git checkout master &&
419 git merge -s ours side -m "empty ok" &&
420 git diff HEAD^ HEAD >actual &&
422 test_cmp expected actual &&
424 git commit --amend -m "empty really ok" &&
425 git diff HEAD^ HEAD >actual &&
427 test_cmp expected actual
431 test_expect_success
'amend using the message from another commit' '
435 git commit --allow-empty -m "old commit" &&
436 old=$(git rev-parse --verify HEAD) &&
438 git commit --allow-empty -m "new commit" &&
439 new=$(git rev-parse --verify HEAD) &&
441 git commit --allow-empty --amend -C "$old" &&
442 git show --pretty="format:%ad %s" "$old" >expected &&
443 git show --pretty="format:%ad %s" HEAD >actual &&
444 test_cmp expected actual
448 test_expect_success
'amend using the message from a commit named with tag' '
452 git commit --allow-empty -m "old commit" &&
453 old=$(git rev-parse --verify HEAD) &&
454 git tag -a -m "tag on old" tagged-old HEAD &&
456 git commit --allow-empty -m "new commit" &&
457 new=$(git rev-parse --verify HEAD) &&
459 git commit --allow-empty --amend -C tagged-old &&
460 git show --pretty="format:%ad %s" "$old" >expected &&
461 git show --pretty="format:%ad %s" HEAD >actual &&
462 test_cmp expected actual
466 test_expect_success
'amend can copy notes' '
468 git config notes.rewrite.amend true &&
469 git config notes.rewriteRef "refs/notes/*" &&
471 git notes add -m"a note" &&
473 git commit --amend -m"new foo" &&
474 test "$(git notes show)" = "a note"