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