Sync with 2.36.6
[git/debian.git] / t / t6006-rev-list-format.sh
blob41d0ca00b1c025beaadedf195586fb81db835bbb
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-lib.sh
12 . "$TEST_DIRECTORY"/lib-terminal.sh
14 test_tick
15 # Tested non-UTF-8 encoding
16 test_encoding="ISO8859-1"
18 # String "added" in German
19 # (translated with Google Translate),
20 # encoded in UTF-8, used as a commit log message below.
21 added_utf8_part=$(printf "\303\274")
22 added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
23 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
24 added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
25 # same but "changed"
26 changed_utf8_part=$(printf "\303\244")
27 changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
28 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
29 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
31 # Count of char to truncate
32 # Number is chosen so, that non-ACSII characters
33 # (see $added_utf8_part and $changed_utf8_part)
34 # fall into truncated parts of appropriate words both from left and right
35 truncate_count=20
37 test_expect_success 'setup' '
38 : >foo &&
39 git add foo &&
40 git config i18n.commitEncoding $test_encoding &&
41 echo "$added_iso88591" | git commit -F - &&
42 head1=$(git rev-parse --verify HEAD) &&
43 head1_short=$(git rev-parse --verify --short $head1) &&
44 head1_short4=$(git rev-parse --verify --short=4 $head1) &&
45 tree1=$(git rev-parse --verify HEAD:) &&
46 tree1_short=$(git rev-parse --verify --short $tree1) &&
47 echo "$changed" > foo &&
48 echo "$changed_iso88591" | git commit -a -F - &&
49 head2=$(git rev-parse --verify HEAD) &&
50 head2_short=$(git rev-parse --verify --short $head2) &&
51 head2_short4=$(git rev-parse --verify --short=4 $head2) &&
52 tree2=$(git rev-parse --verify HEAD:) &&
53 tree2_short=$(git rev-parse --verify --short $tree2) &&
54 git config --unset i18n.commitEncoding
57 # usage: test_format [argument...] name format_string [failure] <expected_output
58 test_format () {
59 local args=
60 while true
62 case "$1" in
63 --*)
64 args="$args $1"
65 shift;;
67 break;;
68 esac
69 done
70 cat >expect.$1
71 test_expect_${3:-success} "format $1" "
72 git rev-list $args --pretty=format:'$2' main >output.$1 &&
73 test_cmp expect.$1 output.$1
77 # usage: test_pretty [argument...] name format_name [failure] <expected_output
78 test_pretty () {
79 local args=
80 while true
82 case "$1" in
83 --*)
84 args="$args $1"
85 shift;;
87 break;;
88 esac
89 done
90 cat >expect.$1
91 test_expect_${3:-success} "pretty $1 (without --no-commit-header)" "
92 git rev-list $args --pretty='$2' main >output.$1 &&
93 test_cmp expect.$1 output.$1
95 test_expect_${3:-success} "pretty $1 (with --no-commit-header)" "
96 git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
97 test_cmp expect.$1 output.$1
101 # Feed to --format to provide predictable colored sequences.
102 BASIC_COLOR='%Credfoo%Creset'
103 COLOR='%C(red)foo%C(reset)'
104 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
105 ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
106 has_color () {
107 test_decode_color <"$1" >decoded &&
108 echo "<RED>foo<RESET>" >expect &&
109 test_cmp expect decoded
112 has_no_color () {
113 echo foo >expect &&
114 test_cmp expect "$1"
117 test_format percent %%h <<EOF
118 commit $head2
120 commit $head1
124 test_format hash %H%n%h <<EOF
125 commit $head2
126 $head2
127 $head2_short
128 commit $head1
129 $head1
130 $head1_short
133 test_format --no-commit-header hash-no-header %H%n%h <<EOF
134 $head2
135 $head2_short
136 $head1
137 $head1_short
140 test_format --abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev %H%n%h <<EOF
141 $head2
142 $head2_short4
143 $head1
144 $head1_short4
147 test_format tree %T%n%t <<EOF
148 commit $head2
149 $tree2
150 $tree2_short
151 commit $head1
152 $tree1
153 $tree1_short
156 test_format parents %P%n%p <<EOF
157 commit $head2
158 $head1
159 $head1_short
160 commit $head1
165 # we don't test relative here
166 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
167 commit $head2
168 $GIT_AUTHOR_NAME
169 $GIT_AUTHOR_EMAIL
170 $TEST_AUTHOR_LOCALNAME
171 Thu Apr 7 15:13:13 2005 -0700
172 Thu, 7 Apr 2005 15:13:13 -0700
173 1112911993
174 commit $head1
175 $GIT_AUTHOR_NAME
176 $GIT_AUTHOR_EMAIL
177 $TEST_AUTHOR_LOCALNAME
178 Thu Apr 7 15:13:13 2005 -0700
179 Thu, 7 Apr 2005 15:13:13 -0700
180 1112911993
183 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
184 commit $head2
185 $GIT_COMMITTER_NAME
186 $GIT_COMMITTER_EMAIL
187 $TEST_COMMITTER_LOCALNAME
188 Thu Apr 7 15:13:13 2005 -0700
189 Thu, 7 Apr 2005 15:13:13 -0700
190 1112911993
191 commit $head1
192 $GIT_COMMITTER_NAME
193 $GIT_COMMITTER_EMAIL
194 $TEST_COMMITTER_LOCALNAME
195 Thu Apr 7 15:13:13 2005 -0700
196 Thu, 7 Apr 2005 15:13:13 -0700
197 1112911993
200 test_format encoding %e <<EOF
201 commit $head2
202 $test_encoding
203 commit $head1
204 $test_encoding
207 test_format subject %s <<EOF
208 commit $head2
209 $changed
210 commit $head1
211 $added
214 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
215 commit $head2
216 changed (ge${changed_utf8_part}ndert)..
217 commit $head1
218 added (hinzugef${added_utf8_part}gt..
221 test_format body %b <<EOF
222 commit $head2
223 commit $head1
226 test_format raw-body %B <<EOF
227 commit $head2
228 $changed
230 commit $head1
231 $added
235 test_format --no-commit-header raw-body-no-header %B <<EOF
236 $changed
238 $added
242 test_pretty oneline oneline <<EOF
243 $head2 $changed
244 $head1 $added
247 test_pretty short short <<EOF
248 commit $head2
249 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
251 $changed
253 commit $head1
254 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
256 $added
260 test_expect_success 'basic colors' '
261 cat >expect <<-EOF &&
262 commit $head2
263 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
265 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
266 git rev-list --color --format="$format" -1 main >actual.raw &&
267 test_decode_color <actual.raw >actual &&
268 test_cmp expect actual
271 test_expect_success '%S is not a placeholder for rev-list yet' '
272 git rev-list --format="%S" -1 main | grep "%S"
275 test_expect_success 'advanced colors' '
276 cat >expect <<-EOF &&
277 commit $head2
278 <BOLD;RED;BYELLOW>foo<RESET>
280 format="%C(red yellow bold)foo%C(reset)" &&
281 git rev-list --color --format="$format" -1 main >actual.raw &&
282 test_decode_color <actual.raw >actual &&
283 test_cmp expect actual
286 for spec in \
287 "%Cred:$BASIC_COLOR" \
288 "%C(...):$COLOR" \
289 "%C(auto,...):$AUTO_COLOR"
291 desc=${spec%%:*}
292 color=${spec#*:}
293 test_expect_success "$desc does not enable color by default" '
294 git log --format=$color -1 >actual &&
295 has_no_color actual
298 test_expect_success "$desc enables colors for color.diff" '
299 git -c color.diff=always log --format=$color -1 >actual &&
300 has_color actual
303 test_expect_success "$desc enables colors for color.ui" '
304 git -c color.ui=always log --format=$color -1 >actual &&
305 has_color actual
308 test_expect_success "$desc respects --color" '
309 git log --format=$color -1 --color >actual &&
310 has_color actual
313 test_expect_success "$desc respects --no-color" '
314 git -c color.ui=always log --format=$color -1 --no-color >actual &&
315 has_no_color actual
318 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
319 test_terminal git log --format=$color -1 --color=auto >actual &&
320 has_color actual
323 test_expect_success "$desc respects --color=auto (stdout not tty)" '
325 TERM=vt100 && export TERM &&
326 git log --format=$color -1 --color=auto >actual &&
327 has_no_color actual
330 done
332 test_expect_success '%C(always,...) enables color even without tty' '
333 git log --format=$ALWAYS_COLOR -1 >actual &&
334 has_color actual
337 test_expect_success '%C(auto) respects --color' '
338 git log --color --format="%C(auto)%H" -1 >actual.raw &&
339 test_decode_color <actual.raw >actual &&
340 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
341 test_cmp expect actual
344 test_expect_success '%C(auto) respects --no-color' '
345 git log --no-color --format="%C(auto)%H" -1 >actual &&
346 git rev-parse HEAD >expect &&
347 test_cmp expect actual
350 test_expect_success 'rev-list %C(auto,...) respects --color' '
351 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
352 -1 HEAD >actual.raw &&
353 test_decode_color <actual.raw >actual &&
354 cat >expect <<-EOF &&
355 commit $(git rev-parse HEAD)
356 <GREEN>foo<RESET>
358 test_cmp expect actual
361 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
362 Test printing of complex bodies
364 This commit message is much longer than the others,
365 and it will be encoded in $test_encoding. We should therefore
366 include an ISO8859 character: ¡bueno!
369 test_expect_success 'setup complex body' '
370 git config i18n.commitencoding $test_encoding &&
371 echo change2 >foo && git commit -a -F commit-msg &&
372 head3=$(git rev-parse --verify HEAD) &&
373 head3_short=$(git rev-parse --short $head3)
376 test_format complex-encoding %e <<EOF
377 commit $head3
378 $test_encoding
379 commit $head2
380 $test_encoding
381 commit $head1
382 $test_encoding
385 test_format complex-subject %s <<EOF
386 commit $head3
387 Test printing of complex bodies
388 commit $head2
389 $changed_iso88591
390 commit $head1
391 $added_iso88591
394 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
395 commit $head3
396 Test printing of c..
397 commit $head2
398 changed (ge${changed_utf8_part_iso88591}ndert)..
399 commit $head1
400 added (hinzugef${added_utf8_part_iso88591}gt..
403 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
404 commit $head3
405 Test prin..ex bodies
406 commit $head2
407 changed (..dert) foo
408 commit $head1
409 added (hi..f${added_utf8_part_iso88591}gt) foo
412 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
413 commit $head3
414 .. of complex bodies
415 commit $head2
416 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
417 commit $head1
418 .. (hinzugef${added_utf8_part_iso88591}gt) foo
421 test_expect_success 'setup expected messages (for test %b)' '
422 cat <<-EOF >expected.utf-8 &&
423 commit $head3
424 This commit message is much longer than the others,
425 and it will be encoded in $test_encoding. We should therefore
426 include an ISO8859 character: ¡bueno!
428 commit $head2
429 commit $head1
431 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
434 test_format complex-body %b <expected.ISO8859-1
436 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
437 # so unset i18n.commitEncoding to test encoding conversion
438 git config --unset i18n.commitEncoding
440 test_format complex-subject-commitencoding-unset %s <<EOF
441 commit $head3
442 Test printing of complex bodies
443 commit $head2
444 $changed
445 commit $head1
446 $added
449 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
450 commit $head3
451 Test printing of c..
452 commit $head2
453 changed (ge${changed_utf8_part}ndert)..
454 commit $head1
455 added (hinzugef${added_utf8_part}gt..
458 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
459 commit $head3
460 Test prin..ex bodies
461 commit $head2
462 changed (..dert) foo
463 commit $head1
464 added (hi..f${added_utf8_part}gt) foo
467 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
468 commit $head3
469 .. of complex bodies
470 commit $head2
471 ..ged (ge${changed_utf8_part}ndert) foo
472 commit $head1
473 .. (hinzugef${added_utf8_part}gt) foo
476 test_format complex-body-commitencoding-unset %b <expected.utf-8
478 test_expect_success '%x00 shows NUL' '
479 echo >expect commit $head3 &&
480 echo >>expect fooQbar &&
481 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
482 nul_to_q <actual.nul >actual &&
483 test_cmp expect actual
486 test_expect_success '%ad respects --date=' '
487 echo 2005-04-07 >expect.ad-short &&
488 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
489 test_cmp expect.ad-short output.ad-short
492 test_expect_success 'empty email' '
493 test_tick &&
494 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
495 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
496 verbose test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
499 test_expect_success 'del LF before empty (1)' '
500 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
501 test_line_count = 2 actual
504 test_expect_success 'del LF before empty (2)' '
505 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
506 test_line_count = 6 actual &&
507 grep "^$" actual
510 test_expect_success 'add LF before non-empty (1)' '
511 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
512 test_line_count = 2 actual
515 test_expect_success 'add LF before non-empty (2)' '
516 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
517 test_line_count = 6 actual &&
518 grep "^$" actual
521 test_expect_success 'add SP before non-empty (1)' '
522 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
523 test $(wc -w <actual) = 3
526 test_expect_success 'add SP before non-empty (2)' '
527 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
528 test $(wc -w <actual) = 6
531 test_expect_success '--abbrev' '
532 echo SHORT SHORT SHORT >expect2 &&
533 echo LONG LONG LONG >expect3 &&
534 git log -1 --format="%h %h %h" HEAD >actual1 &&
535 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
536 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
537 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
538 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
539 test_cmp expect2 fuzzy2 &&
540 test_cmp expect3 fuzzy3 &&
541 ! test_cmp actual1 actual2
544 test_expect_success '%H is not affected by --abbrev-commit' '
545 expected=$(($(test_oid hexsz) + 1)) &&
546 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
547 len=$(wc -c <actual) &&
548 test $len = $expected
551 test_expect_success '%h is not affected by --abbrev-commit' '
552 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
553 len=$(wc -c <actual) &&
554 test $len = 21
557 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
558 git reflog >expect &&
559 git log -g --format="%h %gD: %gs" >actual &&
560 test_cmp expect actual
563 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
564 git reflog --date=raw >expect &&
565 git log -g --format="%h %gD: %gs" --date=raw >actual &&
566 test_cmp expect actual
569 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
570 git reflog --abbrev=13 --date=raw >expect &&
571 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
572 test_cmp expect actual
575 test_expect_success '%gd shortens ref name' '
576 echo "main@{0}" >expect.gd-short &&
577 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
578 test_cmp expect.gd-short actual.gd-short
581 test_expect_success 'reflog identity' '
582 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
583 git log -g -1 --format="%gn:%ge" >actual &&
584 test_cmp expect actual
587 test_expect_success 'oneline with empty message' '
588 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
589 git commit --allow-empty --allow-empty-message &&
590 git rev-list --oneline HEAD >test.txt &&
591 test_line_count = 5 test.txt &&
592 git rev-list --oneline --graph HEAD >testg.txt &&
593 test_line_count = 5 testg.txt
596 test_expect_success 'single-character name is parsed correctly' '
597 git commit --author="a <a@example.com>" --allow-empty -m foo &&
598 echo "a <a@example.com>" >expect &&
599 git log -1 --format="%an <%ae>" >actual &&
600 test_cmp expect actual
603 test_expect_success 'unused %G placeholders are passed through' '
604 echo "%GX %G" >expect &&
605 git log -1 --format="%GX %G" >actual &&
606 test_cmp expect actual
609 test_done