]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6006-rev-list-format.sh
ref-filter: consult want_color() before emitting colors
[thirdparty/git.git] / t / t6006-rev-list-format.sh
CommitLineData
fa21b602
JK
1#!/bin/sh
2
de6029a2
AS
3# Copyright (c) 2009 Jens Lehmann
4# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5
5be60078 6test_description='git rev-list --pretty=format test'
fa21b602
JK
7
8. ./test-lib.sh
30825178 9. "$TEST_DIRECTORY"/lib-terminal.sh
fa21b602
JK
10
11test_tick
ee3efaf6
AS
12# Tested non-UTF-8 encoding
13test_encoding="ISO8859-1"
14
17cc2ef1
AS
15# String "added" in German
16# (translated with Google Translate),
17# encoded in UTF-8, used as a commit log message below.
d928d810
AS
18added_utf8_part=$(printf "\303\274")
19added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
20added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
ee3efaf6 21added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
de6029a2 22# same but "changed"
d928d810
AS
23changed_utf8_part=$(printf "\303\244")
24changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
25changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
ee3efaf6 26changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
de6029a2 27
d928d810
AS
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
32truncate_count=20
33
fa21b602 34test_expect_success 'setup' '
77a6815d
AS
35 : >foo &&
36 git add foo &&
ee3efaf6 37 git config i18n.commitEncoding $test_encoding &&
e6ce2be2 38 echo "$added_iso88591" | git commit -F - &&
77a6815d
AS
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) &&
de6029a2 43 echo "$changed" > foo &&
e6ce2be2 44 echo "$changed_iso88591" | git commit -a -F - &&
77a6815d
AS
45 head2=$(git rev-parse --verify HEAD) &&
46 head2_short=$(git rev-parse --verify --short $head2) &&
47 tree2=$(git rev-parse --verify HEAD:) &&
99094a7a 48 tree2_short=$(git rev-parse --verify --short $tree2) &&
de6029a2 49 git config --unset i18n.commitEncoding
fa21b602
JK
50'
51
17cc2ef1 52# usage: test_format name format_string [failure] <expected_output
2581ad5e 53test_format () {
fa21b602 54 cat >expect.$1
17cc2ef1
AS
55 test_expect_${3:-success} "format $1" "
56 git rev-list --pretty=format:'$2' master >output.$1 &&
57 test_cmp expect.$1 output.$1
58 "
fa21b602
JK
59}
60
30825178 61# Feed to --format to provide predictable colored sequences.
18fb7ffc
JK
62BASIC_COLOR='%Credfoo%Creset'
63COLOR='%C(red)foo%C(reset)'
30825178 64AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
18fb7ffc 65ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
30825178 66has_color () {
097b681b
JK
67 test_decode_color <"$1" >decoded &&
68 echo "<RED>foo<RESET>" >expect &&
69 test_cmp expect decoded
30825178
JH
70}
71
72has_no_color () {
73 echo foo >expect &&
74 test_cmp expect "$1"
75}
76
77a6815d
AS
77test_format percent %%h <<EOF
78commit $head2
0a0416a3 79%h
77a6815d 80commit $head1
0a0416a3
JK
81%h
82EOF
83
77a6815d
AS
84test_format hash %H%n%h <<EOF
85commit $head2
86$head2
87$head2_short
88commit $head1
89$head1
90$head1_short
fa21b602
JK
91EOF
92
77a6815d
AS
93test_format tree %T%n%t <<EOF
94commit $head2
95$tree2
96$tree2_short
97commit $head1
98$tree1
99$tree1_short
fa21b602
JK
100EOF
101
77a6815d
AS
102test_format parents %P%n%p <<EOF
103commit $head2
104$head1
105$head1_short
106commit $head1
542e165c
JH
107
108
fa21b602
JK
109EOF
110
111# we don't test relative here
77a6815d
AS
112test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
113commit $head2
fa21b602
JK
114A U Thor
115author@example.com
116Thu Apr 7 15:13:13 2005 -0700
117Thu, 7 Apr 2005 15:13:13 -0700
1181112911993
77a6815d 119commit $head1
fa21b602
JK
120A U Thor
121author@example.com
122Thu Apr 7 15:13:13 2005 -0700
123Thu, 7 Apr 2005 15:13:13 -0700
1241112911993
125EOF
126
77a6815d
AS
127test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
128commit $head2
fa21b602
JK
129C O Mitter
130committer@example.com
131Thu Apr 7 15:13:13 2005 -0700
132Thu, 7 Apr 2005 15:13:13 -0700
1331112911993
77a6815d 134commit $head1
fa21b602
JK
135C O Mitter
136committer@example.com
137Thu Apr 7 15:13:13 2005 -0700
138Thu, 7 Apr 2005 15:13:13 -0700
1391112911993
140EOF
141
77a6815d
AS
142test_format encoding %e <<EOF
143commit $head2
ee3efaf6 144$test_encoding
77a6815d 145commit $head1
ee3efaf6 146$test_encoding
fa21b602
JK
147EOF
148
ecaee805 149test_format subject %s <<EOF
77a6815d 150commit $head2
de6029a2 151$changed
77a6815d 152commit $head1
de6029a2 153$added
fa21b602
JK
154EOF
155
d928d810
AS
156test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
157commit $head2
158changed (ge${changed_utf8_part}ndert)..
159commit $head1
160added (hinzugef${added_utf8_part}gt..
161EOF
162
77a6815d
AS
163test_format body %b <<EOF
164commit $head2
165commit $head1
fa21b602
JK
166EOF
167
ecaee805 168test_format raw-body %B <<EOF
77a6815d 169commit $head2
de6029a2 170$changed
1367b12a 171
77a6815d 172commit $head1
de6029a2 173$added
1367b12a
EB
174
175EOF
176
097b681b
JK
177test_expect_success 'basic colors' '
178 cat >expect <<-EOF &&
179 commit $head2
180 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
181 EOF
182 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
18fb7ffc 183 git rev-list --color --format="$format" -1 master >actual.raw &&
097b681b
JK
184 test_decode_color <actual.raw >actual &&
185 test_cmp expect actual
186'
fa21b602 187
097b681b
JK
188test_expect_success 'advanced colors' '
189 cat >expect <<-EOF &&
190 commit $head2
191 <BOLD;RED;BYELLOW>foo<RESET>
192 EOF
193 format="%C(red yellow bold)foo%C(reset)" &&
18fb7ffc 194 git rev-list --color --format="$format" -1 master >actual.raw &&
097b681b
JK
195 test_decode_color <actual.raw >actual &&
196 test_cmp expect actual
197'
c002922a 198
18fb7ffc
JK
199for spec in \
200 "%Cred:$BASIC_COLOR" \
201 "%C(...):$COLOR" \
202 "%C(auto,...):$AUTO_COLOR"
203do
204 desc=${spec%%:*}
205 color=${spec#*:}
206 test_expect_success "$desc does not enable color by default" '
207 git log --format=$color -1 >actual &&
208 has_no_color actual
209 '
30825178 210
18fb7ffc
JK
211 test_expect_success "$desc enables colors for color.diff" '
212 git -c color.diff=always log --format=$color -1 >actual &&
213 has_color actual
214 '
30825178 215
18fb7ffc
JK
216 test_expect_success "$desc enables colors for color.ui" '
217 git -c color.ui=always log --format=$color -1 >actual &&
218 has_color actual
219 '
30825178 220
18fb7ffc
JK
221 test_expect_success "$desc respects --color" '
222 git log --format=$color -1 --color >actual &&
223 has_color actual
224 '
30825178 225
18fb7ffc
JK
226 test_expect_success "$desc respects --no-color" '
227 git -c color.ui=always log --format=$color -1 --no-color >actual &&
30825178 228 has_no_color actual
18fb7ffc
JK
229 '
230
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 &&
234 has_color actual
235 '
236
237 test_expect_success "$desc respects --color=auto (stdout not tty)" '
238 (
239 TERM=vt100 && export TERM &&
240 git log --format=$color -1 --color=auto >actual &&
241 has_no_color actual
242 )
243 '
244done
245
246test_expect_success '%C(always,...) enables color even without tty' '
247 git log --format=$ALWAYS_COLOR -1 >actual &&
248 has_color actual
30825178
JH
249'
250
b15a3e00 251test_expect_success '%C(auto) respects --color' '
097b681b
JK
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 &&
b15a3e00
ET
255 test_cmp expect actual
256'
257
258test_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
262'
263
d75dfb10
JK
264test_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)
270 <GREEN>foo<RESET>
271 EOF
272 test_cmp expect actual
273'
274
ee3efaf6 275iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
03bcaaca
JK
276Test printing of complex bodies
277
278This commit message is much longer than the others,
ee3efaf6
AS
279and it will be encoded in $test_encoding. We should therefore
280include an ISO8859 character: ¡bueno!
03bcaaca 281EOF
77a6815d 282
03bcaaca 283test_expect_success 'setup complex body' '
ee3efaf6 284 git config i18n.commitencoding $test_encoding &&
77a6815d
AS
285 echo change2 >foo && git commit -a -F commit-msg &&
286 head3=$(git rev-parse --verify HEAD) &&
0fe6df3c 287 head3_short=$(git rev-parse --short $head3)
03bcaaca
JK
288'
289
77a6815d
AS
290test_format complex-encoding %e <<EOF
291commit $head3
ee3efaf6 292$test_encoding
77a6815d 293commit $head2
ee3efaf6 294$test_encoding
77a6815d 295commit $head1
ee3efaf6 296$test_encoding
03bcaaca
JK
297EOF
298
ecaee805 299test_format complex-subject %s <<EOF
77a6815d 300commit $head3
03bcaaca 301Test printing of complex bodies
77a6815d 302commit $head2
0fe6df3c 303$changed_iso88591
77a6815d 304commit $head1
0fe6df3c 305$added_iso88591
03bcaaca
JK
306EOF
307
7d509878 308test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
d928d810
AS
309commit $head3
310Test printing of c..
311commit $head2
312changed (ge${changed_utf8_part_iso88591}ndert)..
313commit $head1
314added (hinzugef${added_utf8_part_iso88591}gt..
315EOF
316
7d509878 317test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
d928d810
AS
318commit $head3
319Test prin..ex bodies
320commit $head2
321changed (..dert) foo
322commit $head1
323added (hi..f${added_utf8_part_iso88591}gt) foo
324EOF
325
7d509878 326test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
d928d810
AS
327commit $head3
328.. of complex bodies
329commit $head2
330..ged (ge${changed_utf8_part_iso88591}ndert) foo
331commit $head1
332.. (hinzugef${added_utf8_part_iso88591}gt) foo
333EOF
334
0fe6df3c
AS
335test_expect_success 'prepare expected messages (for test %b)' '
336 cat <<-EOF >expected.utf-8 &&
337 commit $head3
338 This commit message is much longer than the others,
ee3efaf6
AS
339 and it will be encoded in $test_encoding. We should therefore
340 include an ISO8859 character: ¡bueno!
0fe6df3c
AS
341
342 commit $head2
343 commit $head1
344 EOF
ee3efaf6 345 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
0fe6df3c
AS
346'
347
ee3efaf6 348test_format complex-body %b <expected.ISO8859-1
03bcaaca 349
0fe6df3c
AS
350# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
351# so unset i18n.commitEncoding to test encoding conversion
352git config --unset i18n.commitEncoding
353
354test_format complex-subject-commitencoding-unset %s <<EOF
355commit $head3
356Test printing of complex bodies
77a6815d 357commit $head2
0fe6df3c 358$changed
77a6815d 359commit $head1
0fe6df3c 360$added
03bcaaca
JK
361EOF
362
d928d810
AS
363test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
364commit $head3
365Test printing of c..
366commit $head2
367changed (ge${changed_utf8_part}ndert)..
368commit $head1
369added (hinzugef${added_utf8_part}gt..
370EOF
371
372test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
373commit $head3
374Test prin..ex bodies
375commit $head2
376changed (..dert) foo
377commit $head1
378added (hi..f${added_utf8_part}gt) foo
379EOF
380
381test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
382commit $head3
383.. of complex bodies
384commit $head2
385..ged (ge${changed_utf8_part}ndert) foo
386commit $head1
387.. (hinzugef${added_utf8_part}gt) foo
388EOF
389
0fe6df3c
AS
390test_format complex-body-commitencoding-unset %b <expected.utf-8
391
9130ac9f 392test_expect_success '%x00 shows NUL' '
77a6815d 393 echo >expect commit $head3 &&
9130ac9f
JK
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
398'
399
d36f8679
JK
400test_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
404'
405
f7ab5c79
JH
406test_expect_success 'empty email' '
407 test_tick &&
408 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
409 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
a167ece0 410 verbose test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700"
f7ab5c79
JH
411'
412
9fa708da
JH
413test_expect_success 'del LF before empty (1)' '
414 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
3fb0459b 415 test_line_count = 2 actual
9fa708da
JH
416'
417
418test_expect_success 'del LF before empty (2)' '
419 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
3fb0459b 420 test_line_count = 6 actual &&
9fa708da
JH
421 grep "^$" actual
422'
423
424test_expect_success 'add LF before non-empty (1)' '
425 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
3fb0459b 426 test_line_count = 2 actual
9fa708da
JH
427'
428
429test_expect_success 'add LF before non-empty (2)' '
430 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
3fb0459b 431 test_line_count = 6 actual &&
9fa708da
JH
432 grep "^$" actual
433'
434
7b88176e
MG
435test_expect_success 'add SP before non-empty (1)' '
436 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
de6029a2 437 test $(wc -w <actual) = 3
7b88176e
MG
438'
439
440test_expect_success 'add SP before non-empty (2)' '
441 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
de6029a2 442 test $(wc -w <actual) = 6
7b88176e
MG
443'
444
c1977021
WP
445test_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
456'
457
458test_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) &&
461 test $len = 41
462'
463
464test_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) &&
467 test $len = 21
468'
469
8f8f5476
TR
470test_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
474'
475
476test_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
480'
481
c1977021
WP
482test_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
486'
487
8f8f5476
TR
488test_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
492'
493
cd1957f5
JK
494test_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
498'
499
1fb5fdd2
EFL
500test_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^^.. &&
f02dd06e 504 git rev-list --oneline HEAD >test.txt &&
3fb0459b
SL
505 test_line_count = 5 test.txt &&
506 git rev-list --oneline --graph HEAD >testg.txt &&
507 test_line_count = 5 testg.txt
1fb5fdd2
EFL
508'
509
d9955fd6
JK
510test_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
515'
516
958b2eb2
JK
517test_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
521'
522
fa21b602 523test_done