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