Merge branch 'tc/commit-abbrev-fix' into maint
[git/git.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 test_cmp exp act
26 }
27
28 test_expect_success 'output summary format' '
29
30 echo new >file1 &&
31 git add file1 &&
32 check_summary_oneline "root-commit" "initial" &&
33
34 echo change >>file1 &&
35 git add file1 &&
36 check_summary_oneline "" "a change"
37 '
38
39 test_expect_success 'output summary format for commit with an empty diff' '
40
41 check_summary_oneline "" "empty" "--allow-empty"
42 '
43
44 test_expect_success 'output summary format for merges' '
45
46 git checkout -b recursive-base &&
47 test_commit base file1 &&
48
49 git checkout -b recursive-a recursive-base &&
50 test_commit commit-a file1 &&
51
52 git checkout -b recursive-b recursive-base &&
53 test_commit commit-b file1 &&
54
55 # conflict
56 git checkout recursive-a &&
57 test_must_fail git merge recursive-b &&
58 # resolve the conflict
59 echo commit-a > file1 &&
60 git add file1 &&
61 check_summary_oneline "" "Merge"
62 '
63
64 output_tests_cleanup() {
65 # this is needed for "do not fire editor in the presence of conflicts"
66 git checkout master &&
67
68 # this is needed for the "partial removal" test to pass
69 git rm file1 &&
70 git commit -m "cleanup"
71 }
72
73 test_expect_success 'the basics' '
74
75 output_tests_cleanup &&
76
77 echo doing partial >"commit is" &&
78 mkdir not &&
79 echo very much encouraged but we should >not/forbid &&
80 git add "commit is" not &&
81 echo update added "commit is" file >"commit is" &&
82 echo also update another >not/forbid &&
83 test_tick &&
84 git commit -a -m "initial with -a" &&
85
86 git cat-file blob HEAD:"commit is" >current.1 &&
87 git cat-file blob HEAD:not/forbid >current.2 &&
88
89 cmp current.1 "commit is" &&
90 cmp current.2 not/forbid
91
92 '
93
94 test_expect_success 'partial' '
95
96 echo another >"commit is" &&
97 echo another >not/forbid &&
98 test_tick &&
99 git commit -m "partial commit to handle a file" "commit is" &&
100
101 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
102 test "$changed" = "commit is"
103
104 '
105
106 test_expect_success 'partial modification in a subdirectory' '
107
108 test_tick &&
109 git commit -m "partial commit to subdirectory" not &&
110
111 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
112 test "$changed" = "not/forbid"
113
114 '
115
116 test_expect_success 'partial removal' '
117
118 git rm not/forbid &&
119 git commit -m "partial commit to remove not/forbid" not &&
120
121 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
122 test "$changed" = "not/forbid" &&
123 remain=$(git ls-tree -r --name-only HEAD) &&
124 test "$remain" = "commit is"
125
126 '
127
128 test_expect_success 'sign off' '
129
130 >positive &&
131 git add positive &&
132 git commit -s -m "thank you" &&
133 actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
134 expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
135 test "z$actual" = "z$expected"
136
137 '
138
139 test_expect_success 'multiple -m' '
140
141 >negative &&
142 git add negative &&
143 git commit -m "one" -m "two" -m "three" &&
144 actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
145 expected=$(echo one; echo; echo two; echo; echo three) &&
146 test "z$actual" = "z$expected"
147
148 '
149
150 test_expect_success 'verbose' '
151
152 echo minus >negative &&
153 git add negative &&
154 git status -v | sed -ne "/^diff --git /p" >actual &&
155 echo "diff --git a/negative b/negative" >expect &&
156 test_cmp expect actual
157
158 '
159
160 test_expect_success 'verbose respects diff config' '
161
162 git config color.diff always &&
163 git status -v >actual &&
164 grep "\[1mdiff --git" actual &&
165 git config --unset color.diff
166 '
167
168 test_expect_success 'cleanup commit messages (verbatim,-t)' '
169
170 echo >>negative &&
171 { echo;echo "# text";echo; } >expect &&
172 git commit --cleanup=verbatim -t expect -a &&
173 git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
174 test_cmp expect actual
175
176 '
177
178 test_expect_success 'cleanup commit messages (verbatim,-F)' '
179
180 echo >>negative &&
181 git commit --cleanup=verbatim -F expect -a &&
182 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
183 test_cmp expect actual
184
185 '
186
187 test_expect_success 'cleanup commit messages (verbatim,-m)' '
188
189 echo >>negative &&
190 git commit --cleanup=verbatim -m "$(cat expect)" -a &&
191 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
192 test_cmp expect actual
193
194 '
195
196 test_expect_success 'cleanup commit messages (whitespace,-F)' '
197
198 echo >>negative &&
199 { echo;echo "# text";echo; } >text &&
200 echo "# text" >expect &&
201 git commit --cleanup=whitespace -F text -a &&
202 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
203 test_cmp expect actual
204
205 '
206
207 test_expect_success 'cleanup commit messages (strip,-F)' '
208
209 echo >>negative &&
210 { echo;echo "# text";echo sample;echo; } >text &&
211 echo sample >expect &&
212 git commit --cleanup=strip -F text -a &&
213 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
214 test_cmp expect actual
215
216 '
217
218 echo "sample
219
220 # Please enter the commit message for your changes. Lines starting
221 # with '#' will be ignored, and an empty message aborts the commit." >expect
222
223 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
224
225 echo >>negative &&
226 { echo;echo sample;echo; } >text &&
227 git commit -e -F text -a &&
228 head -n 4 .git/COMMIT_EDITMSG >actual &&
229 test_cmp expect actual
230
231 '
232
233 echo "#
234 # Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
235 #" >> expect
236
237 test_expect_success 'author different from committer' '
238
239 echo >>negative &&
240 git commit -e -m "sample"
241 head -n 7 .git/COMMIT_EDITMSG >actual &&
242 test_cmp expect actual
243 '
244
245 mv expect expect.tmp
246 sed '$d' < expect.tmp > expect
247 rm -f expect.tmp
248 echo "# Committer:
249 #" >> expect
250
251 test_expect_success 'committer is automatic' '
252
253 echo >>negative &&
254 (
255 unset GIT_COMMITTER_EMAIL
256 unset GIT_COMMITTER_NAME
257 # must fail because there is no change
258 test_must_fail git commit -e -m "sample"
259 ) &&
260 head -n 8 .git/COMMIT_EDITMSG | \
261 sed "s/^# Committer: .*/# Committer:/" >actual &&
262 test_cmp expect actual
263 '
264
265 pwd=`pwd`
266 cat >> .git/FAKE_EDITOR << EOF
267 #! /bin/sh
268 echo editor started > "$pwd/.git/result"
269 exit 0
270 EOF
271 chmod +x .git/FAKE_EDITOR
272
273 test_expect_success 'do not fire editor in the presence of conflicts' '
274
275 git clean -f &&
276 echo f >g &&
277 git add g &&
278 git commit -m "add g" &&
279 git branch second &&
280 echo master >g &&
281 echo g >h &&
282 git add g h &&
283 git commit -m "modify g and add h" &&
284 git checkout second &&
285 echo second >g &&
286 git add g &&
287 git commit -m second &&
288 # Must fail due to conflict
289 test_must_fail git cherry-pick -n master &&
290 echo "editor not started" >.git/result &&
291 (
292 GIT_EDITOR="$(pwd)/.git/FAKE_EDITOR" &&
293 export GIT_EDITOR &&
294 test_must_fail git commit
295 ) &&
296 test "$(cat .git/result)" = "editor not started"
297 '
298
299 pwd=`pwd`
300 cat >.git/FAKE_EDITOR <<EOF
301 #! $SHELL_PATH
302 # kill -TERM command added below.
303 EOF
304
305 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
306 echo >>negative &&
307 ! "$SHELL_PATH" -c '\''
308 echo kill -TERM $$ >> .git/FAKE_EDITOR
309 GIT_EDITOR=.git/FAKE_EDITOR
310 export GIT_EDITOR
311 exec git commit -a'\'' &&
312 test ! -f .git/index.lock
313 '
314
315 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
316 git reset -q --hard
317
318 test_expect_success 'Hand committing of a redundant merge removes dups' '
319
320 git rev-parse second master >expect &&
321 test_must_fail git merge second master &&
322 git checkout master g &&
323 EDITOR=: git commit -a &&
324 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
325 test_cmp expect actual
326
327 '
328
329 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
330
331 git reset --hard &&
332 git commit -s -m "hello: kitty" --allow-empty &&
333 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
334 test $(wc -l <actual) = 3
335
336 '
337
338 cat >.git/FAKE_EDITOR <<EOF
339 #!$SHELL_PATH
340 mv "\$1" "\$1.orig"
341 (
342 echo message
343 cat "\$1.orig"
344 ) >"\$1"
345 EOF
346
347 echo '## Custom template' >template
348
349 clear_config () {
350 (
351 git config --unset-all "$1"
352 case $? in
353 0|5) exit 0 ;;
354 *) exit 1 ;;
355 esac
356 )
357 }
358
359 try_commit () {
360 git reset --hard &&
361 echo >>negative &&
362 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
363 case "$use_template" in
364 '')
365 ! grep "^## Custom template" .git/COMMIT_EDITMSG ;;
366 *)
367 grep "^## Custom template" .git/COMMIT_EDITMSG ;;
368 esac
369 }
370
371 try_commit_status_combo () {
372
373 test_expect_success 'commit' '
374 clear_config commit.status &&
375 try_commit "" &&
376 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
377 '
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 --status' '
386 clear_config commit.status &&
387 try_commit --status &&
388 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
389 '
390
391 test_expect_success 'commit --no-status' '
392 clear_config commit.status &&
393 try_commit --no-status
394 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
395 '
396
397 test_expect_success 'commit with commit.status = yes' '
398 clear_config commit.status &&
399 git config commit.status yes &&
400 try_commit "" &&
401 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
402 '
403
404 test_expect_success 'commit with commit.status = no' '
405 clear_config commit.status &&
406 git config commit.status no &&
407 try_commit "" &&
408 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
409 '
410
411 test_expect_success 'commit --status with commit.status = yes' '
412 clear_config commit.status &&
413 git config commit.status yes &&
414 try_commit --status &&
415 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
416 '
417
418 test_expect_success 'commit --no-status with commit.status = yes' '
419 clear_config commit.status &&
420 git config commit.status yes &&
421 try_commit --no-status &&
422 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
423 '
424
425 test_expect_success 'commit --status with commit.status = no' '
426 clear_config commit.status &&
427 git config commit.status no &&
428 try_commit --status &&
429 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
430 '
431
432 test_expect_success 'commit --no-status with commit.status = no' '
433 clear_config commit.status &&
434 git config commit.status no &&
435 try_commit --no-status &&
436 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
437 '
438
439 }
440
441 try_commit_status_combo
442
443 use_template="-t template"
444
445 try_commit_status_combo
446
447 test_done