1#!/bin/sh
2
3test_description='git commit porcelain-ish'
4
5. ./test-lib.sh
6
7# Arguments: [<prefix] [<commit message>] [<commit options>]
8check_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
31test_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
41test_expect_success 'output summary format: root-commit' '
42 check_summary_oneline "" "a change"
43'
44
45test_expect_success 'output summary format for commit with an empty diff' '
46
47 check_summary_oneline "" "empty" "--allow-empty"
48'
49
50test_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
70output_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
79test_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
100test_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
112test_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
122test_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
134test_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
145test_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
156test_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
166test_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
174test_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
184test_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
193test_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
202test_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
213test_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
224echo "sample
225
226# Please enter the commit message for your changes. Lines starting
227# with '#' will be ignored, and an empty message aborts the commit." >expect
228
229test_expect_success 'cleanup commit messages (strip,-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 test_cmp expect actual
236
237'
238
239echo "#
240# Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
241#" >> expect
242
243test_expect_success C_LOCALE_OUTPUT 'author different from committer' '
244
245 echo >>negative &&
246 git commit -e -m "sample"
247 head -n 7 .git/COMMIT_EDITMSG >actual &&
248 test_cmp expect actual
249'
250
251mv expect expect.tmp
252sed '$d' < expect.tmp > expect
253rm -f expect.tmp
254echo "# Committer:
255#" >> expect
256
257test_expect_success C_LOCALE_OUTPUT 'committer is automatic' '
258
259 echo >>negative &&
260 (
261 sane_unset GIT_COMMITTER_EMAIL &&
262 sane_unset GIT_COMMITTER_NAME &&
263 # must fail because there is no change
264 test_must_fail git commit -e -m "sample"
265 ) &&
266 head -n 8 .git/COMMIT_EDITMSG | \
267 sed "s/^# Committer: .*/# Committer:/" >actual &&
268 test_cmp expect actual
269'
270
271pwd=`pwd`
272cat >> .git/FAKE_EDITOR << EOF
273#! /bin/sh
274echo editor started > "$pwd/.git/result"
275exit 0
276EOF
277chmod +x .git/FAKE_EDITOR
278
279test_expect_success 'do not fire editor in the presence of conflicts' '
280
281 git clean -f &&
282 echo f >g &&
283 git add g &&
284 git commit -m "add g" &&
285 git branch second &&
286 echo master >g &&
287 echo g >h &&
288 git add g h &&
289 git commit -m "modify g and add h" &&
290 git checkout second &&
291 echo second >g &&
292 git add g &&
293 git commit -m second &&
294 # Must fail due to conflict
295 test_must_fail git cherry-pick -n master &&
296 echo "editor not started" >.git/result &&
297 (
298 GIT_EDITOR="$(pwd)/.git/FAKE_EDITOR" &&
299 export GIT_EDITOR &&
300 test_must_fail git commit
301 ) &&
302 test "$(cat .git/result)" = "editor not started"
303'
304
305pwd=`pwd`
306cat >.git/FAKE_EDITOR <<EOF
307#! $SHELL_PATH
308# kill -TERM command added below.
309EOF
310
311test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
312 echo >>negative &&
313 ! "$SHELL_PATH" -c '\''
314 echo kill -TERM $$ >> .git/FAKE_EDITOR
315 GIT_EDITOR=.git/FAKE_EDITOR
316 export GIT_EDITOR
317 exec git commit -a'\'' &&
318 test ! -f .git/index.lock
319'
320
321rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
322git reset -q --hard
323
324test_expect_success 'Hand committing of a redundant merge removes dups' '
325
326 git rev-parse second master >expect &&
327 test_must_fail git merge second master &&
328 git checkout master g &&
329 EDITOR=: git commit -a &&
330 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
331 test_cmp expect actual
332
333'
334
335test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
336
337 git reset --hard &&
338 git commit -s -m "hello: kitty" --allow-empty &&
339 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
340 test $(wc -l <actual) = 3
341
342'
343
344cat >.git/FAKE_EDITOR <<EOF
345#!$SHELL_PATH
346mv "\$1" "\$1.orig"
347(
348 echo message
349 cat "\$1.orig"
350) >"\$1"
351EOF
352
353echo '## Custom template' >template
354
355clear_config () {
356 (
357 git config --unset-all "$1"
358 case $? in
359 0|5) exit 0 ;;
360 *) exit 1 ;;
361 esac
362 )
363}
364
365try_commit () {
366 git reset --hard &&
367 echo >>negative &&
368 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
369 case "$use_template" in
370 '')
371 ! grep "^## Custom template" .git/COMMIT_EDITMSG ;;
372 *)
373 grep "^## Custom template" .git/COMMIT_EDITMSG ;;
374 esac
375}
376
377try_commit_status_combo () {
378
379 test_expect_success 'commit' '
380 clear_config commit.status &&
381 try_commit "" &&
382 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
383 '
384
385 test_expect_success 'commit' '
386 clear_config commit.status &&
387 try_commit "" &&
388 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
389 '
390
391 test_expect_success 'commit --status' '
392 clear_config commit.status &&
393 try_commit --status &&
394 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
395 '
396
397 test_expect_success 'commit --no-status' '
398 clear_config commit.status &&
399 try_commit --no-status &&
400 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
401 '
402
403 test_expect_success 'commit with commit.status = yes' '
404 clear_config commit.status &&
405 git config commit.status yes &&
406 try_commit "" &&
407 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
408 '
409
410 test_expect_success 'commit with commit.status = no' '
411 clear_config commit.status &&
412 git config commit.status no &&
413 try_commit "" &&
414 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
415 '
416
417 test_expect_success 'commit --status with commit.status = yes' '
418 clear_config commit.status &&
419 git config commit.status yes &&
420 try_commit --status &&
421 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
422 '
423
424 test_expect_success 'commit --no-status with commit.status = yes' '
425 clear_config commit.status &&
426 git config commit.status yes &&
427 try_commit --no-status &&
428 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
429 '
430
431 test_expect_success 'commit --status with commit.status = no' '
432 clear_config commit.status &&
433 git config commit.status no &&
434 try_commit --status &&
435 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
436 '
437
438 test_expect_success 'commit --no-status with commit.status = no' '
439 clear_config commit.status &&
440 git config commit.status no &&
441 try_commit --no-status &&
442 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
443 '
444
445}
446
447try_commit_status_combo
448
449use_template="-t template"
450
451try_commit_status_combo
452
453test_done