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'
9 .
"$TEST_DIRECTORY"/lib-terminal.sh
12 # Tested non-UTF-8 encoding
13 test_encoding
="ISO8859-1"
15 # String "added" in German
16 # (translated with Google Translate),
17 # encoded in UTF-8, used as a commit log message below.
18 added_utf8_part
=$
(printf "\303\274")
19 added_utf8_part_iso88591
=$
(echo "$added_utf8_part" |
iconv -f utf-8
-t $test_encoding)
20 added
=$
(printf "added (hinzugef${added_utf8_part}gt) foo")
21 added_iso88591
=$
(echo "$added" |
iconv -f utf-8
-t $test_encoding)
23 changed_utf8_part
=$
(printf "\303\244")
24 changed_utf8_part_iso88591
=$
(echo "$changed_utf8_part" |
iconv -f utf-8
-t $test_encoding)
25 changed
=$
(printf "changed (ge${changed_utf8_part}ndert) foo")
26 changed_iso88591
=$
(echo "$changed" |
iconv -f utf-8
-t $test_encoding)
28 # Count of char to truncate
29 # Number is chosen so, that non-ACSII characters
30 # (see $added_utf8_part and $changed_utf8_part)
31 # fall into truncated parts of appropriate words both from left and right
34 test_expect_success
'setup' '
37 git config i18n.commitEncoding $test_encoding &&
38 echo "$added_iso88591" | git commit -F - &&
39 head1=$(git rev-parse --verify HEAD) &&
40 head1_short=$(git rev-parse --verify --short $head1) &&
41 tree1=$(git rev-parse --verify HEAD:) &&
42 tree1_short=$(git rev-parse --verify --short $tree1) &&
43 echo "$changed" > foo &&
44 echo "$changed_iso88591" | git commit -a -F - &&
45 head2=$(git rev-parse --verify HEAD) &&
46 head2_short=$(git rev-parse --verify --short $head2) &&
47 tree2=$(git rev-parse --verify HEAD:) &&
48 tree2_short=$(git rev-parse --verify --short $tree2) &&
49 git config --unset i18n.commitEncoding
52 # usage: test_format name format_string [failure] <expected_output
55 test_expect_
${3:-success} "format $1" "
56 git rev-list --pretty=format:'$2' master >output.$1 &&
57 test_cmp expect.$1 output.$1
61 # Feed to --format to provide predictable colored sequences.
62 BASIC_COLOR
='%Credfoo%Creset'
63 COLOR
='%C(red)foo%C(reset)'
64 AUTO_COLOR
='%C(auto,red)foo%C(auto,reset)'
65 ALWAYS_COLOR
='%C(always,red)foo%C(always,reset)'
67 test_decode_color
<"$1" >decoded
&&
68 echo "<RED>foo<RESET>" >expect
&&
69 test_cmp expect decoded
77 test_format percent
%%h
<<EOF
84 test_format
hash %H
%n
%h
<<EOF
93 test_format tree
%T
%n
%t
<<EOF
102 test_format parents
%P
%n
%p
<<EOF
111 # we don't test relative here
112 test_format author
%an
%n
%ae
%n
%ad
%n
%aD
%n
%at <<EOF
116 Thu Apr 7 15:13:13 2005 -0700
117 Thu, 7 Apr 2005 15:13:13 -0700
122 Thu Apr 7 15:13:13 2005 -0700
123 Thu, 7 Apr 2005 15:13:13 -0700
127 test_format committer
%cn
%n
%ce
%n
%cd%n
%cD
%n
%ct
<<EOF
130 committer@example.com
131 Thu Apr 7 15:13:13 2005 -0700
132 Thu, 7 Apr 2005 15:13:13 -0700
136 committer@example.com
137 Thu Apr 7 15:13:13 2005 -0700
138 Thu, 7 Apr 2005 15:13:13 -0700
142 test_format encoding
%e
<<EOF
149 test_format subject
%s
<<EOF
156 test_format subject-truncated
"%<($truncate_count,trunc)%s" <<EOF
158 changed (ge${changed_utf8_part}ndert)..
160 added (hinzugef${added_utf8_part}gt..
163 test_format body
%b
<<EOF
168 test_format raw-body
%B
<<EOF
177 test_expect_success
'basic colors' '
178 cat >expect <<-EOF &&
180 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
182 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
183 git rev-list --color --format="$format" -1 master >actual.raw &&
184 test_decode_color <actual.raw >actual &&
185 test_cmp expect actual
188 test_expect_success
'advanced colors' '
189 cat >expect <<-EOF &&
191 <BOLD;RED;BYELLOW>foo<RESET>
193 format="%C(red yellow bold)foo%C(reset)" &&
194 git rev-list --color --format="$format" -1 master >actual.raw &&
195 test_decode_color <actual.raw >actual &&
196 test_cmp expect actual
200 "%Cred:$BASIC_COLOR" \
202 "%C(auto,...):$AUTO_COLOR"
206 test_expect_success
"$desc does not enable color by default" '
207 git log --format=$color -1 >actual &&
211 test_expect_success
"$desc enables colors for color.diff" '
212 git -c color.diff=always log --format=$color -1 >actual &&
216 test_expect_success
"$desc enables colors for color.ui" '
217 git -c color.ui=always log --format=$color -1 >actual &&
221 test_expect_success
"$desc respects --color" '
222 git log --format=$color -1 --color >actual &&
226 test_expect_success
"$desc respects --no-color" '
227 git -c color.ui=always log --format=$color -1 --no-color >actual &&
231 test_expect_success TTY
"$desc respects --color=auto (stdout is tty)" '
232 test_terminal env TERM=vt100 \
233 git log --format=$color -1 --color=auto >actual &&
237 test_expect_success
"$desc respects --color=auto (stdout not tty)" '
239 TERM=vt100 && export TERM &&
240 git log --format=$color -1 --color=auto >actual &&
246 test_expect_success
'%C(always,...) enables color even without tty' '
247 git log --format=$ALWAYS_COLOR -1 >actual &&
251 test_expect_success
'%C(auto) respects --color' '
252 git log --color --format="%C(auto)%H" -1 >actual.raw &&
253 test_decode_color <actual.raw >actual &&
254 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
255 test_cmp expect actual
258 test_expect_success
'%C(auto) respects --no-color' '
259 git log --no-color --format="%C(auto)%H" -1 >actual &&
260 git rev-parse HEAD >expect &&
261 test_cmp expect actual
264 test_expect_success
'rev-list %C(auto,...) respects --color' '
265 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
266 -1 HEAD >actual.raw &&
267 test_decode_color <actual.raw >actual &&
268 cat >expect <<-EOF &&
269 commit $(git rev-parse HEAD)
272 test_cmp expect actual
275 iconv -f utf-8
-t $test_encoding > commit-msg
<<EOF
276 Test printing of complex bodies
278 This commit message is much longer than the others,
279 and it will be encoded in $test_encoding. We should therefore
280 include an ISO8859 character: ¡bueno!
283 test_expect_success
'setup complex body' '
284 git config i18n.commitencoding $test_encoding &&
285 echo change2 >foo && git commit -a -F commit-msg &&
286 head3=$(git rev-parse --verify HEAD) &&
287 head3_short=$(git rev-parse --short $head3)
290 test_format complex-encoding
%e
<<EOF
299 test_format complex-subject
%s
<<EOF
301 Test printing of complex bodies
308 test_format complex-subject-trunc
"%<($truncate_count,trunc)%s" <<EOF
312 changed (ge${changed_utf8_part_iso88591}ndert)..
314 added (hinzugef${added_utf8_part_iso88591}gt..
317 test_format complex-subject-mtrunc
"%<($truncate_count,mtrunc)%s" <<EOF
323 added (hi..f${added_utf8_part_iso88591}gt) foo
326 test_format complex-subject-ltrunc
"%<($truncate_count,ltrunc)%s" <<EOF
330 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
332 .. (hinzugef${added_utf8_part_iso88591}gt) foo
335 test_expect_success
'prepare expected messages (for test %b)' '
336 cat <<-EOF >expected.utf-8 &&
338 This commit message is much longer than the others,
339 and it will be encoded in $test_encoding. We should therefore
340 include an ISO8859 character: ¡bueno!
345 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
348 test_format complex-body
%b
<expected.ISO8859-1
350 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
351 # so unset i18n.commitEncoding to test encoding conversion
352 git config
--unset i18n.commitEncoding
354 test_format complex-subject-commitencoding-unset
%s
<<EOF
356 Test printing of complex bodies
363 test_format complex-subject-commitencoding-unset-trunc
"%<($truncate_count,trunc)%s" <<EOF
367 changed (ge${changed_utf8_part}ndert)..
369 added (hinzugef${added_utf8_part}gt..
372 test_format complex-subject-commitencoding-unset-mtrunc
"%<($truncate_count,mtrunc)%s" <<EOF
378 added (hi..f${added_utf8_part}gt) foo
381 test_format complex-subject-commitencoding-unset-ltrunc
"%<($truncate_count,ltrunc)%s" <<EOF
385 ..ged (ge${changed_utf8_part}ndert) foo
387 .. (hinzugef${added_utf8_part}gt) foo
390 test_format complex-body-commitencoding-unset
%b
<expected.utf-8
392 test_expect_success
'%x00 shows NUL' '
393 echo >expect commit $head3 &&
394 echo >>expect fooQbar &&
395 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
396 nul_to_q <actual.nul >actual &&
397 test_cmp expect actual
400 test_expect_success
'%ad respects --date=' '
401 echo 2005-04-07 >expect.ad-short &&
402 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
403 test_cmp expect.ad-short output.ad-short
406 test_expect_success
'empty email' '
408 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
409 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
410 verbose test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700"
413 test_expect_success
'del LF before empty (1)' '
414 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
415 test_line_count = 2 actual
418 test_expect_success
'del LF before empty (2)' '
419 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
420 test_line_count = 6 actual &&
424 test_expect_success
'add LF before non-empty (1)' '
425 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
426 test_line_count = 2 actual
429 test_expect_success
'add LF before non-empty (2)' '
430 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
431 test_line_count = 6 actual &&
435 test_expect_success
'add SP before non-empty (1)' '
436 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
437 test $(wc -w <actual) = 3
440 test_expect_success
'add SP before non-empty (2)' '
441 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
442 test $(wc -w <actual) = 6
445 test_expect_success
'--abbrev' '
446 echo SHORT SHORT SHORT >expect2 &&
447 echo LONG LONG LONG >expect3 &&
448 git log -1 --format="%h %h %h" HEAD >actual1 &&
449 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
450 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
451 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
452 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
453 test_cmp expect2 fuzzy2 &&
454 test_cmp expect3 fuzzy3 &&
455 ! test_cmp actual1 actual2
458 test_expect_success
'%H is not affected by --abbrev-commit' '
459 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
460 len=$(wc -c <actual) &&
464 test_expect_success
'%h is not affected by --abbrev-commit' '
465 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
466 len=$(wc -c <actual) &&
470 test_expect_success
'"%h %gD: %gs" is same as git-reflog' '
471 git reflog >expect &&
472 git log -g --format="%h %gD: %gs" >actual &&
473 test_cmp expect actual
476 test_expect_success
'"%h %gD: %gs" is same as git-reflog (with date)' '
477 git reflog --date=raw >expect &&
478 git log -g --format="%h %gD: %gs" --date=raw >actual &&
479 test_cmp expect actual
482 test_expect_success
'"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
483 git reflog --abbrev=13 --date=raw >expect &&
484 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
485 test_cmp expect actual
488 test_expect_success
'%gd shortens ref name' '
489 echo "master@{0}" >expect.gd-short &&
490 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
491 test_cmp expect.gd-short actual.gd-short
494 test_expect_success
'reflog identity' '
495 echo "C O Mitter:committer@example.com" >expect &&
496 git log -g -1 --format="%gn:%ge" >actual &&
497 test_cmp expect actual
500 test_expect_success
'oneline with empty message' '
501 git commit -m "dummy" --allow-empty &&
502 git commit -m "dummy" --allow-empty &&
503 git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
504 git rev-list --oneline HEAD >test.txt &&
505 test_line_count = 5 test.txt &&
506 git rev-list --oneline --graph HEAD >testg.txt &&
507 test_line_count = 5 testg.txt
510 test_expect_success
'single-character name is parsed correctly' '
511 git commit --author="a <a@example.com>" --allow-empty -m foo &&
512 echo "a <a@example.com>" >expect &&
513 git log -1 --format="%an <%ae>" >actual &&
514 test_cmp expect actual
517 test_expect_success
'unused %G placeholders are passed through' '
518 echo "%GX %G" >expect &&
519 git log -1 --format="%GX %G" >actual &&
520 test_cmp expect actual