i18n: git-commit print_summary messages
[alt-git.git] / t / t7502-commit.sh
blobdad1b2b80d0754001adde2e09b2a331d0e4207bf
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 if test_have_prereq C_LOCALE_OUTPUT
26 then
27 test_cmp exp act
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
41 test_expect_success 'output summary format: root-commit' '
42 check_summary_oneline "" "a change"
45 test_expect_success 'output summary format for commit with an empty diff' '
47 check_summary_oneline "" "empty" "--allow-empty"
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"
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"
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
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 echo "sample
226 # Please enter the commit message for your changes. Lines starting
227 # with '#' will be ignored, and an empty message aborts the commit." >expect
229 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
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
239 echo "#
240 # Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
241 #" >> expect
243 test_expect_success C_LOCALE_OUTPUT 'author different from committer' '
245 echo >>negative &&
246 git commit -e -m "sample"
247 head -n 7 .git/COMMIT_EDITMSG >actual &&
248 test_cmp expect actual
251 mv expect expect.tmp
252 sed '$d' < expect.tmp > expect
253 rm -f expect.tmp
254 echo "# Committer:
255 #" >> expect
257 test_expect_success C_LOCALE_OUTPUT 'committer is automatic' '
259 echo >>negative &&
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
271 pwd=`pwd`
272 cat >> .git/FAKE_EDITOR << EOF
273 #! /bin/sh
274 echo editor started > "$pwd/.git/result"
275 exit 0
277 chmod +x .git/FAKE_EDITOR
279 test_expect_success 'do not fire editor in the presence of conflicts' '
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 &&
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"
305 pwd=`pwd`
306 cat >.git/FAKE_EDITOR <<EOF
307 #! $SHELL_PATH
308 # kill -TERM command added below.
311 test_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
321 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
322 git reset -q --hard
324 test_expect_success 'Hand committing of a redundant merge removes dups' '
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
335 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
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
344 cat >.git/FAKE_EDITOR <<EOF
345 #!$SHELL_PATH
346 mv "\$1" "\$1.orig"
348 echo message
349 cat "\$1.orig"
350 ) >"\$1"
353 echo '## Custom template' >template
355 clear_config () {
357 git config --unset-all "$1"
358 case $? in
359 0|5) exit 0 ;;
360 *) exit 1 ;;
361 esac
365 try_commit () {
366 git reset --hard &&
367 echo >>negative &&
368 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
369 case "$use_template" in
371 ! grep "^## Custom template" .git/COMMIT_EDITMSG ;;
373 grep "^## Custom template" .git/COMMIT_EDITMSG ;;
374 esac
377 try_commit_status_combo () {
379 test_expect_success 'commit' '
380 clear_config commit.status &&
381 try_commit "" &&
382 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
385 test_expect_success 'commit' '
386 clear_config commit.status &&
387 try_commit "" &&
388 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
391 test_expect_success 'commit --status' '
392 clear_config commit.status &&
393 try_commit --status &&
394 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
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
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
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
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
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
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
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
447 try_commit_status_combo
449 use_template="-t template"
451 try_commit_status_combo
453 test_done