t7502-commit: add tests for summary output
[alt-git.git] / t / t7502-commit.sh
blob478b637b0c82fa7145bfbf4297e1fa14bd118271
1 #!/bin/sh
3 test_description='git commit porcelain-ish'
5 . ./test-lib.sh
7 # Arguments: [<prefix] [<commit message>]
8 check_summary_oneline() {
9 test_tick &&
10 git commit -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 test_cmp exp act
28 test_expect_success 'output summary format' '
30 echo new >file1 &&
31 git add file1 &&
32 check_summary_oneline "root-commit" "initial" &&
34 echo change >>file1 &&
35 git add file1 &&
36 check_summary_oneline "" "a change"
39 output_tests_cleanup() {
40 # this is needed for "do not fire editor in the presence of conflicts"
41 git checkout master &&
43 # this is needed for the "partial removal" test to pass
44 git rm file1 &&
45 git commit -m "cleanup"
48 test_expect_success 'the basics' '
50 output_tests_cleanup &&
52 echo doing partial >"commit is" &&
53 mkdir not &&
54 echo very much encouraged but we should >not/forbid &&
55 git add "commit is" not &&
56 echo update added "commit is" file >"commit is" &&
57 echo also update another >not/forbid &&
58 test_tick &&
59 git commit -a -m "initial with -a" &&
61 git cat-file blob HEAD:"commit is" >current.1 &&
62 git cat-file blob HEAD:not/forbid >current.2 &&
64 cmp current.1 "commit is" &&
65 cmp current.2 not/forbid
69 test_expect_success 'partial' '
71 echo another >"commit is" &&
72 echo another >not/forbid &&
73 test_tick &&
74 git commit -m "partial commit to handle a file" "commit is" &&
76 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
77 test "$changed" = "commit is"
81 test_expect_success 'partial modification in a subdirecotry' '
83 test_tick &&
84 git commit -m "partial commit to subdirectory" not &&
86 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
87 test "$changed" = "not/forbid"
91 test_expect_success 'partial removal' '
93 git rm not/forbid &&
94 git commit -m "partial commit to remove not/forbid" not &&
96 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
97 test "$changed" = "not/forbid" &&
98 remain=$(git ls-tree -r --name-only HEAD) &&
99 test "$remain" = "commit is"
103 test_expect_success 'sign off' '
105 >positive &&
106 git add positive &&
107 git commit -s -m "thank you" &&
108 actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
109 expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
110 test "z$actual" = "z$expected"
114 test_expect_success 'multiple -m' '
116 >negative &&
117 git add negative &&
118 git commit -m "one" -m "two" -m "three" &&
119 actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
120 expected=$(echo one; echo; echo two; echo; echo three) &&
121 test "z$actual" = "z$expected"
125 test_expect_success 'verbose' '
127 echo minus >negative &&
128 git add negative &&
129 git status -v | sed -ne "/^diff --git /p" >actual &&
130 echo "diff --git a/negative b/negative" >expect &&
131 test_cmp expect actual
135 test_expect_success 'verbose respects diff config' '
137 git config color.diff always &&
138 git status -v >actual &&
139 grep "\[1mdiff --git" actual &&
140 git config --unset color.diff
143 test_expect_success 'cleanup commit messages (verbatim,-t)' '
145 echo >>negative &&
146 { echo;echo "# text";echo; } >expect &&
147 git commit --cleanup=verbatim -t expect -a &&
148 git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
149 test_cmp expect actual
153 test_expect_success 'cleanup commit messages (verbatim,-F)' '
155 echo >>negative &&
156 git commit --cleanup=verbatim -F expect -a &&
157 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
158 test_cmp expect actual
162 test_expect_success 'cleanup commit messages (verbatim,-m)' '
164 echo >>negative &&
165 git commit --cleanup=verbatim -m "$(cat expect)" -a &&
166 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
167 test_cmp expect actual
171 test_expect_success 'cleanup commit messages (whitespace,-F)' '
173 echo >>negative &&
174 { echo;echo "# text";echo; } >text &&
175 echo "# text" >expect &&
176 git commit --cleanup=whitespace -F text -a &&
177 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
178 test_cmp expect actual
182 test_expect_success 'cleanup commit messages (strip,-F)' '
184 echo >>negative &&
185 { echo;echo "# text";echo sample;echo; } >text &&
186 echo sample >expect &&
187 git commit --cleanup=strip -F text -a &&
188 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
189 test_cmp expect actual
193 echo "sample
195 # Please enter the commit message for your changes. Lines starting
196 # with '#' will be ignored, and an empty message aborts the commit." >expect
198 test_expect_success 'cleanup commit messages (strip,-F,-e)' '
200 echo >>negative &&
201 { echo;echo sample;echo; } >text &&
202 git commit -e -F text -a &&
203 head -n 4 .git/COMMIT_EDITMSG >actual &&
204 test_cmp expect actual
208 echo "#
209 # Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
210 #" >> expect
212 test_expect_success 'author different from committer' '
214 echo >>negative &&
215 git commit -e -m "sample"
216 head -n 7 .git/COMMIT_EDITMSG >actual &&
217 test_cmp expect actual
220 mv expect expect.tmp
221 sed '$d' < expect.tmp > expect
222 rm -f expect.tmp
223 echo "# Committer:
224 #" >> expect
226 test_expect_success 'committer is automatic' '
228 echo >>negative &&
230 unset GIT_COMMITTER_EMAIL
231 unset GIT_COMMITTER_NAME
232 # must fail because there is no change
233 test_must_fail git commit -e -m "sample"
234 ) &&
235 head -n 8 .git/COMMIT_EDITMSG | \
236 sed "s/^# Committer: .*/# Committer:/" >actual &&
237 test_cmp expect actual
240 pwd=`pwd`
241 cat >> .git/FAKE_EDITOR << EOF
242 #! /bin/sh
243 echo editor started > "$pwd/.git/result"
244 exit 0
246 chmod +x .git/FAKE_EDITOR
248 test_expect_success 'do not fire editor in the presence of conflicts' '
250 git clean -f &&
251 echo f >g &&
252 git add g &&
253 git commit -m "add g" &&
254 git branch second &&
255 echo master >g &&
256 echo g >h &&
257 git add g h &&
258 git commit -m "modify g and add h" &&
259 git checkout second &&
260 echo second >g &&
261 git add g &&
262 git commit -m second &&
263 # Must fail due to conflict
264 test_must_fail git cherry-pick -n master &&
265 echo "editor not started" >.git/result &&
267 GIT_EDITOR="$(pwd)/.git/FAKE_EDITOR" &&
268 export GIT_EDITOR &&
269 test_must_fail git commit
270 ) &&
271 test "$(cat .git/result)" = "editor not started"
274 pwd=`pwd`
275 cat >.git/FAKE_EDITOR <<EOF
276 #! $SHELL_PATH
277 # kill -TERM command added below.
280 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
281 echo >>negative &&
282 ! "$SHELL_PATH" -c '\''
283 echo kill -TERM $$ >> .git/FAKE_EDITOR
284 GIT_EDITOR=.git/FAKE_EDITOR
285 export GIT_EDITOR
286 exec git commit -a'\'' &&
287 test ! -f .git/index.lock
290 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
291 git reset -q --hard
293 test_expect_success 'Hand committing of a redundant merge removes dups' '
295 git rev-parse second master >expect &&
296 test_must_fail git merge second master &&
297 git checkout master g &&
298 EDITOR=: git commit -a &&
299 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
300 test_cmp expect actual
304 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
306 git reset --hard &&
307 git commit -s -m "hello: kitty" --allow-empty &&
308 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
309 test $(wc -l <actual) = 3
313 cat >.git/FAKE_EDITOR <<EOF
314 #!$SHELL_PATH
315 mv "\$1" "\$1.orig"
317 echo message
318 cat "\$1.orig"
319 ) >"\$1"
322 echo '## Custom template' >template
324 clear_config () {
326 git config --unset-all "$1"
327 case $? in
328 0|5) exit 0 ;;
329 *) exit 1 ;;
330 esac
334 try_commit () {
335 git reset --hard &&
336 echo >>negative &&
337 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
338 case "$use_template" in
340 ! grep "^## Custom template" .git/COMMIT_EDITMSG ;;
342 grep "^## Custom template" .git/COMMIT_EDITMSG ;;
343 esac
346 try_commit_status_combo () {
348 test_expect_success 'commit' '
349 clear_config commit.status &&
350 try_commit "" &&
351 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
354 test_expect_success 'commit' '
355 clear_config commit.status &&
356 try_commit "" &&
357 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
360 test_expect_success 'commit --status' '
361 clear_config commit.status &&
362 try_commit --status &&
363 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
366 test_expect_success 'commit --no-status' '
367 clear_config commit.status &&
368 try_commit --no-status
369 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
372 test_expect_success 'commit with commit.status = yes' '
373 clear_config commit.status &&
374 git config commit.status yes &&
375 try_commit "" &&
376 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
379 test_expect_success 'commit with commit.status = no' '
380 clear_config commit.status &&
381 git config commit.status no &&
382 try_commit "" &&
383 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
386 test_expect_success 'commit --status with commit.status = yes' '
387 clear_config commit.status &&
388 git config commit.status yes &&
389 try_commit --status &&
390 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
393 test_expect_success 'commit --no-status with commit.status = yes' '
394 clear_config commit.status &&
395 git config commit.status yes &&
396 try_commit --no-status &&
397 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
400 test_expect_success 'commit --status with commit.status = no' '
401 clear_config commit.status &&
402 git config commit.status no &&
403 try_commit --status &&
404 grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
407 test_expect_success 'commit --no-status with commit.status = no' '
408 clear_config commit.status &&
409 git config commit.status no &&
410 try_commit --no-status &&
411 ! grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
416 try_commit_status_combo
418 use_template="-t template"
420 try_commit_status_combo
422 test_done