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 'setup: commit message from file' '
90 git checkout HEAD file && echo >>file && git add file &&
91 echo this is the commit message, coming from a file >msg &&
95 test_expect_success 'amend commit' '
96 cat >editor <<-\EOF &&
98 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
102 EDITOR=./editor git commit --amend
105 test_expect_success 'set up editor' '
106 cat >editor <<-\EOF &&
108 sed -e "s/unamended/amended/g" <"$1" >"$1-"
114 test_expect_success 'amend without launching editor' '
115 echo unamended >expect &&
116 git commit --allow-empty -m "unamended" &&
117 echo needs more bongo >file &&
119 EDITOR=./editor git commit --no-edit --amend &&
120 git diff --exit-code HEAD -- file &&
121 git diff-tree -s --format=%s HEAD >msg &&
125 test_expect_success '--amend --edit' '
126 echo amended >expect &&
127 git commit --allow-empty -m "unamended" &&
128 echo bongo again >file &&
130 EDITOR=./editor git commit --edit --amend &&
131 git diff-tree -s --format=%s HEAD >msg &&
135 test_expect_success '-m --edit' '
136 echo amended >expect &&
137 git commit --allow-empty -m buffer &&
138 echo bongo bongo >file &&
140 EDITOR=./editor git commit -m unamended --edit &&
141 git diff-tree -s --format=%s HEAD >msg &&
145 test_expect_success '-m and -F do not mix' '
146 echo enough with the bongos >file &&
147 test_must_fail git commit -F msg -m amending .
150 test_expect_success 'using message from other commit' '
151 git commit -C HEAD^ .
154 test_expect_success 'editing message from other commit' '
155 cat >editor <<-\EOF &&
157 sed -e "s/amend/older/g" < "$1" > "$1-"
161 echo hula hula >file &&
162 EDITOR=./editor git commit -c HEAD^ -a
165 test_expect_success 'message from stdin' '
166 echo silly new contents >file &&
167 echo commit message from stdin |
171 test_expect_success 'overriding author from command line' '
173 git commit -m author \
174 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
175 grep Rubber.Duck output
178 test_expect_success PERL 'interactive add' '
180 git commit --interactive |
184 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
186 test_must_fail git diff --exit-code >diff1 &&
187 (echo u ; echo "*" ; echo q) |
191 test_must_fail git commit --interactive
194 compare_diff_patch diff1 diff2
197 test_expect_success 'editor not invoked if -F is given' '
198 cat >editor <<-\EOF &&
200 sed -e s/good/bad/g <"$1" >"$1-"
205 echo A good commit message. >msg &&
208 EDITOR=./editor git commit -a -F msg &&
209 git show -s --pretty=format:%s >subject &&
210 grep -q good subject &&
213 echo Another good message. |
214 EDITOR=./editor git commit -a -F - &&
215 git show -s --pretty=format:%s >subject &&
219 test_expect_success 'partial commit that involves removal (1)' '
221 git rm --cached file &&
224 git commit -m "Partial: add elif" elif &&
225 git diff-tree --name-status HEAD^ HEAD >current &&
226 echo "A elif" >expected &&
227 test_cmp expected current
231 test_expect_success 'partial commit that involves removal (2)' '
233 git commit -m "Partial: remove file" file &&
234 git diff-tree --name-status HEAD^ HEAD >current &&
235 echo "D file" >expected &&
236 test_cmp expected current
240 test_expect_success 'partial commit that involves removal (3)' '
242 git rm --cached elif &&
244 git commit -m "Partial: modify elif" elif &&
245 git diff-tree --name-status HEAD^ HEAD >current &&
246 echo "M elif" >expected &&
247 test_cmp expected current
251 test_expect_success 'amend commit to fix author' '
253 oldtick=$GIT_AUTHOR_DATE &&
256 git cat-file -p HEAD |
257 sed -e "s/author.*/author $author $oldtick/" \
258 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
260 git commit --amend --author="$author" &&
261 git cat-file -p HEAD > current &&
262 test_cmp expected current
266 test_expect_success 'amend commit to fix date' '
269 newtick=$GIT_AUTHOR_DATE &&
271 git cat-file -p HEAD |
272 sed -e "s/author.*/author $author $newtick/" \
273 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
275 git commit --amend --date="$newtick" &&
276 git cat-file -p HEAD > current &&
277 test_cmp expected current
281 test_expect_success 'commit complains about bogus date' '
282 test_must_fail git commit --amend --date=10.11.2010
285 test_expect_success 'sign off (1)' '
289 git commit -s -m "thank you" &&
290 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
294 git var GIT_COMMITTER_IDENT |
295 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
297 test_cmp expected actual
301 test_expect_success 'sign off (2)' '
305 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
306 git commit -s -m "thank you
309 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
314 git var GIT_COMMITTER_IDENT |
315 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
317 test_cmp expected actual
321 test_expect_success 'signoff gap' '
325 alt="Alt-RFC-822-Header: Value" &&
326 git commit -s -m "welcome
329 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
334 git var GIT_COMMITTER_IDENT |
335 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
337 test_cmp expected actual
340 test_expect_success 'signoff gap 2' '
345 git commit -s -m "welcome
349 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
356 git var GIT_COMMITTER_IDENT |
357 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
359 test_cmp expected actual
362 test_expect_success 'multiple -m' '
366 git commit -m "one" -m "two" -m "three" &&
367 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
375 test_cmp expected actual
379 test_expect_success 'amend commit to fix author' '
381 oldtick=$GIT_AUTHOR_DATE &&
384 git cat-file -p HEAD |
385 sed -e "s/author.*/author $author $oldtick/" \
386 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
388 git commit --amend --author="$author" &&
389 git cat-file -p HEAD > current &&
390 test_cmp expected current
394 test_expect_success 'git commit <file> with dirty index' '
395 echo tacocat > elif &&
396 echo tehlulz > chz &&
398 git commit elif -m "tacocat is a palindrome" &&
399 git show --stat | grep elif &&
400 git diff --cached | grep chz
403 test_expect_success 'same tree (single parent)' '
406 test_must_fail git commit -m empty
410 test_expect_success 'same tree (single parent) --allow-empty' '
412 git commit --allow-empty -m "forced empty" &&
413 git cat-file commit HEAD | grep forced
417 test_expect_success 'same tree (merge and amend merge)' '
419 git checkout -b side HEAD^ &&
422 git commit -m "add zero" &&
423 git checkout master &&
425 git merge -s ours side -m "empty ok" &&
426 git diff HEAD^ HEAD >actual &&
428 test_cmp expected actual &&
430 git commit --amend -m "empty really ok" &&
431 git diff HEAD^ HEAD >actual &&
433 test_cmp expected actual
437 test_expect_success 'amend using the message from another commit' '
441 git commit --allow-empty -m "old commit" &&
442 old=$(git rev-parse --verify HEAD) &&
444 git commit --allow-empty -m "new commit" &&
445 new=$(git rev-parse --verify HEAD) &&
447 git commit --allow-empty --amend -C "$old" &&
448 git show --pretty="format:%ad %s" "$old" >expected &&
449 git show --pretty="format:%ad %s" HEAD >actual &&
450 test_cmp expected actual
454 test_expect_success 'amend using the message from a commit named with tag' '
458 git commit --allow-empty -m "old commit" &&
459 old=$(git rev-parse --verify HEAD) &&
460 git tag -a -m "tag on old" tagged-old HEAD &&
462 git commit --allow-empty -m "new commit" &&
463 new=$(git rev-parse --verify HEAD) &&
465 git commit --allow-empty --amend -C tagged-old &&
466 git show --pretty="format:%ad %s" "$old" >expected &&
467 git show --pretty="format:%ad %s" HEAD >actual &&
468 test_cmp expected actual
472 test_expect_success 'amend can copy notes' '
474 git config notes.rewrite.amend true &&
475 git config notes.rewriteRef "refs/notes/*" &&
477 git notes add -m"a note" &&
479 git commit --amend -m"new foo" &&
480 test "$(git notes show)" = "a note"