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
'-m --edit' '
142 echo amended >expect &&
143 git commit --allow-empty -m buffer &&
144 echo bongo bongo >file &&
146 EDITOR=./editor git commit -m unamended --edit &&
147 git diff-tree -s --format=%s HEAD >msg &&
151 test_expect_success
'-m and -F do not mix' '
152 echo enough with the bongos >file &&
153 test_must_fail git commit -F msg -m amending .
156 test_expect_success
'using message from other commit' '
157 git commit -C HEAD^ .
160 test_expect_success
'editing message from other commit' '
161 cat >editor <<-\EOF &&
163 sed -e "s/amend/older/g" < "$1" > "$1-"
167 echo hula hula >file &&
168 EDITOR=./editor git commit -c HEAD^ -a
171 test_expect_success
'message from stdin' '
172 echo silly new contents >file &&
173 echo commit message from stdin |
177 test_expect_success
'overriding author from command line' '
179 git commit -m author \
180 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
181 grep Rubber.Duck output
184 test_expect_success PERL
'interactive add' '
186 git commit --interactive |
190 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
192 test_must_fail git diff --exit-code >diff1 &&
193 (echo u ; echo "*" ; echo q) |
197 test_must_fail git commit --interactive
200 compare_diff_patch diff1 diff2
203 test_expect_success
'editor not invoked if -F is given' '
204 cat >editor <<-\EOF &&
206 sed -e s/good/bad/g <"$1" >"$1-"
211 echo A good commit message. >msg &&
214 EDITOR=./editor git commit -a -F msg &&
215 git show -s --pretty=format:%s >subject &&
216 grep -q good subject &&
219 echo Another good message. |
220 EDITOR=./editor git commit -a -F - &&
221 git show -s --pretty=format:%s >subject &&
225 test_expect_success
'partial commit that involves removal (1)' '
227 git rm --cached file &&
230 git commit -m "Partial: add elif" elif &&
231 git diff-tree --name-status HEAD^ HEAD >current &&
232 echo "A elif" >expected &&
233 test_cmp expected current
237 test_expect_success
'partial commit that involves removal (2)' '
239 git commit -m "Partial: remove file" file &&
240 git diff-tree --name-status HEAD^ HEAD >current &&
241 echo "D file" >expected &&
242 test_cmp expected current
246 test_expect_success
'partial commit that involves removal (3)' '
248 git rm --cached elif &&
250 git commit -m "Partial: modify elif" elif &&
251 git diff-tree --name-status HEAD^ HEAD >current &&
252 echo "M elif" >expected &&
253 test_cmp expected current
257 test_expect_success
'amend commit to fix author' '
259 oldtick=$GIT_AUTHOR_DATE &&
262 git cat-file -p HEAD |
263 sed -e "s/author.*/author $author $oldtick/" \
264 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
266 git commit --amend --author="$author" &&
267 git cat-file -p HEAD > current &&
268 test_cmp expected current
272 test_expect_success
'amend commit to fix date' '
275 newtick=$GIT_AUTHOR_DATE &&
277 git cat-file -p HEAD |
278 sed -e "s/author.*/author $author $newtick/" \
279 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
281 git commit --amend --date="$newtick" &&
282 git cat-file -p HEAD > current &&
283 test_cmp expected current
287 test_expect_success
'commit complains about bogus date' '
288 test_must_fail git commit --amend --date=10.11.2010
291 test_expect_success
'sign off (1)' '
295 git commit -s -m "thank you" &&
296 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
300 git var GIT_COMMITTER_IDENT |
301 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
303 test_cmp expected actual
307 test_expect_success
'sign off (2)' '
311 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
312 git commit -s -m "thank you
315 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
320 git var GIT_COMMITTER_IDENT |
321 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
323 test_cmp expected actual
327 test_expect_success
'signoff gap' '
331 alt="Alt-RFC-822-Header: Value" &&
332 git commit -s -m "welcome
335 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
340 git var GIT_COMMITTER_IDENT |
341 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
343 test_cmp expected actual
346 test_expect_success
'signoff gap 2' '
351 git commit -s -m "welcome
355 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
362 git var GIT_COMMITTER_IDENT |
363 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
365 test_cmp expected actual
368 test_expect_success
'multiple -m' '
372 git commit -m "one" -m "two" -m "three" &&
373 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
381 test_cmp expected actual
385 test_expect_success
'amend commit to fix author' '
387 oldtick=$GIT_AUTHOR_DATE &&
390 git cat-file -p HEAD |
391 sed -e "s/author.*/author $author $oldtick/" \
392 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
394 git commit --amend --author="$author" &&
395 git cat-file -p HEAD > current &&
396 test_cmp expected current
400 test_expect_success
'git commit <file> with dirty index' '
401 echo tacocat > elif &&
402 echo tehlulz > chz &&
404 git commit elif -m "tacocat is a palindrome" &&
405 git show --stat | grep elif &&
406 git diff --cached | grep chz
409 test_expect_success
'same tree (single parent)' '
412 test_must_fail git commit -m empty
416 test_expect_success
'same tree (single parent) --allow-empty' '
418 git commit --allow-empty -m "forced empty" &&
419 git cat-file commit HEAD | grep forced
423 test_expect_success
'same tree (merge and amend merge)' '
425 git checkout -b side HEAD^ &&
428 git commit -m "add zero" &&
429 git checkout master &&
431 git merge -s ours side -m "empty ok" &&
432 git diff HEAD^ HEAD >actual &&
434 test_cmp expected actual &&
436 git commit --amend -m "empty really ok" &&
437 git diff HEAD^ HEAD >actual &&
439 test_cmp expected actual
443 test_expect_success
'amend using the message from another commit' '
447 git commit --allow-empty -m "old commit" &&
448 old=$(git rev-parse --verify HEAD) &&
450 git commit --allow-empty -m "new commit" &&
451 new=$(git rev-parse --verify HEAD) &&
453 git commit --allow-empty --amend -C "$old" &&
454 git show --pretty="format:%ad %s" "$old" >expected &&
455 git show --pretty="format:%ad %s" HEAD >actual &&
456 test_cmp expected actual
460 test_expect_success
'amend using the message from a commit named with tag' '
464 git commit --allow-empty -m "old commit" &&
465 old=$(git rev-parse --verify HEAD) &&
466 git tag -a -m "tag on old" tagged-old HEAD &&
468 git commit --allow-empty -m "new commit" &&
469 new=$(git rev-parse --verify HEAD) &&
471 git commit --allow-empty --amend -C tagged-old &&
472 git show --pretty="format:%ad %s" "$old" >expected &&
473 git show --pretty="format:%ad %s" HEAD >actual &&
474 test_cmp expected actual
478 test_expect_success
'amend can copy notes' '
480 git config notes.rewrite.amend true &&
481 git config notes.rewriteRef "refs/notes/*" &&
483 git notes add -m"a note" &&
485 git commit --amend -m"new foo" &&
486 test "$(git notes show)" = "a note"