Merge branch 'bc/maint-submodule-fix-parked' into maint
[git] / t / t7502-commit.sh
1 #!/bin/sh
2
3 test_description='git commit porcelain-ish'
4
5 . ./test-lib.sh
6
7 # Arguments: [<prefix] [<commit message>] [<commit options>]
8 check_summary_oneline() {
9         test_tick &&
10         git commit ${3+"$3"} -m "$2" | head -1 > act &&
11
12         # branch name
13         SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
14
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
20
21         # abbrev SHA-1
22         SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
23         echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
24
25         if test_have_prereq C_LOCALE_OUTPUT
26         then
27                 test_cmp exp act
28         fi
29 }
30
31 test_expect_success 'output summary format' '
32
33         echo new >file1 &&
34         git add file1 &&
35         check_summary_oneline "root-commit" "initial" &&
36
37         echo change >>file1 &&
38         git add file1
39 '
40
41 test_expect_success 'output summary format: root-commit' '
42         check_summary_oneline "" "a change"
43 '
44
45 test_expect_success 'output summary format for commit with an empty diff' '
46
47         check_summary_oneline "" "empty" "--allow-empty"
48 '
49
50 test_expect_success 'output summary format for merges' '
51
52         git checkout -b recursive-base &&
53         test_commit base file1 &&
54
55         git checkout -b recursive-a recursive-base &&
56         test_commit commit-a file1 &&
57
58         git checkout -b recursive-b recursive-base &&
59         test_commit commit-b file1 &&
60
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 '
69
70 output_tests_cleanup() {
71         # this is needed for "do not fire editor in the presence of conflicts"
72         git checkout master &&
73
74         # this is needed for the "partial removal" test to pass
75         git rm file1 &&
76         git commit -m "cleanup"
77 }
78
79 test_expect_success 'the basics' '
80
81         output_tests_cleanup &&
82
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" &&
91
92         git cat-file blob HEAD:"commit is" >current.1 &&
93         git cat-file blob HEAD:not/forbid >current.2 &&
94
95         cmp current.1 "commit is" &&
96         cmp current.2 not/forbid
97
98 '
99
100 test_expect_success 'partial' '
101
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" &&
106
107         changed=$(git diff-tree --name-only HEAD^ HEAD) &&
108         test "$changed" = "commit is"
109
110 '
111
112 test_expect_success 'partial modification in a subdirectory' '
113
114         test_tick &&
115         git commit -m "partial commit to subdirectory" not &&
116
117         changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
118         test "$changed" = "not/forbid"
119
120 '
121
122 test_expect_success 'partial removal' '
123
124         git rm not/forbid &&
125         git commit -m "partial commit to remove not/forbid" not &&
126
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"
131
132 '
133
134 test_expect_success 'sign off' '
135
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"
142
143 '
144
145 test_expect_success 'multiple -m' '
146
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"
153
154 '
155
156 test_expect_success 'verbose' '
157
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
163
164 '
165
166 test_expect_success 'verbose respects diff config' '
167
168         git config color.diff always &&
169         git status -v >actual &&
170         grep "\[1mdiff --git" actual &&
171         git config --unset color.diff
172 '
173
174 test_expect_success 'cleanup commit messages (verbatim,-t)' '
175
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
181
182 '
183
184 test_expect_success 'cleanup commit messages (verbatim,-F)' '
185
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
190
191 '
192
193 test_expect_success 'cleanup commit messages (verbatim,-m)' '
194
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
199
200 '
201
202 test_expect_success 'cleanup commit messages (whitespace,-F)' '
203
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
210
211 '
212
213 test_expect_success 'cleanup commit messages (strip,-F)' '
214
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
221
222 '
223
224 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
225
226         echo >>negative &&
227         { echo;echo sample;echo; } >text &&
228         git commit -e -F text -a &&
229         head -n 4 .git/COMMIT_EDITMSG >actual
230 '
231
232 echo "sample
233
234 # Please enter the commit message for your changes. Lines starting
235 # with '#' will be ignored, and an empty message aborts the commit." >expect
236
237 test_expect_success C_LOCALE_OUTPUT 'cleanup commit messages (strip,-F,-e): output' '
238         test_cmp expect actual
239 '
240
241 echo "#
242 # Author:    $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
243 #" >> expect
244
245 test_expect_success C_LOCALE_OUTPUT 'author different from committer' '
246
247         echo >>negative &&
248         git commit -e -m "sample"
249         head -n 7 .git/COMMIT_EDITMSG >actual
250 '
251
252 test_expect_success C_LOCALE_OUTPUT 'author different from committer: output' '
253         test_cmp expect actual
254 '
255
256 mv expect expect.tmp
257 sed '$d' < expect.tmp > expect
258 rm -f expect.tmp
259 echo "# Committer:
260 #" >> expect
261
262 test_expect_success C_LOCALE_OUTPUT 'committer is automatic' '
263
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
273 '
274
275 test_expect_success C_LOCALE_OUTPUT 'committer is automatic: output' '
276         test_cmp expect actual
277 '
278
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
286
287 test_expect_success 'do not fire editor in the presence of conflicts' '
288
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"
311 '
312
313 pwd=`pwd`
314 cat >.git/FAKE_EDITOR <<EOF
315 #! $SHELL_PATH
316 # kill -TERM command added below.
317 EOF
318
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
327 '
328
329 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
330 git reset -q --hard
331
332 test_expect_success 'Hand committing of a redundant merge removes dups' '
333
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
340
341 '
342
343 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
344
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
349
350 '
351
352 cat >.git/FAKE_EDITOR <<EOF
353 #!$SHELL_PATH
354 mv "\$1" "\$1.orig"
355 (
356         echo message
357         cat "\$1.orig"
358 ) >"\$1"
359 EOF
360
361 echo '## Custom template' >template
362
363 clear_config () {
364         (
365                 git config --unset-all "$1"
366                 case $? in
367                 0|5)    exit 0 ;;
368                 *)      exit 1 ;;
369                 esac
370         )
371 }
372
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
383 }
384
385 try_commit_status_combo () {
386
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         '
392
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         '
398
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         '
404
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         '
410
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         '
417
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         '
424
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         '
431
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         '
438
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         '
445
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         '
452
453 }
454
455 try_commit_status_combo
456
457 use_template="-t template"
458
459 try_commit_status_combo
460
461 test_done