wt-status.c: set commitable bit if there is a meaningful merge.
[git/git.git] / t / t7501-commit.sh
CommitLineData
12ace0b2
KH
1#!/bin/sh
2#
3# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
4#
5
6# FIXME: Test the various index usages, -i and -o, test reflog,
264474f2 7# signoff
12ace0b2 8
47a528ad 9test_description='git commit'
12ace0b2 10. ./test-lib.sh
1af524eb 11. "$TEST_DIRECTORY/diff-lib.sh"
12ace0b2 12
1af524eb 13author='The Real Author <someguy@his.email.org>'
12ace0b2 14
1af524eb 15test_tick
b3b298af 16
1af524eb
JN
17test_expect_success 'initial status' '
18 echo bongo bongo >file &&
19 git add file &&
32177ba6
JH
20 git status >actual &&
21 test_i18ngrep "Initial commit" actual
22'
12ace0b2 23
1af524eb
JN
24test_expect_success 'fail initial amend' '
25 test_must_fail git commit --amend
26'
12ace0b2 27
1af524eb
JN
28test_expect_success 'setup: initial commit' '
29 git commit -m initial
30'
12ace0b2 31
1af524eb 32test_expect_success '-m and -F do not mix' '
c65dc351 33 git checkout HEAD file && echo >>file && git add file &&
1af524eb
JN
34 test_must_fail git commit -m foo -m bar -F file
35'
12ace0b2 36
1af524eb 37test_expect_success '-m and -C do not mix' '
c65dc351 38 git checkout HEAD file && echo >>file && git add file &&
1af524eb
JN
39 test_must_fail git commit -C HEAD -m illegal
40'
12ace0b2 41
1af524eb
JN
42test_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
45'
9d87442f 46
e41fcfe9 47test_expect_success PERL 'can use paths with --interactive' '
587ac8c9
JK
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 ) |
e41fcfe9
CI
51 git commit -m foo --interactive file &&
52 git reset --hard HEAD^
587ac8c9 53'
9d87442f 54
1af524eb 55test_expect_success 'using invalid commit with -C' '
0bb0c155 56 test_must_fail git commit --allow-empty -C bogus
1af524eb 57'
12ace0b2 58
1af524eb 59test_expect_success 'nothing to commit' '
088304bf 60 git reset --hard &&
1af524eb
JN
61 test_must_fail git commit -m initial
62'
12ace0b2 63
9cbcc2a7
TRC
64test_expect_success '--dry-run fails with nothing to commit' '
65 test_must_fail git commit -m initial --dry-run
66'
67
68test_expect_success '--short fails with nothing to commit' '
69 test_must_fail git commit -m initial --short
70'
71
72test_expect_success '--porcelain fails with nothing to commit' '
73 test_must_fail git commit -m initial --porcelain
74'
75
76test_expect_success '--long fails with nothing to commit' '
77 test_must_fail git commit -m initial --long
78'
79
1af524eb
JN
80test_expect_success 'setup: non-initial commit' '
81 echo bongo bongo bongo >file &&
82 git commit -m next -a
83'
12ace0b2 84
9cbcc2a7
TRC
85test_expect_success '--dry-run with stuff to commit returns ok' '
86 echo bongo bongo bongo >>file &&
87 git commit -m next -a --dry-run
88'
89
90test_expect_failure '--short with stuff to commit returns ok' '
91 echo bongo bongo bongo >>file &&
92 git commit -m next -a --short
93'
94
95test_expect_failure '--porcelain with stuff to commit returns ok' '
96 echo bongo bongo bongo >>file &&
97 git commit -m next -a --porcelain
98'
99
100test_expect_success '--long with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --long
103'
104
1af524eb
JN
105test_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
108'
12ace0b2 109
1af524eb
JN
110test_expect_success 'empty commit message' '
111 # Empty except stray tabs and spaces on a few lines.
112 sed -e "s/@//g" >msg <<-\EOF &&
113 @ @
114 @@
115 @ @
116 @Signed-off-by: hula@
117 EOF
118 test_must_fail git commit -F msg -a
119'
12ace0b2 120
010c7dbc
JH
121test_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
124'
125
b2eda9bd
JH
126test_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
130'
131
1af524eb 132test_expect_success 'setup: commit message from file' '
010c7dbc 133 git checkout HEAD file && echo >>file && git add file &&
1af524eb
JN
134 echo this is the commit message, coming from a file >msg &&
135 git commit -F msg -a
136'
12ace0b2 137
1af524eb
JN
138test_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"
143 EOF
144 chmod 755 editor &&
145 EDITOR=./editor git commit --amend
146'
12ace0b2 147
ea2d4ed3
JK
148test_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
156'
157
bc821899
JN
158test_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"
163 EOF
164 chmod 755 editor
165'
166
167test_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
176'
177
178test_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
186'
187
d9a93575
CW
188test_expect_success '--amend --edit of empty message' '
189 cat >replace <<-\EOF &&
190 #!/bin/sh
191 echo "amended" >"$1"
192 EOF
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
201'
202
bc821899
JN
203test_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
211'
212
1af524eb
JN
213test_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 .
216'
217
218test_expect_success 'using message from other commit' '
219 git commit -C HEAD^ .
220'
12ace0b2 221
1af524eb
JN
222test_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"
227 EOF
228 chmod 755 editor &&
229 echo hula hula >file &&
230 EDITOR=./editor git commit -c HEAD^ -a
231'
12ace0b2 232
1af524eb
JN
233test_expect_success 'message from stdin' '
234 echo silly new contents >file &&
235 echo commit message from stdin |
236 git commit -F - -a
237'
12ace0b2 238
1af524eb
JN
239test_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
244'
12ace0b2 245
1af524eb
JN
246test_expect_success PERL 'interactive add' '
247 echo 7 |
248 git commit --interactive |
249 grep "What now"
250'
251
252test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
253 echo zoo >file &&
02a481fc 254 test_must_fail git diff --exit-code >diff1 &&
1af524eb
JN
255 (echo u ; echo "*" ; echo q) |
256 (
257 EDITOR=: &&
258 export EDITOR &&
259 test_must_fail git commit --interactive
260 ) &&
02a481fc 261 git diff >diff2 &&
1af524eb
JN
262 compare_diff_patch diff1 diff2
263'
12ace0b2 264
1af524eb
JN
265test_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"
270 EOF
271 chmod 755 editor &&
272
273 echo A good commit message. >msg &&
274 echo moo >file &&
275
276 EDITOR=./editor git commit -a -F msg &&
277 git show -s --pretty=format:%s >subject &&
278 grep -q good subject &&
279
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
285'
12ace0b2 286
db33af0a 287test_expect_success 'partial commit that involves removal (1)' '
80bffaf7
JH
288
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 &&
1e368681 295 test_cmp expected current
80bffaf7
JH
296
297'
298
db33af0a 299test_expect_success 'partial commit that involves removal (2)' '
80bffaf7
JH
300
301 git commit -m "Partial: remove file" file &&
302 git diff-tree --name-status HEAD^ HEAD >current &&
303 echo "D file" >expected &&
1e368681 304 test_cmp expected current
80bffaf7
JH
305
306'
307
db33af0a
JH
308test_expect_success 'partial commit that involves removal (3)' '
309
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 &&
1e368681 315 test_cmp expected current
db33af0a
JH
316
317'
318
5aa5cd46
JH
319test_expect_success 'amend commit to fix author' '
320
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 &&
1e368681 330 test_cmp expected current
5aa5cd46
JH
331
332'
333
02b47cd7
MV
334test_expect_success 'amend commit to fix date' '
335
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
346
347'
348
b7242b8c
JK
349test_expect_success 'commit mentions forced date in output' '
350 git commit --amend --date=2010-01-02T03:04:05 >output &&
351 grep "Date: *Sat Jan 2 03:04:05 2010" output
352'
353
14ac2864
JK
354test_expect_success 'commit complains about completely bogus dates' '
355 test_must_fail git commit --amend --date=seventeen
356'
357
358test_expect_success 'commit --date allows approxidate' '
359 git commit --amend \
360 --date="midnight the 12th of october, anno domini 1979" &&
361 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
362 git log -1 --format=%ad >actual &&
363 test_cmp expect actual
4579bb41
JK
364'
365
5aa5cd46
JH
366test_expect_success 'sign off (1)' '
367
368 echo 1 >positive &&
369 git add positive &&
370 git commit -s -m "thank you" &&
371 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
372 (
373 echo thank you
374 echo
375 git var GIT_COMMITTER_IDENT |
376 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
377 ) >expected &&
82ebb0b6 378 test_cmp expected actual
5aa5cd46
JH
379
380'
381
382test_expect_success 'sign off (2)' '
383
384 echo 2 >positive &&
385 git add positive &&
386 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
387 git commit -s -m "thank you
388
389$existing" &&
390 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
391 (
392 echo thank you
393 echo
394 echo $existing
395 git var GIT_COMMITTER_IDENT |
396 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
397 ) >expected &&
82ebb0b6 398 test_cmp expected actual
5aa5cd46
JH
399
400'
401
c1e01b0c
DB
402test_expect_success 'signoff gap' '
403
404 echo 3 >positive &&
405 git add positive &&
406 alt="Alt-RFC-822-Header: Value" &&
407 git commit -s -m "welcome
408
409$alt" &&
410 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
411 (
412 echo welcome
413 echo
414 echo $alt
415 git var GIT_COMMITTER_IDENT |
416 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
417 ) >expected &&
418 test_cmp expected actual
419'
420
421test_expect_success 'signoff gap 2' '
422
423 echo 4 >positive &&
424 git add positive &&
425 alt="fixed: 34" &&
426 git commit -s -m "welcome
427
428We have now
429$alt" &&
430 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
431 (
432 echo welcome
433 echo
434 echo We have now
435 echo $alt
436 echo
437 git var GIT_COMMITTER_IDENT |
438 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
439 ) >expected &&
440 test_cmp expected actual
441'
442
5aa5cd46
JH
443test_expect_success 'multiple -m' '
444
445 >negative &&
446 git add negative &&
447 git commit -m "one" -m "two" -m "three" &&
448 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
449 (
450 echo one
451 echo
452 echo two
453 echo
454 echo three
455 ) >expected &&
82ebb0b6 456 test_cmp expected actual
5aa5cd46
JH
457
458'
459
d63c2fd1
KH
460test_expect_success 'amend commit to fix author' '
461
462 oldtick=$GIT_AUTHOR_DATE &&
463 test_tick &&
464 git reset --hard &&
465 git cat-file -p HEAD |
466 sed -e "s/author.*/author $author $oldtick/" \
467 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
468 expected &&
469 git commit --amend --author="$author" &&
470 git cat-file -p HEAD > current &&
1e368681 471 test_cmp expected current
d63c2fd1
KH
472
473'
1200993a
KH
474
475test_expect_success 'git commit <file> with dirty index' '
476 echo tacocat > elif &&
477 echo tehlulz > chz &&
478 git add chz &&
479 git commit elif -m "tacocat is a palindrome" &&
480 git show --stat | grep elif &&
481 git diff --cached | grep chz
482'
483
13aba1e5
JS
484test_expect_success 'same tree (single parent)' '
485
1af524eb
JN
486 git reset --hard &&
487 test_must_fail git commit -m empty
13aba1e5
JS
488
489'
490
36863af1
JH
491test_expect_success 'same tree (single parent) --allow-empty' '
492
493 git commit --allow-empty -m "forced empty" &&
494 git cat-file commit HEAD | grep forced
495
496'
497
13aba1e5
JS
498test_expect_success 'same tree (merge and amend merge)' '
499
500 git checkout -b side HEAD^ &&
501 echo zero >zero &&
502 git add zero &&
503 git commit -m "add zero" &&
504 git checkout master &&
505
506 git merge -s ours side -m "empty ok" &&
507 git diff HEAD^ HEAD >actual &&
508 : >expected &&
82ebb0b6 509 test_cmp expected actual &&
13aba1e5
JS
510
511 git commit --amend -m "empty really ok" &&
512 git diff HEAD^ HEAD >actual &&
513 : >expected &&
82ebb0b6 514 test_cmp expected actual
13aba1e5
JS
515
516'
517
1eb1e9ee
JH
518test_expect_success 'amend using the message from another commit' '
519
520 git reset --hard &&
521 test_tick &&
522 git commit --allow-empty -m "old commit" &&
523 old=$(git rev-parse --verify HEAD) &&
524 test_tick &&
525 git commit --allow-empty -m "new commit" &&
526 new=$(git rev-parse --verify HEAD) &&
527 test_tick &&
528 git commit --allow-empty --amend -C "$old" &&
529 git show --pretty="format:%ad %s" "$old" >expected &&
530 git show --pretty="format:%ad %s" HEAD >actual &&
82ebb0b6 531 test_cmp expected actual
1eb1e9ee
JH
532
533'
534
8a2f8733
JH
535test_expect_success 'amend using the message from a commit named with tag' '
536
537 git reset --hard &&
538 test_tick &&
539 git commit --allow-empty -m "old commit" &&
540 old=$(git rev-parse --verify HEAD) &&
541 git tag -a -m "tag on old" tagged-old HEAD &&
542 test_tick &&
543 git commit --allow-empty -m "new commit" &&
544 new=$(git rev-parse --verify HEAD) &&
545 test_tick &&
546 git commit --allow-empty --amend -C tagged-old &&
547 git show --pretty="format:%ad %s" "$old" >expected &&
548 git show --pretty="format:%ad %s" HEAD >actual &&
82ebb0b6 549 test_cmp expected actual
8a2f8733
JH
550
551'
552
6360d343
TR
553test_expect_success 'amend can copy notes' '
554
555 git config notes.rewrite.amend true &&
556 git config notes.rewriteRef "refs/notes/*" &&
557 test_commit foo &&
558 git notes add -m"a note" &&
559 test_tick &&
560 git commit --amend -m"new foo" &&
561 test "$(git notes show)" = "a note"
562
563'
564
4682d852
JH
565test_expect_success 'commit a file whose name is a dash' '
566 git reset --hard &&
567 for i in 1 2 3 4 5
568 do
569 echo $i
570 done >./- &&
571 git add ./- &&
572 test_tick &&
573 git commit -m "add dash" >output </dev/null &&
574 test_i18ngrep " changed, 5 insertions" output
575'
576
f0c73200
TR
577test_expect_success '--only works on to-be-born branch' '
578 # This test relies on having something in the index, as it
579 # would not otherwise actually prove much. So check this.
580 test -n "$(git ls-files)" &&
581 git checkout --orphan orphan &&
582 echo foo >newfile &&
583 git add newfile &&
584 git commit --only newfile -m"--only on unborn branch" &&
585 echo newfile >expected &&
586 git ls-tree -r --name-only HEAD >actual &&
587 test_cmp expected actual
588'
589
8dc874b2
SS
590test_expect_success '--dry-run with conflicts fixed from a merge' '
591 # setup two branches with conflicting information
592 # in the same file, resolve the conflict,
593 # call commit with --dry-run
594 echo "Initial contents, unimportant" >test-file &&
595 git add test-file &&
596 git commit -m "Initial commit" &&
597 echo "commit-1-state" >test-file &&
598 git commit -m "commit 1" -i test-file &&
599 git tag commit-1 &&
600 git checkout -b branch-2 HEAD^1 &&
601 echo "commit-2-state" >test-file &&
602 git commit -m "commit 2" -i test-file &&
603 ! $(git merge --no-commit commit-1) &&
604 echo "commit-2-state" >test-file &&
605 git add test-file &&
606 git commit --dry-run &&
607 git commit -m "conflicts fixed from merge."
608'
609
12ace0b2 610test_done