t7501: test the right kind of breakage
[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 'setup: commit message from file' '
85         echo this is the commit message, coming from a file >msg &&
86         git commit -F msg -a
87 '
88
89 test_expect_success 'amend commit' '
90         cat >editor <<-\EOF &&
91         #!/bin/sh
92         sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
93         mv "$1-" "$1"
94         EOF
95         chmod 755 editor &&
96         EDITOR=./editor git commit --amend
97 '
98
99 test_expect_success 'set up editor' '
100         cat >editor <<-\EOF &&
101         #!/bin/sh
102         sed -e "s/unamended/amended/g" <"$1" >"$1-"
103         mv "$1-" "$1"
104         EOF
105         chmod 755 editor
106 '
107
108 test_expect_success 'amend without launching editor' '
109         echo unamended >expect &&
110         git commit --allow-empty -m "unamended" &&
111         echo needs more bongo >file &&
112         git add file &&
113         EDITOR=./editor git commit --no-edit --amend &&
114         git diff --exit-code HEAD -- file &&
115         git diff-tree -s --format=%s HEAD >msg &&
116         test_cmp expect msg
117 '
118
119 test_expect_success '--amend --edit' '
120         echo amended >expect &&
121         git commit --allow-empty -m "unamended" &&
122         echo bongo again >file &&
123         git add file &&
124         EDITOR=./editor git commit --edit --amend &&
125         git diff-tree -s --format=%s HEAD >msg &&
126         test_cmp expect msg
127 '
128
129 test_expect_success '-m --edit' '
130         echo amended >expect &&
131         git commit --allow-empty -m buffer &&
132         echo bongo bongo >file &&
133         git add file &&
134         EDITOR=./editor git commit -m unamended --edit &&
135         git diff-tree -s  --format=%s HEAD >msg &&
136         test_cmp expect msg
137 '
138
139 test_expect_success '-m and -F do not mix' '
140         echo enough with the bongos >file &&
141         test_must_fail git commit -F msg -m amending .
142 '
143
144 test_expect_success 'using message from other commit' '
145         git commit -C HEAD^ .
146 '
147
148 test_expect_success 'editing message from other commit' '
149         cat >editor <<-\EOF &&
150         #!/bin/sh
151         sed -e "s/amend/older/g"  < "$1" > "$1-"
152         mv "$1-" "$1"
153         EOF
154         chmod 755 editor &&
155         echo hula hula >file &&
156         EDITOR=./editor git commit -c HEAD^ -a
157 '
158
159 test_expect_success 'message from stdin' '
160         echo silly new contents >file &&
161         echo commit message from stdin |
162         git commit -F - -a
163 '
164
165 test_expect_success 'overriding author from command line' '
166         echo gak >file &&
167         git commit -m author \
168                 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
169         grep Rubber.Duck output
170 '
171
172 test_expect_success PERL 'interactive add' '
173         echo 7 |
174         git commit --interactive |
175         grep "What now"
176 '
177
178 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
179         echo zoo >file &&
180         test_must_fail git diff --exit-code >diff1 &&
181         (echo u ; echo "*" ; echo q) |
182         (
183                 EDITOR=: &&
184                 export EDITOR &&
185                 test_must_fail git commit --interactive
186         ) &&
187         git diff >diff2 &&
188         compare_diff_patch diff1 diff2
189 '
190
191 test_expect_success 'editor not invoked if -F is given' '
192         cat >editor <<-\EOF &&
193         #!/bin/sh
194         sed -e s/good/bad/g <"$1" >"$1-"
195         mv "$1-" "$1"
196         EOF
197         chmod 755 editor &&
198
199         echo A good commit message. >msg &&
200         echo moo >file &&
201
202         EDITOR=./editor git commit -a -F msg &&
203         git show -s --pretty=format:%s >subject &&
204         grep -q good subject &&
205
206         echo quack >file &&
207         echo Another good message. |
208         EDITOR=./editor git commit -a -F - &&
209         git show -s --pretty=format:%s >subject &&
210         grep -q good subject
211 '
212
213 test_expect_success 'partial commit that involves removal (1)' '
214
215         git rm --cached file &&
216         mv file elif &&
217         git add elif &&
218         git commit -m "Partial: add elif" elif &&
219         git diff-tree --name-status HEAD^ HEAD >current &&
220         echo "A elif" >expected &&
221         test_cmp expected current
222
223 '
224
225 test_expect_success 'partial commit that involves removal (2)' '
226
227         git commit -m "Partial: remove file" file &&
228         git diff-tree --name-status HEAD^ HEAD >current &&
229         echo "D file" >expected &&
230         test_cmp expected current
231
232 '
233
234 test_expect_success 'partial commit that involves removal (3)' '
235
236         git rm --cached elif &&
237         echo elif >elif &&
238         git commit -m "Partial: modify elif" elif &&
239         git diff-tree --name-status HEAD^ HEAD >current &&
240         echo "M elif" >expected &&
241         test_cmp expected current
242
243 '
244
245 test_expect_success 'amend commit to fix author' '
246
247         oldtick=$GIT_AUTHOR_DATE &&
248         test_tick &&
249         git reset --hard &&
250         git cat-file -p HEAD |
251         sed -e "s/author.*/author $author $oldtick/" \
252                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
253                 expected &&
254         git commit --amend --author="$author" &&
255         git cat-file -p HEAD > current &&
256         test_cmp expected current
257
258 '
259
260 test_expect_success 'amend commit to fix date' '
261
262         test_tick &&
263         newtick=$GIT_AUTHOR_DATE &&
264         git reset --hard &&
265         git cat-file -p HEAD |
266         sed -e "s/author.*/author $author $newtick/" \
267                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
268                 expected &&
269         git commit --amend --date="$newtick" &&
270         git cat-file -p HEAD > current &&
271         test_cmp expected current
272
273 '
274
275 test_expect_success 'commit complains about bogus date' '
276         test_must_fail git commit --amend --date=10.11.2010
277 '
278
279 test_expect_success 'sign off (1)' '
280
281         echo 1 >positive &&
282         git add positive &&
283         git commit -s -m "thank you" &&
284         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
285         (
286                 echo thank you
287                 echo
288                 git var GIT_COMMITTER_IDENT |
289                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
290         ) >expected &&
291         test_cmp expected actual
292
293 '
294
295 test_expect_success 'sign off (2)' '
296
297         echo 2 >positive &&
298         git add positive &&
299         existing="Signed-off-by: Watch This <watchthis@example.com>" &&
300         git commit -s -m "thank you
301
302 $existing" &&
303         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
304         (
305                 echo thank you
306                 echo
307                 echo $existing
308                 git var GIT_COMMITTER_IDENT |
309                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
310         ) >expected &&
311         test_cmp expected actual
312
313 '
314
315 test_expect_success 'signoff gap' '
316
317         echo 3 >positive &&
318         git add positive &&
319         alt="Alt-RFC-822-Header: Value" &&
320         git commit -s -m "welcome
321
322 $alt" &&
323         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
324         (
325                 echo welcome
326                 echo
327                 echo $alt
328                 git var GIT_COMMITTER_IDENT |
329                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
330         ) >expected &&
331         test_cmp expected actual
332 '
333
334 test_expect_success 'signoff gap 2' '
335
336         echo 4 >positive &&
337         git add positive &&
338         alt="fixed: 34" &&
339         git commit -s -m "welcome
340
341 We have now
342 $alt" &&
343         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
344         (
345                 echo welcome
346                 echo
347                 echo We have now
348                 echo $alt
349                 echo
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 'multiple -m' '
357
358         >negative &&
359         git add negative &&
360         git commit -m "one" -m "two" -m "three" &&
361         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
362         (
363                 echo one
364                 echo
365                 echo two
366                 echo
367                 echo three
368         ) >expected &&
369         test_cmp expected actual
370
371 '
372
373 test_expect_success 'amend commit to fix author' '
374
375         oldtick=$GIT_AUTHOR_DATE &&
376         test_tick &&
377         git reset --hard &&
378         git cat-file -p HEAD |
379         sed -e "s/author.*/author $author $oldtick/" \
380                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
381                 expected &&
382         git commit --amend --author="$author" &&
383         git cat-file -p HEAD > current &&
384         test_cmp expected current
385
386 '
387
388 test_expect_success 'git commit <file> with dirty index' '
389         echo tacocat > elif &&
390         echo tehlulz > chz &&
391         git add chz &&
392         git commit elif -m "tacocat is a palindrome" &&
393         git show --stat | grep elif &&
394         git diff --cached | grep chz
395 '
396
397 test_expect_success 'same tree (single parent)' '
398
399         git reset --hard &&
400         test_must_fail git commit -m empty
401
402 '
403
404 test_expect_success 'same tree (single parent) --allow-empty' '
405
406         git commit --allow-empty -m "forced empty" &&
407         git cat-file commit HEAD | grep forced
408
409 '
410
411 test_expect_success 'same tree (merge and amend merge)' '
412
413         git checkout -b side HEAD^ &&
414         echo zero >zero &&
415         git add zero &&
416         git commit -m "add zero" &&
417         git checkout master &&
418
419         git merge -s ours side -m "empty ok" &&
420         git diff HEAD^ HEAD >actual &&
421         : >expected &&
422         test_cmp expected actual &&
423
424         git commit --amend -m "empty really ok" &&
425         git diff HEAD^ HEAD >actual &&
426         : >expected &&
427         test_cmp expected actual
428
429 '
430
431 test_expect_success 'amend using the message from another commit' '
432
433         git reset --hard &&
434         test_tick &&
435         git commit --allow-empty -m "old commit" &&
436         old=$(git rev-parse --verify HEAD) &&
437         test_tick &&
438         git commit --allow-empty -m "new commit" &&
439         new=$(git rev-parse --verify HEAD) &&
440         test_tick &&
441         git commit --allow-empty --amend -C "$old" &&
442         git show --pretty="format:%ad %s" "$old" >expected &&
443         git show --pretty="format:%ad %s" HEAD >actual &&
444         test_cmp expected actual
445
446 '
447
448 test_expect_success 'amend using the message from a commit named with tag' '
449
450         git reset --hard &&
451         test_tick &&
452         git commit --allow-empty -m "old commit" &&
453         old=$(git rev-parse --verify HEAD) &&
454         git tag -a -m "tag on old" tagged-old HEAD &&
455         test_tick &&
456         git commit --allow-empty -m "new commit" &&
457         new=$(git rev-parse --verify HEAD) &&
458         test_tick &&
459         git commit --allow-empty --amend -C tagged-old &&
460         git show --pretty="format:%ad %s" "$old" >expected &&
461         git show --pretty="format:%ad %s" HEAD >actual &&
462         test_cmp expected actual
463
464 '
465
466 test_expect_success 'amend can copy notes' '
467
468         git config notes.rewrite.amend true &&
469         git config notes.rewriteRef "refs/notes/*" &&
470         test_commit foo &&
471         git notes add -m"a note" &&
472         test_tick &&
473         git commit --amend -m"new foo" &&
474         test "$(git notes show)" = "a note"
475
476 '
477
478 test_done