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
9test_description='git commit'
10. ./test-lib.sh
11
12test_tick
13
14test_expect_success \
15 "initial status" \
16 "echo 'bongo bongo' >file &&
17 git add file"
18
19test_expect_success C_LOCALE_OUTPUT \
20 "Constructing initial commit" \
21 "git status | grep 'Initial commit'"
22
23test_expect_success \
24 "fail initial amend" \
25 "test_must_fail git commit --amend"
26
27test_expect_success \
28 "initial commit" \
29 "git commit -m initial"
30
31test_expect_success \
32 "invalid options 1" \
33 "test_must_fail git commit -m foo -m bar -F file"
34
35test_expect_success \
36 "invalid options 2" \
37 "test_must_fail git commit -C HEAD -m illegal"
38
39test_expect_success \
40 "using paths with -a" \
41 "echo King of the bongo >file &&
42 test_must_fail git commit -m foo -a file"
43
44test_expect_success PERL \
45 "using paths with --interactive" \
46 "echo bong-o-bong >file &&
47 ! (echo 7 | git commit -m foo --interactive file)"
48
49test_expect_success \
50 "using invalid commit with -C" \
51 "test_must_fail git commit -C bogus"
52
53test_expect_success \
54 "testing nothing to commit" \
55 "test_must_fail git commit -m initial"
56
57test_expect_success \
58 "next commit" \
59 "echo 'bongo bongo bongo' >file \
60 git commit -m next -a"
61
62test_expect_success \
63 "commit message from non-existing file" \
64 "echo 'more bongo: bongo bongo bongo bongo' >file && \
65 test_must_fail git commit -F gah -a"
66
67# Empty except stray tabs and spaces on a few lines.
68sed -e 's/@$//' >msg <<EOF
69 @
70
71 @
72Signed-off-by: hula
73EOF
74test_expect_success \
75 "empty commit message" \
76 "test_must_fail git commit -F msg -a"
77
78test_expect_success \
79 "commit message from file" \
80 "echo 'this is the commit message, coming from a file' >msg && \
81 git commit -F msg -a"
82
83cat >editor <<\EOF
84#!/bin/sh
85sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
86mv "$1-" "$1"
87EOF
88chmod 755 editor
89
90test_expect_success \
91 "amend commit" \
92 "EDITOR=./editor git commit --amend"
93
94test_expect_success \
95 "passing -m and -F" \
96 "echo 'enough with the bongos' >file && \
97 test_must_fail git commit -F msg -m amending ."
98
99test_expect_success \
100 "using message from other commit" \
101 "git commit -C HEAD^ ."
102
103cat >editor <<\EOF
104#!/bin/sh
105sed -e "s/amend/older/g" < "$1" > "$1-"
106mv "$1-" "$1"
107EOF
108chmod 755 editor
109
110test_expect_success \
111 "editing message from other commit" \
112 "echo 'hula hula' >file && \
113 EDITOR=./editor git commit -c HEAD^ -a"
114
115test_expect_success \
116 "message from stdin" \
117 "echo 'silly new contents' >file && \
118 echo commit message from stdin | git commit -F - -a"
119
120test_expect_success \
121 "overriding author from command line" \
122 "echo 'gak' >file && \
123 git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a >output 2>&1"
124
125test_expect_success \
126 "commit --author output mentions author" \
127 "grep Rubber.Duck output"
128
129test_expect_success PERL \
130 "interactive add" \
131 "echo 7 | git commit --interactive | grep 'What now'"
132
133test_expect_success \
134 "showing committed revisions" \
135 "git rev-list HEAD >current"
136
137cat >editor <<\EOF
138#!/bin/sh
139sed -e "s/good/bad/g" < "$1" > "$1-"
140mv "$1-" "$1"
141EOF
142chmod 755 editor
143
144cat >msg <<EOF
145A good commit message.
146EOF
147
148test_expect_success \
149 'editor not invoked if -F is given' '
150 echo "moo" >file &&
151 EDITOR=./editor git commit -a -F msg &&
152 git show -s --pretty=format:"%s" | grep -q good &&
153 echo "quack" >file &&
154 echo "Another good message." | EDITOR=./editor git commit -a -F - &&
155 git show -s --pretty=format:"%s" | grep -q good
156 '
157# We could just check the head sha1, but checking each commit makes it
158# easier to isolate bugs.
159
160cat >expected <<\EOF
16172c0dc9855b0c9dadcbfd5a31cab072e0cb774ca
1629b88fc14ce6b32e3d9ee021531a54f18a5cf38a2
1633536bbb352c3a1ef9a420f5b4242d48578b92aa7
164d381ac431806e53f3dd7ac2f1ae0534f36d738b9
1654fd44095ad6334f3ef72e4c5ec8ddf108174b54a
166402702b49136e7587daa9280e91e4bb7cb2179f7
167EOF
168
169test_expect_success \
170 'validate git rev-list output.' \
171 'test_cmp expected current'
172
173test_expect_success 'partial commit that involves removal (1)' '
174
175 git rm --cached file &&
176 mv file elif &&
177 git add elif &&
178 git commit -m "Partial: add elif" elif &&
179 git diff-tree --name-status HEAD^ HEAD >current &&
180 echo "A elif" >expected &&
181 test_cmp expected current
182
183'
184
185test_expect_success 'partial commit that involves removal (2)' '
186
187 git commit -m "Partial: remove file" file &&
188 git diff-tree --name-status HEAD^ HEAD >current &&
189 echo "D file" >expected &&
190 test_cmp expected current
191
192'
193
194test_expect_success 'partial commit that involves removal (3)' '
195
196 git rm --cached elif &&
197 echo elif >elif &&
198 git commit -m "Partial: modify elif" elif &&
199 git diff-tree --name-status HEAD^ HEAD >current &&
200 echo "M elif" >expected &&
201 test_cmp expected current
202
203'
204
205author="The Real Author <someguy@his.email.org>"
206test_expect_success 'amend commit to fix author' '
207
208 oldtick=$GIT_AUTHOR_DATE &&
209 test_tick &&
210 git reset --hard &&
211 git cat-file -p HEAD |
212 sed -e "s/author.*/author $author $oldtick/" \
213 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
214 expected &&
215 git commit --amend --author="$author" &&
216 git cat-file -p HEAD > current &&
217 test_cmp expected current
218
219'
220
221test_expect_success 'amend commit to fix date' '
222
223 test_tick &&
224 newtick=$GIT_AUTHOR_DATE &&
225 git reset --hard &&
226 git cat-file -p HEAD |
227 sed -e "s/author.*/author $author $newtick/" \
228 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
229 expected &&
230 git commit --amend --date="$newtick" &&
231 git cat-file -p HEAD > current &&
232 test_cmp expected current
233
234'
235
236test_expect_success 'commit complains about bogus date' '
237 test_must_fail git commit --amend --date=10.11.2010
238'
239
240test_expect_success 'sign off (1)' '
241
242 echo 1 >positive &&
243 git add positive &&
244 git commit -s -m "thank you" &&
245 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
246 (
247 echo thank you
248 echo
249 git var GIT_COMMITTER_IDENT |
250 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
251 ) >expected &&
252 test_cmp expected actual
253
254'
255
256test_expect_success 'sign off (2)' '
257
258 echo 2 >positive &&
259 git add positive &&
260 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
261 git commit -s -m "thank you
262
263$existing" &&
264 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
265 (
266 echo thank you
267 echo
268 echo $existing
269 git var GIT_COMMITTER_IDENT |
270 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
271 ) >expected &&
272 test_cmp expected actual
273
274'
275
276test_expect_success 'signoff gap' '
277
278 echo 3 >positive &&
279 git add positive &&
280 alt="Alt-RFC-822-Header: Value" &&
281 git commit -s -m "welcome
282
283$alt" &&
284 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
285 (
286 echo welcome
287 echo
288 echo $alt
289 git var GIT_COMMITTER_IDENT |
290 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
291 ) >expected &&
292 test_cmp expected actual
293'
294
295test_expect_success 'signoff gap 2' '
296
297 echo 4 >positive &&
298 git add positive &&
299 alt="fixed: 34" &&
300 git commit -s -m "welcome
301
302We have now
303$alt" &&
304 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
305 (
306 echo welcome
307 echo
308 echo We have now
309 echo $alt
310 echo
311 git var GIT_COMMITTER_IDENT |
312 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
313 ) >expected &&
314 test_cmp expected actual
315'
316
317test_expect_success 'multiple -m' '
318
319 >negative &&
320 git add negative &&
321 git commit -m "one" -m "two" -m "three" &&
322 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
323 (
324 echo one
325 echo
326 echo two
327 echo
328 echo three
329 ) >expected &&
330 test_cmp expected actual
331
332'
333
334author="The Real Author <someguy@his.email.org>"
335test_expect_success 'amend commit to fix author' '
336
337 oldtick=$GIT_AUTHOR_DATE &&
338 test_tick &&
339 git reset --hard &&
340 git cat-file -p HEAD |
341 sed -e "s/author.*/author $author $oldtick/" \
342 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
343 expected &&
344 git commit --amend --author="$author" &&
345 git cat-file -p HEAD > current &&
346 test_cmp expected current
347
348'
349
350test_expect_success 'git commit <file> with dirty index' '
351 echo tacocat > elif &&
352 echo tehlulz > chz &&
353 git add chz &&
354 git commit elif -m "tacocat is a palindrome" &&
355 git show --stat | grep elif &&
356 git diff --cached | grep chz
357'
358
359test_expect_success 'same tree (single parent)' '
360
361 git reset --hard
362
363 if git commit -m empty
364 then
365 echo oops -- should have complained
366 false
367 else
368 : happy
369 fi
370
371'
372
373test_expect_success 'same tree (single parent) --allow-empty' '
374
375 git commit --allow-empty -m "forced empty" &&
376 git cat-file commit HEAD | grep forced
377
378'
379
380test_expect_success 'same tree (merge and amend merge)' '
381
382 git checkout -b side HEAD^ &&
383 echo zero >zero &&
384 git add zero &&
385 git commit -m "add zero" &&
386 git checkout master &&
387
388 git merge -s ours side -m "empty ok" &&
389 git diff HEAD^ HEAD >actual &&
390 : >expected &&
391 test_cmp expected actual &&
392
393 git commit --amend -m "empty really ok" &&
394 git diff HEAD^ HEAD >actual &&
395 : >expected &&
396 test_cmp expected actual
397
398'
399
400test_expect_success 'amend using the message from another commit' '
401
402 git reset --hard &&
403 test_tick &&
404 git commit --allow-empty -m "old commit" &&
405 old=$(git rev-parse --verify HEAD) &&
406 test_tick &&
407 git commit --allow-empty -m "new commit" &&
408 new=$(git rev-parse --verify HEAD) &&
409 test_tick &&
410 git commit --allow-empty --amend -C "$old" &&
411 git show --pretty="format:%ad %s" "$old" >expected &&
412 git show --pretty="format:%ad %s" HEAD >actual &&
413 test_cmp expected actual
414
415'
416
417test_expect_success 'amend using the message from a commit named with tag' '
418
419 git reset --hard &&
420 test_tick &&
421 git commit --allow-empty -m "old commit" &&
422 old=$(git rev-parse --verify HEAD) &&
423 git tag -a -m "tag on old" tagged-old HEAD &&
424 test_tick &&
425 git commit --allow-empty -m "new commit" &&
426 new=$(git rev-parse --verify HEAD) &&
427 test_tick &&
428 git commit --allow-empty --amend -C tagged-old &&
429 git show --pretty="format:%ad %s" "$old" >expected &&
430 git show --pretty="format:%ad %s" HEAD >actual &&
431 test_cmp expected actual
432
433'
434
435test_expect_success 'amend can copy notes' '
436
437 git config notes.rewrite.amend true &&
438 git config notes.rewriteRef "refs/notes/*" &&
439 test_commit foo &&
440 git notes add -m"a note" &&
441 test_tick &&
442 git commit --amend -m"new foo" &&
443 test "$(git notes show)" = "a note"
444
445'
446
447test_done