document that statusHints affects git checkout
[git/raj.git] / t / t7502-commit.sh
blobdeb187eb7b4277a43f46e73c1d85012b728ce14a
1 #!/bin/sh
3 test_description='git commit porcelain-ish'
5 . ./test-lib.sh
7 # Arguments: [<prefix] [<commit message>] [<commit options>]
8 check_summary_oneline() {
9 test_tick &&
10 git commit ${3+"$3"} -m "$2" | head -1 > act &&
12 # branch name
13 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
15 # append the "special" prefix, like "root-commit", "detached HEAD"
16 if test -n "$1"
17 then
18 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
21 # abbrev SHA-1
22 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
23 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
25 test_i18ncmp exp act
28 test_expect_success 'output summary format' '
30 echo new >file1 &&
31 git add file1 &&
32 check_summary_oneline "root-commit" "initial" &&
34 echo change >>file1 &&
35 git add file1
38 test_expect_success 'output summary format: root-commit' '
39 check_summary_oneline "" "a change"
42 test_expect_success 'output summary format for commit with an empty diff' '
44 check_summary_oneline "" "empty" "--allow-empty"
47 test_expect_success 'output summary format for merges' '
49 git checkout -b recursive-base &&
50 test_commit base file1 &&
52 git checkout -b recursive-a recursive-base &&
53 test_commit commit-a file1 &&
55 git checkout -b recursive-b recursive-base &&
56 test_commit commit-b file1 &&
58 # conflict
59 git checkout recursive-a &&
60 test_must_fail git merge recursive-b &&
61 # resolve the conflict
62 echo commit-a > file1 &&
63 git add file1 &&
64 check_summary_oneline "" "Merge"
67 output_tests_cleanup() {
68 # this is needed for "do not fire editor in the presence of conflicts"
69 git checkout master &&
71 # this is needed for the "partial removal" test to pass
72 git rm file1 &&
73 git commit -m "cleanup"
76 test_expect_success 'the basics' '
78 output_tests_cleanup &&
80 echo doing partial >"commit is" &&
81 mkdir not &&
82 echo very much encouraged but we should >not/forbid &&
83 git add "commit is" not &&
84 echo update added "commit is" file >"commit is" &&
85 echo also update another >not/forbid &&
86 test_tick &&
87 git commit -a -m "initial with -a" &&
89 git cat-file blob HEAD:"commit is" >current.1 &&
90 git cat-file blob HEAD:not/forbid >current.2 &&
92 cmp current.1 "commit is" &&
93 cmp current.2 not/forbid
97 test_expect_success 'partial' '
99 echo another >"commit is" &&
100 echo another >not/forbid &&
101 test_tick &&
102 git commit -m "partial commit to handle a file" "commit is" &&
104 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
105 test "$changed" = "commit is"
109 test_expect_success 'partial modification in a subdirectory' '
111 test_tick &&
112 git commit -m "partial commit to subdirectory" not &&
114 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
115 test "$changed" = "not/forbid"
119 test_expect_success 'partial removal' '
121 git rm not/forbid &&
122 git commit -m "partial commit to remove not/forbid" not &&
124 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
125 test "$changed" = "not/forbid" &&
126 remain=$(git ls-tree -r --name-only HEAD) &&
127 test "$remain" = "commit is"
131 test_expect_success 'sign off' '
133 >positive &&
134 git add positive &&
135 git commit -s -m "thank you" &&
136 actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
137 expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
138 test "z$actual" = "z$expected"
142 test_expect_success 'multiple -m' '
144 >negative &&
145 git add negative &&
146 git commit -m "one" -m "two" -m "three" &&
147 actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
148 expected=$(echo one; echo; echo two; echo; echo three) &&
149 test "z$actual" = "z$expected"
153 test_expect_success 'verbose' '
155 echo minus >negative &&
156 git add negative &&
157 git status -v | sed -ne "/^diff --git /p" >actual &&
158 echo "diff --git a/negative b/negative" >expect &&
159 test_cmp expect actual
163 test_expect_success 'verbose respects diff config' '
165 git config color.diff always &&
166 git status -v >actual &&
167 grep "\[1mdiff --git" actual &&
168 git config --unset color.diff
171 test_expect_success 'cleanup commit messages (verbatim,-t)' '
173 echo >>negative &&
174 { echo;echo "# text";echo; } >expect &&
175 git commit --cleanup=verbatim -t expect -a &&
176 git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
177 test_cmp expect actual
181 test_expect_success 'cleanup commit messages (verbatim,-F)' '
183 echo >>negative &&
184 git commit --cleanup=verbatim -F expect -a &&
185 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
186 test_cmp expect actual
190 test_expect_success 'cleanup commit messages (verbatim,-m)' '
192 echo >>negative &&
193 git commit --cleanup=verbatim -m "$(cat expect)" -a &&
194 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
195 test_cmp expect actual
199 test_expect_success 'cleanup commit messages (whitespace,-F)' '
201 echo >>negative &&
202 { echo;echo "# text";echo; } >text &&
203 echo "# text" >expect &&
204 git commit --cleanup=whitespace -F text -a &&
205 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
206 test_cmp expect actual
210 test_expect_success 'cleanup commit messages (strip,-F)' '
212 echo >>negative &&
213 { echo;echo "# text";echo sample;echo; } >text &&
214 echo sample >expect &&
215 git commit --cleanup=strip -F text -a &&
216 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
217 test_cmp expect actual
221 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
223 echo >>negative &&
224 { echo;echo sample;echo; } >text &&
225 git commit -e -F text -a &&
226 head -n 4 .git/COMMIT_EDITMSG >actual
229 echo "sample
231 # Please enter the commit message for your changes. Lines starting
232 # with '#' will be ignored, and an empty message aborts the commit." >expect
234 test_expect_success 'cleanup commit messages (strip,-F,-e): output' '
235 test_i18ncmp expect actual
238 test_expect_success 'message shows author when it is not equal to committer' '
239 echo >>negative &&
240 git commit -e -m "sample" -a &&
241 test_i18ngrep \
242 "^# Author: *A U Thor <author@example.com>\$" \
243 .git/COMMIT_EDITMSG
246 test_expect_success 'setup auto-ident prerequisite' '
247 if (sane_unset GIT_COMMITTER_EMAIL &&
248 sane_unset GIT_COMMITTER_NAME &&
249 git var GIT_COMMITTER_IDENT); then
250 test_set_prereq AUTOIDENT
251 else
252 test_set_prereq NOAUTOIDENT
256 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
258 echo >>negative &&
260 sane_unset GIT_COMMITTER_EMAIL &&
261 sane_unset GIT_COMMITTER_NAME &&
262 git commit -e -m "sample" -a
263 ) &&
264 # the ident is calculated from the system, so we cannot
265 # check the actual value, only that it is there
266 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
269 write_script .git/FAKE_EDITOR <<EOF
270 echo editor started > "$(pwd)/.git/result"
271 exit 0
274 test_expect_success NOAUTOIDENT 'do not fire editor when committer is bogus' '
275 >.git/result
276 >expect &&
278 echo >>negative &&
280 sane_unset GIT_COMMITTER_EMAIL &&
281 sane_unset GIT_COMMITTER_NAME &&
282 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
283 export GIT_EDITOR &&
284 test_must_fail git commit -e -m sample -a
285 ) &&
286 test_cmp expect .git/result
289 test_expect_success 'do not fire editor in the presence of conflicts' '
291 git clean -f &&
292 echo f >g &&
293 git add g &&
294 git commit -m "add g" &&
295 git branch second &&
296 echo master >g &&
297 echo g >h &&
298 git add g h &&
299 git commit -m "modify g and add h" &&
300 git checkout second &&
301 echo second >g &&
302 git add g &&
303 git commit -m second &&
304 # Must fail due to conflict
305 test_must_fail git cherry-pick -n master &&
306 echo "editor not started" >.git/result &&
308 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
309 export GIT_EDITOR &&
310 test_must_fail git commit
311 ) &&
312 test "$(cat .git/result)" = "editor not started"
315 write_script .git/FAKE_EDITOR <<EOF
316 # kill -TERM command added below.
319 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
320 echo >>negative &&
321 ! "$SHELL_PATH" -c '\''
322 echo kill -TERM $$ >> .git/FAKE_EDITOR
323 GIT_EDITOR=.git/FAKE_EDITOR
324 export GIT_EDITOR
325 exec git commit -a'\'' &&
326 test ! -f .git/index.lock
329 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
330 git reset -q --hard
332 test_expect_success 'Hand committing of a redundant merge removes dups' '
334 git rev-parse second master >expect &&
335 test_must_fail git merge second master &&
336 git checkout master g &&
337 EDITOR=: git commit -a &&
338 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
339 test_cmp expect actual
343 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
345 git reset --hard &&
346 git commit -s -m "hello: kitty" --allow-empty &&
347 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
348 test_line_count = 3 actual
352 write_script .git/FAKE_EDITOR <<\EOF
353 mv "$1" "$1.orig"
355 echo message
356 cat "$1.orig"
357 ) >"$1"
360 echo '## Custom template' >template
362 clear_config () {
364 git config --unset-all "$1"
365 case $? in
366 0|5) exit 0 ;;
367 *) exit 1 ;;
368 esac
372 try_commit () {
373 git reset --hard &&
374 echo >>negative &&
375 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
376 case "$use_template" in
378 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
380 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
381 esac
384 try_commit_status_combo () {
386 test_expect_success 'commit' '
387 clear_config commit.status &&
388 try_commit "" &&
389 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
392 test_expect_success 'commit' '
393 clear_config commit.status &&
394 try_commit "" &&
395 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
398 test_expect_success 'commit --status' '
399 clear_config commit.status &&
400 try_commit --status &&
401 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
404 test_expect_success 'commit --no-status' '
405 clear_config commit.status &&
406 try_commit --no-status &&
407 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
410 test_expect_success 'commit with commit.status = yes' '
411 clear_config commit.status &&
412 git config commit.status yes &&
413 try_commit "" &&
414 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
417 test_expect_success 'commit with commit.status = no' '
418 clear_config commit.status &&
419 git config commit.status no &&
420 try_commit "" &&
421 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
424 test_expect_success 'commit --status with commit.status = yes' '
425 clear_config commit.status &&
426 git config commit.status yes &&
427 try_commit --status &&
428 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
431 test_expect_success 'commit --no-status with commit.status = yes' '
432 clear_config commit.status &&
433 git config commit.status yes &&
434 try_commit --no-status &&
435 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
438 test_expect_success 'commit --status with commit.status = no' '
439 clear_config commit.status &&
440 git config commit.status no &&
441 try_commit --status &&
442 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
445 test_expect_success 'commit --no-status with commit.status = no' '
446 clear_config commit.status &&
447 git config commit.status no &&
448 try_commit --no-status &&
449 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
454 try_commit_status_combo
456 use_template="-t template"
458 try_commit_status_combo
460 test_done