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