Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Steven Grimm
4 #
5
6 test_description='git commit
7
8 Tests for template, signoff, squash and -F functions.'
9
10 . ./test-lib.sh
11
12 . "$TEST_DIRECTORY"/lib-rebase.sh
13
14 commit_msg_is () {
15 expect=commit_msg_is.expect
16 actual=commit_msg_is.actual
17
18 printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" &&
19 printf "%s" "$1" >"$expect" &&
20 test_cmp "$expect" "$actual"
21 }
22
23 # A sanity check to see if commit is working at all.
24 test_expect_success 'a basic commit in an empty tree should succeed' '
25 echo content > foo &&
26 git add foo &&
27 git commit -m "initial commit"
28 '
29
30 test_expect_success 'nonexistent template file should return error' '
31 echo changes >> foo &&
32 git add foo &&
33 (
34 GIT_EDITOR="echo hello >" &&
35 export GIT_EDITOR &&
36 test_must_fail git commit --template "$(pwd)"/notexist
37 )
38 '
39
40 test_expect_success 'nonexistent optional template file on command line' '
41 echo changes >> foo &&
42 git add foo &&
43 (
44 GIT_EDITOR="echo hello >\"\$1\"" &&
45 export GIT_EDITOR &&
46 git commit --template ":(optional)$(pwd)/notexist"
47 )
48 '
49
50 test_expect_success 'nonexistent template file in config should return error' '
51 test_config commit.template "$(pwd)"/notexist &&
52 (
53 GIT_EDITOR="echo hello >" &&
54 export GIT_EDITOR &&
55 test_must_fail git commit --allow-empty
56 )
57 '
58
59 test_expect_success 'nonexistent optional template file in config' '
60 test_config commit.template ":(optional)$(pwd)"/notexist &&
61 GIT_EDITOR="echo hello >" git commit --allow-empty &&
62 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
63 echo hello >expect &&
64 test_cmp expect actual
65 '
66
67 # From now on we'll use a template file that exists.
68 TEMPLATE="$(pwd)"/template
69
70 test_expect_success 'unedited template should not commit' '
71 echo "template line" >"$TEMPLATE" &&
72 test_must_fail git commit --allow-empty --template "$TEMPLATE"
73 '
74
75 test_expect_success 'unedited template with comments should not commit' '
76 echo "# comment in template" >>"$TEMPLATE" &&
77 test_must_fail git commit --allow-empty --template "$TEMPLATE"
78 '
79
80 test_expect_success 'a Signed-off-by line by itself should not commit' '
81 (
82 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
83 test_must_fail git commit --allow-empty --template "$TEMPLATE"
84 )
85 '
86
87 test_expect_success 'adding comments to a template should not commit' '
88 (
89 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
90 test_must_fail git commit --allow-empty --template "$TEMPLATE"
91 )
92 '
93
94 test_expect_success 'adding real content to a template should commit' '
95 (
96 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
97 git commit --allow-empty --template "$TEMPLATE"
98 ) &&
99 commit_msg_is "template linecommit message"
100 '
101
102 test_expect_success 'existent template marked optional should commit' '
103 echo "existent template" >"$TEMPLATE" &&
104 (
105 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
106 git commit --allow-empty --template ":(optional)$TEMPLATE"
107 ) &&
108 commit_msg_is "existent templatecommit message"
109 '
110
111 test_expect_success '-t option should be short for --template' '
112 echo "short template" > "$TEMPLATE" &&
113 echo "new content" >> foo &&
114 git add foo &&
115 (
116 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
117 git commit -t "$TEMPLATE"
118 ) &&
119 commit_msg_is "short templatecommit message"
120 '
121
122 test_expect_success 'config-specified template should commit' '
123 echo "new template" > "$TEMPLATE" &&
124 test_config commit.template "$TEMPLATE" &&
125 echo "more content" >> foo &&
126 git add foo &&
127 (
128 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
129 git commit
130 ) &&
131 commit_msg_is "new templatecommit message"
132 '
133
134 test_expect_success 'explicit commit message should override template' '
135 echo "still more content" >> foo &&
136 git add foo &&
137 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
138 -m "command line msg" &&
139 commit_msg_is "command line msg"
140 '
141
142 test_expect_success 'commit message from file should override template' '
143 echo "content galore" >> foo &&
144 git add foo &&
145 echo "standard input msg" |
146 (
147 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
148 git commit --template "$TEMPLATE" --file -
149 ) &&
150 commit_msg_is "standard input msg"
151 '
152
153 cat >"$TEMPLATE" <<\EOF
154
155
156 ### template
157
158 EOF
159 test_expect_success 'commit message from template with whitespace issue' '
160 echo "content galore" >>foo &&
161 git add foo &&
162 GIT_EDITOR=\""$TEST_DIRECTORY"\"/t7500/add-whitespaced-content \
163 git commit --template "$TEMPLATE" &&
164 commit_msg_is "commit message"
165 '
166
167 test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
168
169 cp .git/index saved-index &&
170 (
171 echo some new content >file &&
172 GIT_INDEX_FILE=.git/another_index &&
173 export GIT_INDEX_FILE &&
174 git add file &&
175 git commit -m "commit using another index" &&
176 git diff-index --exit-code HEAD &&
177 git diff-files --exit-code
178 ) &&
179 cmp .git/index saved-index >/dev/null
180
181 '
182
183 test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
184
185 cp .git/index saved-index &&
186 (
187 rm -f .git/no-such-index &&
188 GIT_INDEX_FILE=.git/no-such-index &&
189 export GIT_INDEX_FILE &&
190 git commit -m "commit using nonexistent index" &&
191 test -z "$(git ls-files)" &&
192 test -z "$(git ls-tree HEAD)"
193
194 ) &&
195 cmp .git/index saved-index >/dev/null
196 '
197
198 cat > expect << EOF
199 zort
200
201 Signed-off-by: C O Mitter <committer@example.com>
202 EOF
203
204 test_expect_success '--signoff' '
205 echo "yet another content *narf*" >> foo &&
206 echo "zort" | git commit -s -F - foo &&
207 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
208 test_cmp expect output
209 '
210
211 test_expect_success 'commit message from file (1)' '
212 mkdir subdir &&
213 echo "Log in top directory" >log &&
214 echo "Log in sub directory" >subdir/log &&
215 (
216 cd subdir &&
217 git commit --allow-empty -F log
218 ) &&
219 commit_msg_is "Log in sub directory"
220 '
221
222 test_expect_success 'commit message from file (2)' '
223 rm -f log &&
224 echo "Log in sub directory" >subdir/log &&
225 (
226 cd subdir &&
227 git commit --allow-empty -F log
228 ) &&
229 commit_msg_is "Log in sub directory"
230 '
231
232 test_expect_success 'commit message from stdin' '
233 (
234 cd subdir &&
235 echo "Log with foo word" | git commit --allow-empty -F -
236 ) &&
237 commit_msg_is "Log with foo word"
238 '
239
240 test_expect_success 'commit -F overrides -t' '
241 (
242 cd subdir &&
243 echo "-F log" > f.log &&
244 echo "-t template" > t.template &&
245 git commit --allow-empty -F f.log -t t.template
246 ) &&
247 commit_msg_is "-F log"
248 '
249
250 test_expect_success 'Commit without message is allowed with --allow-empty-message' '
251 echo "more content" >>foo &&
252 git add foo &&
253 >empty &&
254 git commit --allow-empty-message <empty &&
255 commit_msg_is "" &&
256 git tag empty-message-commit
257 '
258
259 test_expect_success 'Commit without message is no-no without --allow-empty-message' '
260 echo "more content" >>foo &&
261 git add foo &&
262 >empty &&
263 test_must_fail git commit <empty
264 '
265
266 test_expect_success 'Commit a message with --allow-empty-message' '
267 echo "even more content" >>foo &&
268 git add foo &&
269 git commit --allow-empty-message -m"hello there" &&
270 commit_msg_is "hello there"
271 '
272
273 test_expect_success 'commit -C empty respects --allow-empty-message' '
274 echo more >>foo &&
275 git add foo &&
276 test_must_fail git commit -C empty-message-commit &&
277 git commit -C empty-message-commit --allow-empty-message &&
278 commit_msg_is ""
279 '
280
281 commit_for_rebase_autosquash_setup () {
282 echo "first content line" >>foo &&
283 git add foo &&
284 cat >log <<EOF &&
285 target message subject line
286
287 target message body line 1
288 target message body line 2
289 EOF
290 git commit -F log &&
291 echo "second content line" >>foo &&
292 git add foo &&
293 git commit -m "intermediate commit" &&
294 echo "third content line" >>foo &&
295 git add foo
296 }
297
298 test_expect_success 'commit --fixup provides correct one-line commit message' '
299 commit_for_rebase_autosquash_setup &&
300 EDITOR="echo ignored >>" git commit --fixup HEAD~1 &&
301 commit_msg_is "fixup! target message subject line"
302 '
303
304 test_expect_success 'commit --fixup -m"something" -m"extra"' '
305 commit_for_rebase_autosquash_setup &&
306 git commit --fixup HEAD~1 -m"something" -m"extra" &&
307 commit_msg_is "fixup! target message subject linesomething
308
309 extra"
310 '
311 test_expect_success 'commit --fixup --edit' '
312 commit_for_rebase_autosquash_setup &&
313 EDITOR="printf \"something\nextra\" >>" git commit --fixup HEAD~1 --edit &&
314 commit_msg_is "fixup! target message subject linesomething
315 extra"
316 '
317
318 get_commit_msg () {
319 rev="$1" &&
320 git log -1 --pretty=format:"%B" "$rev"
321 }
322
323 test_expect_success 'commit --fixup=amend: creates amend! commit' '
324 commit_for_rebase_autosquash_setup &&
325 cat >expected <<-EOF &&
326 amend! $(git log -1 --format=%s HEAD~)
327
328 $(get_commit_msg HEAD~)
329
330 edited
331 EOF
332 (
333 set_fake_editor &&
334 FAKE_COMMIT_AMEND="edited" \
335 git commit --fixup=amend:HEAD~
336 ) &&
337 get_commit_msg HEAD >actual &&
338 test_cmp expected actual
339 '
340
341 test_expect_success '--fixup=amend: --only ignores staged changes' '
342 commit_for_rebase_autosquash_setup &&
343 cat >expected <<-EOF &&
344 amend! $(git log -1 --format=%s HEAD~)
345
346 $(get_commit_msg HEAD~)
347
348 edited
349 EOF
350 (
351 set_fake_editor &&
352 FAKE_COMMIT_AMEND="edited" \
353 git commit --fixup=amend:HEAD~ --only
354 ) &&
355 get_commit_msg HEAD >actual &&
356 test_cmp expected actual &&
357 test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
358 test_cmp_rev HEAD@{1} HEAD^ &&
359 test_expect_code 1 git diff --cached --exit-code &&
360 git cat-file blob :foo >actual &&
361 test_cmp foo actual
362 '
363
364 test_expect_success '--fixup=reword: ignores staged changes' '
365 commit_for_rebase_autosquash_setup &&
366 cat >expected <<-EOF &&
367 amend! $(git log -1 --format=%s HEAD~)
368
369 $(get_commit_msg HEAD~)
370
371 edited
372 EOF
373 (
374 set_fake_editor &&
375 FAKE_COMMIT_AMEND="edited" \
376 git commit --fixup=reword:HEAD~
377 ) &&
378 get_commit_msg HEAD >actual &&
379 test_cmp expected actual &&
380 test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
381 test_cmp_rev HEAD@{1} HEAD^ &&
382 test_expect_code 1 git diff --cached --exit-code &&
383 git cat-file blob :foo >actual &&
384 test_cmp foo actual
385 '
386
387 test_expect_success 'commit --fixup=reword: works with -m' '
388 commit_for_rebase_autosquash_setup &&
389 git commit --fixup=reword:HEAD~ -m "reword commit message" &&
390 test_commit_message HEAD <<-EOF
391 amend! $(git log -1 --format=%s HEAD~2)
392
393 reword commit message
394 EOF
395 '
396
397 test_expect_success 'commit --fixup=amend: works with -m' '
398 commit_for_rebase_autosquash_setup &&
399 git commit --fixup=amend:HEAD~ -m "amend commit message" &&
400 test_commit_message HEAD <<-EOF
401 amend! $(git log -1 --format=%s HEAD~2)
402
403 amend commit message
404 EOF
405 '
406
407 test_expect_success 'consecutive amend! commits remove amend! line from commit msg body' '
408 commit_for_rebase_autosquash_setup &&
409 cat >expected <<-EOF &&
410 amend! amend! $(git log -1 --format=%s HEAD~)
411
412 $(get_commit_msg HEAD~)
413
414 edited 1
415
416 edited 2
417 EOF
418 echo "reword new commit message" >actual &&
419 (
420 set_fake_editor &&
421 FAKE_COMMIT_AMEND="edited 1" \
422 git commit --fixup=reword:HEAD~ &&
423 FAKE_COMMIT_AMEND="edited 2" \
424 git commit --fixup=reword:HEAD
425 ) &&
426 get_commit_msg HEAD >actual &&
427 test_cmp expected actual
428 '
429
430 test_expect_success 'deny to create amend! commit if its commit msg body is empty' '
431 commit_for_rebase_autosquash_setup &&
432 echo "Aborting commit due to empty commit message body." >expected &&
433 (
434 set_fake_editor &&
435 test_must_fail env FAKE_COMMIT_MESSAGE="amend! target message subject line" \
436 git commit --fixup=amend:HEAD~ 2>actual
437 ) &&
438 test_cmp expected actual
439 '
440
441 test_expect_success 'deny to create amend! commit if -m is empty' '
442 commit_for_rebase_autosquash_setup &&
443 echo "Aborting commit due to empty commit message body." >expect &&
444 test_must_fail git commit --fixup=amend:HEAD~ -m "" 2>actual &&
445 test_cmp expect actual
446 '
447
448 test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' '
449 commit_for_rebase_autosquash_setup &&
450 cat >expected <<-EOF &&
451 amend! $(git log -1 --format=%s HEAD~)
452 EOF
453 (
454 set_fake_editor &&
455 FAKE_COMMIT_MESSAGE="amend! target message subject line" \
456 git commit --fixup=amend:HEAD~ --allow-empty-message &&
457 get_commit_msg HEAD >actual
458 ) &&
459 test_cmp expected actual
460 '
461
462 test_fixup_reword_opt () {
463 test_expect_success "--fixup=reword: incompatible with $1" "
464 echo 'fatal: reword option of '\''--fixup'\'' and' \
465 ''\''--patch/--interactive/--all/--include/--only'\' \
466 'cannot be used together' >expect &&
467 test_must_fail git commit --fixup=reword:HEAD~ $1 2>actual &&
468 test_cmp expect actual
469 "
470 }
471
472 for opt in --all --include --only --interactive --patch
473 do
474 test_fixup_reword_opt $opt
475 done
476
477 test_expect_success '--fixup=reword: give error with pathsec' '
478 commit_for_rebase_autosquash_setup &&
479 echo "fatal: reword option of '\''--fixup'\'' and path '\''foo'\'' cannot be used together" >expect &&
480 test_must_fail git commit --fixup=reword:HEAD~ -- foo 2>actual &&
481 test_cmp expect actual
482 '
483
484 test_expect_success 'commit --fixup works with -F' '
485 commit_for_rebase_autosquash_setup &&
486 echo "message" >msgfile &&
487 git commit --fixup HEAD~ -F msgfile &&
488 test_commit_message HEAD <<-EOF
489 fixup! $(git log -1 --format=%s HEAD~2)
490
491 message
492 EOF
493 '
494
495 test_expect_success 'commit --fixup works with -C' '
496 commit_for_rebase_autosquash_setup &&
497 git commit --fixup HEAD~ -C HEAD &&
498 test_commit_message HEAD <<-EOF
499 fixup! $(git log -1 --format=%s HEAD~2)
500
501 $(get_commit_msg HEAD~)
502 EOF
503 '
504
505 test_expect_success 'commit --fixup=amend: works with -c' '
506 commit_for_rebase_autosquash_setup &&
507 test_set_editor : &&
508 git commit --fixup=amend:HEAD -c HEAD~ &&
509 test_commit_message HEAD <<-EOF
510 amend! intermediate commit
511
512 target message subject line
513
514 target message body line 1
515 target message body line 2
516 EOF
517 '
518
519 test_expect_success 'commit --fixup=amend:HEAD with -C HEAD and without have the same message' '
520 commit_for_rebase_autosquash_setup &&
521 start=$(git rev-parse HEAD) &&
522
523 git commit --fixup=amend:HEAD -C HEAD &&
524 git commit --fixup=amend:HEAD -C HEAD &&
525 git log -1 --pretty=%B >with-c &&
526
527 git reset --hard "$start" &&
528 test_set_editor : &&
529 git commit --fixup=amend:HEAD &&
530 git commit --fixup=amend:HEAD &&
531 git log -1 --pretty=%B >without-c &&
532
533 test_cmp with-c without-c
534 '
535
536 test_expect_success 'commit --fixup=amend: with -C copies full subject + body of squash commit' '
537 commit_for_rebase_autosquash_setup &&
538 git commit --squash HEAD~ -m "inner body" &&
539 echo "extra" >>foo &&
540 git add foo &&
541 git commit --fixup=amend:HEAD -C HEAD &&
542 test_commit_message HEAD <<-EOF
543 amend! squash! $(git log -1 --format=%s HEAD~3)
544
545 squash! $(git log -1 --format=%s HEAD~3)
546
547 inner body
548 EOF
549 '
550
551 test_expect_success 'commit --fixup=reword: works with -F' '
552 commit_for_rebase_autosquash_setup &&
553 echo "message from file" >msgfile &&
554 git commit --fixup=reword:HEAD~ -F msgfile &&
555 test_commit_message HEAD <<-EOF
556 amend! $(git log -1 --format=%s HEAD~2)
557
558 $(cat msgfile)
559 EOF
560 '
561
562 test_expect_success 'commit --squash works with -F' '
563 commit_for_rebase_autosquash_setup &&
564 echo "log message from file" >msgfile &&
565 git commit --squash HEAD~1 -F msgfile &&
566 commit_msg_is "squash! target message subject linelog message from file"
567 '
568
569 test_expect_success 'commit --squash works with -m' '
570 commit_for_rebase_autosquash_setup &&
571 git commit --squash HEAD~1 -m "foo bar\nbaz" &&
572 commit_msg_is "squash! target message subject linefoo bar\nbaz"
573 '
574
575 test_expect_success 'commit --squash works with -C' '
576 commit_for_rebase_autosquash_setup &&
577 git commit --squash HEAD~1 -C HEAD &&
578 commit_msg_is "squash! target message subject lineintermediate commit"
579 '
580
581 test_expect_success 'commit --squash works with -c' '
582 commit_for_rebase_autosquash_setup &&
583 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
584 git commit --squash HEAD~1 -c HEAD &&
585 commit_msg_is "squash! target message subject lineedited commit"
586 '
587
588 test_expect_success 'commit --squash works with -C for same commit' '
589 commit_for_rebase_autosquash_setup &&
590 git commit --squash HEAD -C HEAD &&
591 commit_msg_is "squash! intermediate commit"
592 '
593
594 test_expect_success 'commit --squash works with -c for same commit' '
595 commit_for_rebase_autosquash_setup &&
596 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
597 git commit --squash HEAD -c HEAD &&
598 commit_msg_is "squash! edited commit"
599 '
600
601 test_expect_success 'commit --squash works with editor' '
602 commit_for_rebase_autosquash_setup &&
603 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
604 git commit --squash HEAD~1 &&
605 commit_msg_is "squash! target message subject linecommit message"
606 '
607
608 test_expect_success 'invalid message options when using --fixup' '
609 echo changes >>foo &&
610 echo "message" >log &&
611 git add foo &&
612 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2
613 '
614
615 cat >expected-template <<EOF
616
617 # Please enter the commit message for your changes. Lines starting
618 # with '#' will be ignored.
619 #
620 # Author: A U Thor <author@example.com>
621 #
622 # On branch commit-template-check
623 # Changes to be committed:
624 # new file: commit-template-check
625 #
626 # Untracked files not listed
627 EOF
628
629 test_expect_success 'new line found before status message in commit template' '
630 git checkout -b commit-template-check &&
631 git reset --hard HEAD &&
632 touch commit-template-check &&
633 git add commit-template-check &&
634 GIT_EDITOR="cat >editor-input" git commit --untracked-files=no --allow-empty-message &&
635 test_cmp expected-template editor-input
636 '
637
638 test_expect_success 'setup empty commit with unstaged rename and copy' '
639 test_create_repo unstaged_rename_and_copy &&
640 (
641 cd unstaged_rename_and_copy &&
642
643 echo content >orig &&
644 git add orig &&
645 test_commit orig &&
646
647 cp orig new_copy &&
648 mv orig new_rename &&
649 git add -N new_copy new_rename
650 )
651 '
652
653 test_expect_success 'check commit with unstaged rename and copy' '
654 (
655 cd unstaged_rename_and_copy &&
656
657 test_must_fail git -c diff.renames=copy commit
658 )
659 '
660
661 test_expect_success 'commit without staging files fails and displays hints' '
662 echo "initial" >file &&
663 git add file &&
664 git commit -m initial &&
665 echo "changes" >>file &&
666 test_must_fail git commit -m update >actual &&
667 test_grep "no changes added to commit (use \"git add\" and/or \"git commit -a\")" actual
668 '
669
670 test_done