Merge branch 'jk/subtree-prefix'
[alt-git.git] / t / t7501-commit.sh
blobd58b097ff3b3d0e6e68d15491d38f1f32460f6e7
1 #!/bin/sh
3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, -i and -o, test reflog,
7 # signoff
9 test_description='git commit'
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY/diff-lib.sh"
13 author='The Real Author <someguy@his.email.org>'
15 test_tick
17 test_expect_success 'initial status' '
18 echo bongo bongo >file &&
19 git add file &&
20 git status >actual &&
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' '
29 git commit -m initial
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 --allow-empty -C bogus
59 test_expect_success 'nothing to commit' '
60 git reset --hard &&
61 test_must_fail git commit -m initial
64 test_expect_success '--dry-run fails with nothing to commit' '
65 test_must_fail git commit -m initial --dry-run
68 test_expect_success '--short fails with nothing to commit' '
69 test_must_fail git commit -m initial --short
72 test_expect_success '--porcelain fails with nothing to commit' '
73 test_must_fail git commit -m initial --porcelain
76 test_expect_success '--long fails with nothing to commit' '
77 test_must_fail git commit -m initial --long
80 test_expect_success 'setup: non-initial commit' '
81 echo bongo bongo bongo >file &&
82 git commit -m next -a
85 test_expect_success '--dry-run with stuff to commit returns ok' '
86 echo bongo bongo bongo >>file &&
87 git commit -m next -a --dry-run
90 test_expect_failure '--short with stuff to commit returns ok' '
91 echo bongo bongo bongo >>file &&
92 git commit -m next -a --short
95 test_expect_failure '--porcelain with stuff to commit returns ok' '
96 echo bongo bongo bongo >>file &&
97 git commit -m next -a --porcelain
100 test_expect_success '--long with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --long
105 test_expect_success 'commit message from non-existing file' '
106 echo more bongo: bongo bongo bongo bongo >file &&
107 test_must_fail git commit -F gah -a
110 test_expect_success 'empty commit message' '
111 # Empty except stray tabs and spaces on a few lines.
112 sed -e "s/@//g" >msg <<-\EOF &&
116 @Signed-off-by: hula@
118 test_must_fail git commit -F msg -a
121 test_expect_success 'template "emptyness" check does not kick in with -F' '
122 git checkout HEAD file && echo >>file && git add file &&
123 git commit -t file -F file
126 test_expect_success 'template "emptyness" check' '
127 git checkout HEAD file && echo >>file && git add file &&
128 test_must_fail git commit -t file 2>err &&
129 test_i18ngrep "did not edit" err
132 test_expect_success 'setup: commit message from file' '
133 git checkout HEAD file && echo >>file && git add file &&
134 echo this is the commit message, coming from a file >msg &&
135 git commit -F msg -a
138 test_expect_success 'amend commit' '
139 cat >editor <<-\EOF &&
140 #!/bin/sh
141 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
142 mv "$1-" "$1"
144 chmod 755 editor &&
145 EDITOR=./editor git commit --amend
148 test_expect_success 'amend --only ignores staged contents' '
149 cp file file.expect &&
150 echo changed >file &&
151 git add file &&
152 git commit --no-edit --amend --only &&
153 git cat-file blob HEAD:file >file.actual &&
154 test_cmp file.expect file.actual &&
155 git diff --exit-code
158 test_expect_success 'set up editor' '
159 cat >editor <<-\EOF &&
160 #!/bin/sh
161 sed -e "s/unamended/amended/g" <"$1" >"$1-"
162 mv "$1-" "$1"
164 chmod 755 editor
167 test_expect_success 'amend without launching editor' '
168 echo unamended >expect &&
169 git commit --allow-empty -m "unamended" &&
170 echo needs more bongo >file &&
171 git add file &&
172 EDITOR=./editor git commit --no-edit --amend &&
173 git diff --exit-code HEAD -- file &&
174 git diff-tree -s --format=%s HEAD >msg &&
175 test_cmp expect msg
178 test_expect_success '--amend --edit' '
179 echo amended >expect &&
180 git commit --allow-empty -m "unamended" &&
181 echo bongo again >file &&
182 git add file &&
183 EDITOR=./editor git commit --edit --amend &&
184 git diff-tree -s --format=%s HEAD >msg &&
185 test_cmp expect msg
188 test_expect_success '--amend --edit of empty message' '
189 cat >replace <<-\EOF &&
190 #!/bin/sh
191 echo "amended" >"$1"
193 chmod 755 replace &&
194 git commit --allow-empty --allow-empty-message -m "" &&
195 echo more bongo >file &&
196 git add file &&
197 EDITOR=./replace git commit --edit --amend &&
198 git diff-tree -s --format=%s HEAD >msg &&
199 ./replace expect &&
200 test_cmp expect msg
203 test_expect_success '-m --edit' '
204 echo amended >expect &&
205 git commit --allow-empty -m buffer &&
206 echo bongo bongo >file &&
207 git add file &&
208 EDITOR=./editor git commit -m unamended --edit &&
209 git diff-tree -s --format=%s HEAD >msg &&
210 test_cmp expect msg
213 test_expect_success '-m and -F do not mix' '
214 echo enough with the bongos >file &&
215 test_must_fail git commit -F msg -m amending .
218 test_expect_success 'using message from other commit' '
219 git commit -C HEAD^ .
222 test_expect_success 'editing message from other commit' '
223 cat >editor <<-\EOF &&
224 #!/bin/sh
225 sed -e "s/amend/older/g" < "$1" > "$1-"
226 mv "$1-" "$1"
228 chmod 755 editor &&
229 echo hula hula >file &&
230 EDITOR=./editor git commit -c HEAD^ -a
233 test_expect_success 'message from stdin' '
234 echo silly new contents >file &&
235 echo commit message from stdin |
236 git commit -F - -a
239 test_expect_success 'overriding author from command line' '
240 echo gak >file &&
241 git commit -m author \
242 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
243 grep Rubber.Duck output
246 test_expect_success PERL 'interactive add' '
247 echo 7 |
248 git commit --interactive |
249 grep "What now"
252 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
253 echo zoo >file &&
254 test_must_fail git diff --exit-code >diff1 &&
255 (echo u ; echo "*" ; echo q) |
257 EDITOR=: &&
258 export EDITOR &&
259 test_must_fail git commit --interactive
260 ) &&
261 git diff >diff2 &&
262 compare_diff_patch diff1 diff2
265 test_expect_success 'editor not invoked if -F is given' '
266 cat >editor <<-\EOF &&
267 #!/bin/sh
268 sed -e s/good/bad/g <"$1" >"$1-"
269 mv "$1-" "$1"
271 chmod 755 editor &&
273 echo A good commit message. >msg &&
274 echo moo >file &&
276 EDITOR=./editor git commit -a -F msg &&
277 git show -s --pretty=format:%s >subject &&
278 grep -q good subject &&
280 echo quack >file &&
281 echo Another good message. |
282 EDITOR=./editor git commit -a -F - &&
283 git show -s --pretty=format:%s >subject &&
284 grep -q good subject
287 test_expect_success 'partial commit that involves removal (1)' '
289 git rm --cached file &&
290 mv file elif &&
291 git add elif &&
292 git commit -m "Partial: add elif" elif &&
293 git diff-tree --name-status HEAD^ HEAD >current &&
294 echo "A elif" >expected &&
295 test_cmp expected current
299 test_expect_success 'partial commit that involves removal (2)' '
301 git commit -m "Partial: remove file" file &&
302 git diff-tree --name-status HEAD^ HEAD >current &&
303 echo "D file" >expected &&
304 test_cmp expected current
308 test_expect_success 'partial commit that involves removal (3)' '
310 git rm --cached elif &&
311 echo elif >elif &&
312 git commit -m "Partial: modify elif" elif &&
313 git diff-tree --name-status HEAD^ HEAD >current &&
314 echo "M elif" >expected &&
315 test_cmp expected current
319 test_expect_success 'amend commit to fix author' '
321 oldtick=$GIT_AUTHOR_DATE &&
322 test_tick &&
323 git reset --hard &&
324 git cat-file -p HEAD |
325 sed -e "s/author.*/author $author $oldtick/" \
326 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
327 expected &&
328 git commit --amend --author="$author" &&
329 git cat-file -p HEAD > current &&
330 test_cmp expected current
334 test_expect_success 'amend commit to fix date' '
336 test_tick &&
337 newtick=$GIT_AUTHOR_DATE &&
338 git reset --hard &&
339 git cat-file -p HEAD |
340 sed -e "s/author.*/author $author $newtick/" \
341 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
342 expected &&
343 git commit --amend --date="$newtick" &&
344 git cat-file -p HEAD > current &&
345 test_cmp expected current
349 test_expect_success 'commit complains about bogus date' '
350 test_must_fail git commit --amend --date=10.11.2010
353 test_expect_success 'sign off (1)' '
355 echo 1 >positive &&
356 git add positive &&
357 git commit -s -m "thank you" &&
358 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
360 echo thank you
361 echo
362 git var GIT_COMMITTER_IDENT |
363 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
364 ) >expected &&
365 test_cmp expected actual
369 test_expect_success 'sign off (2)' '
371 echo 2 >positive &&
372 git add positive &&
373 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
374 git commit -s -m "thank you
376 $existing" &&
377 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
379 echo thank you
380 echo
381 echo $existing
382 git var GIT_COMMITTER_IDENT |
383 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
384 ) >expected &&
385 test_cmp expected actual
389 test_expect_success 'signoff gap' '
391 echo 3 >positive &&
392 git add positive &&
393 alt="Alt-RFC-822-Header: Value" &&
394 git commit -s -m "welcome
396 $alt" &&
397 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
399 echo welcome
400 echo
401 echo $alt
402 git var GIT_COMMITTER_IDENT |
403 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
404 ) >expected &&
405 test_cmp expected actual
408 test_expect_success 'signoff gap 2' '
410 echo 4 >positive &&
411 git add positive &&
412 alt="fixed: 34" &&
413 git commit -s -m "welcome
415 We have now
416 $alt" &&
417 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
419 echo welcome
420 echo
421 echo We have now
422 echo $alt
423 echo
424 git var GIT_COMMITTER_IDENT |
425 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
426 ) >expected &&
427 test_cmp expected actual
430 test_expect_success 'multiple -m' '
432 >negative &&
433 git add negative &&
434 git commit -m "one" -m "two" -m "three" &&
435 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
437 echo one
438 echo
439 echo two
440 echo
441 echo three
442 ) >expected &&
443 test_cmp expected actual
447 test_expect_success 'amend commit to fix author' '
449 oldtick=$GIT_AUTHOR_DATE &&
450 test_tick &&
451 git reset --hard &&
452 git cat-file -p HEAD |
453 sed -e "s/author.*/author $author $oldtick/" \
454 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
455 expected &&
456 git commit --amend --author="$author" &&
457 git cat-file -p HEAD > current &&
458 test_cmp expected current
462 test_expect_success 'git commit <file> with dirty index' '
463 echo tacocat > elif &&
464 echo tehlulz > chz &&
465 git add chz &&
466 git commit elif -m "tacocat is a palindrome" &&
467 git show --stat | grep elif &&
468 git diff --cached | grep chz
471 test_expect_success 'same tree (single parent)' '
473 git reset --hard &&
474 test_must_fail git commit -m empty
478 test_expect_success 'same tree (single parent) --allow-empty' '
480 git commit --allow-empty -m "forced empty" &&
481 git cat-file commit HEAD | grep forced
485 test_expect_success 'same tree (merge and amend merge)' '
487 git checkout -b side HEAD^ &&
488 echo zero >zero &&
489 git add zero &&
490 git commit -m "add zero" &&
491 git checkout master &&
493 git merge -s ours side -m "empty ok" &&
494 git diff HEAD^ HEAD >actual &&
495 : >expected &&
496 test_cmp expected actual &&
498 git commit --amend -m "empty really ok" &&
499 git diff HEAD^ HEAD >actual &&
500 : >expected &&
501 test_cmp expected actual
505 test_expect_success 'amend using the message from another commit' '
507 git reset --hard &&
508 test_tick &&
509 git commit --allow-empty -m "old commit" &&
510 old=$(git rev-parse --verify HEAD) &&
511 test_tick &&
512 git commit --allow-empty -m "new commit" &&
513 new=$(git rev-parse --verify HEAD) &&
514 test_tick &&
515 git commit --allow-empty --amend -C "$old" &&
516 git show --pretty="format:%ad %s" "$old" >expected &&
517 git show --pretty="format:%ad %s" HEAD >actual &&
518 test_cmp expected actual
522 test_expect_success 'amend using the message from a commit named with tag' '
524 git reset --hard &&
525 test_tick &&
526 git commit --allow-empty -m "old commit" &&
527 old=$(git rev-parse --verify HEAD) &&
528 git tag -a -m "tag on old" tagged-old HEAD &&
529 test_tick &&
530 git commit --allow-empty -m "new commit" &&
531 new=$(git rev-parse --verify HEAD) &&
532 test_tick &&
533 git commit --allow-empty --amend -C tagged-old &&
534 git show --pretty="format:%ad %s" "$old" >expected &&
535 git show --pretty="format:%ad %s" HEAD >actual &&
536 test_cmp expected actual
540 test_expect_success 'amend can copy notes' '
542 git config notes.rewrite.amend true &&
543 git config notes.rewriteRef "refs/notes/*" &&
544 test_commit foo &&
545 git notes add -m"a note" &&
546 test_tick &&
547 git commit --amend -m"new foo" &&
548 test "$(git notes show)" = "a note"
552 test_expect_success 'commit a file whose name is a dash' '
553 git reset --hard &&
554 for i in 1 2 3 4 5
556 echo $i
557 done >./- &&
558 git add ./- &&
559 test_tick &&
560 git commit -m "add dash" >output </dev/null &&
561 test_i18ngrep " changed, 5 insertions" output
564 test_expect_success '--only works on to-be-born branch' '
565 # This test relies on having something in the index, as it
566 # would not otherwise actually prove much. So check this.
567 test -n "$(git ls-files)" &&
568 git checkout --orphan orphan &&
569 echo foo >newfile &&
570 git add newfile &&
571 git commit --only newfile -m"--only on unborn branch" &&
572 echo newfile >expected &&
573 git ls-tree -r --name-only HEAD >actual &&
574 test_cmp expected actual
577 test_done