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