The sixteenth batch
[alt-git.git] / t / t6006-rev-list-format.sh
blobf1623b1c06d0fa177dd2ae05c688238b927b3b9f
1 #!/bin/sh
3 # Copyright (c) 2009 Jens Lehmann
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
6 test_description='git rev-list --pretty=format test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-terminal.sh
15 test_tick
16 # Tested non-UTF-8 encoding
17 test_encoding="ISO8859-1"
19 # String "added" in German
20 # (translated with Google Translate),
21 # encoded in UTF-8, used as a commit log message below.
22 added_utf8_part=$(printf "\303\274")
23 added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
24 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
25 added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
26 # same but "changed"
27 changed_utf8_part=$(printf "\303\244")
28 changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
29 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
30 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
32 # Count of char to truncate
33 # Number is chosen so, that non-ACSII characters
34 # (see $added_utf8_part and $changed_utf8_part)
35 # fall into truncated parts of appropriate words both from left and right
36 truncate_count=20
38 test_expect_success 'setup' '
39 : >foo &&
40 git add foo &&
41 git config i18n.commitEncoding $test_encoding &&
42 echo "$added_iso88591" | git commit -F - &&
43 head1=$(git rev-parse --verify HEAD) &&
44 head1_short=$(git rev-parse --verify --short $head1) &&
45 head1_short4=$(git rev-parse --verify --short=4 $head1) &&
46 tree1=$(git rev-parse --verify HEAD:) &&
47 tree1_short=$(git rev-parse --verify --short $tree1) &&
48 echo "$changed" > foo &&
49 echo "$changed_iso88591" | git commit -a -F - &&
50 head2=$(git rev-parse --verify HEAD) &&
51 head2_short=$(git rev-parse --verify --short $head2) &&
52 head2_short4=$(git rev-parse --verify --short=4 $head2) &&
53 tree2=$(git rev-parse --verify HEAD:) &&
54 tree2_short=$(git rev-parse --verify --short $tree2) &&
55 git config --unset i18n.commitEncoding
58 # usage: test_format [argument...] name format_string [failure] <expected_output
59 test_format () {
60 local args=
61 while true
63 case "$1" in
64 --*)
65 args="$args $1"
66 shift;;
68 break;;
69 esac
70 done
71 cat >expect.$1
72 test_expect_${3:-success} "format $1" "
73 git rev-list $args --pretty=format:'$2' main >output.$1 &&
74 test_cmp expect.$1 output.$1
78 # usage: test_pretty [argument...] name format_name [failure] <expected_output
79 test_pretty () {
80 local args=
81 while true
83 case "$1" in
84 --*)
85 args="$args $1"
86 shift;;
88 break;;
89 esac
90 done
91 cat >expect.$1
92 test_expect_${3:-success} "pretty $1 (without --no-commit-header)" "
93 git rev-list $args --pretty='$2' main >output.$1 &&
94 test_cmp expect.$1 output.$1
96 test_expect_${3:-success} "pretty $1 (with --no-commit-header)" "
97 git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
98 test_cmp expect.$1 output.$1
102 # Feed to --format to provide predictable colored sequences.
103 BASIC_COLOR='%Credfoo%Creset'
104 COLOR='%C(red)foo%C(reset)'
105 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
106 ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
107 has_color () {
108 test_decode_color <"$1" >decoded &&
109 echo "<RED>foo<RESET>" >expect &&
110 test_cmp expect decoded
113 has_no_color () {
114 echo foo >expect &&
115 test_cmp expect "$1"
118 test_format percent %%h <<EOF
119 commit $head2
121 commit $head1
125 test_format hash %H%n%h <<EOF
126 commit $head2
127 $head2
128 $head2_short
129 commit $head1
130 $head1
131 $head1_short
134 test_format --no-commit-header hash-no-header %H%n%h <<EOF
135 $head2
136 $head2_short
137 $head1
138 $head1_short
141 test_format --abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev %H%n%h <<EOF
142 $head2
143 $head2_short4
144 $head1
145 $head1_short4
148 test_format tree %T%n%t <<EOF
149 commit $head2
150 $tree2
151 $tree2_short
152 commit $head1
153 $tree1
154 $tree1_short
157 test_format parents %P%n%p <<EOF
158 commit $head2
159 $head1
160 $head1_short
161 commit $head1
166 # we don't test relative here
167 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
168 commit $head2
169 $GIT_AUTHOR_NAME
170 $GIT_AUTHOR_EMAIL
171 $TEST_AUTHOR_LOCALNAME
172 Thu Apr 7 15:13:13 2005 -0700
173 Thu, 7 Apr 2005 15:13:13 -0700
174 1112911993
175 commit $head1
176 $GIT_AUTHOR_NAME
177 $GIT_AUTHOR_EMAIL
178 $TEST_AUTHOR_LOCALNAME
179 Thu Apr 7 15:13:13 2005 -0700
180 Thu, 7 Apr 2005 15:13:13 -0700
181 1112911993
184 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
185 commit $head2
186 $GIT_COMMITTER_NAME
187 $GIT_COMMITTER_EMAIL
188 $TEST_COMMITTER_LOCALNAME
189 Thu Apr 7 15:13:13 2005 -0700
190 Thu, 7 Apr 2005 15:13:13 -0700
191 1112911993
192 commit $head1
193 $GIT_COMMITTER_NAME
194 $GIT_COMMITTER_EMAIL
195 $TEST_COMMITTER_LOCALNAME
196 Thu Apr 7 15:13:13 2005 -0700
197 Thu, 7 Apr 2005 15:13:13 -0700
198 1112911993
201 test_format encoding %e <<EOF
202 commit $head2
203 $test_encoding
204 commit $head1
205 $test_encoding
208 test_format subject %s <<EOF
209 commit $head2
210 $changed
211 commit $head1
212 $added
215 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
216 commit $head2
217 changed (ge${changed_utf8_part}ndert)..
218 commit $head1
219 added (hinzugef${added_utf8_part}gt..
222 test_format body %b <<EOF
223 commit $head2
224 commit $head1
227 test_format raw-body %B <<EOF
228 commit $head2
229 $changed
231 commit $head1
232 $added
236 test_format --no-commit-header raw-body-no-header %B <<EOF
237 $changed
239 $added
243 test_pretty oneline oneline <<EOF
244 $head2 $changed
245 $head1 $added
248 test_pretty short short <<EOF
249 commit $head2
250 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
252 $changed
254 commit $head1
255 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
257 $added
261 test_expect_success 'basic colors' '
262 cat >expect <<-EOF &&
263 commit $head2
264 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
266 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
267 git rev-list --color --format="$format" -1 main >actual.raw &&
268 test_decode_color <actual.raw >actual &&
269 test_cmp expect actual
272 test_expect_success '%S is not a placeholder for rev-list yet' '
273 git rev-list --format="%S" -1 main | grep "%S"
276 test_expect_success 'advanced colors' '
277 cat >expect <<-EOF &&
278 commit $head2
279 <BOLD;RED;BYELLOW>foo<RESET>
281 format="%C(red yellow bold)foo%C(reset)" &&
282 git rev-list --color --format="$format" -1 main >actual.raw &&
283 test_decode_color <actual.raw >actual &&
284 test_cmp expect actual
287 for spec in \
288 "%Cred:$BASIC_COLOR" \
289 "%C(...):$COLOR" \
290 "%C(auto,...):$AUTO_COLOR"
292 desc=${spec%%:*}
293 color=${spec#*:}
294 test_expect_success "$desc does not enable color by default" '
295 git log --format=$color -1 >actual &&
296 has_no_color actual
299 test_expect_success "$desc enables colors for color.diff" '
300 git -c color.diff=always log --format=$color -1 >actual &&
301 has_color actual
304 test_expect_success "$desc enables colors for color.ui" '
305 git -c color.ui=always log --format=$color -1 >actual &&
306 has_color actual
309 test_expect_success "$desc respects --color" '
310 git log --format=$color -1 --color >actual &&
311 has_color actual
314 test_expect_success "$desc respects --no-color" '
315 git -c color.ui=always log --format=$color -1 --no-color >actual &&
316 has_no_color actual
319 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
320 test_terminal git log --format=$color -1 --color=auto >actual &&
321 has_color actual
324 test_expect_success "$desc respects --color=auto (stdout not tty)" '
326 TERM=vt100 && export TERM &&
327 git log --format=$color -1 --color=auto >actual &&
328 has_no_color actual
331 done
333 test_expect_success '%C(always,...) enables color even without tty' '
334 git log --format=$ALWAYS_COLOR -1 >actual &&
335 has_color actual
338 test_expect_success '%C(auto) respects --color' '
339 git log --color --format="%C(auto)%H" -1 >actual.raw &&
340 test_decode_color <actual.raw >actual &&
341 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
342 test_cmp expect actual
345 test_expect_success '%C(auto) respects --no-color' '
346 git log --no-color --format="%C(auto)%H" -1 >actual &&
347 git rev-parse HEAD >expect &&
348 test_cmp expect actual
351 test_expect_success 'rev-list %C(auto,...) respects --color' '
352 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
353 -1 HEAD >actual.raw &&
354 test_decode_color <actual.raw >actual &&
355 cat >expect <<-EOF &&
356 commit $(git rev-parse HEAD)
357 <GREEN>foo<RESET>
359 test_cmp expect actual
362 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
363 Test printing of complex bodies
365 This commit message is much longer than the others,
366 and it will be encoded in $test_encoding. We should therefore
367 include an ISO8859 character: ¡bueno!
370 test_expect_success 'setup complex body' '
371 git config i18n.commitencoding $test_encoding &&
372 echo change2 >foo && git commit -a -F commit-msg &&
373 head3=$(git rev-parse --verify HEAD) &&
374 head3_short=$(git rev-parse --short $head3)
377 test_format complex-encoding %e <<EOF
378 commit $head3
379 $test_encoding
380 commit $head2
381 $test_encoding
382 commit $head1
383 $test_encoding
386 test_format complex-subject %s <<EOF
387 commit $head3
388 Test printing of complex bodies
389 commit $head2
390 $changed_iso88591
391 commit $head1
392 $added_iso88591
395 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
396 commit $head3
397 Test printing of c..
398 commit $head2
399 changed (ge${changed_utf8_part_iso88591}ndert)..
400 commit $head1
401 added (hinzugef${added_utf8_part_iso88591}gt..
404 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
405 commit $head3
406 Test prin..ex bodies
407 commit $head2
408 changed (..dert) foo
409 commit $head1
410 added (hi..f${added_utf8_part_iso88591}gt) foo
413 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
414 commit $head3
415 .. of complex bodies
416 commit $head2
417 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
418 commit $head1
419 .. (hinzugef${added_utf8_part_iso88591}gt) foo
422 test_expect_success 'setup expected messages (for test %b)' '
423 cat <<-EOF >expected.utf-8 &&
424 commit $head3
425 This commit message is much longer than the others,
426 and it will be encoded in $test_encoding. We should therefore
427 include an ISO8859 character: ¡bueno!
429 commit $head2
430 commit $head1
432 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
435 test_format complex-body %b <expected.ISO8859-1
437 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
438 # so unset i18n.commitEncoding to test encoding conversion
439 git config --unset i18n.commitEncoding
441 test_format complex-subject-commitencoding-unset %s <<EOF
442 commit $head3
443 Test printing of complex bodies
444 commit $head2
445 $changed
446 commit $head1
447 $added
450 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
451 commit $head3
452 Test printing of c..
453 commit $head2
454 changed (ge${changed_utf8_part}ndert)..
455 commit $head1
456 added (hinzugef${added_utf8_part}gt..
459 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
460 commit $head3
461 Test prin..ex bodies
462 commit $head2
463 changed (..dert) foo
464 commit $head1
465 added (hi..f${added_utf8_part}gt) foo
468 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
469 commit $head3
470 .. of complex bodies
471 commit $head2
472 ..ged (ge${changed_utf8_part}ndert) foo
473 commit $head1
474 .. (hinzugef${added_utf8_part}gt) foo
477 test_format complex-body-commitencoding-unset %b <expected.utf-8
479 test_expect_success '%x00 shows NUL' '
480 echo >expect commit $head3 &&
481 echo >>expect fooQbar &&
482 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
483 nul_to_q <actual.nul >actual &&
484 test_cmp expect actual
487 test_expect_success '%ad respects --date=' '
488 echo 2005-04-07 >expect.ad-short &&
489 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
490 test_cmp expect.ad-short output.ad-short
493 test_expect_success 'empty email' '
494 test_tick &&
495 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
496 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
497 test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
500 test_expect_success 'del LF before empty (1)' '
501 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
502 test_line_count = 2 actual
505 test_expect_success 'del LF before empty (2)' '
506 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
507 test_line_count = 6 actual &&
508 grep "^$" actual
511 test_expect_success 'add LF before non-empty (1)' '
512 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
513 test_line_count = 2 actual
516 test_expect_success 'add LF before non-empty (2)' '
517 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
518 test_line_count = 6 actual &&
519 grep "^$" actual
522 test_expect_success 'add SP before non-empty (1)' '
523 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
524 test $(wc -w <actual) = 3
527 test_expect_success 'add SP before non-empty (2)' '
528 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
529 test $(wc -w <actual) = 6
532 test_expect_success '--abbrev' '
533 echo SHORT SHORT SHORT >expect2 &&
534 echo LONG LONG LONG >expect3 &&
535 git log -1 --format="%h %h %h" HEAD >actual1 &&
536 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
537 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
538 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
539 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
540 test_cmp expect2 fuzzy2 &&
541 test_cmp expect3 fuzzy3 &&
542 ! test_cmp actual1 actual2
545 test_expect_success '%H is not affected by --abbrev-commit' '
546 expected=$(($(test_oid hexsz) + 1)) &&
547 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
548 len=$(wc -c <actual) &&
549 test $len = $expected
552 test_expect_success '%h is not affected by --abbrev-commit' '
553 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
554 len=$(wc -c <actual) &&
555 test $len = 21
558 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
559 git reflog >expect &&
560 git log -g --format="%h %gD: %gs" >actual &&
561 test_cmp expect actual
564 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
565 git reflog --date=raw >expect &&
566 git log -g --format="%h %gD: %gs" --date=raw >actual &&
567 test_cmp expect actual
570 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
571 git reflog --abbrev=13 --date=raw >expect &&
572 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
573 test_cmp expect actual
576 test_expect_success '%gd shortens ref name' '
577 echo "main@{0}" >expect.gd-short &&
578 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
579 test_cmp expect.gd-short actual.gd-short
582 test_expect_success 'reflog identity' '
583 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
584 git log -g -1 --format="%gn:%ge" >actual &&
585 test_cmp expect actual
588 test_expect_success 'oneline with empty message' '
589 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
590 git commit --allow-empty --allow-empty-message &&
591 git rev-list --oneline HEAD >test.txt &&
592 test_line_count = 5 test.txt &&
593 git rev-list --oneline --graph HEAD >testg.txt &&
594 test_line_count = 5 testg.txt
597 test_expect_success 'single-character name is parsed correctly' '
598 git commit --author="a <a@example.com>" --allow-empty -m foo &&
599 echo "a <a@example.com>" >expect &&
600 git log -1 --format="%an <%ae>" >actual &&
601 test_cmp expect actual
604 test_expect_success 'unused %G placeholders are passed through' '
605 echo "%GX %G" >expect &&
606 git log -1 --format="%GX %G" >actual &&
607 test_cmp expect actual
610 test_done