Code

t/t2023-checkout-m.sh: fix use of test_must_fail
[git.git] / t / t7502-commit.sh
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)"
19         fi
21         # abbrev SHA-1
22         SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
23         echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
25         if test_have_prereq C_LOCALE_OUTPUT
26         then
27                 test_cmp exp act
28         fi
29 }
31 test_expect_success 'output summary format' '
33         echo new >file1 &&
34         git add file1 &&
35         check_summary_oneline "root-commit" "initial" &&
37         echo change >>file1 &&
38         git add file1
39 '
41 test_expect_success 'output summary format: root-commit' '
42         check_summary_oneline "" "a change"
43 '
45 test_expect_success 'output summary format for commit with an empty diff' '
47         check_summary_oneline "" "empty" "--allow-empty"
48 '
50 test_expect_success 'output summary format for merges' '
52         git checkout -b recursive-base &&
53         test_commit base file1 &&
55         git checkout -b recursive-a recursive-base &&
56         test_commit commit-a file1 &&
58         git checkout -b recursive-b recursive-base &&
59         test_commit commit-b file1 &&
61         # conflict
62         git checkout recursive-a &&
63         test_must_fail git merge recursive-b &&
64         # resolve the conflict
65         echo commit-a > file1 &&
66         git add file1 &&
67         check_summary_oneline "" "Merge"
68 '
70 output_tests_cleanup() {
71         # this is needed for "do not fire editor in the presence of conflicts"
72         git checkout master &&
74         # this is needed for the "partial removal" test to pass
75         git rm file1 &&
76         git commit -m "cleanup"
77 }
79 test_expect_success 'the basics' '
81         output_tests_cleanup &&
83         echo doing partial >"commit is" &&
84         mkdir not &&
85         echo very much encouraged but we should >not/forbid &&
86         git add "commit is" not &&
87         echo update added "commit is" file >"commit is" &&
88         echo also update another >not/forbid &&
89         test_tick &&
90         git commit -a -m "initial with -a" &&
92         git cat-file blob HEAD:"commit is" >current.1 &&
93         git cat-file blob HEAD:not/forbid >current.2 &&
95         cmp current.1 "commit is" &&
96         cmp current.2 not/forbid
98 '
100 test_expect_success 'partial' '
102         echo another >"commit is" &&
103         echo another >not/forbid &&
104         test_tick &&
105         git commit -m "partial commit to handle a file" "commit is" &&
107         changed=$(git diff-tree --name-only HEAD^ HEAD) &&
108         test "$changed" = "commit is"
112 test_expect_success 'partial modification in a subdirectory' '
114         test_tick &&
115         git commit -m "partial commit to subdirectory" not &&
117         changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
118         test "$changed" = "not/forbid"
122 test_expect_success 'partial removal' '
124         git rm not/forbid &&
125         git commit -m "partial commit to remove not/forbid" not &&
127         changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
128         test "$changed" = "not/forbid" &&
129         remain=$(git ls-tree -r --name-only HEAD) &&
130         test "$remain" = "commit is"
134 test_expect_success 'sign off' '
136         >positive &&
137         git add positive &&
138         git commit -s -m "thank you" &&
139         actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
140         expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
141         test "z$actual" = "z$expected"
145 test_expect_success 'multiple -m' '
147         >negative &&
148         git add negative &&
149         git commit -m "one" -m "two" -m "three" &&
150         actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
151         expected=$(echo one; echo; echo two; echo; echo three) &&
152         test "z$actual" = "z$expected"
156 test_expect_success 'verbose' '
158         echo minus >negative &&
159         git add negative &&
160         git status -v | sed -ne "/^diff --git /p" >actual &&
161         echo "diff --git a/negative b/negative" >expect &&
162         test_cmp expect actual
166 test_expect_success 'verbose respects diff config' '
168         git config color.diff always &&
169         git status -v >actual &&
170         grep "\[1mdiff --git" actual &&
171         git config --unset color.diff
174 test_expect_success 'cleanup commit messages (verbatim,-t)' '
176         echo >>negative &&
177         { echo;echo "# text";echo; } >expect &&
178         git commit --cleanup=verbatim -t expect -a &&
179         git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
180         test_cmp expect actual
184 test_expect_success 'cleanup commit messages (verbatim,-F)' '
186         echo >>negative &&
187         git commit --cleanup=verbatim -F expect -a &&
188         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
189         test_cmp expect actual
193 test_expect_success 'cleanup commit messages (verbatim,-m)' '
195         echo >>negative &&
196         git commit --cleanup=verbatim -m "$(cat expect)" -a &&
197         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
198         test_cmp expect actual
202 test_expect_success 'cleanup commit messages (whitespace,-F)' '
204         echo >>negative &&
205         { echo;echo "# text";echo; } >text &&
206         echo "# text" >expect &&
207         git commit --cleanup=whitespace -F text -a &&
208         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
209         test_cmp expect actual
213 test_expect_success 'cleanup commit messages (strip,-F)' '
215         echo >>negative &&
216         { echo;echo "# text";echo sample;echo; } >text &&
217         echo sample >expect &&
218         git commit --cleanup=strip -F text -a &&
219         git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
220         test_cmp expect actual
224 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
226         echo >>negative &&
227         { echo;echo sample;echo; } >text &&
228         git commit -e -F text -a &&
229         head -n 4 .git/COMMIT_EDITMSG >actual
232 echo "sample
234 # Please enter the commit message for your changes. Lines starting
235 # with '#' will be ignored, and an empty message aborts the commit." >expect
237 test_expect_success C_LOCALE_OUTPUT 'cleanup commit messages (strip,-F,-e): output' '
238         test_cmp expect actual
241 echo "#
242 # Author:    $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
243 #" >> expect
245 test_expect_success C_LOCALE_OUTPUT 'author different from committer' '
247         echo >>negative &&
248         git commit -e -m "sample"
249         head -n 7 .git/COMMIT_EDITMSG >actual
252 test_expect_success C_LOCALE_OUTPUT 'author different from committer: output' '
253         test_cmp expect actual
256 mv expect expect.tmp
257 sed '$d' < expect.tmp > expect
258 rm -f expect.tmp
259 echo "# Committer:
260 #" >> expect
262 test_expect_success C_LOCALE_OUTPUT 'committer is automatic' '
264         echo >>negative &&
265         (
266                 sane_unset GIT_COMMITTER_EMAIL &&
267                 sane_unset GIT_COMMITTER_NAME &&
268                 # must fail because there is no change
269                 test_must_fail git commit -e -m "sample"
270         ) &&
271         head -n 8 .git/COMMIT_EDITMSG | \
272         sed "s/^# Committer: .*/# Committer:/" >actual
275 test_expect_success C_LOCALE_OUTPUT 'committer is automatic: output' '
276         test_cmp expect actual
279 pwd=`pwd`
280 cat >> .git/FAKE_EDITOR << EOF
281 #! /bin/sh
282 echo editor started > "$pwd/.git/result"
283 exit 0
284 EOF
285 chmod +x .git/FAKE_EDITOR
287 test_expect_success 'do not fire editor in the presence of conflicts' '
289         git clean -f &&
290         echo f >g &&
291         git add g &&
292         git commit -m "add g" &&
293         git branch second &&
294         echo master >g &&
295         echo g >h &&
296         git add g h &&
297         git commit -m "modify g and add h" &&
298         git checkout second &&
299         echo second >g &&
300         git add g &&
301         git commit -m second &&
302         # Must fail due to conflict
303         test_must_fail git cherry-pick -n master &&
304         echo "editor not started" >.git/result &&
305         (
306                 GIT_EDITOR="$(pwd)/.git/FAKE_EDITOR" &&
307                 export GIT_EDITOR &&
308                 test_must_fail git commit
309         ) &&
310         test "$(cat .git/result)" = "editor not started"
313 pwd=`pwd`
314 cat >.git/FAKE_EDITOR <<EOF
315 #! $SHELL_PATH
316 # kill -TERM command added below.
317 EOF
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 $(wc -l <actual) = 3
352 cat >.git/FAKE_EDITOR <<EOF
353 #!$SHELL_PATH
354 mv "\$1" "\$1.orig"
356         echo message
357         cat "\$1.orig"
358 ) >"\$1"
359 EOF
361 echo '## Custom template' >template
363 clear_config () {
364         (
365                 git config --unset-all "$1"
366                 case $? in
367                 0|5)    exit 0 ;;
368                 *)      exit 1 ;;
369                 esac
370         )
373 try_commit () {
374         git reset --hard &&
375         echo >>negative &&
376         GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
377         case "$use_template" in
378         '')
379                 ! grep "^## Custom template" .git/COMMIT_EDITMSG ;;
380         *)
381                 grep "^## Custom template" .git/COMMIT_EDITMSG ;;
382         esac
385 try_commit_status_combo () {
387         test_expect_success C_LOCALE_OUTPUT 'commit' '
388                 clear_config commit.status &&
389                 try_commit "" &&
390                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
391         '
393         test_expect_success C_LOCALE_OUTPUT 'commit' '
394                 clear_config commit.status &&
395                 try_commit "" &&
396                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
397         '
399         test_expect_success C_LOCALE_OUTPUT 'commit --status' '
400                 clear_config commit.status &&
401                 try_commit --status &&
402                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
403         '
405         test_expect_success C_LOCALE_OUTPUT 'commit --no-status' '
406                 clear_config commit.status &&
407                 try_commit --no-status &&
408                 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
409         '
411         test_expect_success C_LOCALE_OUTPUT 'commit with commit.status = yes' '
412                 clear_config commit.status &&
413                 git config commit.status yes &&
414                 try_commit "" &&
415                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
416         '
418         test_expect_success C_LOCALE_OUTPUT 'commit with commit.status = no' '
419                 clear_config commit.status &&
420                 git config commit.status no &&
421                 try_commit "" &&
422                 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
423         '
425         test_expect_success C_LOCALE_OUTPUT 'commit --status with commit.status = yes' '
426                 clear_config commit.status &&
427                 git config commit.status yes &&
428                 try_commit --status &&
429                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
430         '
432         test_expect_success C_LOCALE_OUTPUT 'commit --no-status with commit.status = yes' '
433                 clear_config commit.status &&
434                 git config commit.status yes &&
435                 try_commit --no-status &&
436                 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
437         '
439         test_expect_success C_LOCALE_OUTPUT 'commit --status with commit.status = no' '
440                 clear_config commit.status &&
441                 git config commit.status no &&
442                 try_commit --status &&
443                 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
444         '
446         test_expect_success C_LOCALE_OUTPUT 'commit --no-status with commit.status = no' '
447                 clear_config commit.status &&
448                 git config commit.status no &&
449                 try_commit --no-status &&
450                 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
451         '
455 try_commit_status_combo
457 use_template="-t template"
459 try_commit_status_combo
461 test_done