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