Merge branch 'jk/maint-commit-amend-only-no-paths' into maint
[git] / t / t7501-commit.sh
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,
7 # signoff
8
9 test_description='git commit'
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY/diff-lib.sh"
12
13 author='The Real Author <someguy@his.email.org>'
14
15 test_tick
16
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
22 '
23
24 test_expect_success 'fail initial amend' '
25         test_must_fail git commit --amend
26 '
27
28 test_expect_success 'setup: initial commit' '
29         git commit -m initial
30 '
31
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
35 '
36
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
40 '
41
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
45 '
46
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^
53 '
54
55 test_expect_success 'using invalid commit with -C' '
56         test_must_fail git commit -C bogus
57 '
58
59 test_expect_success 'nothing to commit' '
60         test_must_fail git commit -m initial
61 '
62
63 test_expect_success 'setup: non-initial commit' '
64         echo bongo bongo bongo >file &&
65         git commit -m next -a
66 '
67
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
71 '
72
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 &&
76                 @               @
77                 @@
78                 @  @
79                 @Signed-off-by: hula@
80         EOF
81         test_must_fail git commit -F msg -a
82 '
83
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
87 '
88
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
93 '
94
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 &&
98         git commit -F msg -a
99 '
100
101 test_expect_success 'amend commit' '
102         cat >editor <<-\EOF &&
103         #!/bin/sh
104         sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
105         mv "$1-" "$1"
106         EOF
107         chmod 755 editor &&
108         EDITOR=./editor git commit --amend
109 '
110
111 test_expect_success 'amend --only ignores staged contents' '
112         cp file file.expect &&
113         echo changed >file &&
114         git add file &&
115         git commit --no-edit --amend --only &&
116         git cat-file blob HEAD:file >file.actual &&
117         test_cmp file.expect file.actual &&
118         git diff --exit-code
119 '
120
121 test_expect_success 'set up editor' '
122         cat >editor <<-\EOF &&
123         #!/bin/sh
124         sed -e "s/unamended/amended/g" <"$1" >"$1-"
125         mv "$1-" "$1"
126         EOF
127         chmod 755 editor
128 '
129
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 &&
134         git add 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 &&
138         test_cmp expect msg
139 '
140
141 test_expect_success '--amend --edit' '
142         echo amended >expect &&
143         git commit --allow-empty -m "unamended" &&
144         echo bongo again >file &&
145         git add file &&
146         EDITOR=./editor git commit --edit --amend &&
147         git diff-tree -s --format=%s HEAD >msg &&
148         test_cmp expect msg
149 '
150
151 test_expect_success '-m --edit' '
152         echo amended >expect &&
153         git commit --allow-empty -m buffer &&
154         echo bongo bongo >file &&
155         git add file &&
156         EDITOR=./editor git commit -m unamended --edit &&
157         git diff-tree -s  --format=%s HEAD >msg &&
158         test_cmp expect msg
159 '
160
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 .
164 '
165
166 test_expect_success 'using message from other commit' '
167         git commit -C HEAD^ .
168 '
169
170 test_expect_success 'editing message from other commit' '
171         cat >editor <<-\EOF &&
172         #!/bin/sh
173         sed -e "s/amend/older/g"  < "$1" > "$1-"
174         mv "$1-" "$1"
175         EOF
176         chmod 755 editor &&
177         echo hula hula >file &&
178         EDITOR=./editor git commit -c HEAD^ -a
179 '
180
181 test_expect_success 'message from stdin' '
182         echo silly new contents >file &&
183         echo commit message from stdin |
184         git commit -F - -a
185 '
186
187 test_expect_success 'overriding author from command line' '
188         echo gak >file &&
189         git commit -m author \
190                 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
191         grep Rubber.Duck output
192 '
193
194 test_expect_success PERL 'interactive add' '
195         echo 7 |
196         git commit --interactive |
197         grep "What now"
198 '
199
200 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
201         echo zoo >file &&
202         test_must_fail git diff --exit-code >diff1 &&
203         (echo u ; echo "*" ; echo q) |
204         (
205                 EDITOR=: &&
206                 export EDITOR &&
207                 test_must_fail git commit --interactive
208         ) &&
209         git diff >diff2 &&
210         compare_diff_patch diff1 diff2
211 '
212
213 test_expect_success 'editor not invoked if -F is given' '
214         cat >editor <<-\EOF &&
215         #!/bin/sh
216         sed -e s/good/bad/g <"$1" >"$1-"
217         mv "$1-" "$1"
218         EOF
219         chmod 755 editor &&
220
221         echo A good commit message. >msg &&
222         echo moo >file &&
223
224         EDITOR=./editor git commit -a -F msg &&
225         git show -s --pretty=format:%s >subject &&
226         grep -q good subject &&
227
228         echo quack >file &&
229         echo Another good message. |
230         EDITOR=./editor git commit -a -F - &&
231         git show -s --pretty=format:%s >subject &&
232         grep -q good subject
233 '
234
235 test_expect_success 'partial commit that involves removal (1)' '
236
237         git rm --cached file &&
238         mv file elif &&
239         git add elif &&
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
244
245 '
246
247 test_expect_success 'partial commit that involves removal (2)' '
248
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
253
254 '
255
256 test_expect_success 'partial commit that involves removal (3)' '
257
258         git rm --cached elif &&
259         echo elif >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
264
265 '
266
267 test_expect_success 'amend commit to fix author' '
268
269         oldtick=$GIT_AUTHOR_DATE &&
270         test_tick &&
271         git reset --hard &&
272         git cat-file -p HEAD |
273         sed -e "s/author.*/author $author $oldtick/" \
274                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
275                 expected &&
276         git commit --amend --author="$author" &&
277         git cat-file -p HEAD > current &&
278         test_cmp expected current
279
280 '
281
282 test_expect_success 'amend commit to fix date' '
283
284         test_tick &&
285         newtick=$GIT_AUTHOR_DATE &&
286         git reset --hard &&
287         git cat-file -p HEAD |
288         sed -e "s/author.*/author $author $newtick/" \
289                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
290                 expected &&
291         git commit --amend --date="$newtick" &&
292         git cat-file -p HEAD > current &&
293         test_cmp expected current
294
295 '
296
297 test_expect_success 'commit complains about bogus date' '
298         test_must_fail git commit --amend --date=10.11.2010
299 '
300
301 test_expect_success 'sign off (1)' '
302
303         echo 1 >positive &&
304         git add positive &&
305         git commit -s -m "thank you" &&
306         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
307         (
308                 echo thank you
309                 echo
310                 git var GIT_COMMITTER_IDENT |
311                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
312         ) >expected &&
313         test_cmp expected actual
314
315 '
316
317 test_expect_success 'sign off (2)' '
318
319         echo 2 >positive &&
320         git add positive &&
321         existing="Signed-off-by: Watch This <watchthis@example.com>" &&
322         git commit -s -m "thank you
323
324 $existing" &&
325         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
326         (
327                 echo thank you
328                 echo
329                 echo $existing
330                 git var GIT_COMMITTER_IDENT |
331                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
332         ) >expected &&
333         test_cmp expected actual
334
335 '
336
337 test_expect_success 'signoff gap' '
338
339         echo 3 >positive &&
340         git add positive &&
341         alt="Alt-RFC-822-Header: Value" &&
342         git commit -s -m "welcome
343
344 $alt" &&
345         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
346         (
347                 echo welcome
348                 echo
349                 echo $alt
350                 git var GIT_COMMITTER_IDENT |
351                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
352         ) >expected &&
353         test_cmp expected actual
354 '
355
356 test_expect_success 'signoff gap 2' '
357
358         echo 4 >positive &&
359         git add positive &&
360         alt="fixed: 34" &&
361         git commit -s -m "welcome
362
363 We have now
364 $alt" &&
365         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
366         (
367                 echo welcome
368                 echo
369                 echo We have now
370                 echo $alt
371                 echo
372                 git var GIT_COMMITTER_IDENT |
373                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
374         ) >expected &&
375         test_cmp expected actual
376 '
377
378 test_expect_success 'multiple -m' '
379
380         >negative &&
381         git add negative &&
382         git commit -m "one" -m "two" -m "three" &&
383         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
384         (
385                 echo one
386                 echo
387                 echo two
388                 echo
389                 echo three
390         ) >expected &&
391         test_cmp expected actual
392
393 '
394
395 test_expect_success 'amend commit to fix author' '
396
397         oldtick=$GIT_AUTHOR_DATE &&
398         test_tick &&
399         git reset --hard &&
400         git cat-file -p HEAD |
401         sed -e "s/author.*/author $author $oldtick/" \
402                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
403                 expected &&
404         git commit --amend --author="$author" &&
405         git cat-file -p HEAD > current &&
406         test_cmp expected current
407
408 '
409
410 test_expect_success 'git commit <file> with dirty index' '
411         echo tacocat > elif &&
412         echo tehlulz > chz &&
413         git add chz &&
414         git commit elif -m "tacocat is a palindrome" &&
415         git show --stat | grep elif &&
416         git diff --cached | grep chz
417 '
418
419 test_expect_success 'same tree (single parent)' '
420
421         git reset --hard &&
422         test_must_fail git commit -m empty
423
424 '
425
426 test_expect_success 'same tree (single parent) --allow-empty' '
427
428         git commit --allow-empty -m "forced empty" &&
429         git cat-file commit HEAD | grep forced
430
431 '
432
433 test_expect_success 'same tree (merge and amend merge)' '
434
435         git checkout -b side HEAD^ &&
436         echo zero >zero &&
437         git add zero &&
438         git commit -m "add zero" &&
439         git checkout master &&
440
441         git merge -s ours side -m "empty ok" &&
442         git diff HEAD^ HEAD >actual &&
443         : >expected &&
444         test_cmp expected actual &&
445
446         git commit --amend -m "empty really ok" &&
447         git diff HEAD^ HEAD >actual &&
448         : >expected &&
449         test_cmp expected actual
450
451 '
452
453 test_expect_success 'amend using the message from another commit' '
454
455         git reset --hard &&
456         test_tick &&
457         git commit --allow-empty -m "old commit" &&
458         old=$(git rev-parse --verify HEAD) &&
459         test_tick &&
460         git commit --allow-empty -m "new commit" &&
461         new=$(git rev-parse --verify HEAD) &&
462         test_tick &&
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
467
468 '
469
470 test_expect_success 'amend using the message from a commit named with tag' '
471
472         git reset --hard &&
473         test_tick &&
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 &&
477         test_tick &&
478         git commit --allow-empty -m "new commit" &&
479         new=$(git rev-parse --verify HEAD) &&
480         test_tick &&
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
485
486 '
487
488 test_expect_success 'amend can copy notes' '
489
490         git config notes.rewrite.amend true &&
491         git config notes.rewriteRef "refs/notes/*" &&
492         test_commit foo &&
493         git notes add -m"a note" &&
494         test_tick &&
495         git commit --amend -m"new foo" &&
496         test "$(git notes show)" = "a note"
497
498 '
499
500 test_expect_success 'commit a file whose name is a dash' '
501         git reset --hard &&
502         for i in 1 2 3 4 5
503         do
504                 echo $i
505         done >./- &&
506         git add ./- &&
507         test_tick &&
508         git commit -m "add dash" >output </dev/null &&
509         test_i18ngrep " changed, 5 insertions" output
510 '
511
512 test_done