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
'-m --edit' '
152 echo amended >expect &&
153 git commit --allow-empty -m buffer &&
154 echo bongo bongo >file &&
156 EDITOR=./editor git commit -m unamended --edit &&
157 git diff-tree -s --format=%s HEAD >msg &&
161 test_expect_success
'-m and -F do not mix' '
162 echo enough with the bongos >file &&
163 test_must_fail git commit -F msg -m amending .
166 test_expect_success
'using message from other commit' '
167 git commit -C HEAD^ .
170 test_expect_success
'editing message from other commit' '
171 cat >editor <<-\EOF &&
173 sed -e "s/amend/older/g" < "$1" > "$1-"
177 echo hula hula >file &&
178 EDITOR=./editor git commit -c HEAD^ -a
181 test_expect_success
'message from stdin' '
182 echo silly new contents >file &&
183 echo commit message from stdin |
187 test_expect_success
'overriding author from command line' '
189 git commit -m author \
190 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
191 grep Rubber.Duck output
194 test_expect_success PERL
'interactive add' '
196 git commit --interactive |
200 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
202 test_must_fail git diff --exit-code >diff1 &&
203 (echo u ; echo "*" ; echo q) |
207 test_must_fail git commit --interactive
210 compare_diff_patch diff1 diff2
213 test_expect_success
'editor not invoked if -F is given' '
214 cat >editor <<-\EOF &&
216 sed -e s/good/bad/g <"$1" >"$1-"
221 echo A good commit message. >msg &&
224 EDITOR=./editor git commit -a -F msg &&
225 git show -s --pretty=format:%s >subject &&
226 grep -q good subject &&
229 echo Another good message. |
230 EDITOR=./editor git commit -a -F - &&
231 git show -s --pretty=format:%s >subject &&
235 test_expect_success
'partial commit that involves removal (1)' '
237 git rm --cached file &&
240 git commit -m "Partial: add elif" elif &&
241 git diff-tree --name-status HEAD^ HEAD >current &&
242 echo "A elif" >expected &&
243 test_cmp expected current
247 test_expect_success
'partial commit that involves removal (2)' '
249 git commit -m "Partial: remove file" file &&
250 git diff-tree --name-status HEAD^ HEAD >current &&
251 echo "D file" >expected &&
252 test_cmp expected current
256 test_expect_success
'partial commit that involves removal (3)' '
258 git rm --cached elif &&
260 git commit -m "Partial: modify elif" elif &&
261 git diff-tree --name-status HEAD^ HEAD >current &&
262 echo "M elif" >expected &&
263 test_cmp expected current
267 test_expect_success
'amend commit to fix author' '
269 oldtick=$GIT_AUTHOR_DATE &&
272 git cat-file -p HEAD |
273 sed -e "s/author.*/author $author $oldtick/" \
274 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
276 git commit --amend --author="$author" &&
277 git cat-file -p HEAD > current &&
278 test_cmp expected current
282 test_expect_success
'amend commit to fix date' '
285 newtick=$GIT_AUTHOR_DATE &&
287 git cat-file -p HEAD |
288 sed -e "s/author.*/author $author $newtick/" \
289 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
291 git commit --amend --date="$newtick" &&
292 git cat-file -p HEAD > current &&
293 test_cmp expected current
297 test_expect_success
'commit complains about bogus date' '
298 test_must_fail git commit --amend --date=10.11.2010
301 test_expect_success
'sign off (1)' '
305 git commit -s -m "thank you" &&
306 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
310 git var GIT_COMMITTER_IDENT |
311 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
313 test_cmp expected actual
317 test_expect_success
'sign off (2)' '
321 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
322 git commit -s -m "thank you
325 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
330 git var GIT_COMMITTER_IDENT |
331 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
333 test_cmp expected actual
337 test_expect_success
'signoff gap' '
341 alt="Alt-RFC-822-Header: Value" &&
342 git commit -s -m "welcome
345 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
'signoff gap 2' '
361 git commit -s -m "welcome
365 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
372 git var GIT_COMMITTER_IDENT |
373 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
375 test_cmp expected actual
378 test_expect_success
'multiple -m' '
382 git commit -m "one" -m "two" -m "three" &&
383 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
391 test_cmp expected actual
395 test_expect_success
'amend commit to fix author' '
397 oldtick=$GIT_AUTHOR_DATE &&
400 git cat-file -p HEAD |
401 sed -e "s/author.*/author $author $oldtick/" \
402 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
404 git commit --amend --author="$author" &&
405 git cat-file -p HEAD > current &&
406 test_cmp expected current
410 test_expect_success
'git commit <file> with dirty index' '
411 echo tacocat > elif &&
412 echo tehlulz > chz &&
414 git commit elif -m "tacocat is a palindrome" &&
415 git show --stat | grep elif &&
416 git diff --cached | grep chz
419 test_expect_success
'same tree (single parent)' '
422 test_must_fail git commit -m empty
426 test_expect_success
'same tree (single parent) --allow-empty' '
428 git commit --allow-empty -m "forced empty" &&
429 git cat-file commit HEAD | grep forced
433 test_expect_success
'same tree (merge and amend merge)' '
435 git checkout -b side HEAD^ &&
438 git commit -m "add zero" &&
439 git checkout master &&
441 git merge -s ours side -m "empty ok" &&
442 git diff HEAD^ HEAD >actual &&
444 test_cmp expected actual &&
446 git commit --amend -m "empty really ok" &&
447 git diff HEAD^ HEAD >actual &&
449 test_cmp expected actual
453 test_expect_success
'amend using the message from another commit' '
457 git commit --allow-empty -m "old commit" &&
458 old=$(git rev-parse --verify HEAD) &&
460 git commit --allow-empty -m "new commit" &&
461 new=$(git rev-parse --verify HEAD) &&
463 git commit --allow-empty --amend -C "$old" &&
464 git show --pretty="format:%ad %s" "$old" >expected &&
465 git show --pretty="format:%ad %s" HEAD >actual &&
466 test_cmp expected actual
470 test_expect_success
'amend using the message from a commit named with tag' '
474 git commit --allow-empty -m "old commit" &&
475 old=$(git rev-parse --verify HEAD) &&
476 git tag -a -m "tag on old" tagged-old HEAD &&
478 git commit --allow-empty -m "new commit" &&
479 new=$(git rev-parse --verify HEAD) &&
481 git commit --allow-empty --amend -C tagged-old &&
482 git show --pretty="format:%ad %s" "$old" >expected &&
483 git show --pretty="format:%ad %s" HEAD >actual &&
484 test_cmp expected actual
488 test_expect_success
'amend can copy notes' '
490 git config notes.rewrite.amend true &&
491 git config notes.rewriteRef "refs/notes/*" &&
493 git notes add -m"a note" &&
495 git commit --amend -m"new foo" &&
496 test "$(git notes show)" = "a note"
500 test_expect_success
'commit a file whose name is a dash' '
508 git commit -m "add dash" >output </dev/null &&
509 test_i18ngrep " changed, 5 insertions" output