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