]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7501-commit.sh
t7501: fix "empty commit" test with NO_PERL
[thirdparty/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
1af524eb
JN
64test_expect_success 'setup: non-initial commit' '
65 echo bongo bongo bongo >file &&
66 git commit -m next -a
67'
12ace0b2 68
1af524eb
JN
69test_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
72'
12ace0b2 73
1af524eb
JN
74test_expect_success 'empty commit message' '
75 # Empty except stray tabs and spaces on a few lines.
76 sed -e "s/@//g" >msg <<-\EOF &&
77 @ @
78 @@
79 @ @
80 @Signed-off-by: hula@
81 EOF
82 test_must_fail git commit -F msg -a
83'
12ace0b2 84
010c7dbc
JH
85test_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
88'
89
b2eda9bd
JH
90test_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
94'
95
1af524eb 96test_expect_success 'setup: commit message from file' '
010c7dbc 97 git checkout HEAD file && echo >>file && git add file &&
1af524eb
JN
98 echo this is the commit message, coming from a file >msg &&
99 git commit -F msg -a
100'
12ace0b2 101
1af524eb
JN
102test_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"
107 EOF
108 chmod 755 editor &&
109 EDITOR=./editor git commit --amend
110'
12ace0b2 111
ea2d4ed3
JK
112test_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
120'
121
bc821899
JN
122test_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"
127 EOF
128 chmod 755 editor
129'
130
131test_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
140'
141
142test_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
150'
151
d9a93575
CW
152test_expect_success '--amend --edit of empty message' '
153 cat >replace <<-\EOF &&
154 #!/bin/sh
155 echo "amended" >"$1"
156 EOF
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
165'
166
bc821899
JN
167test_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
175'
176
1af524eb
JN
177test_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 .
180'
181
182test_expect_success 'using message from other commit' '
183 git commit -C HEAD^ .
184'
12ace0b2 185
1af524eb
JN
186test_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"
191 EOF
192 chmod 755 editor &&
193 echo hula hula >file &&
194 EDITOR=./editor git commit -c HEAD^ -a
195'
12ace0b2 196
1af524eb
JN
197test_expect_success 'message from stdin' '
198 echo silly new contents >file &&
199 echo commit message from stdin |
200 git commit -F - -a
201'
12ace0b2 202
1af524eb
JN
203test_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
208'
12ace0b2 209
1af524eb
JN
210test_expect_success PERL 'interactive add' '
211 echo 7 |
212 git commit --interactive |
213 grep "What now"
214'
215
216test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
217 echo zoo >file &&
02a481fc 218 test_must_fail git diff --exit-code >diff1 &&
1af524eb
JN
219 (echo u ; echo "*" ; echo q) |
220 (
221 EDITOR=: &&
222 export EDITOR &&
223 test_must_fail git commit --interactive
224 ) &&
02a481fc 225 git diff >diff2 &&
1af524eb
JN
226 compare_diff_patch diff1 diff2
227'
12ace0b2 228
1af524eb
JN
229test_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"
234 EOF
235 chmod 755 editor &&
236
237 echo A good commit message. >msg &&
238 echo moo >file &&
239
240 EDITOR=./editor git commit -a -F msg &&
241 git show -s --pretty=format:%s >subject &&
242 grep -q good subject &&
243
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
249'
12ace0b2 250
db33af0a 251test_expect_success 'partial commit that involves removal (1)' '
80bffaf7
JH
252
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 &&
1e368681 259 test_cmp expected current
80bffaf7
JH
260
261'
262
db33af0a 263test_expect_success 'partial commit that involves removal (2)' '
80bffaf7
JH
264
265 git commit -m "Partial: remove file" file &&
266 git diff-tree --name-status HEAD^ HEAD >current &&
267 echo "D file" >expected &&
1e368681 268 test_cmp expected current
80bffaf7
JH
269
270'
271
db33af0a
JH
272test_expect_success 'partial commit that involves removal (3)' '
273
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 &&
1e368681 279 test_cmp expected current
db33af0a
JH
280
281'
282
5aa5cd46
JH
283test_expect_success 'amend commit to fix author' '
284
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 &&
1e368681 294 test_cmp expected current
5aa5cd46
JH
295
296'
297
02b47cd7
MV
298test_expect_success 'amend commit to fix date' '
299
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
310
311'
312
4579bb41
JK
313test_expect_success 'commit complains about bogus date' '
314 test_must_fail git commit --amend --date=10.11.2010
315'
316
5aa5cd46
JH
317test_expect_success 'sign off (1)' '
318
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 &&
323 (
324 echo thank you
325 echo
326 git var GIT_COMMITTER_IDENT |
327 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
328 ) >expected &&
82ebb0b6 329 test_cmp expected actual
5aa5cd46
JH
330
331'
332
333test_expect_success 'sign off (2)' '
334
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
339
340$existing" &&
341 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
342 (
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 &&
82ebb0b6 349 test_cmp expected actual
5aa5cd46
JH
350
351'
352
c1e01b0c
DB
353test_expect_success 'signoff gap' '
354
355 echo 3 >positive &&
356 git add positive &&
357 alt="Alt-RFC-822-Header: Value" &&
358 git commit -s -m "welcome
359
360$alt" &&
361 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
362 (
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
370'
371
372test_expect_success 'signoff gap 2' '
373
374 echo 4 >positive &&
375 git add positive &&
376 alt="fixed: 34" &&
377 git commit -s -m "welcome
378
379We have now
380$alt" &&
381 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
382 (
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
392'
393
5aa5cd46
JH
394test_expect_success 'multiple -m' '
395
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 &&
400 (
401 echo one
402 echo
403 echo two
404 echo
405 echo three
406 ) >expected &&
82ebb0b6 407 test_cmp expected actual
5aa5cd46
JH
408
409'
410
d63c2fd1
KH
411test_expect_success 'amend commit to fix author' '
412
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 &&
1e368681 422 test_cmp expected current
d63c2fd1
KH
423
424'
1200993a
KH
425
426test_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
433'
434
13aba1e5
JS
435test_expect_success 'same tree (single parent)' '
436
1af524eb
JN
437 git reset --hard &&
438 test_must_fail git commit -m empty
13aba1e5
JS
439
440'
441
36863af1
JH
442test_expect_success 'same tree (single parent) --allow-empty' '
443
444 git commit --allow-empty -m "forced empty" &&
445 git cat-file commit HEAD | grep forced
446
447'
448
13aba1e5
JS
449test_expect_success 'same tree (merge and amend merge)' '
450
451 git checkout -b side HEAD^ &&
452 echo zero >zero &&
453 git add zero &&
454 git commit -m "add zero" &&
455 git checkout master &&
456
457 git merge -s ours side -m "empty ok" &&
458 git diff HEAD^ HEAD >actual &&
459 : >expected &&
82ebb0b6 460 test_cmp expected actual &&
13aba1e5
JS
461
462 git commit --amend -m "empty really ok" &&
463 git diff HEAD^ HEAD >actual &&
464 : >expected &&
82ebb0b6 465 test_cmp expected actual
13aba1e5
JS
466
467'
468
1eb1e9ee
JH
469test_expect_success 'amend using the message from another commit' '
470
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 &&
82ebb0b6 482 test_cmp expected actual
1eb1e9ee
JH
483
484'
485
8a2f8733
JH
486test_expect_success 'amend using the message from a commit named with tag' '
487
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 &&
82ebb0b6 500 test_cmp expected actual
8a2f8733
JH
501
502'
503
6360d343
TR
504test_expect_success 'amend can copy notes' '
505
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"
513
514'
515
4682d852
JH
516test_expect_success 'commit a file whose name is a dash' '
517 git reset --hard &&
518 for i in 1 2 3 4 5
519 do
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
526'
527
f0c73200
TR
528test_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
539'
540
12ace0b2 541test_done