Merge branch 'jr/doc-ignore-typofix'
[git] / t / t6006-rev-list-format.sh
1 #!/bin/sh
2
3 # Copyright (c) 2009 Jens Lehmann
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5
6 test_description='git rev-list --pretty=format test'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-terminal.sh
13
14 test_tick
15 # Tested non-UTF-8 encoding
16 test_encoding="ISO8859-1"
17
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)
25 # same but "changed"
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)
30
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
35 truncate_count=20
36
37 test_expect_success 'setup' '
38         : >foo &&
39         git add foo &&
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
53 '
54
55 # usage: test_format name format_string [failure] <expected_output
56 test_format () {
57         cat >expect.$1
58         test_expect_${3:-success} "format $1" "
59                 git rev-list --pretty=format:'$2' main >output.$1 &&
60                 test_cmp expect.$1 output.$1
61         "
62 }
63
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)'
69 has_color () {
70         test_decode_color <"$1" >decoded &&
71         echo "<RED>foo<RESET>" >expect &&
72         test_cmp expect decoded
73 }
74
75 has_no_color () {
76         echo foo >expect &&
77         test_cmp expect "$1"
78 }
79
80 test_format percent %%h <<EOF
81 commit $head2
82 %h
83 commit $head1
84 %h
85 EOF
86
87 test_format hash %H%n%h <<EOF
88 commit $head2
89 $head2
90 $head2_short
91 commit $head1
92 $head1
93 $head1_short
94 EOF
95
96 test_format tree %T%n%t <<EOF
97 commit $head2
98 $tree2
99 $tree2_short
100 commit $head1
101 $tree1
102 $tree1_short
103 EOF
104
105 test_format parents %P%n%p <<EOF
106 commit $head2
107 $head1
108 $head1_short
109 commit $head1
110
111
112 EOF
113
114 # we don't test relative here
115 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
116 commit $head2
117 $GIT_AUTHOR_NAME
118 $GIT_AUTHOR_EMAIL
119 $TEST_AUTHOR_LOCALNAME
120 Thu Apr 7 15:13:13 2005 -0700
121 Thu, 7 Apr 2005 15:13:13 -0700
122 1112911993
123 commit $head1
124 $GIT_AUTHOR_NAME
125 $GIT_AUTHOR_EMAIL
126 $TEST_AUTHOR_LOCALNAME
127 Thu Apr 7 15:13:13 2005 -0700
128 Thu, 7 Apr 2005 15:13:13 -0700
129 1112911993
130 EOF
131
132 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
133 commit $head2
134 $GIT_COMMITTER_NAME
135 $GIT_COMMITTER_EMAIL
136 $TEST_COMMITTER_LOCALNAME
137 Thu Apr 7 15:13:13 2005 -0700
138 Thu, 7 Apr 2005 15:13:13 -0700
139 1112911993
140 commit $head1
141 $GIT_COMMITTER_NAME
142 $GIT_COMMITTER_EMAIL
143 $TEST_COMMITTER_LOCALNAME
144 Thu Apr 7 15:13:13 2005 -0700
145 Thu, 7 Apr 2005 15:13:13 -0700
146 1112911993
147 EOF
148
149 test_format encoding %e <<EOF
150 commit $head2
151 $test_encoding
152 commit $head1
153 $test_encoding
154 EOF
155
156 test_format subject %s <<EOF
157 commit $head2
158 $changed
159 commit $head1
160 $added
161 EOF
162
163 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
164 commit $head2
165 changed (ge${changed_utf8_part}ndert)..
166 commit $head1
167 added (hinzugef${added_utf8_part}gt..
168 EOF
169
170 test_format body %b <<EOF
171 commit $head2
172 commit $head1
173 EOF
174
175 test_format raw-body %B <<EOF
176 commit $head2
177 $changed
178
179 commit $head1
180 $added
181
182 EOF
183
184 test_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" &&
190         git rev-list --color --format="$format" -1 main >actual.raw &&
191         test_decode_color <actual.raw >actual &&
192         test_cmp expect actual
193 '
194
195 test_expect_success '%S is not a placeholder for rev-list yet' '
196         git rev-list --format="%S" -1 main | grep "%S"
197 '
198
199 test_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)" &&
205         git rev-list --color --format="$format" -1 main >actual.raw &&
206         test_decode_color <actual.raw >actual &&
207         test_cmp expect actual
208 '
209
210 for spec in \
211         "%Cred:$BASIC_COLOR" \
212         "%C(...):$COLOR" \
213         "%C(auto,...):$AUTO_COLOR"
214 do
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         '
221
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
232         test_expect_success "$desc respects --color" '
233                 git log --format=$color -1 --color >actual &&
234                 has_color actual
235         '
236
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
242         test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
243                 test_terminal git log --format=$color -1 --color=auto >actual &&
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         '
254 done
255
256 test_expect_success '%C(always,...) enables color even without tty' '
257         git log --format=$ALWAYS_COLOR -1 >actual &&
258         has_color actual
259 '
260
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
266 '
267
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
272 '
273
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)
280         <GREEN>foo<RESET>
281         EOF
282         test_cmp expect actual
283 '
284
285 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
286 Test printing of complex bodies
287
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!
291 EOF
292
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)
298 '
299
300 test_format complex-encoding %e <<EOF
301 commit $head3
302 $test_encoding
303 commit $head2
304 $test_encoding
305 commit $head1
306 $test_encoding
307 EOF
308
309 test_format complex-subject %s <<EOF
310 commit $head3
311 Test printing of complex bodies
312 commit $head2
313 $changed_iso88591
314 commit $head1
315 $added_iso88591
316 EOF
317
318 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
319 commit $head3
320 Test printing of c..
321 commit $head2
322 changed (ge${changed_utf8_part_iso88591}ndert)..
323 commit $head1
324 added (hinzugef${added_utf8_part_iso88591}gt..
325 EOF
326
327 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
328 commit $head3
329 Test prin..ex bodies
330 commit $head2
331 changed (..dert) foo
332 commit $head1
333 added (hi..f${added_utf8_part_iso88591}gt) foo
334 EOF
335
336 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
337 commit $head3
338 .. of complex bodies
339 commit $head2
340 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
341 commit $head1
342 .. (hinzugef${added_utf8_part_iso88591}gt) foo
343 EOF
344
345 test_expect_success 'setup expected messages (for test %b)' '
346         cat <<-EOF >expected.utf-8 &&
347         commit $head3
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!
351
352         commit $head2
353         commit $head1
354         EOF
355         iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
356 '
357
358 test_format complex-body %b <expected.ISO8859-1
359
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
363
364 test_format complex-subject-commitencoding-unset %s <<EOF
365 commit $head3
366 Test printing of complex bodies
367 commit $head2
368 $changed
369 commit $head1
370 $added
371 EOF
372
373 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
374 commit $head3
375 Test printing of c..
376 commit $head2
377 changed (ge${changed_utf8_part}ndert)..
378 commit $head1
379 added (hinzugef${added_utf8_part}gt..
380 EOF
381
382 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
383 commit $head3
384 Test prin..ex bodies
385 commit $head2
386 changed (..dert) foo
387 commit $head1
388 added (hi..f${added_utf8_part}gt) foo
389 EOF
390
391 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
392 commit $head3
393 .. of complex bodies
394 commit $head2
395 ..ged (ge${changed_utf8_part}ndert) foo
396 commit $head1
397 .. (hinzugef${added_utf8_part}gt) foo
398 EOF
399
400 test_format complex-body-commitencoding-unset %b <expected.utf-8
401
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
408 '
409
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
414 '
415
416 test_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) &&
420         verbose test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
421 '
422
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
426 '
427
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 &&
431         grep "^$" actual
432 '
433
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
437 '
438
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 &&
442         grep "^$" actual
443 '
444
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
448 '
449
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
453 '
454
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
466 '
467
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
473 '
474
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) &&
478         test $len = 21
479 '
480
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
485 '
486
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
491 '
492
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
497 '
498
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
503 '
504
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
509 '
510
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
518 '
519
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
525 '
526
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
531 '
532
533 test_done