]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6006-rev-list-format.sh
Merge branch 'pb/pretty-email-without-domain-part'
[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
d8b8217c 112test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
77a6815d 113commit $head2
2ae4944a
PB
114$GIT_AUTHOR_NAME
115$GIT_AUTHOR_EMAIL
d8b8217c 116$TEST_AUTHOR_LOCALNAME
fa21b602
JK
117Thu Apr 7 15:13:13 2005 -0700
118Thu, 7 Apr 2005 15:13:13 -0700
1191112911993
77a6815d 120commit $head1
2ae4944a
PB
121$GIT_AUTHOR_NAME
122$GIT_AUTHOR_EMAIL
d8b8217c 123$TEST_AUTHOR_LOCALNAME
fa21b602
JK
124Thu Apr 7 15:13:13 2005 -0700
125Thu, 7 Apr 2005 15:13:13 -0700
1261112911993
127EOF
128
d8b8217c 129test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
77a6815d 130commit $head2
2ae4944a
PB
131$GIT_COMMITTER_NAME
132$GIT_COMMITTER_EMAIL
d8b8217c 133$TEST_COMMITTER_LOCALNAME
fa21b602
JK
134Thu Apr 7 15:13:13 2005 -0700
135Thu, 7 Apr 2005 15:13:13 -0700
1361112911993
77a6815d 137commit $head1
2ae4944a
PB
138$GIT_COMMITTER_NAME
139$GIT_COMMITTER_EMAIL
d8b8217c 140$TEST_COMMITTER_LOCALNAME
fa21b602
JK
141Thu Apr 7 15:13:13 2005 -0700
142Thu, 7 Apr 2005 15:13:13 -0700
1431112911993
144EOF
145
77a6815d
AS
146test_format encoding %e <<EOF
147commit $head2
ee3efaf6 148$test_encoding
77a6815d 149commit $head1
ee3efaf6 150$test_encoding
fa21b602
JK
151EOF
152
ecaee805 153test_format subject %s <<EOF
77a6815d 154commit $head2
de6029a2 155$changed
77a6815d 156commit $head1
de6029a2 157$added
fa21b602
JK
158EOF
159
d928d810
AS
160test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
161commit $head2
162changed (ge${changed_utf8_part}ndert)..
163commit $head1
164added (hinzugef${added_utf8_part}gt..
165EOF
166
77a6815d
AS
167test_format body %b <<EOF
168commit $head2
169commit $head1
fa21b602
JK
170EOF
171
ecaee805 172test_format raw-body %B <<EOF
77a6815d 173commit $head2
de6029a2 174$changed
1367b12a 175
77a6815d 176commit $head1
de6029a2 177$added
1367b12a
EB
178
179EOF
180
097b681b
JK
181test_expect_success 'basic colors' '
182 cat >expect <<-EOF &&
183 commit $head2
184 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
185 EOF
186 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
18fb7ffc 187 git rev-list --color --format="$format" -1 master >actual.raw &&
097b681b
JK
188 test_decode_color <actual.raw >actual &&
189 test_cmp expect actual
190'
fa21b602 191
ad6f028f
IT
192test_expect_success '%S is not a placeholder for rev-list yet' '
193 git rev-list --format="%S" -1 master | grep "%S"
194'
195
097b681b
JK
196test_expect_success 'advanced colors' '
197 cat >expect <<-EOF &&
198 commit $head2
199 <BOLD;RED;BYELLOW>foo<RESET>
200 EOF
201 format="%C(red yellow bold)foo%C(reset)" &&
18fb7ffc 202 git rev-list --color --format="$format" -1 master >actual.raw &&
097b681b
JK
203 test_decode_color <actual.raw >actual &&
204 test_cmp expect actual
205'
c002922a 206
18fb7ffc
JK
207for spec in \
208 "%Cred:$BASIC_COLOR" \
209 "%C(...):$COLOR" \
210 "%C(auto,...):$AUTO_COLOR"
211do
212 desc=${spec%%:*}
213 color=${spec#*:}
214 test_expect_success "$desc does not enable color by default" '
215 git log --format=$color -1 >actual &&
216 has_no_color actual
217 '
30825178 218
1d4b12fe
JK
219 test_expect_success "$desc enables colors for color.diff" '
220 git -c color.diff=always log --format=$color -1 >actual &&
221 has_color actual
222 '
223
224 test_expect_success "$desc enables colors for color.ui" '
225 git -c color.ui=always log --format=$color -1 >actual &&
226 has_color actual
227 '
228
18fb7ffc
JK
229 test_expect_success "$desc respects --color" '
230 git log --format=$color -1 --color >actual &&
231 has_color actual
232 '
30825178 233
1d4b12fe
JK
234 test_expect_success "$desc respects --no-color" '
235 git -c color.ui=always log --format=$color -1 --no-color >actual &&
236 has_no_color actual
237 '
238
18fb7ffc 239 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
e433749d 240 test_terminal git log --format=$color -1 --color=auto >actual &&
18fb7ffc
JK
241 has_color actual
242 '
243
244 test_expect_success "$desc respects --color=auto (stdout not tty)" '
245 (
246 TERM=vt100 && export TERM &&
247 git log --format=$color -1 --color=auto >actual &&
248 has_no_color actual
249 )
250 '
251done
252
253test_expect_success '%C(always,...) enables color even without tty' '
254 git log --format=$ALWAYS_COLOR -1 >actual &&
255 has_color actual
30825178
JH
256'
257
b15a3e00 258test_expect_success '%C(auto) respects --color' '
097b681b
JK
259 git log --color --format="%C(auto)%H" -1 >actual.raw &&
260 test_decode_color <actual.raw >actual &&
261 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
b15a3e00
ET
262 test_cmp expect actual
263'
264
265test_expect_success '%C(auto) respects --no-color' '
266 git log --no-color --format="%C(auto)%H" -1 >actual &&
267 git rev-parse HEAD >expect &&
268 test_cmp expect actual
269'
270
d75dfb10
JK
271test_expect_success 'rev-list %C(auto,...) respects --color' '
272 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
273 -1 HEAD >actual.raw &&
274 test_decode_color <actual.raw >actual &&
275 cat >expect <<-EOF &&
276 commit $(git rev-parse HEAD)
277 <GREEN>foo<RESET>
278 EOF
279 test_cmp expect actual
280'
281
ee3efaf6 282iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
03bcaaca
JK
283Test printing of complex bodies
284
285This commit message is much longer than the others,
ee3efaf6
AS
286and it will be encoded in $test_encoding. We should therefore
287include an ISO8859 character: ¡bueno!
03bcaaca 288EOF
77a6815d 289
03bcaaca 290test_expect_success 'setup complex body' '
ee3efaf6 291 git config i18n.commitencoding $test_encoding &&
77a6815d
AS
292 echo change2 >foo && git commit -a -F commit-msg &&
293 head3=$(git rev-parse --verify HEAD) &&
0fe6df3c 294 head3_short=$(git rev-parse --short $head3)
03bcaaca
JK
295'
296
77a6815d
AS
297test_format complex-encoding %e <<EOF
298commit $head3
ee3efaf6 299$test_encoding
77a6815d 300commit $head2
ee3efaf6 301$test_encoding
77a6815d 302commit $head1
ee3efaf6 303$test_encoding
03bcaaca
JK
304EOF
305
ecaee805 306test_format complex-subject %s <<EOF
77a6815d 307commit $head3
03bcaaca 308Test printing of complex bodies
77a6815d 309commit $head2
0fe6df3c 310$changed_iso88591
77a6815d 311commit $head1
0fe6df3c 312$added_iso88591
03bcaaca
JK
313EOF
314
7d509878 315test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
d928d810
AS
316commit $head3
317Test printing of c..
318commit $head2
319changed (ge${changed_utf8_part_iso88591}ndert)..
320commit $head1
321added (hinzugef${added_utf8_part_iso88591}gt..
322EOF
323
7d509878 324test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
d928d810
AS
325commit $head3
326Test prin..ex bodies
327commit $head2
328changed (..dert) foo
329commit $head1
330added (hi..f${added_utf8_part_iso88591}gt) foo
331EOF
332
7d509878 333test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
d928d810
AS
334commit $head3
335.. of complex bodies
336commit $head2
337..ged (ge${changed_utf8_part_iso88591}ndert) foo
338commit $head1
339.. (hinzugef${added_utf8_part_iso88591}gt) foo
340EOF
341
0fe6df3c
AS
342test_expect_success 'prepare expected messages (for test %b)' '
343 cat <<-EOF >expected.utf-8 &&
344 commit $head3
345 This commit message is much longer than the others,
ee3efaf6
AS
346 and it will be encoded in $test_encoding. We should therefore
347 include an ISO8859 character: ¡bueno!
0fe6df3c
AS
348
349 commit $head2
350 commit $head1
351 EOF
ee3efaf6 352 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
0fe6df3c
AS
353'
354
ee3efaf6 355test_format complex-body %b <expected.ISO8859-1
03bcaaca 356
0fe6df3c
AS
357# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
358# so unset i18n.commitEncoding to test encoding conversion
359git config --unset i18n.commitEncoding
360
361test_format complex-subject-commitencoding-unset %s <<EOF
362commit $head3
363Test printing of complex bodies
77a6815d 364commit $head2
0fe6df3c 365$changed
77a6815d 366commit $head1
0fe6df3c 367$added
03bcaaca
JK
368EOF
369
d928d810
AS
370test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
371commit $head3
372Test printing of c..
373commit $head2
374changed (ge${changed_utf8_part}ndert)..
375commit $head1
376added (hinzugef${added_utf8_part}gt..
377EOF
378
379test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
380commit $head3
381Test prin..ex bodies
382commit $head2
383changed (..dert) foo
384commit $head1
385added (hi..f${added_utf8_part}gt) foo
386EOF
387
388test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
389commit $head3
390.. of complex bodies
391commit $head2
392..ged (ge${changed_utf8_part}ndert) foo
393commit $head1
394.. (hinzugef${added_utf8_part}gt) foo
395EOF
396
0fe6df3c
AS
397test_format complex-body-commitencoding-unset %b <expected.utf-8
398
9130ac9f 399test_expect_success '%x00 shows NUL' '
77a6815d 400 echo >expect commit $head3 &&
9130ac9f
JK
401 echo >>expect fooQbar &&
402 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
403 nul_to_q <actual.nul >actual &&
404 test_cmp expect actual
405'
406
d36f8679
JK
407test_expect_success '%ad respects --date=' '
408 echo 2005-04-07 >expect.ad-short &&
409 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
410 test_cmp expect.ad-short output.ad-short
411'
412
f7ab5c79
JH
413test_expect_success 'empty email' '
414 test_tick &&
415 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
416 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
2ae4944a 417 verbose test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
f7ab5c79
JH
418'
419
9fa708da
JH
420test_expect_success 'del LF before empty (1)' '
421 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
3fb0459b 422 test_line_count = 2 actual
9fa708da
JH
423'
424
425test_expect_success 'del LF before empty (2)' '
426 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
3fb0459b 427 test_line_count = 6 actual &&
9fa708da
JH
428 grep "^$" actual
429'
430
431test_expect_success 'add LF before non-empty (1)' '
432 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
3fb0459b 433 test_line_count = 2 actual
9fa708da
JH
434'
435
436test_expect_success 'add LF before non-empty (2)' '
437 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
3fb0459b 438 test_line_count = 6 actual &&
9fa708da
JH
439 grep "^$" actual
440'
441
7b88176e
MG
442test_expect_success 'add SP before non-empty (1)' '
443 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
de6029a2 444 test $(wc -w <actual) = 3
7b88176e
MG
445'
446
447test_expect_success 'add SP before non-empty (2)' '
448 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
de6029a2 449 test $(wc -w <actual) = 6
7b88176e
MG
450'
451
c1977021
WP
452test_expect_success '--abbrev' '
453 echo SHORT SHORT SHORT >expect2 &&
454 echo LONG LONG LONG >expect3 &&
455 git log -1 --format="%h %h %h" HEAD >actual1 &&
456 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
457 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
2ece6ad2 458 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
459 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
c1977021
WP
460 test_cmp expect2 fuzzy2 &&
461 test_cmp expect3 fuzzy3 &&
462 ! test_cmp actual1 actual2
463'
464
465test_expect_success '%H is not affected by --abbrev-commit' '
466 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
467 len=$(wc -c <actual) &&
468 test $len = 41
469'
470
471test_expect_success '%h is not affected by --abbrev-commit' '
472 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
473 len=$(wc -c <actual) &&
474 test $len = 21
475'
476
8f8f5476
TR
477test_expect_success '"%h %gD: %gs" is same as git-reflog' '
478 git reflog >expect &&
479 git log -g --format="%h %gD: %gs" >actual &&
480 test_cmp expect actual
481'
482
483test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
484 git reflog --date=raw >expect &&
485 git log -g --format="%h %gD: %gs" --date=raw >actual &&
486 test_cmp expect actual
487'
488
c1977021
WP
489test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
490 git reflog --abbrev=13 --date=raw >expect &&
491 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
492 test_cmp expect actual
493'
494
8f8f5476
TR
495test_expect_success '%gd shortens ref name' '
496 echo "master@{0}" >expect.gd-short &&
497 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
498 test_cmp expect.gd-short actual.gd-short
499'
500
cd1957f5 501test_expect_success 'reflog identity' '
2ae4944a 502 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
cd1957f5
JK
503 git log -g -1 --format="%gn:%ge" >actual &&
504 test_cmp expect actual
505'
506
1fb5fdd2 507test_expect_success 'oneline with empty message' '
7b6ad979
EN
508 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
509 git commit --allow-empty --allow-empty-message &&
f02dd06e 510 git rev-list --oneline HEAD >test.txt &&
3fb0459b
SL
511 test_line_count = 5 test.txt &&
512 git rev-list --oneline --graph HEAD >testg.txt &&
513 test_line_count = 5 testg.txt
1fb5fdd2
EFL
514'
515
d9955fd6
JK
516test_expect_success 'single-character name is parsed correctly' '
517 git commit --author="a <a@example.com>" --allow-empty -m foo &&
518 echo "a <a@example.com>" >expect &&
519 git log -1 --format="%an <%ae>" >actual &&
520 test_cmp expect actual
521'
522
958b2eb2
JK
523test_expect_success 'unused %G placeholders are passed through' '
524 echo "%GX %G" >expect &&
525 git log -1 --format="%GX %G" >actual &&
526 test_cmp expect actual
527'
528
fa21b602 529test_done