Merge 'install-wincred' into HEAD
[git/mingw/4msysgit.git] / t / t7501-commit.sh
blob94eec83b37f38699af91b3808051515f267f3a5f
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 'setup: non-initial commit' '
65 echo bongo bongo bongo >file &&
66 git commit -m next -a
69 test_expect_success 'commit message from non-existing file' '
70 echo more bongo: bongo bongo bongo bongo >file &&
71 test_must_fail git commit -F gah -a
74 test_expect_success 'empty commit message' '
75 # Empty except stray tabs and spaces on a few lines.
76 sed -e "s/@//g" >msg <<-\EOF &&
77 @ @
79 @ @
80 @Signed-off-by: hula@
81 EOF
82 test_must_fail git commit -F msg -a
85 test_expect_success 'template "emptyness" check does not kick in with -F' '
86 git checkout HEAD file && echo >>file && git add file &&
87 git commit -t file -F file
90 test_expect_success 'template "emptyness" check' '
91 git checkout HEAD file && echo >>file && git add file &&
92 test_must_fail git commit -t file 2>err &&
93 test_i18ngrep "did not edit" err
96 test_expect_success 'setup: commit message from file' '
97 git checkout HEAD file && echo >>file && git add file &&
98 echo this is the commit message, coming from a file >msg &&
99 git commit -F msg -a
102 test_expect_success 'amend commit' '
103 cat >editor <<-\EOF &&
104 #!/bin/sh
105 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
106 mv "$1-" "$1"
108 chmod 755 editor &&
109 EDITOR=./editor git commit --amend
112 test_expect_success 'amend --only ignores staged contents' '
113 cp file file.expect &&
114 echo changed >file &&
115 git add file &&
116 git commit --no-edit --amend --only &&
117 git cat-file blob HEAD:file >file.actual &&
118 test_cmp file.expect file.actual &&
119 git diff --exit-code
122 test_expect_success 'set up editor' '
123 cat >editor <<-\EOF &&
124 #!/bin/sh
125 sed -e "s/unamended/amended/g" <"$1" >"$1-"
126 mv "$1-" "$1"
128 chmod 755 editor
131 test_expect_success 'amend without launching editor' '
132 echo unamended >expect &&
133 git commit --allow-empty -m "unamended" &&
134 echo needs more bongo >file &&
135 git add file &&
136 EDITOR=./editor git commit --no-edit --amend &&
137 git diff --exit-code HEAD -- file &&
138 git diff-tree -s --format=%s HEAD >msg &&
139 test_cmp expect msg
142 test_expect_success '--amend --edit' '
143 echo amended >expect &&
144 git commit --allow-empty -m "unamended" &&
145 echo bongo again >file &&
146 git add file &&
147 EDITOR=./editor git commit --edit --amend &&
148 git diff-tree -s --format=%s HEAD >msg &&
149 test_cmp expect msg
152 test_expect_success '--amend --edit of empty message' '
153 cat >replace <<-\EOF &&
154 #!/bin/sh
155 echo "amended" >"$1"
157 chmod 755 replace &&
158 git commit --allow-empty --allow-empty-message -m "" &&
159 echo more bongo >file &&
160 git add file &&
161 EDITOR=./replace git commit --edit --amend &&
162 git diff-tree -s --format=%s HEAD >msg &&
163 ./replace expect &&
164 test_cmp expect msg
167 test_expect_success '-m --edit' '
168 echo amended >expect &&
169 git commit --allow-empty -m buffer &&
170 echo bongo bongo >file &&
171 git add file &&
172 EDITOR=./editor git commit -m unamended --edit &&
173 git diff-tree -s --format=%s HEAD >msg &&
174 test_cmp expect msg
177 test_expect_success '-m and -F do not mix' '
178 echo enough with the bongos >file &&
179 test_must_fail git commit -F msg -m amending .
182 test_expect_success 'using message from other commit' '
183 git commit -C HEAD^ .
186 test_expect_success 'editing message from other commit' '
187 cat >editor <<-\EOF &&
188 #!/bin/sh
189 sed -e "s/amend/older/g" < "$1" > "$1-"
190 mv "$1-" "$1"
192 chmod 755 editor &&
193 echo hula hula >file &&
194 EDITOR=./editor git commit -c HEAD^ -a
197 test_expect_success 'message from stdin' '
198 echo silly new contents >file &&
199 echo commit message from stdin |
200 git commit -F - -a
203 test_expect_success 'overriding author from command line' '
204 echo gak >file &&
205 git commit -m author \
206 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
207 grep Rubber.Duck output
210 test_expect_success PERL 'interactive add' '
211 echo 7 |
212 git commit --interactive |
213 grep "What now"
216 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
217 echo zoo >file &&
218 test_must_fail git diff --exit-code >diff1 &&
219 (echo u ; echo "*" ; echo q) |
221 EDITOR=: &&
222 export EDITOR &&
223 test_must_fail git commit --interactive
224 ) &&
225 git diff >diff2 &&
226 compare_diff_patch diff1 diff2
229 test_expect_success 'editor not invoked if -F is given' '
230 cat >editor <<-\EOF &&
231 #!/bin/sh
232 sed -e s/good/bad/g <"$1" >"$1-"
233 mv "$1-" "$1"
235 chmod 755 editor &&
237 echo A good commit message. >msg &&
238 echo moo >file &&
240 EDITOR=./editor git commit -a -F msg &&
241 git show -s --pretty=format:%s >subject &&
242 grep -q good subject &&
244 echo quack >file &&
245 echo Another good message. |
246 EDITOR=./editor git commit -a -F - &&
247 git show -s --pretty=format:%s >subject &&
248 grep -q good subject
251 test_expect_success 'partial commit that involves removal (1)' '
253 git rm --cached file &&
254 mv file elif &&
255 git add elif &&
256 git commit -m "Partial: add elif" elif &&
257 git diff-tree --name-status HEAD^ HEAD >current &&
258 echo "A elif" >expected &&
259 test_cmp expected current
263 test_expect_success 'partial commit that involves removal (2)' '
265 git commit -m "Partial: remove file" file &&
266 git diff-tree --name-status HEAD^ HEAD >current &&
267 echo "D file" >expected &&
268 test_cmp expected current
272 test_expect_success 'partial commit that involves removal (3)' '
274 git rm --cached elif &&
275 echo elif >elif &&
276 git commit -m "Partial: modify elif" elif &&
277 git diff-tree --name-status HEAD^ HEAD >current &&
278 echo "M elif" >expected &&
279 test_cmp expected current
283 test_expect_success 'amend commit to fix author' '
285 oldtick=$GIT_AUTHOR_DATE &&
286 test_tick &&
287 git reset --hard &&
288 git cat-file -p HEAD |
289 sed -e "s/author.*/author $author $oldtick/" \
290 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
291 expected &&
292 git commit --amend --author="$author" &&
293 git cat-file -p HEAD > current &&
294 test_cmp expected current
298 test_expect_success 'amend commit to fix date' '
300 test_tick &&
301 newtick=$GIT_AUTHOR_DATE &&
302 git reset --hard &&
303 git cat-file -p HEAD |
304 sed -e "s/author.*/author $author $newtick/" \
305 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
306 expected &&
307 git commit --amend --date="$newtick" &&
308 git cat-file -p HEAD > current &&
309 test_cmp expected current
313 test_expect_success 'commit complains about bogus date' '
314 test_must_fail git commit --amend --date=10.11.2010
317 test_expect_success 'sign off (1)' '
319 echo 1 >positive &&
320 git add positive &&
321 git commit -s -m "thank you" &&
322 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
324 echo thank you
325 echo
326 git var GIT_COMMITTER_IDENT |
327 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
328 ) >expected &&
329 test_cmp expected actual
333 test_expect_success 'sign off (2)' '
335 echo 2 >positive &&
336 git add positive &&
337 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
338 git commit -s -m "thank you
340 $existing" &&
341 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
343 echo thank you
344 echo
345 echo $existing
346 git var GIT_COMMITTER_IDENT |
347 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
348 ) >expected &&
349 test_cmp expected actual
353 test_expect_success 'signoff gap' '
355 echo 3 >positive &&
356 git add positive &&
357 alt="Alt-RFC-822-Header: Value" &&
358 git commit -s -m "welcome
360 $alt" &&
361 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
363 echo welcome
364 echo
365 echo $alt
366 git var GIT_COMMITTER_IDENT |
367 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
368 ) >expected &&
369 test_cmp expected actual
372 test_expect_success 'signoff gap 2' '
374 echo 4 >positive &&
375 git add positive &&
376 alt="fixed: 34" &&
377 git commit -s -m "welcome
379 We have now
380 $alt" &&
381 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
383 echo welcome
384 echo
385 echo We have now
386 echo $alt
387 echo
388 git var GIT_COMMITTER_IDENT |
389 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
390 ) >expected &&
391 test_cmp expected actual
394 test_expect_success 'multiple -m' '
396 >negative &&
397 git add negative &&
398 git commit -m "one" -m "two" -m "three" &&
399 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
401 echo one
402 echo
403 echo two
404 echo
405 echo three
406 ) >expected &&
407 test_cmp expected actual
411 test_expect_success 'amend commit to fix author' '
413 oldtick=$GIT_AUTHOR_DATE &&
414 test_tick &&
415 git reset --hard &&
416 git cat-file -p HEAD |
417 sed -e "s/author.*/author $author $oldtick/" \
418 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
419 expected &&
420 git commit --amend --author="$author" &&
421 git cat-file -p HEAD > current &&
422 test_cmp expected current
426 test_expect_success 'git commit <file> with dirty index' '
427 echo tacocat > elif &&
428 echo tehlulz > chz &&
429 git add chz &&
430 git commit elif -m "tacocat is a palindrome" &&
431 git show --stat | grep elif &&
432 git diff --cached | grep chz
435 test_expect_success 'same tree (single parent)' '
437 git reset --hard &&
438 test_must_fail git commit -m empty
442 test_expect_success 'same tree (single parent) --allow-empty' '
444 git commit --allow-empty -m "forced empty" &&
445 git cat-file commit HEAD | grep forced
449 test_expect_success 'same tree (merge and amend merge)' '
451 git checkout -b side HEAD^ &&
452 echo zero >zero &&
453 git add zero &&
454 git commit -m "add zero" &&
455 git checkout master &&
457 git merge -s ours side -m "empty ok" &&
458 git diff HEAD^ HEAD >actual &&
459 : >expected &&
460 test_cmp expected actual &&
462 git commit --amend -m "empty really ok" &&
463 git diff HEAD^ HEAD >actual &&
464 : >expected &&
465 test_cmp expected actual
469 test_expect_success 'amend using the message from another commit' '
471 git reset --hard &&
472 test_tick &&
473 git commit --allow-empty -m "old commit" &&
474 old=$(git rev-parse --verify HEAD) &&
475 test_tick &&
476 git commit --allow-empty -m "new commit" &&
477 new=$(git rev-parse --verify HEAD) &&
478 test_tick &&
479 git commit --allow-empty --amend -C "$old" &&
480 git show --pretty="format:%ad %s" "$old" >expected &&
481 git show --pretty="format:%ad %s" HEAD >actual &&
482 test_cmp expected actual
486 test_expect_success 'amend using the message from a commit named with tag' '
488 git reset --hard &&
489 test_tick &&
490 git commit --allow-empty -m "old commit" &&
491 old=$(git rev-parse --verify HEAD) &&
492 git tag -a -m "tag on old" tagged-old HEAD &&
493 test_tick &&
494 git commit --allow-empty -m "new commit" &&
495 new=$(git rev-parse --verify HEAD) &&
496 test_tick &&
497 git commit --allow-empty --amend -C tagged-old &&
498 git show --pretty="format:%ad %s" "$old" >expected &&
499 git show --pretty="format:%ad %s" HEAD >actual &&
500 test_cmp expected actual
504 test_expect_success 'amend can copy notes' '
506 git config notes.rewrite.amend true &&
507 git config notes.rewriteRef "refs/notes/*" &&
508 test_commit foo &&
509 git notes add -m"a note" &&
510 test_tick &&
511 git commit --amend -m"new foo" &&
512 test "$(git notes show)" = "a note"
516 test_expect_success 'commit a file whose name is a dash' '
517 git reset --hard &&
518 for i in 1 2 3 4 5
520 echo $i
521 done >./- &&
522 git add ./- &&
523 test_tick &&
524 git commit -m "add dash" >output </dev/null &&
525 test_i18ngrep " changed, 5 insertions" output
528 test_expect_success '--only works on to-be-born branch' '
529 # This test relies on having something in the index, as it
530 # would not otherwise actually prove much. So check this.
531 test -n "$(git ls-files)" &&
532 git checkout --orphan orphan &&
533 echo foo >newfile &&
534 git add newfile &&
535 git commit --only newfile -m"--only on unborn branch" &&
536 echo newfile >expected &&
537 git ls-tree -r --name-only HEAD >actual &&
538 test_cmp expected actual
541 test_done