t7502: use test_config to set/unset git config variables
[git] / t / t7502-commit.sh
1 #!/bin/sh
2
3 test_description='git commit porcelain-ish'
4
5 . ./test-lib.sh
6
7 commit_msg_is () {
8         expect=commit_msg_is.expect
9         actual=commit_msg_is.actual
10
11         printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
12         printf "%s" "$1" >$expect &&
13         test_i18ncmp $expect $actual
14 }
15
16 # Arguments: [<prefix] [<commit message>] [<commit options>]
17 check_summary_oneline() {
18         test_tick &&
19         git commit ${3+"$3"} -m "$2" | head -1 > act &&
20
21         # branch name
22         SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
23
24         # append the "special" prefix, like "root-commit", "detached HEAD"
25         if test -n "$1"
26         then
27                 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
28         fi
29
30         # abbrev SHA-1
31         SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
32         echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
33
34         test_i18ncmp exp act
35 }
36
37 test_expect_success 'output summary format' '
38
39         echo new >file1 &&
40         git add file1 &&
41         check_summary_oneline "root-commit" "initial" &&
42
43         echo change >>file1 &&
44         git add file1
45 '
46
47 test_expect_success 'output summary format: root-commit' '
48         check_summary_oneline "" "a change"
49 '
50
51 test_expect_success 'output summary format for commit with an empty diff' '
52
53         check_summary_oneline "" "empty" "--allow-empty"
54 '
55
56 test_expect_success 'output summary format for merges' '
57
58         git checkout -b recursive-base &&
59         test_commit base file1 &&
60
61         git checkout -b recursive-a recursive-base &&
62         test_commit commit-a file1 &&
63
64         git checkout -b recursive-b recursive-base &&
65         test_commit commit-b file1 &&
66
67         # conflict
68         git checkout recursive-a &&
69         test_must_fail git merge recursive-b &&
70         # resolve the conflict
71         echo commit-a > file1 &&
72         git add file1 &&
73         check_summary_oneline "" "Merge"
74 '
75
76 output_tests_cleanup() {
77         # this is needed for "do not fire editor in the presence of conflicts"
78         git checkout master &&
79
80         # this is needed for the "partial removal" test to pass
81         git rm file1 &&
82         git commit -m "cleanup"
83 }
84
85 test_expect_success 'the basics' '
86
87         output_tests_cleanup &&
88
89         echo doing partial >"commit is" &&
90         mkdir not &&
91         echo very much encouraged but we should >not/forbid &&
92         git add "commit is" not &&
93         echo update added "commit is" file >"commit is" &&
94         echo also update another >not/forbid &&
95         test_tick &&
96         git commit -a -m "initial with -a" &&
97
98         git cat-file blob HEAD:"commit is" >current.1 &&
99         git cat-file blob HEAD:not/forbid >current.2 &&
100
101         cmp current.1 "commit is" &&
102         cmp current.2 not/forbid
103
104 '
105
106 test_expect_success 'partial' '
107
108         echo another >"commit is" &&
109         echo another >not/forbid &&
110         test_tick &&
111         git commit -m "partial commit to handle a file" "commit is" &&
112
113         changed=$(git diff-tree --name-only HEAD^ HEAD) &&
114         test "$changed" = "commit is"
115
116 '
117
118 test_expect_success 'partial modification in a subdirectory' '
119
120         test_tick &&
121         git commit -m "partial commit to subdirectory" not &&
122
123         changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
124         test "$changed" = "not/forbid"
125
126 '
127
128 test_expect_success 'partial removal' '
129
130         git rm not/forbid &&
131         git commit -m "partial commit to remove not/forbid" not &&
132
133         changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
134         test "$changed" = "not/forbid" &&
135         remain=$(git ls-tree -r --name-only HEAD) &&
136         test "$remain" = "commit is"
137
138 '
139
140 test_expect_success 'sign off' '
141
142         >positive &&
143         git add positive &&
144         git commit -s -m "thank you" &&
145         actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
146         expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
147         test "z$actual" = "z$expected"
148
149 '
150
151 test_expect_success 'multiple -m' '
152
153         >negative &&
154         git add negative &&
155         git commit -m "one" -m "two" -m "three" &&
156         actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
157         expected=$(echo one; echo; echo two; echo; echo three) &&
158         test "z$actual" = "z$expected"
159
160 '
161
162 test_expect_success 'verbose' '
163
164         echo minus >negative &&
165         git add negative &&
166         git status -v | sed -ne "/^diff --git /p" >actual &&
167         echo "diff --git a/negative b/negative" >expect &&
168         test_cmp expect actual
169
170 '
171
172 test_expect_success 'verbose respects diff config' '
173
174         test_config color.diff always &&
175         git status -v >actual &&
176         grep "\[1mdiff --git" actual
177 '
178
179 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
180
181         echo >>negative &&
182         { echo;echo "# text";echo; } >expect &&
183         git commit --cleanup=verbatim -t expect -a &&
184         git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
185         test_cmp expect actual
186
187 '
188
189 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
190
191         echo >>negative &&
192         git commit --cleanup=verbatim -F expect -a &&
193         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
194         test_cmp expect actual
195
196 '
197
198 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
199
200         echo >>negative &&
201         git commit --cleanup=verbatim -m "$(cat expect)" -a &&
202         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
203         test_cmp expect actual
204
205 '
206
207 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
208
209         echo >>negative &&
210         { echo;echo "# text";echo; } >text &&
211         echo "# text" >expect &&
212         git commit --cleanup=whitespace -F text -a &&
213         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
214         test_cmp expect actual
215
216 '
217
218 test_expect_success 'cleanup commit messages (strip option,-F)' '
219
220         echo >>negative &&
221         { echo;echo "# text";echo sample;echo; } >text &&
222         echo sample >expect &&
223         git commit --cleanup=strip -F text -a &&
224         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
225         test_cmp expect actual
226
227 '
228
229 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
230
231         echo >>negative &&
232         { echo;echo sample;echo; } >text &&
233         git commit -e -F text -a &&
234         head -n 4 .git/COMMIT_EDITMSG >actual
235 '
236
237 echo "sample
238
239 # Please enter the commit message for your changes. Lines starting
240 # with '#' will be ignored, and an empty message aborts the commit." >expect
241
242 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
243         test_i18ncmp expect actual
244 '
245
246 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
247         test_must_fail git commit --cleanup=non-existent
248 '
249
250 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
251         test_must_fail git -c commit.cleanup=non-existent commit
252 '
253
254 test_expect_success 'cleanup commit message (no config and no option uses default)' '
255         echo content >>file &&
256         git add file &&
257         test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
258         git commit --no-status &&
259         commit_msg_is "commit message"
260 '
261
262 test_expect_success 'cleanup commit message (option overrides default)' '
263         echo content >>file &&
264         git add file &&
265         test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
266         git commit --cleanup=whitespace --no-status &&
267         commit_msg_is "commit message # comment"
268 '
269
270 test_expect_success 'cleanup commit message (config overrides default)' '
271         echo content >>file &&
272         git add file &&
273         test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
274         git -c commit.cleanup=whitespace commit --no-status &&
275         commit_msg_is "commit message # comment"
276 '
277
278 test_expect_success 'cleanup commit message (option overrides config)' '
279         echo content >>file &&
280         git add file &&
281         test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
282         git -c commit.cleanup=whitespace commit --cleanup=default &&
283         commit_msg_is "commit message"
284 '
285
286 test_expect_success 'cleanup commit message (default, -m)' '
287         echo content >>file &&
288         git add file &&
289         git commit -m "message #comment " &&
290         commit_msg_is "message #comment"
291 '
292
293 test_expect_success 'cleanup commit message (whitespace option, -m)' '
294         echo content >>file &&
295         git add file &&
296         git commit --cleanup=whitespace --no-status -m "message #comment " &&
297         commit_msg_is "message #comment"
298 '
299
300 test_expect_success 'cleanup commit message (whitespace config, -m)' '
301         echo content >>file &&
302         git add file &&
303         git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
304         commit_msg_is "message #comment"
305 '
306
307 test_expect_success 'message shows author when it is not equal to committer' '
308         echo >>negative &&
309         git commit -e -m "sample" -a &&
310         test_i18ngrep \
311           "^# Author: *A U Thor <author@example.com>\$" \
312           .git/COMMIT_EDITMSG
313 '
314
315 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
316
317         echo >>negative &&
318         (
319                 sane_unset GIT_COMMITTER_EMAIL &&
320                 sane_unset GIT_COMMITTER_NAME &&
321                 git commit -e -m "sample" -a
322         ) &&
323         # the ident is calculated from the system, so we cannot
324         # check the actual value, only that it is there
325         test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
326 '
327
328 write_script .git/FAKE_EDITOR <<EOF
329 echo editor started > "$(pwd)/.git/result"
330 exit 0
331 EOF
332
333 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
334         >.git/result
335         >expect &&
336
337         echo >>negative &&
338         (
339                 sane_unset GIT_COMMITTER_EMAIL &&
340                 sane_unset GIT_COMMITTER_NAME &&
341                 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
342                 export GIT_EDITOR &&
343                 test_must_fail git commit -e -m sample -a
344         ) &&
345         test_cmp expect .git/result
346 '
347
348 test_expect_success 'do not fire editor in the presence of conflicts' '
349
350         git clean -f &&
351         echo f >g &&
352         git add g &&
353         git commit -m "add g" &&
354         git branch second &&
355         echo master >g &&
356         echo g >h &&
357         git add g h &&
358         git commit -m "modify g and add h" &&
359         git checkout second &&
360         echo second >g &&
361         git add g &&
362         git commit -m second &&
363         # Must fail due to conflict
364         test_must_fail git cherry-pick -n master &&
365         echo "editor not started" >.git/result &&
366         (
367                 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
368                 export GIT_EDITOR &&
369                 test_must_fail git commit
370         ) &&
371         test "$(cat .git/result)" = "editor not started"
372 '
373
374 write_script .git/FAKE_EDITOR <<EOF
375 # kill -TERM command added below.
376 EOF
377
378 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
379         echo >>negative &&
380         ! "$SHELL_PATH" -c '\''
381           echo kill -TERM $$ >> .git/FAKE_EDITOR
382           GIT_EDITOR=.git/FAKE_EDITOR
383           export GIT_EDITOR
384           exec git commit -a'\'' &&
385         test ! -f .git/index.lock
386 '
387
388 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
389 git reset -q --hard
390
391 test_expect_success 'Hand committing of a redundant merge removes dups' '
392
393         git rev-parse second master >expect &&
394         test_must_fail git merge second master &&
395         git checkout master g &&
396         EDITOR=: git commit -a &&
397         git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
398         test_cmp expect actual
399
400 '
401
402 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
403
404         git reset --hard &&
405         git commit -s -m "hello: kitty" --allow-empty &&
406         git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
407         test_line_count = 3 actual
408
409 '
410
411 write_script .git/FAKE_EDITOR <<\EOF
412 mv "$1" "$1.orig"
413 (
414         echo message
415         cat "$1.orig"
416 ) >"$1"
417 EOF
418
419 echo '## Custom template' >template
420
421 clear_config () {
422         (
423                 git config --unset-all "$1"
424                 case $? in
425                 0|5)    exit 0 ;;
426                 *)      exit 1 ;;
427                 esac
428         )
429 }
430
431 try_commit () {
432         git reset --hard &&
433         echo >>negative &&
434         GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
435         case "$use_template" in
436         '')
437                 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
438         *)
439                 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
440         esac
441 }
442
443 try_commit_status_combo () {
444
445         test_expect_success 'commit' '
446                 clear_config commit.status &&
447                 try_commit "" &&
448                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
449         '
450
451         test_expect_success 'commit' '
452                 clear_config commit.status &&
453                 try_commit "" &&
454                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
455         '
456
457         test_expect_success 'commit --status' '
458                 clear_config commit.status &&
459                 try_commit --status &&
460                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
461         '
462
463         test_expect_success 'commit --no-status' '
464                 clear_config commit.status &&
465                 try_commit --no-status &&
466                 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
467         '
468
469         test_expect_success 'commit with commit.status = yes' '
470                 clear_config commit.status &&
471                 git config commit.status yes &&
472                 try_commit "" &&
473                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
474         '
475
476         test_expect_success 'commit with commit.status = no' '
477                 clear_config commit.status &&
478                 git config commit.status no &&
479                 try_commit "" &&
480                 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
481         '
482
483         test_expect_success 'commit --status with commit.status = yes' '
484                 clear_config commit.status &&
485                 git config commit.status yes &&
486                 try_commit --status &&
487                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
488         '
489
490         test_expect_success 'commit --no-status with commit.status = yes' '
491                 clear_config commit.status &&
492                 git config commit.status yes &&
493                 try_commit --no-status &&
494                 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
495         '
496
497         test_expect_success 'commit --status with commit.status = no' '
498                 clear_config commit.status &&
499                 git config commit.status no &&
500                 try_commit --status &&
501                 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
502         '
503
504         test_expect_success 'commit --no-status with commit.status = no' '
505                 clear_config commit.status &&
506                 git config commit.status no &&
507                 try_commit --no-status &&
508                 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
509         '
510
511 }
512
513 try_commit_status_combo
514
515 use_template="-t template"
516
517 try_commit_status_combo
518
519 test_expect_success 'commit --status with custom comment character' '
520         test_config core.commentchar ";" &&
521         try_commit --status &&
522         test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
523 '
524
525 test_done