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