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