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
12 . "$TEST_DIRECTORY"/lib-terminal.sh
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)
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
37 test_expect_success 'setup' '
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 tree1=$(git rev-parse --verify HEAD:) &&
45 tree1_short=$(git rev-parse --verify --short $tree1) &&
46 echo "$changed" > foo &&
47 echo "$changed_iso88591" | git commit -a -F - &&
48 head2=$(git rev-parse --verify HEAD) &&
49 head2_short=$(git rev-parse --verify --short $head2) &&
50 tree2=$(git rev-parse --verify HEAD:) &&
51 tree2_short=$(git rev-parse --verify --short $tree2) &&
52 git config --unset i18n.commitEncoding
55 # usage: test_format name format_string [failure] <expected_output
58 test_expect_${3:-success} "format $1" "
59 git rev-list --pretty=format:'$2' main >output.$1 &&
60 test_cmp expect.$1 output.$1
64 # Feed to --format to provide predictable colored sequences.
65 BASIC_COLOR='%Credfoo%Creset'
66 COLOR='%C(red)foo%C(reset)'
67 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
68 ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
70 test_decode_color <"$1" >decoded &&
71 echo "<RED>foo<RESET>" >expect &&
72 test_cmp expect decoded
80 test_format percent %%h <<EOF
87 test_format hash %H%n%h <<EOF
96 test_format tree %T%n%t <<EOF
105 test_format parents %P%n%p <<EOF
114 # we don't test relative here
115 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
119 $TEST_AUTHOR_LOCALNAME
120 Thu Apr 7 15:13:13 2005 -0700
121 Thu, 7 Apr 2005 15:13:13 -0700
126 $TEST_AUTHOR_LOCALNAME
127 Thu Apr 7 15:13:13 2005 -0700
128 Thu, 7 Apr 2005 15:13:13 -0700
132 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
136 $TEST_COMMITTER_LOCALNAME
137 Thu Apr 7 15:13:13 2005 -0700
138 Thu, 7 Apr 2005 15:13:13 -0700
143 $TEST_COMMITTER_LOCALNAME
144 Thu Apr 7 15:13:13 2005 -0700
145 Thu, 7 Apr 2005 15:13:13 -0700
149 test_format encoding %e <<EOF
156 test_format subject %s <<EOF
163 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
165 changed (ge${changed_utf8_part}ndert)..
167 added (hinzugef${added_utf8_part}gt..
170 test_format body %b <<EOF
175 test_format raw-body %B <<EOF
184 test_expect_success 'basic colors' '
185 cat >expect <<-EOF &&
187 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
189 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
190 git rev-list --color --format="$format" -1 main >actual.raw &&
191 test_decode_color <actual.raw >actual &&
192 test_cmp expect actual
195 test_expect_success '%S is not a placeholder for rev-list yet' '
196 git rev-list --format="%S" -1 main | grep "%S"
199 test_expect_success 'advanced colors' '
200 cat >expect <<-EOF &&
202 <BOLD;RED;BYELLOW>foo<RESET>
204 format="%C(red yellow bold)foo%C(reset)" &&
205 git rev-list --color --format="$format" -1 main >actual.raw &&
206 test_decode_color <actual.raw >actual &&
207 test_cmp expect actual
211 "%Cred:$BASIC_COLOR" \
213 "%C(auto,...):$AUTO_COLOR"
217 test_expect_success "$desc does not enable color by default" '
218 git log --format=$color -1 >actual &&
222 test_expect_success "$desc enables colors for color.diff" '
223 git -c color.diff=always log --format=$color -1 >actual &&
227 test_expect_success "$desc enables colors for color.ui" '
228 git -c color.ui=always log --format=$color -1 >actual &&
232 test_expect_success "$desc respects --color" '
233 git log --format=$color -1 --color >actual &&
237 test_expect_success "$desc respects --no-color" '
238 git -c color.ui=always log --format=$color -1 --no-color >actual &&
242 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
243 test_terminal git log --format=$color -1 --color=auto >actual &&
247 test_expect_success "$desc respects --color=auto (stdout not tty)" '
249 TERM=vt100 && export TERM &&
250 git log --format=$color -1 --color=auto >actual &&
256 test_expect_success '%C(always,...) enables color even without tty' '
257 git log --format=$ALWAYS_COLOR -1 >actual &&
261 test_expect_success '%C(auto) respects --color' '
262 git log --color --format="%C(auto)%H" -1 >actual.raw &&
263 test_decode_color <actual.raw >actual &&
264 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
265 test_cmp expect actual
268 test_expect_success '%C(auto) respects --no-color' '
269 git log --no-color --format="%C(auto)%H" -1 >actual &&
270 git rev-parse HEAD >expect &&
271 test_cmp expect actual
274 test_expect_success 'rev-list %C(auto,...) respects --color' '
275 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
276 -1 HEAD >actual.raw &&
277 test_decode_color <actual.raw >actual &&
278 cat >expect <<-EOF &&
279 commit $(git rev-parse HEAD)
282 test_cmp expect actual
285 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
286 Test printing of complex bodies
288 This commit message is much longer than the others,
289 and it will be encoded in $test_encoding. We should therefore
290 include an ISO8859 character: ¡bueno!
293 test_expect_success 'setup complex body' '
294 git config i18n.commitencoding $test_encoding &&
295 echo change2 >foo && git commit -a -F commit-msg &&
296 head3=$(git rev-parse --verify HEAD) &&
297 head3_short=$(git rev-parse --short $head3)
300 test_format complex-encoding %e <<EOF
309 test_format complex-subject %s <<EOF
311 Test printing of complex bodies
318 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
322 changed (ge${changed_utf8_part_iso88591}ndert)..
324 added (hinzugef${added_utf8_part_iso88591}gt..
327 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
333 added (hi..f${added_utf8_part_iso88591}gt) foo
336 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
340 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
342 .. (hinzugef${added_utf8_part_iso88591}gt) foo
345 test_expect_success 'setup expected messages (for test %b)' '
346 cat <<-EOF >expected.utf-8 &&
348 This commit message is much longer than the others,
349 and it will be encoded in $test_encoding. We should therefore
350 include an ISO8859 character: ¡bueno!
355 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
358 test_format complex-body %b <expected.ISO8859-1
360 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
361 # so unset i18n.commitEncoding to test encoding conversion
362 git config --unset i18n.commitEncoding
364 test_format complex-subject-commitencoding-unset %s <<EOF
366 Test printing of complex bodies
373 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
377 changed (ge${changed_utf8_part}ndert)..
379 added (hinzugef${added_utf8_part}gt..
382 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
388 added (hi..f${added_utf8_part}gt) foo
391 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
395 ..ged (ge${changed_utf8_part}ndert) foo
397 .. (hinzugef${added_utf8_part}gt) foo
400 test_format complex-body-commitencoding-unset %b <expected.utf-8
402 test_expect_success '%x00 shows NUL' '
403 echo >expect commit $head3 &&
404 echo >>expect fooQbar &&
405 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
406 nul_to_q <actual.nul >actual &&
407 test_cmp expect actual
410 test_expect_success '%ad respects --date=' '
411 echo 2005-04-07 >expect.ad-short &&
412 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
413 test_cmp expect.ad-short output.ad-short
416 test_expect_success 'empty email' '
418 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
419 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
420 verbose test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
423 test_expect_success 'del LF before empty (1)' '
424 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
425 test_line_count = 2 actual
428 test_expect_success 'del LF before empty (2)' '
429 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
430 test_line_count = 6 actual &&
434 test_expect_success 'add LF before non-empty (1)' '
435 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
436 test_line_count = 2 actual
439 test_expect_success 'add LF before non-empty (2)' '
440 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
441 test_line_count = 6 actual &&
445 test_expect_success 'add SP before non-empty (1)' '
446 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
447 test $(wc -w <actual) = 3
450 test_expect_success 'add SP before non-empty (2)' '
451 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
452 test $(wc -w <actual) = 6
455 test_expect_success '--abbrev' '
456 echo SHORT SHORT SHORT >expect2 &&
457 echo LONG LONG LONG >expect3 &&
458 git log -1 --format="%h %h %h" HEAD >actual1 &&
459 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
460 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
461 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
462 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
463 test_cmp expect2 fuzzy2 &&
464 test_cmp expect3 fuzzy3 &&
465 ! test_cmp actual1 actual2
468 test_expect_success '%H is not affected by --abbrev-commit' '
469 expected=$(($(test_oid hexsz) + 1)) &&
470 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
471 len=$(wc -c <actual) &&
472 test $len = $expected
475 test_expect_success '%h is not affected by --abbrev-commit' '
476 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
477 len=$(wc -c <actual) &&
481 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
482 git reflog >expect &&
483 git log -g --format="%h %gD: %gs" >actual &&
484 test_cmp expect actual
487 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
488 git reflog --date=raw >expect &&
489 git log -g --format="%h %gD: %gs" --date=raw >actual &&
490 test_cmp expect actual
493 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
494 git reflog --abbrev=13 --date=raw >expect &&
495 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
496 test_cmp expect actual
499 test_expect_success '%gd shortens ref name' '
500 echo "main@{0}" >expect.gd-short &&
501 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
502 test_cmp expect.gd-short actual.gd-short
505 test_expect_success 'reflog identity' '
506 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
507 git log -g -1 --format="%gn:%ge" >actual &&
508 test_cmp expect actual
511 test_expect_success 'oneline with empty message' '
512 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
513 git commit --allow-empty --allow-empty-message &&
514 git rev-list --oneline HEAD >test.txt &&
515 test_line_count = 5 test.txt &&
516 git rev-list --oneline --graph HEAD >testg.txt &&
517 test_line_count = 5 testg.txt
520 test_expect_success 'single-character name is parsed correctly' '
521 git commit --author="a <a@example.com>" --allow-empty -m foo &&
522 echo "a <a@example.com>" >expect &&
523 git log -1 --format="%an <%ae>" >actual &&
524 test_cmp expect actual
527 test_expect_success 'unused %G placeholders are passed through' '
528 echo "%GX %G" >expect &&
529 git log -1 --format="%GX %G" >actual &&
530 test_cmp expect actual