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
13 .
"$TEST_DIRECTORY"/lib-terminal.sh
17 if test_have_prereq ICONV
19 # Tested non-UTF-8 encoding
20 test_encoding
="ISO8859-1"
22 # String "added" in German
23 # (translated with Google Translate),
24 # encoded in UTF-8, used as a commit log message below.
25 added_utf8_part
=$
(printf "\303\274")
26 added_utf8_part_iso88591
=$
(echo "$added_utf8_part" |
iconv -f utf-8
-t $test_encoding)
27 added
=$
(printf "added (hinzugef${added_utf8_part}gt) foo")
28 added_iso88591
=$
(echo "$added" |
iconv -f utf-8
-t $test_encoding)
30 changed_utf8_part
=$
(printf "\303\244")
31 changed_utf8_part_iso88591
=$
(echo "$changed_utf8_part" |
iconv -f utf-8
-t $test_encoding)
32 changed
=$
(printf "changed (ge${changed_utf8_part}ndert) foo")
33 changed_iso88591
=$
(echo "$changed" |
iconv -f utf-8
-t $test_encoding)
35 # Tested non-UTF-8 encoding
38 # String "added" in German
39 # (translated with Google Translate),
40 # encoded in UTF-8, used as a commit log message below.
42 added_utf8_part_iso88591
="u"
43 added
=$
(printf "added (hinzugef${added_utf8_part}gt) foo")
44 added_iso88591
="$added"
47 changed_utf8_part_iso88591
="a"
48 changed
=$
(printf "changed (ge${changed_utf8_part}ndert) foo")
49 changed_iso88591
="$changed"
52 # Count of char to truncate
53 # Number is chosen so, that non-ACSII characters
54 # (see $added_utf8_part and $changed_utf8_part)
55 # fall into truncated parts of appropriate words both from left and right
58 test_expect_success
'setup' '
61 git config i18n.commitEncoding $test_encoding &&
62 echo "$added_iso88591" | git commit -F - &&
63 head1=$(git rev-parse --verify HEAD) &&
64 head1_short=$(git rev-parse --verify --short $head1) &&
65 head1_short4=$(git rev-parse --verify --short=4 $head1) &&
66 tree1=$(git rev-parse --verify HEAD:) &&
67 tree1_short=$(git rev-parse --verify --short $tree1) &&
68 echo "$changed" > foo &&
69 echo "$changed_iso88591" | git commit -a -F - &&
70 head2=$(git rev-parse --verify HEAD) &&
71 head2_short=$(git rev-parse --verify --short $head2) &&
72 head2_short4=$(git rev-parse --verify --short=4 $head2) &&
73 tree2=$(git rev-parse --verify HEAD:) &&
74 tree2_short=$(git rev-parse --verify --short $tree2) &&
75 git config --unset i18n.commitEncoding
78 # usage: test_format [argument...] name format_string [success|failure] [prereq] <expected_output
92 test_expect_
${3:-success} $4 "format $1" "
93 git rev-list $args --pretty=format:'$2' main >output.$1 &&
94 test_cmp expect.$1 output.$1
98 # usage: test_pretty [argument...] name format_name [failure] <expected_output
112 test_expect_
${3:-success} "pretty $1 (without --no-commit-header)" "
113 git rev-list $args --pretty='$2' main >output.$1 &&
114 test_cmp expect.$1 output.$1
116 test_expect_
${3:-success} "pretty $1 (with --no-commit-header)" "
117 git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
118 test_cmp expect.$1 output.$1
122 # Feed to --format to provide predictable colored sequences.
123 BASIC_COLOR
='%Credfoo%Creset'
124 COLOR
='%C(red)foo%C(reset)'
125 AUTO_COLOR
='%C(auto,red)foo%C(auto,reset)'
126 ALWAYS_COLOR
='%C(always,red)foo%C(always,reset)'
128 test_decode_color
<"$1" >decoded
&&
129 echo "<RED>foo<RESET>" >expect
&&
130 test_cmp expect decoded
138 test_format percent
%%h
<<EOF
145 test_format
hash %H
%n
%h
<<EOF
154 test_format
--no-commit-header hash-no-header
%H
%n
%h
<<EOF
161 test_format
--abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev
%H
%n
%h
<<EOF
168 test_format tree
%T
%n
%t
<<EOF
177 test_format parents
%P
%n
%p
<<EOF
186 # we don't test relative here
187 test_format author
%an
%n
%ae
%n
%al
%n
%ad
%n
%aD
%n
%at <<EOF
191 $TEST_AUTHOR_LOCALNAME
192 Thu Apr 7 15:13:13 2005 -0700
193 Thu, 7 Apr 2005 15:13:13 -0700
198 $TEST_AUTHOR_LOCALNAME
199 Thu Apr 7 15:13:13 2005 -0700
200 Thu, 7 Apr 2005 15:13:13 -0700
204 test_format committer
%cn
%n
%ce
%n
%cl
%n
%cd%n
%cD
%n
%ct
<<EOF
208 $TEST_COMMITTER_LOCALNAME
209 Thu Apr 7 15:13:13 2005 -0700
210 Thu, 7 Apr 2005 15:13:13 -0700
215 $TEST_COMMITTER_LOCALNAME
216 Thu Apr 7 15:13:13 2005 -0700
217 Thu, 7 Apr 2005 15:13:13 -0700
221 test_format encoding
%e success ICONV
<<EOF
228 test_format subject
%s
<<EOF
235 test_format subject-truncated
"%<($truncate_count,trunc)%s" <<EOF
237 changed (ge${changed_utf8_part}ndert)..
239 added (hinzugef${added_utf8_part}gt..
242 test_format body
%b
<<EOF
247 test_format raw-body
%B
<<EOF
256 test_format
--no-commit-header raw-body-no-header
%B
<<EOF
263 test_pretty oneline oneline
<<EOF
268 test_pretty short short
<<EOF
270 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
275 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
281 test_expect_success
'basic colors' '
282 cat >expect <<-EOF &&
284 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
286 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
287 git rev-list --color --format="$format" -1 main >actual.raw &&
288 test_decode_color <actual.raw >actual &&
289 test_cmp expect actual
292 test_expect_success
'%S is not a placeholder for rev-list yet' '
293 git rev-list --format="%S" -1 main | grep "%S"
296 test_expect_success
'advanced colors' '
297 cat >expect <<-EOF &&
299 <BOLD;RED;BYELLOW>foo<RESET>
301 format="%C(red yellow bold)foo%C(reset)" &&
302 git rev-list --color --format="$format" -1 main >actual.raw &&
303 test_decode_color <actual.raw >actual &&
304 test_cmp expect actual
308 "%Cred:$BASIC_COLOR" \
310 "%C(auto,...):$AUTO_COLOR"
314 test_expect_success
"$desc does not enable color by default" '
315 git log --format=$color -1 >actual &&
319 test_expect_success
"$desc enables colors for color.diff" '
320 git -c color.diff=always log --format=$color -1 >actual &&
324 test_expect_success
"$desc enables colors for color.ui" '
325 git -c color.ui=always log --format=$color -1 >actual &&
329 test_expect_success
"$desc respects --color" '
330 git log --format=$color -1 --color >actual &&
334 test_expect_success
"$desc respects --no-color" '
335 git -c color.ui=always log --format=$color -1 --no-color >actual &&
339 test_expect_success TTY
"$desc respects --color=auto (stdout is tty)" '
340 test_terminal git log --format=$color -1 --color=auto >actual &&
344 test_expect_success
"$desc respects --color=auto (stdout not tty)" '
346 TERM=vt100 && export TERM &&
347 git log --format=$color -1 --color=auto >actual &&
353 test_expect_success
'%C(always,...) enables color even without tty' '
354 git log --format=$ALWAYS_COLOR -1 >actual &&
358 test_expect_success
'%C(auto) respects --color' '
359 git log --color --format="%C(auto)%H" -1 >actual.raw &&
360 test_decode_color <actual.raw >actual &&
361 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
362 test_cmp expect actual
365 test_expect_success
'%C(auto) respects --no-color' '
366 git log --no-color --format="%C(auto)%H" -1 >actual &&
367 git rev-parse HEAD >expect &&
368 test_cmp expect actual
371 test_expect_success
'rev-list %C(auto,...) respects --color' '
372 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
373 -1 HEAD >actual.raw &&
374 test_decode_color <actual.raw >actual &&
375 cat >expect <<-EOF &&
376 commit $(git rev-parse HEAD)
379 test_cmp expect actual
382 iconv -f utf-8
-t $test_encoding > commit-msg
<<EOF
383 Test printing of complex bodies
385 This commit message is much longer than the others,
386 and it will be encoded in $test_encoding. We should therefore
387 include an ISO8859 character: ¡bueno!
390 test_expect_success
'setup complex body' '
391 git config i18n.commitencoding $test_encoding &&
392 echo change2 >foo && git commit -a -F commit-msg &&
393 head3=$(git rev-parse --verify HEAD) &&
394 head3_short=$(git rev-parse --short $head3)
397 test_format complex-encoding
%e success ICONV
<<EOF
406 test_format complex-subject
%s
<<EOF
408 Test printing of complex bodies
415 test_format complex-subject-trunc
"%<($truncate_count,trunc)%s" <<EOF
419 changed (ge${changed_utf8_part_iso88591}ndert)..
421 added (hinzugef${added_utf8_part_iso88591}gt..
424 test_format complex-subject-mtrunc
"%<($truncate_count,mtrunc)%s" <<EOF
430 added (hi..f${added_utf8_part_iso88591}gt) foo
433 test_format complex-subject-ltrunc
"%<($truncate_count,ltrunc)%s" <<EOF
437 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
439 .. (hinzugef${added_utf8_part_iso88591}gt) foo
442 test_expect_success
'setup expected messages (for test %b)' '
443 cat <<-EOF >expected.utf-8 &&
445 This commit message is much longer than the others,
446 and it will be encoded in $test_encoding. We should therefore
447 include an ISO8859 character: ¡bueno!
452 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
455 test_format complex-body
%b
<expected.ISO8859-1
457 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
458 # so unset i18n.commitEncoding to test encoding conversion
459 git config
--unset i18n.commitEncoding
461 test_format complex-subject-commitencoding-unset
%s
<<EOF
463 Test printing of complex bodies
470 test_format complex-subject-commitencoding-unset-trunc
"%<($truncate_count,trunc)%s" <<EOF
474 changed (ge${changed_utf8_part}ndert)..
476 added (hinzugef${added_utf8_part}gt..
479 test_format complex-subject-commitencoding-unset-mtrunc
"%<($truncate_count,mtrunc)%s" <<EOF
485 added (hi..f${added_utf8_part}gt) foo
488 test_format complex-subject-commitencoding-unset-ltrunc
"%<($truncate_count,ltrunc)%s" <<EOF
492 ..ged (ge${changed_utf8_part}ndert) foo
494 .. (hinzugef${added_utf8_part}gt) foo
497 test_format complex-body-commitencoding-unset
%b
<expected.utf-8
499 test_expect_success
'%x00 shows NUL' '
500 echo >expect commit $head3 &&
501 echo >>expect fooQbar &&
502 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
503 nul_to_q <actual.nul >actual &&
504 test_cmp expect actual
507 test_expect_success
'%ad respects --date=' '
508 echo 2005-04-07 >expect.ad-short &&
509 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
510 test_cmp expect.ad-short output.ad-short
513 test_expect_success
'empty email' '
515 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
516 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
517 test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
520 test_expect_success
'del LF before empty (1)' '
521 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
522 test_line_count = 2 actual
525 test_expect_success
'del LF before empty (2)' '
526 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
527 test_line_count = 6 actual &&
531 test_expect_success
'add LF before non-empty (1)' '
532 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
533 test_line_count = 2 actual
536 test_expect_success
'add LF before non-empty (2)' '
537 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
538 test_line_count = 6 actual &&
542 test_expect_success
'add SP before non-empty (1)' '
543 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
544 test $(wc -w <actual) = 3
547 test_expect_success
'add SP before non-empty (2)' '
548 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
549 test $(wc -w <actual) = 6
552 test_expect_success
'--abbrev' '
553 echo SHORT SHORT SHORT >expect2 &&
554 echo LONG LONG LONG >expect3 &&
555 git log -1 --format="%h %h %h" HEAD >actual1 &&
556 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
557 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
558 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
559 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
560 test_cmp expect2 fuzzy2 &&
561 test_cmp expect3 fuzzy3 &&
562 ! test_cmp actual1 actual2
565 test_expect_success
'%H is not affected by --abbrev-commit' '
566 expected=$(($(test_oid hexsz) + 1)) &&
567 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
568 len=$(wc -c <actual) &&
569 test $len = $expected
572 test_expect_success
'%h is not affected by --abbrev-commit' '
573 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
574 len=$(wc -c <actual) &&
578 test_expect_success
'"%h %gD: %gs" is same as git-reflog' '
579 git reflog >expect &&
580 git log -g --format="%h %gD: %gs" >actual &&
581 test_cmp expect actual
584 test_expect_success
'"%h %gD: %gs" is same as git-reflog (with date)' '
585 git reflog --date=raw >expect &&
586 git log -g --format="%h %gD: %gs" --date=raw >actual &&
587 test_cmp expect actual
590 test_expect_success
'"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
591 git reflog --abbrev=13 --date=raw >expect &&
592 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
593 test_cmp expect actual
596 test_expect_success
'%gd shortens ref name' '
597 echo "main@{0}" >expect.gd-short &&
598 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
599 test_cmp expect.gd-short actual.gd-short
602 test_expect_success
'reflog identity' '
603 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
604 git log -g -1 --format="%gn:%ge" >actual &&
605 test_cmp expect actual
608 test_expect_success
'oneline with empty message' '
609 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
610 git commit --allow-empty --allow-empty-message &&
611 git rev-list --oneline HEAD >test.txt &&
612 test_line_count = 5 test.txt &&
613 git rev-list --oneline --graph HEAD >testg.txt &&
614 test_line_count = 5 testg.txt
617 test_expect_success
'single-character name is parsed correctly' '
618 git commit --author="a <a@example.com>" --allow-empty -m foo &&
619 echo "a <a@example.com>" >expect &&
620 git log -1 --format="%an <%ae>" >actual &&
621 test_cmp expect actual
624 test_expect_success
'unused %G placeholders are passed through' '
625 echo "%GX %G" >expect &&
626 git log -1 --format="%GX %G" >actual &&
627 test_cmp expect actual