]>
Commit | Line | Data |
---|---|---|
b468f0ce JH |
1 | #!/bin/sh |
2 | ||
3 | test_description='git commit porcelain-ish' | |
4 | ||
5 | . ./test-lib.sh | |
6 | ||
51fb3a3d RT |
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 | ||
cee9f2b3 | 16 | # Arguments: [<prefix] [<commit message>] [<commit options>] |
fc6fa0d0 TRC |
17 | check_summary_oneline() { |
18 | test_tick && | |
cee9f2b3 | 19 | git commit ${3+"$3"} -m "$2" | head -1 > act && |
fc6fa0d0 TRC |
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 | ||
f79ce8db | 34 | test_i18ncmp exp act |
fc6fa0d0 TRC |
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 && | |
7f5673d7 ÆAB |
44 | git add file1 |
45 | ' | |
46 | ||
47 | test_expect_success 'output summary format: root-commit' ' | |
fc6fa0d0 TRC |
48 | check_summary_oneline "" "a change" |
49 | ' | |
50 | ||
a45e1a87 | 51 | test_expect_success 'output summary format for commit with an empty diff' ' |
cee9f2b3 TRC |
52 | |
53 | check_summary_oneline "" "empty" "--allow-empty" | |
54 | ' | |
55 | ||
a45e1a87 | 56 | test_expect_success 'output summary format for merges' ' |
cee9f2b3 TRC |
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 | ||
fc6fa0d0 TRC |
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 | ||
b468f0ce JH |
85 | test_expect_success 'the basics' ' |
86 | ||
fc6fa0d0 TRC |
87 | output_tests_cleanup && |
88 | ||
b468f0ce JH |
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 | ||
e8f30160 | 118 | test_expect_success 'partial modification in a subdirectory' ' |
b468f0ce JH |
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 && | |
82ebb0b6 | 168 | test_cmp expect actual |
b468f0ce JH |
169 | |
170 | ' | |
171 | ||
4f672ad6 JK |
172 | test_expect_success 'verbose respects diff config' ' |
173 | ||
174 | git config color.diff always && | |
175 | git status -v >actual && | |
176 | grep "\[1mdiff --git" actual && | |
177 | git config --unset color.diff | |
178 | ' | |
179 | ||
51fb3a3d | 180 | test_expect_success 'cleanup commit messages (verbatim option,-t)' ' |
5f065737 AR |
181 | |
182 | echo >>negative && | |
183 | { echo;echo "# text";echo; } >expect && | |
184 | git commit --cleanup=verbatim -t expect -a && | |
185 | git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual && | |
82ebb0b6 | 186 | test_cmp expect actual |
5f065737 AR |
187 | |
188 | ' | |
189 | ||
51fb3a3d | 190 | test_expect_success 'cleanup commit messages (verbatim option,-F)' ' |
5f065737 AR |
191 | |
192 | echo >>negative && | |
193 | git commit --cleanup=verbatim -F expect -a && | |
194 | git cat-file -p HEAD |sed -e "1,/^\$/d">actual && | |
82ebb0b6 | 195 | test_cmp expect actual |
5f065737 AR |
196 | |
197 | ' | |
198 | ||
51fb3a3d | 199 | test_expect_success 'cleanup commit messages (verbatim option,-m)' ' |
5f065737 AR |
200 | |
201 | echo >>negative && | |
202 | git commit --cleanup=verbatim -m "$(cat expect)" -a && | |
203 | git cat-file -p HEAD |sed -e "1,/^\$/d">actual && | |
82ebb0b6 | 204 | test_cmp expect actual |
5f065737 AR |
205 | |
206 | ' | |
207 | ||
51fb3a3d | 208 | test_expect_success 'cleanup commit messages (whitespace option,-F)' ' |
5f065737 AR |
209 | |
210 | echo >>negative && | |
211 | { echo;echo "# text";echo; } >text && | |
212 | echo "# text" >expect && | |
213 | git commit --cleanup=whitespace -F text -a && | |
214 | git cat-file -p HEAD |sed -e "1,/^\$/d">actual && | |
82ebb0b6 | 215 | test_cmp expect actual |
5f065737 AR |
216 | |
217 | ' | |
218 | ||
51fb3a3d | 219 | test_expect_success 'cleanup commit messages (strip option,-F)' ' |
5f065737 AR |
220 | |
221 | echo >>negative && | |
222 | { echo;echo "# text";echo sample;echo; } >text && | |
223 | echo sample >expect && | |
224 | git commit --cleanup=strip -F text -a && | |
225 | git cat-file -p HEAD |sed -e "1,/^\$/d">actual && | |
82ebb0b6 | 226 | test_cmp expect actual |
5f065737 AR |
227 | |
228 | ' | |
229 | ||
51fb3a3d | 230 | test_expect_success 'cleanup commit messages (strip option,-F,-e)' ' |
5f065737 AR |
231 | |
232 | echo >>negative && | |
233 | { echo;echo sample;echo; } >text && | |
234 | git commit -e -F text -a && | |
0b430a17 ÆAB |
235 | head -n 4 .git/COMMIT_EDITMSG >actual |
236 | ' | |
237 | ||
238 | echo "sample | |
5f065737 | 239 | |
0b430a17 ÆAB |
240 | # Please enter the commit message for your changes. Lines starting |
241 | # with '#' will be ignored, and an empty message aborts the commit." >expect | |
242 | ||
51fb3a3d | 243 | test_expect_success 'cleanup commit messages (strip option,-F,-e): output' ' |
f79ce8db | 244 | test_i18ncmp expect actual |
5f065737 AR |
245 | ' |
246 | ||
51fb3a3d RT |
247 | test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' ' |
248 | test_must_fail git commit --cleanup=non-existent | |
249 | ' | |
250 | ||
251 | test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' ' | |
252 | test_must_fail git -c commit.cleanup=non-existent commit | |
253 | ' | |
254 | ||
255 | test_expect_success 'cleanup commit message (no config and no option uses default)' ' | |
256 | echo content >>file && | |
257 | git add file && | |
258 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment && | |
259 | git commit --no-status && | |
260 | commit_msg_is "commit message" | |
261 | ' | |
262 | ||
263 | test_expect_success 'cleanup commit message (option overrides default)' ' | |
264 | echo content >>file && | |
265 | git add file && | |
266 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment && | |
267 | git commit --cleanup=whitespace --no-status && | |
268 | commit_msg_is "commit message # comment" | |
269 | ' | |
270 | ||
271 | test_expect_success 'cleanup commit message (config overrides default)' ' | |
272 | echo content >>file && | |
273 | git add file && | |
274 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment && | |
275 | git -c commit.cleanup=whitespace commit --no-status && | |
276 | commit_msg_is "commit message # comment" | |
277 | ' | |
278 | ||
279 | test_expect_success 'cleanup commit message (option overrides config)' ' | |
280 | echo content >>file && | |
281 | git add file && | |
282 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment && | |
283 | git -c commit.cleanup=whitespace commit --cleanup=default && | |
284 | commit_msg_is "commit message" | |
285 | ' | |
286 | ||
287 | test_expect_success 'cleanup commit message (default, -m)' ' | |
288 | echo content >>file && | |
289 | git add file && | |
290 | git commit -m "message #comment " && | |
291 | commit_msg_is "message #comment" | |
292 | ' | |
293 | ||
294 | test_expect_success 'cleanup commit message (whitespace option, -m)' ' | |
295 | echo content >>file && | |
296 | git add file && | |
297 | git commit --cleanup=whitespace --no-status -m "message #comment " && | |
298 | commit_msg_is "message #comment" | |
299 | ' | |
300 | ||
301 | test_expect_success 'cleanup commit message (whitespace config, -m)' ' | |
302 | echo content >>file && | |
303 | git add file && | |
304 | git -c commit.cleanup=whitespace commit --no-status -m "message #comment " && | |
305 | commit_msg_is "message #comment" | |
306 | ' | |
307 | ||
1f4bf345 | 308 | test_expect_success 'message shows author when it is not equal to committer' ' |
e83dbe80 | 309 | echo >>negative && |
ceacd91a | 310 | git commit -e -m "sample" -a && |
1f4bf345 JK |
311 | test_i18ngrep \ |
312 | "^# Author: *A U Thor <author@example.com>\$" \ | |
313 | .git/COMMIT_EDITMSG | |
e83dbe80 SB |
314 | ' |
315 | ||
1d7dc264 | 316 | test_expect_success AUTOIDENT 'message shows committer when it is automatic' ' |
bb1ae3f6 SB |
317 | |
318 | echo >>negative && | |
7845944c | 319 | ( |
00648ba0 EN |
320 | sane_unset GIT_COMMITTER_EMAIL && |
321 | sane_unset GIT_COMMITTER_NAME && | |
1d7dc264 | 322 | git commit -e -m "sample" -a |
7845944c | 323 | ) && |
1f4bf345 JK |
324 | # the ident is calculated from the system, so we cannot |
325 | # check the actual value, only that it is there | |
326 | test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG | |
bb1ae3f6 SB |
327 | ' |
328 | ||
a9ebc43b JK |
329 | write_script .git/FAKE_EDITOR <<EOF |
330 | echo editor started > "$(pwd)/.git/result" | |
ec84bd00 PB |
331 | exit 0 |
332 | EOF | |
ec84bd00 | 333 | |
09feffb6 | 334 | test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' ' |
8c8b3bc3 JK |
335 | >.git/result |
336 | >expect && | |
337 | ||
338 | echo >>negative && | |
339 | ( | |
340 | sane_unset GIT_COMMITTER_EMAIL && | |
341 | sane_unset GIT_COMMITTER_NAME && | |
342 | GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" && | |
343 | export GIT_EDITOR && | |
344 | test_must_fail git commit -e -m sample -a | |
345 | ) && | |
346 | test_cmp expect .git/result | |
347 | ' | |
348 | ||
ec84bd00 PB |
349 | test_expect_success 'do not fire editor in the presence of conflicts' ' |
350 | ||
a3c91e08 JH |
351 | git clean -f && |
352 | echo f >g && | |
353 | git add g && | |
354 | git commit -m "add g" && | |
355 | git branch second && | |
356 | echo master >g && | |
357 | echo g >h && | |
358 | git add g h && | |
359 | git commit -m "modify g and add h" && | |
360 | git checkout second && | |
361 | echo second >g && | |
362 | git add g && | |
363 | git commit -m second && | |
364 | # Must fail due to conflict | |
365 | test_must_fail git cherry-pick -n master && | |
366 | echo "editor not started" >.git/result && | |
e2007832 | 367 | ( |
34565f27 | 368 | GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" && |
e2007832 BC |
369 | export GIT_EDITOR && |
370 | test_must_fail git commit | |
371 | ) && | |
a3c91e08 | 372 | test "$(cat .git/result)" = "editor not started" |
ec84bd00 PB |
373 | ' |
374 | ||
a9ebc43b | 375 | write_script .git/FAKE_EDITOR <<EOF |
ad5fa3cc PB |
376 | # kill -TERM command added below. |
377 | EOF | |
378 | ||
fb9a2bea | 379 | test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' ' |
ad5fa3cc | 380 | echo >>negative && |
09b78bc1 | 381 | ! "$SHELL_PATH" -c '\'' |
ad5fa3cc | 382 | echo kill -TERM $$ >> .git/FAKE_EDITOR |
09b78bc1 BC |
383 | GIT_EDITOR=.git/FAKE_EDITOR |
384 | export GIT_EDITOR | |
385 | exec git commit -a'\'' && | |
386 | test ! -f .git/index.lock | |
ad5fa3cc PB |
387 | ' |
388 | ||
67bfc030 JH |
389 | rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG |
390 | git reset -q --hard | |
391 | ||
392 | test_expect_success 'Hand committing of a redundant merge removes dups' ' | |
393 | ||
394 | git rev-parse second master >expect && | |
395 | test_must_fail git merge second master && | |
396 | git checkout master g && | |
397 | EDITOR=: git commit -a && | |
398 | git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual && | |
399 | test_cmp expect actual | |
400 | ||
401 | ' | |
402 | ||
e5138436 JH |
403 | test_expect_success 'A single-liner subject with a token plus colon is not a footer' ' |
404 | ||
405 | git reset --hard && | |
406 | git commit -s -m "hello: kitty" --allow-empty && | |
407 | git cat-file commit HEAD | sed -e "1,/^$/d" >actual && | |
3fb0459b | 408 | test_line_count = 3 actual |
e5138436 JH |
409 | |
410 | ' | |
411 | ||
a9ebc43b JK |
412 | write_script .git/FAKE_EDITOR <<\EOF |
413 | mv "$1" "$1.orig" | |
f9c01817 JH |
414 | ( |
415 | echo message | |
a9ebc43b JK |
416 | cat "$1.orig" |
417 | ) >"$1" | |
f9c01817 JH |
418 | EOF |
419 | ||
420 | echo '## Custom template' >template | |
421 | ||
422 | clear_config () { | |
423 | ( | |
424 | git config --unset-all "$1" | |
425 | case $? in | |
426 | 0|5) exit 0 ;; | |
427 | *) exit 1 ;; | |
428 | esac | |
429 | ) | |
430 | } | |
431 | ||
432 | try_commit () { | |
433 | git reset --hard && | |
434 | echo >>negative && | |
435 | GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template && | |
436 | case "$use_template" in | |
437 | '') | |
f79ce8db | 438 | test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;; |
f9c01817 | 439 | *) |
f79ce8db | 440 | test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;; |
f9c01817 JH |
441 | esac |
442 | } | |
443 | ||
444 | try_commit_status_combo () { | |
445 | ||
f79ce8db | 446 | test_expect_success 'commit' ' |
f9c01817 JH |
447 | clear_config commit.status && |
448 | try_commit "" && | |
f79ce8db | 449 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
450 | ' |
451 | ||
f79ce8db | 452 | test_expect_success 'commit' ' |
f9c01817 JH |
453 | clear_config commit.status && |
454 | try_commit "" && | |
f79ce8db | 455 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
456 | ' |
457 | ||
f79ce8db | 458 | test_expect_success 'commit --status' ' |
f9c01817 JH |
459 | clear_config commit.status && |
460 | try_commit --status && | |
f79ce8db | 461 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
462 | ' |
463 | ||
f79ce8db | 464 | test_expect_success 'commit --no-status' ' |
f9c01817 | 465 | clear_config commit.status && |
2dec68cf | 466 | try_commit --no-status && |
f79ce8db | 467 | test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
468 | ' |
469 | ||
f79ce8db | 470 | test_expect_success 'commit with commit.status = yes' ' |
f9c01817 JH |
471 | clear_config commit.status && |
472 | git config commit.status yes && | |
473 | try_commit "" && | |
f79ce8db | 474 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
475 | ' |
476 | ||
f79ce8db | 477 | test_expect_success 'commit with commit.status = no' ' |
f9c01817 JH |
478 | clear_config commit.status && |
479 | git config commit.status no && | |
480 | try_commit "" && | |
f79ce8db | 481 | test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
482 | ' |
483 | ||
f79ce8db | 484 | test_expect_success 'commit --status with commit.status = yes' ' |
f9c01817 JH |
485 | clear_config commit.status && |
486 | git config commit.status yes && | |
487 | try_commit --status && | |
f79ce8db | 488 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
489 | ' |
490 | ||
f79ce8db | 491 | test_expect_success 'commit --no-status with commit.status = yes' ' |
f9c01817 JH |
492 | clear_config commit.status && |
493 | git config commit.status yes && | |
494 | try_commit --no-status && | |
f79ce8db | 495 | test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
496 | ' |
497 | ||
f79ce8db | 498 | test_expect_success 'commit --status with commit.status = no' ' |
f9c01817 JH |
499 | clear_config commit.status && |
500 | git config commit.status no && | |
501 | try_commit --status && | |
f79ce8db | 502 | test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
503 | ' |
504 | ||
f79ce8db | 505 | test_expect_success 'commit --no-status with commit.status = no' ' |
f9c01817 JH |
506 | clear_config commit.status && |
507 | git config commit.status no && | |
508 | try_commit --no-status && | |
f79ce8db | 509 | test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG |
f9c01817 JH |
510 | ' |
511 | ||
512 | } | |
513 | ||
514 | try_commit_status_combo | |
515 | ||
516 | use_template="-t template" | |
517 | ||
518 | try_commit_status_combo | |
519 | ||
eff80a9f JH |
520 | test_expect_success 'commit --status with custom comment character' ' |
521 | test_when_finished "git config --unset core.commentchar" && | |
522 | git config core.commentchar ";" && | |
523 | try_commit --status && | |
524 | test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG | |
525 | ' | |
526 | ||
b468f0ce | 527 | test_done |