revision: fix leaking parents when simplifying commits
[git/gitster.git] / t / t4205-log-pretty-formats.sh
blobeb63ce011fa646d0a8992bc505068fd5ac1a57e8
1 #!/bin/sh
3 # Copyright (c) 2010, Will Palmer
4 # Copyright (c) 2011, Alexey Shumkin (+ non-UTF-8 commit encoding tests)
7 test_description='Test pretty formats'
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
12 # Tested non-UTF-8 encoding
13 test_encoding="ISO8859-1"
15 sample_utf8_part=$(printf "f\303\244ng")
17 commit_msg () {
18 # String "initial. initial" partly in German
19 # (translated with Google Translate),
20 # encoded in UTF-8, used as a commit log message below.
21 msg="initial. an${sample_utf8_part}lich\n"
22 if test -n "$1"
23 then
24 printf "$msg" | iconv -f utf-8 -t "$1"
25 else
26 printf "$msg"
30 test_expect_success 'set up basic repos' '
31 >foo &&
32 >bar &&
33 git add foo &&
34 test_tick &&
35 test_config i18n.commitEncoding $test_encoding &&
36 commit_msg $test_encoding | git commit -F - &&
37 git add bar &&
38 test_tick &&
39 git commit -m "add bar"
42 test_expect_success 'alias builtin format' '
43 git log --pretty=oneline >expected &&
44 test_config pretty.test-alias oneline &&
45 git log --pretty=test-alias >actual &&
46 test_cmp expected actual
49 test_expect_success 'alias masking builtin format' '
50 git log --pretty=oneline >expected &&
51 test_config pretty.oneline "%H" &&
52 git log --pretty=oneline >actual &&
53 test_cmp expected actual
56 test_expect_success 'alias user-defined format' '
57 git log --pretty="format:%h" >expected &&
58 test_config pretty.test-alias "format:%h" &&
59 git log --pretty=test-alias >actual &&
60 test_cmp expected actual
63 test_expect_success 'alias user-defined format is matched case-insensitively' '
64 git log --pretty="format:%h" >expected &&
65 test_config pretty.testone "format:%h" &&
66 test_config pretty.testtwo testOne &&
67 git log --pretty=testTwo >actual &&
68 test_cmp expected actual
71 test_expect_success 'alias user-defined tformat with %s (ISO8859-1 encoding)' '
72 test_config i18n.logOutputEncoding $test_encoding &&
73 git log --oneline >expected-s &&
74 git log --pretty="tformat:%h %s" >actual-s &&
75 test_cmp expected-s actual-s
78 test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
79 git log --oneline >expected-s &&
80 git log --pretty="tformat:%h %s" >actual-s &&
81 test_cmp expected-s actual-s
84 test_expect_success 'alias user-defined tformat' '
85 git log --pretty="tformat:%h" >expected &&
86 test_config pretty.test-alias "tformat:%h" &&
87 git log --pretty=test-alias >actual &&
88 test_cmp expected actual
91 test_expect_success 'alias non-existent format' '
92 test_config pretty.test-alias format-that-will-never-exist &&
93 test_must_fail git log --pretty=test-alias
96 test_expect_success 'alias of an alias' '
97 git log --pretty="tformat:%h" >expected &&
98 test_config pretty.test-foo "tformat:%h" &&
99 test_config pretty.test-bar test-foo &&
100 git log --pretty=test-bar >actual && test_cmp expected actual
103 test_expect_success 'alias masking an alias' '
104 git log --pretty=format:"Two %H" >expected &&
105 test_config pretty.duplicate "format:One %H" &&
106 test_config pretty.duplicate "format:Two %H" --add &&
107 git log --pretty=duplicate >actual &&
108 test_cmp expected actual
111 test_expect_success 'alias loop' '
112 test_config pretty.test-foo test-bar &&
113 test_config pretty.test-bar test-foo &&
114 test_must_fail git log --pretty=test-foo
117 test_expect_success 'NUL separation' '
118 printf "add bar\0$(commit_msg)" >expected &&
119 git log -z --pretty="format:%s" >actual &&
120 test_cmp expected actual
123 test_expect_success 'NUL termination' '
124 printf "add bar\0$(commit_msg)\0" >expected &&
125 git log -z --pretty="tformat:%s" >actual &&
126 test_cmp expected actual
129 test_expect_success 'NUL separation with --stat' '
130 stat0_part=$(git diff --stat HEAD^ HEAD) &&
131 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
132 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
133 git log -z --stat --pretty="format:%s" >actual &&
134 test_cmp expected actual
137 test_expect_failure 'NUL termination with --stat' '
138 stat0_part=$(git diff --stat HEAD^ HEAD) &&
139 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
140 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n0" >expected &&
141 git log -z --stat --pretty="tformat:%s" >actual &&
142 test_cmp expected actual
145 for p in short medium full fuller email raw
147 test_expect_success "NUL termination with --reflog --pretty=$p" '
148 revs="$(git rev-list --reflog)" &&
149 for r in $revs
151 git show -s "$r" --pretty="$p" &&
152 printf "\0" || return 1
153 done >expect &&
155 git log -z --reflog --pretty="$p" &&
156 printf "\0"
157 } >actual &&
158 test_cmp expect actual
160 done
162 test_expect_success 'NUL termination with --reflog --pretty=oneline' '
163 revs="$(git rev-list --reflog)" &&
164 for r in $revs
166 git show -s --pretty=oneline "$r" >raw &&
167 lf_to_nul <raw || return 1
168 done >expect &&
169 # the trailing NUL is already produced so we do not need to
170 # output another one
171 git log -z --pretty=oneline --reflog >actual &&
172 test_cmp expect actual
175 test_expect_success 'setup more commits' '
176 test_commit "message one" one one message-one &&
177 test_commit "message two" two two message-two &&
178 head1=$(git rev-parse --verify --short HEAD~0) &&
179 head2=$(git rev-parse --verify --short HEAD~1) &&
180 head3=$(git rev-parse --verify --short HEAD~2) &&
181 head4=$(git rev-parse --verify --short HEAD~3)
184 test_expect_success 'left alignment formatting' '
185 git log --pretty="tformat:%<(40)%s" >actual &&
186 qz_to_tab_space <<-EOF >expected &&
187 message two Z
188 message one Z
189 add bar Z
190 $(commit_msg) Z
192 test_cmp expected actual
195 test_expect_success 'left alignment formatting. i18n.logOutputEncoding' '
196 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(40)%s" >actual &&
197 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
198 message two Z
199 message one Z
200 add bar Z
201 $(commit_msg) Z
203 test_cmp expected actual
206 test_expect_success 'left alignment formatting at the nth column' '
207 git log --pretty="tformat:%h %<|(40)%s" >actual &&
208 qz_to_tab_space <<-EOF >expected &&
209 $head1 message two Z
210 $head2 message one Z
211 $head3 add bar Z
212 $head4 $(commit_msg) Z
214 test_cmp expected actual
217 test_expect_success 'left alignment formatting at the nth column' '
218 COLUMNS=50 git log --pretty="tformat:%h %<|(-10)%s" >actual &&
219 qz_to_tab_space <<-EOF >expected &&
220 $head1 message two Z
221 $head2 message one Z
222 $head3 add bar Z
223 $head4 $(commit_msg) Z
225 test_cmp expected actual
228 test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
229 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
230 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
231 $head1 message two Z
232 $head2 message one Z
233 $head3 add bar Z
234 $head4 $(commit_msg) Z
236 test_cmp expected actual
239 test_expect_success 'left alignment formatting with no padding' '
240 git log --pretty="tformat:%<(1)%s" >actual &&
241 cat <<-EOF >expected &&
242 message two
243 message one
244 add bar
245 $(commit_msg)
247 test_cmp expected actual
250 test_expect_success 'left alignment formatting with no padding. i18n.logOutputEncoding' '
251 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(1)%s" >actual &&
252 cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
253 message two
254 message one
255 add bar
256 $(commit_msg)
258 test_cmp expected actual
261 test_expect_success 'left alignment formatting with trunc' '
262 git log --pretty="tformat:%<(10,trunc)%s" >actual &&
263 qz_to_tab_space <<-\EOF >expected &&
264 message ..
265 message ..
266 add bar Z
267 initial...
269 test_cmp expected actual
272 test_expect_success 'left alignment formatting with trunc. i18n.logOutputEncoding' '
273 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual &&
274 qz_to_tab_space <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
275 message ..
276 message ..
277 add bar Z
278 initial...
280 test_cmp expected actual
283 test_expect_success 'left alignment formatting with ltrunc' '
284 git log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
285 qz_to_tab_space <<-EOF >expected &&
286 ..sage two
287 ..sage one
288 add bar Z
289 ..${sample_utf8_part}lich
291 test_cmp expected actual
294 test_expect_success 'left alignment formatting with ltrunc. i18n.logOutputEncoding' '
295 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
296 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
297 ..sage two
298 ..sage one
299 add bar Z
300 ..${sample_utf8_part}lich
302 test_cmp expected actual
305 test_expect_success 'left alignment formatting with mtrunc' '
306 git log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
307 qz_to_tab_space <<-\EOF >expected &&
308 mess.. two
309 mess.. one
310 add bar Z
311 init..lich
313 test_cmp expected actual
316 test_expect_success 'left alignment formatting with mtrunc. i18n.logOutputEncoding' '
317 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
318 qz_to_tab_space <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
319 mess.. two
320 mess.. one
321 add bar Z
322 init..lich
324 test_cmp expected actual
327 test_expect_success 'right alignment formatting' '
328 git log --pretty="tformat:%>(40)%s" >actual &&
329 qz_to_tab_space <<-EOF >expected &&
330 Z message two
331 Z message one
332 Z add bar
333 Z $(commit_msg)
335 test_cmp expected actual
338 test_expect_success 'right alignment formatting. i18n.logOutputEncoding' '
339 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(40)%s" >actual &&
340 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
341 Z message two
342 Z message one
343 Z add bar
344 Z $(commit_msg)
346 test_cmp expected actual
349 test_expect_success 'right alignment formatting at the nth column' '
350 git log --pretty="tformat:%h %>|(40)%s" >actual &&
351 qz_to_tab_space <<-EOF >expected &&
352 $head1 message two
353 $head2 message one
354 $head3 add bar
355 $head4 $(commit_msg)
357 test_cmp expected actual
360 test_expect_success 'right alignment formatting at the nth column' '
361 COLUMNS=50 git log --pretty="tformat:%h %>|(-10)%s" >actual &&
362 qz_to_tab_space <<-EOF >expected &&
363 $head1 message two
364 $head2 message one
365 $head3 add bar
366 $head4 $(commit_msg)
368 test_cmp expected actual
371 test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
372 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
373 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
374 $head1 message two
375 $head2 message one
376 $head3 add bar
377 $head4 $(commit_msg)
379 test_cmp expected actual
382 # Note: Space between 'message' and 'two' should be in the same column
383 # as in previous test.
384 test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
385 git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
386 iconv -f utf-8 -t $test_encoding >expected <<-EOF &&
387 * $head1 message two
388 * $head2 message one
389 * $head3 add bar
390 * $head4 $(commit_msg)
392 test_cmp expected actual
395 test_expect_success 'right alignment formatting with no padding' '
396 git log --pretty="tformat:%>(1)%s" >actual &&
397 cat <<-EOF >expected &&
398 message two
399 message one
400 add bar
401 $(commit_msg)
403 test_cmp expected actual
406 test_expect_success 'right alignment formatting with no padding and with --graph' '
407 git log --graph --pretty="tformat:%>(1)%s" >actual &&
408 cat <<-EOF >expected &&
409 * message two
410 * message one
411 * add bar
412 * $(commit_msg)
414 test_cmp expected actual
417 test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
418 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
419 cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
420 message two
421 message one
422 add bar
423 $(commit_msg)
425 test_cmp expected actual
428 test_expect_success 'center alignment formatting' '
429 git log --pretty="tformat:%><(40)%s" >actual &&
430 qz_to_tab_space <<-EOF >expected &&
431 Z message two Z
432 Z message one Z
433 Z add bar Z
434 Z $(commit_msg) Z
436 test_cmp expected actual
439 test_expect_success 'center alignment formatting. i18n.logOutputEncoding' '
440 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(40)%s" >actual &&
441 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
442 Z message two Z
443 Z message one Z
444 Z add bar Z
445 Z $(commit_msg) Z
447 test_cmp expected actual
449 test_expect_success 'center alignment formatting at the nth column' '
450 git log --pretty="tformat:%h %><|(40)%s" >actual &&
451 qz_to_tab_space <<-EOF >expected &&
452 $head1 message two Z
453 $head2 message one Z
454 $head3 add bar Z
455 $head4 $(commit_msg) Z
457 test_cmp expected actual
460 test_expect_success 'center alignment formatting at the nth column' '
461 COLUMNS=70 git log --pretty="tformat:%h %><|(-30)%s" >actual &&
462 qz_to_tab_space <<-EOF >expected &&
463 $head1 message two Z
464 $head2 message one Z
465 $head3 add bar Z
466 $head4 $(commit_msg) Z
468 test_cmp expected actual
471 test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
472 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
473 qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
474 $head1 message two Z
475 $head2 message one Z
476 $head3 add bar Z
477 $head4 $(commit_msg) Z
479 test_cmp expected actual
482 test_expect_success 'center alignment formatting with no padding' '
483 git log --pretty="tformat:%><(1)%s" >actual &&
484 cat <<-EOF >expected &&
485 message two
486 message one
487 add bar
488 $(commit_msg)
490 test_cmp expected actual
493 # save HEAD's SHA-1 digest (with no abbreviations) to use it below
494 # as far as the next test amends HEAD
495 old_head1=$(git rev-parse --verify HEAD~0)
496 test_expect_success 'center alignment formatting with no padding. i18n.logOutputEncoding' '
497 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(1)%s" >actual &&
498 cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
499 message two
500 message one
501 add bar
502 $(commit_msg)
504 test_cmp expected actual
507 test_expect_success 'left/right alignment formatting with stealing' '
508 git commit --amend -m short --author "long long long <long@me.com>" &&
509 git log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
510 cat <<-\EOF >expected &&
511 short long long long
512 message .. A U Thor
513 add bar A U Thor
514 initial... A U Thor
516 test_cmp expected actual
518 test_expect_success 'left/right alignment formatting with stealing. i18n.logOutputEncoding' '
519 git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
520 cat <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
521 short long long long
522 message .. A U Thor
523 add bar A U Thor
524 initial... A U Thor
526 test_cmp expected actual
529 test_expect_success 'strbuf_utf8_replace() not producing NUL' '
530 git log --color --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)%C(auto)%d" |
531 test_decode_color |
532 nul_to_q >actual &&
533 ! grep Q actual
536 # --date=[XXX] and corresponding %a[X] %c[X] format equivalency
537 test_expect_success '--date=iso-strict %ad%cd is the same as %aI%cI' '
538 git log --format=%ad%n%cd --date=iso-strict >expected &&
539 git log --format=%aI%n%cI >actual &&
540 test_cmp expected actual
543 test_expect_success '--date=short %ad%cd is the same as %as%cs' '
544 git log --format=%ad%n%cd --date=short >expected &&
545 git log --format=%as%n%cs >actual &&
546 test_cmp expected actual
549 test_expect_success '--date=human %ad%cd is the same as %ah%ch' '
550 git log --format=%ad%n%cd --date=human >expected &&
551 git log --format=%ah%n%ch >actual &&
552 test_cmp expected actual
555 # get new digests (with no abbreviations)
556 test_expect_success 'set up log decoration tests' '
557 head1=$(git rev-parse --verify HEAD~0) &&
558 head2=$(git rev-parse --verify HEAD~1)
561 test_expect_success 'log decoration properly follows tag chain' '
562 git tag -a tag1 -m tag1 &&
563 git tag -a tag2 -m tag2 tag1 &&
564 git tag -d tag1 &&
565 git commit --amend -m shorter &&
566 git log --no-walk --tags --pretty="%H %d" --decorate=full >actual &&
567 cat <<-EOF >expected &&
568 $head2 (tag: refs/tags/message-one)
569 $old_head1 (tag: refs/tags/message-two)
570 $head1 (tag: refs/tags/tag2)
572 sort -k3 actual >actual1 &&
573 test_cmp expected actual1
576 test_expect_success 'clean log decoration' '
577 git log --no-walk --tags --pretty="%H %D" --decorate=full >actual &&
578 cat >expected <<-EOF &&
579 $head2 tag: refs/tags/message-one
580 $old_head1 tag: refs/tags/message-two
581 $head1 tag: refs/tags/tag2
583 sort -k3 actual >actual1 &&
584 test_cmp expected actual1
587 test_expect_success 'pretty format %decorate' '
588 git checkout -b foo &&
589 git commit --allow-empty -m "new commit" &&
590 git tag bar &&
591 git branch qux &&
593 echo " (HEAD -> foo, tag: bar, qux)" >expect1 &&
594 git log --format="%(decorate)" -1 >actual1 &&
595 test_cmp expect1 actual1 &&
597 echo "HEAD -> foo, tag: bar, qux" >expect2 &&
598 git log --format="%(decorate:prefix=,suffix=)" -1 >actual2 &&
599 test_cmp expect2 actual2 &&
601 echo "[ bar; qux; foo ]" >expect3 &&
602 git log --format="%(decorate:prefix=[ ,suffix= ],separator=%x3B ,tag=)" \
603 --decorate-refs=refs/ -1 >actual3 &&
604 test_cmp expect3 actual3 &&
606 # Try with a typo (in "separator"), in which case the placeholder should
607 # not be replaced.
608 echo "%(decorate:prefix=[ ,suffix= ],separater=; )" >expect4 &&
609 git log --format="%(decorate:prefix=[ ,suffix= ],separater=%x3B )" \
610 -1 >actual4 &&
611 test_cmp expect4 actual4 &&
613 echo "HEAD->foo bar qux" >expect5 &&
614 git log --format="%(decorate:prefix=,suffix=,separator= ,tag=,pointer=->)" \
615 -1 >actual5 &&
616 test_cmp expect5 actual5
619 cat >trailers <<EOF
620 Signed-off-by: A U Thor <author@example.com>
621 Acked-by: A U Thor <author@example.com>
622 [ v2 updated patch description ]
623 Signed-off-by: A U Thor
624 <author@example.com>
627 unfold () {
628 perl -0pe 's/\n\s+/ /g'
631 test_expect_success 'set up trailer tests' '
632 echo "Some contents" >trailerfile &&
633 git add trailerfile &&
634 git commit -F - <<-EOF
635 trailers: this commit message has trailers
637 This commit is a test commit with trailers at the end. We parse this
638 message and display the trailers using %(trailers).
640 $(cat trailers)
644 test_expect_success 'pretty format %(trailers) shows trailers' '
645 git log --no-walk --pretty="%(trailers)" >actual &&
647 cat trailers &&
648 echo
649 } >expect &&
650 test_cmp expect actual
653 test_expect_success 'pretty format %(trailers:) enables no options' '
654 git log --no-walk --pretty="%(trailers:)" >actual &&
655 # "expect" the same as the test above
656 test_cmp expect actual
659 test_expect_success '%(trailers:only) shows only "key: value" trailers' '
660 git log --no-walk --pretty="%(trailers:only)" >actual &&
662 grep -v patch.description <trailers &&
663 echo
664 } >expect &&
665 test_cmp expect actual
668 test_expect_success '%(trailers:only=yes) shows only "key: value" trailers' '
669 git log --no-walk --pretty=format:"%(trailers:only=yes)" >actual &&
670 grep -v patch.description <trailers >expect &&
671 test_cmp expect actual
674 test_expect_success '%(trailers:only=no) shows all trailers' '
675 git log --no-walk --pretty=format:"%(trailers:only=no)" >actual &&
676 cat trailers >expect &&
677 test_cmp expect actual
680 test_expect_success '%(trailers:only=no,only=true) shows only "key: value" trailers' '
681 git log --no-walk --pretty=format:"%(trailers:only=yes)" >actual &&
682 grep -v patch.description <trailers >expect &&
683 test_cmp expect actual
686 test_expect_success '%(trailers:unfold) unfolds trailers' '
687 git log --no-walk --pretty="%(trailers:unfold)" >actual &&
689 unfold <trailers &&
690 echo
691 } >expect &&
692 test_cmp expect actual
695 test_expect_success ':only and :unfold work together' '
696 git log --no-walk --pretty="%(trailers:only,unfold)" >actual &&
697 git log --no-walk --pretty="%(trailers:unfold,only)" >reverse &&
698 test_cmp actual reverse &&
700 grep -v patch.description <trailers | unfold &&
701 echo
702 } >expect &&
703 test_cmp expect actual
706 test_expect_success 'pretty format %(trailers:key=foo) shows that trailer' '
707 git log --no-walk --pretty="format:%(trailers:key=Acked-by)" >actual &&
708 echo "Acked-by: A U Thor <author@example.com>" >expect &&
709 test_cmp expect actual
712 test_expect_success 'pretty format %(trailers:key=foo) is case insensitive' '
713 git log --no-walk --pretty="format:%(trailers:key=AcKed-bY)" >actual &&
714 echo "Acked-by: A U Thor <author@example.com>" >expect &&
715 test_cmp expect actual
718 test_expect_success 'pretty format %(trailers:key=foo:) trailing colon also works' '
719 git log --no-walk --pretty="format:%(trailers:key=Acked-by:)" >actual &&
720 echo "Acked-by: A U Thor <author@example.com>" >expect &&
721 test_cmp expect actual
724 test_expect_success 'pretty format %(trailers:key=foo) multiple keys' '
725 git log --no-walk --pretty="format:%(trailers:key=Acked-by:,key=Signed-off-By)" >actual &&
726 grep -v patch.description <trailers >expect &&
727 test_cmp expect actual
730 test_expect_success '%(trailers:key=nonexistent) becomes empty' '
731 git log --no-walk --pretty="x%(trailers:key=Nacked-by)x" >actual &&
732 echo "xx" >expect &&
733 test_cmp expect actual
736 test_expect_success '%(trailers:key=foo) handles multiple lines even if folded' '
737 git log --no-walk --pretty="format:%(trailers:key=Signed-Off-by)" >actual &&
738 grep -v patch.description <trailers | grep -v Acked-by >expect &&
739 test_cmp expect actual
742 test_expect_success '%(trailers:key=foo,unfold) properly unfolds' '
743 git log --no-walk --pretty="format:%(trailers:key=Signed-Off-by,unfold)" >actual &&
744 unfold <trailers | grep Signed-off-by >expect &&
745 test_cmp expect actual
748 test_expect_success 'pretty format %(trailers:key=foo,only=no) also includes nontrailer lines' '
749 git log --no-walk --pretty="format:%(trailers:key=Acked-by,only=no)" >actual &&
751 echo "Acked-by: A U Thor <author@example.com>" &&
752 grep patch.description <trailers
753 } >expect &&
754 test_cmp expect actual
757 test_expect_success '%(trailers:key) without value is error' '
758 git log --no-walk --pretty="tformat:%(trailers:key)" >actual &&
759 echo "%(trailers:key)" >expect &&
760 test_cmp expect actual
763 test_expect_success '%(trailers:keyonly) shows only keys' '
764 git log --no-walk --pretty="format:%(trailers:keyonly)" >actual &&
765 test_write_lines \
766 "Signed-off-by" \
767 "Acked-by" \
768 "[ v2 updated patch description ]" \
769 "Signed-off-by" >expect &&
770 test_cmp expect actual
773 test_expect_success '%(trailers:key=foo,keyonly) shows only key' '
774 git log --no-walk --pretty="format:%(trailers:key=Acked-by,keyonly)" >actual &&
775 echo "Acked-by" >expect &&
776 test_cmp expect actual
779 test_expect_success '%(trailers:key=foo,valueonly) shows only value' '
780 git log --no-walk --pretty="format:%(trailers:key=Acked-by,valueonly)" >actual &&
781 echo "A U Thor <author@example.com>" >expect &&
782 test_cmp expect actual
785 test_expect_success '%(trailers:valueonly) shows only values' '
786 git log --no-walk --pretty="format:%(trailers:valueonly)" >actual &&
787 test_write_lines \
788 "A U Thor <author@example.com>" \
789 "A U Thor <author@example.com>" \
790 "[ v2 updated patch description ]" \
791 "A U Thor" \
792 " <author@example.com>" >expect &&
793 test_cmp expect actual
796 test_expect_success '%(trailers:key=foo,keyonly,valueonly) shows nothing' '
797 git log --no-walk --pretty="format:%(trailers:key=Acked-by,keyonly,valueonly)" >actual &&
798 echo >expect &&
799 test_cmp expect actual
802 test_expect_success 'pretty format %(trailers:separator) changes separator' '
803 git log --no-walk --pretty=format:"X%(trailers:separator=%x00)X" >actual &&
805 printf "XSigned-off-by: A U Thor <author@example.com>\0" &&
806 printf "Acked-by: A U Thor <author@example.com>\0" &&
807 printf "[ v2 updated patch description ]\0" &&
808 printf "Signed-off-by: A U Thor\n <author@example.com>X"
809 ) >expect &&
810 test_cmp expect actual
813 test_expect_success 'pretty format %(trailers:separator=X,unfold) changes separator' '
814 git log --no-walk --pretty=format:"X%(trailers:separator=%x00,unfold)X" >actual &&
816 printf "XSigned-off-by: A U Thor <author@example.com>\0" &&
817 printf "Acked-by: A U Thor <author@example.com>\0" &&
818 printf "[ v2 updated patch description ]\0" &&
819 printf "Signed-off-by: A U Thor <author@example.com>X"
820 ) >expect &&
821 test_cmp expect actual
824 test_expect_success 'pretty format %(trailers:key_value_separator) changes key-value separator' '
825 git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00)X" >actual &&
827 printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
828 printf "Acked-by\0A U Thor <author@example.com>\n" &&
829 printf "[ v2 updated patch description ]\n" &&
830 printf "Signed-off-by\0A U Thor\n <author@example.com>\nX"
831 ) >expect &&
832 test_cmp expect actual
835 test_expect_success 'pretty format %(trailers:key_value_separator,unfold) changes key-value separator' '
836 git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00,unfold)X" >actual &&
838 printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
839 printf "Acked-by\0A U Thor <author@example.com>\n" &&
840 printf "[ v2 updated patch description ]\n" &&
841 printf "Signed-off-by\0A U Thor <author@example.com>\nX"
842 ) >expect &&
843 test_cmp expect actual
846 test_expect_success 'pretty format %(trailers:separator,key_value_separator) changes both separators' '
847 git log --no-walk --pretty=format:"%(trailers:separator=%x00,key_value_separator=%x00%x00,unfold)" >actual &&
849 printf "Signed-off-by\0\0A U Thor <author@example.com>\0" &&
850 printf "Acked-by\0\0A U Thor <author@example.com>\0" &&
851 printf "[ v2 updated patch description ]\0" &&
852 printf "Signed-off-by\0\0A U Thor <author@example.com>"
853 ) >expect &&
854 test_cmp expect actual
857 test_expect_success 'pretty format %(trailers) combining separator/key/keyonly/valueonly' '
858 git commit --allow-empty -F - <<-\EOF &&
859 Important fix
861 The fix is explained here
863 Closes: #1234
866 git commit --allow-empty -F - <<-\EOF &&
867 Another fix
869 The fix is explained here
871 Closes: #567
872 Closes: #890
875 git commit --allow-empty -F - <<-\EOF &&
876 Does not close any tickets
879 git log --pretty="%s% (trailers:separator=%x2c%x20,key=Closes,valueonly)" HEAD~3.. >actual &&
880 test_write_lines \
881 "Does not close any tickets" \
882 "Another fix #567, #890" \
883 "Important fix #1234" >expect &&
884 test_cmp expect actual &&
886 git log --pretty="%s% (trailers:separator=%x2c%x20,key=Closes,keyonly)" HEAD~3.. >actual &&
887 test_write_lines \
888 "Does not close any tickets" \
889 "Another fix Closes, Closes" \
890 "Important fix Closes" >expect &&
891 test_cmp expect actual
894 test_expect_success 'trailer parsing not fooled by --- line' '
895 git commit --allow-empty -F - <<-\EOF &&
896 this is the subject
898 This is the body. The message has a "---" line which would confuse a
899 message+patch parser. But here we know we have only a commit message,
900 so we get it right.
902 trailer: wrong
904 This is more body.
906 trailer: right
910 echo "trailer: right" &&
911 echo
912 } >expect &&
913 git log --no-walk --format="%(trailers)" >actual &&
914 test_cmp expect actual
917 test_expect_success 'set up %S tests' '
918 git checkout --orphan source-a &&
919 test_commit one &&
920 test_commit two &&
921 git checkout -b source-b HEAD^ &&
922 test_commit three
925 test_expect_success 'log --format=%S paints branch names' '
926 cat >expect <<-\EOF &&
927 source-b
928 source-a
929 source-b
931 git log --format=%S source-a source-b >actual &&
932 test_cmp expect actual
935 test_expect_success 'log --format=%S paints tag names' '
936 git tag -m tagged source-tag &&
937 cat >expect <<-\EOF &&
938 source-tag
939 source-a
940 source-tag
942 git log --format=%S source-tag source-a >actual &&
943 test_cmp expect actual
946 test_expect_success 'log --format=%S paints symmetric ranges' '
947 cat >expect <<-\EOF &&
948 source-b
949 source-a
951 git log --format=%S source-a...source-b >actual &&
952 test_cmp expect actual
955 test_expect_success '%S in git log --format works with other placeholders (part 1)' '
956 git log --format="source-b %h" source-b >expect &&
957 git log --format="%S %h" source-b >actual &&
958 test_cmp expect actual
961 test_expect_success '%S in git log --format works with other placeholders (part 2)' '
962 git log --format="%h source-b" source-b >expect &&
963 git log --format="%h %S" source-b >actual &&
964 test_cmp expect actual
967 test_expect_success 'setup more commits for %S with --bisect' '
968 test_commit four &&
969 test_commit five &&
971 head1=$(git rev-parse --verify HEAD~0) &&
972 head2=$(git rev-parse --verify HEAD~1) &&
973 head3=$(git rev-parse --verify HEAD~2) &&
974 head4=$(git rev-parse --verify HEAD~3)
977 test_expect_success '%S with --bisect labels commits with refs/bisect/bad ref' '
978 git update-ref refs/bisect/bad-$head1 $head1 &&
979 git update-ref refs/bisect/go $head1 &&
980 git update-ref refs/bisect/bad-$head2 $head2 &&
981 git update-ref refs/bisect/b $head3 &&
982 git update-ref refs/bisect/bad-$head4 $head4 &&
983 git update-ref refs/bisect/good-$head4 $head4 &&
985 # We expect to see the range of commits betwee refs/bisect/good-$head4
986 # and refs/bisect/bad-$head1. The "source" ref is the nearest bisect ref
987 # from which the commit is reachable.
988 cat >expect <<-EOF &&
989 $head1 refs/bisect/bad-$head1
990 $head2 refs/bisect/bad-$head2
991 $head3 refs/bisect/bad-$head2
993 git log --bisect --format="%H %S" >actual &&
994 test_cmp expect actual
997 test_expect_success 'log --pretty=reference' '
998 git log --pretty="tformat:%h (%s, %as)" >expect &&
999 git log --pretty=reference >actual &&
1000 test_cmp expect actual
1003 test_expect_success 'log --pretty=reference with log.date is overridden by short date' '
1004 git log --pretty="tformat:%h (%s, %as)" >expect &&
1005 test_config log.date rfc &&
1006 git log --pretty=reference >actual &&
1007 test_cmp expect actual
1010 test_expect_success 'log --pretty=reference with explicit date overrides short date' '
1011 git log --date=rfc --pretty="tformat:%h (%s, %ad)" >expect &&
1012 git log --date=rfc --pretty=reference >actual &&
1013 test_cmp expect actual
1016 test_expect_success 'log --pretty=reference is never unabbreviated' '
1017 git log --pretty="tformat:%h (%s, %as)" >expect &&
1018 git log --no-abbrev-commit --pretty=reference >actual &&
1019 test_cmp expect actual
1022 test_expect_success 'log --pretty=reference is never decorated' '
1023 git log --pretty="tformat:%h (%s, %as)" >expect &&
1024 git log --decorate=short --pretty=reference >actual &&
1025 test_cmp expect actual
1028 test_expect_success 'log --pretty=reference does not output reflog info' '
1029 git log --walk-reflogs --pretty="tformat:%h (%s, %as)" >expect &&
1030 git log --walk-reflogs --pretty=reference >actual &&
1031 test_cmp expect actual
1034 test_expect_success 'log --pretty=reference is colored appropriately' '
1035 git log --color=always --pretty="tformat:%C(auto)%h (%s, %as)" >expect &&
1036 git log --color=always --pretty=reference >actual &&
1037 test_cmp expect actual
1040 test_expect_success '%(describe) vs git describe' '
1041 git log --format="%H" | while read hash
1043 if desc=$(git describe $hash)
1044 then
1045 : >expect-contains-good
1046 else
1047 : >expect-contains-bad
1048 fi &&
1049 echo "$hash $desc" || return 1
1050 done >expect &&
1051 test_path_exists expect-contains-good &&
1052 test_path_exists expect-contains-bad &&
1054 git log --format="%H %(describe)" >actual 2>err &&
1055 test_cmp expect actual &&
1056 test_must_be_empty err
1059 test_expect_success '%(describe:match=...) vs git describe --match ...' '
1060 test_when_finished "git tag -d tag-match" &&
1061 git tag -a -m tagged tag-match &&
1062 git describe --match "*-match" >expect &&
1063 git log -1 --format="%(describe:match=*-match)" >actual &&
1064 test_cmp expect actual
1067 test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
1068 test_when_finished "git tag -d tag-exclude" &&
1069 git tag -a -m tagged tag-exclude &&
1070 git describe --exclude "*-exclude" >expect &&
1071 git log -1 --format="%(describe:exclude=*-exclude)" >actual &&
1072 test_cmp expect actual
1075 test_expect_success '%(describe:tags) vs git describe --tags' '
1076 test_when_finished "git tag -d tagname" &&
1077 git tag tagname &&
1078 git describe --tags >expect &&
1079 git log -1 --format="%(describe:tags)" >actual &&
1080 test_cmp expect actual
1083 test_expect_success '%(describe:abbrev=...) vs git describe --abbrev=...' '
1084 test_when_finished "git tag -d tagname" &&
1086 # Case 1: We have commits between HEAD and the most recent tag
1087 # reachable from it
1088 test_commit --no-tag file &&
1089 git describe --abbrev=15 >expect &&
1090 git log -1 --format="%(describe:abbrev=15)" >actual &&
1091 test_cmp expect actual &&
1093 # Make sure the hash used is at least 15 digits long
1094 sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
1095 test 16 -le $(wc -c <hexpart) &&
1097 # Case 2: We have a tag at HEAD, describe directly gives the
1098 # name of the tag
1099 git tag -a -m tagged tagname &&
1100 git describe --abbrev=15 >expect &&
1101 git log -1 --format="%(describe:abbrev=15)" >actual &&
1102 test_cmp expect actual &&
1103 test tagname = $(cat actual)
1106 test_expect_success 'log --pretty with space stealing' '
1107 printf mm0 >expect &&
1108 git log -1 --pretty="format:mm%>>|(1)%x30" >actual &&
1109 test_cmp expect actual
1112 test_expect_success 'log --pretty with invalid padding format' '
1113 printf "%s%%<(20" "$(git rev-parse HEAD)" >expect &&
1114 git log -1 --pretty="format:%H%<(20" >actual &&
1115 test_cmp expect actual
1118 test_expect_success 'log --pretty with magical wrapping directives' '
1119 commit_id=$(git commit-tree HEAD^{tree} -m "describe me") &&
1120 git tag describe-me $commit_id &&
1121 printf "\n(tag:\ndescribe-me)%%+w(2)" >expect &&
1122 git log -1 --pretty="format:%w(1)%+d%+w(2)" $commit_id >actual &&
1123 test_cmp expect actual
1126 test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing wrapping directive' '
1127 printf "%%w(2147483649,1,1)0" >expect &&
1128 git log -1 --pretty="format:%w(2147483649,1,1)%x30" >actual &&
1129 test_cmp expect actual &&
1130 printf "%%w(1,2147483649,1)0" >expect &&
1131 git log -1 --pretty="format:%w(1,2147483649,1)%x30" >actual &&
1132 test_cmp expect actual &&
1133 printf "%%w(1,1,2147483649)0" >expect &&
1134 git log -1 --pretty="format:%w(1,1,2147483649)%x30" >actual &&
1135 test_cmp expect actual
1138 test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing padding directive' '
1139 printf "%%<(2147483649)0" >expect &&
1140 git log -1 --pretty="format:%<(2147483649)%x30" >actual &&
1141 test_cmp expect actual
1144 test_expect_success 'log --pretty with padding and preceding control chars' '
1145 printf "\20\20 0" >expect &&
1146 git log -1 --pretty="format:%x10%x10%>|(4)%x30" >actual &&
1147 test_cmp expect actual
1150 test_expect_success 'log --pretty truncation with control chars' '
1151 test_commit "$(printf "\20\20\20\20xxxx")" file contents commit-with-control-chars &&
1152 printf "\20\20\20\20x.." >expect &&
1153 git log -1 --pretty="format:%<(3,trunc)%s" commit-with-control-chars >actual &&
1154 test_cmp expect actual
1157 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
1158 # We only assert that this command does not crash. This needs to be
1159 # executed with the address sanitizer to demonstrate failure.
1160 git log -1 --pretty="format:%>(2147483646)%x41%41%>(2147483646)%x41" >/dev/null
1163 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'set up huge commit' '
1164 test-tool genzeros 2147483649 | tr "\000" "1" >expect &&
1165 huge_commit=$(git commit-tree -F expect HEAD^{tree})
1168 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
1169 git log -1 --format="%B%<(1)%x30" $huge_commit >actual &&
1170 echo 0 >>expect &&
1171 test_cmp expect actual
1174 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message does not cause allocation failure' '
1175 test_must_fail git log -1 --format="%<(1)%B" $huge_commit 2>error &&
1176 cat >expect <<-EOF &&
1177 fatal: number too large to represent as int on this platform: 2147483649
1179 test_cmp expect error
1182 # pretty-formats note wide char limitations, and add tests
1183 test_expect_failure 'wide and decomposed characters column counting' '
1185 # from t/lib-unicode-nfc-nfd.sh hex values converted to octal
1186 utf8_nfc=$(printf "\303\251") && # e acute combined.
1187 utf8_nfd=$(printf "\145\314\201") && # e with a combining acute (i.e. decomposed)
1188 utf8_emoji=$(printf "\360\237\221\250") &&
1190 # replacement character when requesting a wide char fits in a single display colum.
1191 # "half wide" alternative could be a plain ASCII dot `.`
1192 utf8_vert_ell=$(printf "\342\213\256") &&
1194 # use ${xxx} here!
1195 nfc10="${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}" &&
1196 nfd10="${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}" &&
1197 emoji5="${utf8_emoji}${utf8_emoji}${utf8_emoji}${utf8_emoji}${utf8_emoji}" &&
1198 # emoji5 uses 10 display columns
1200 test_commit "abcdefghij" &&
1201 test_commit --no-tag "${nfc10}" &&
1202 test_commit --no-tag "${nfd10}" &&
1203 test_commit --no-tag "${emoji5}" &&
1204 printf "${utf8_emoji}..${utf8_emoji}${utf8_vert_ell}\n${utf8_nfd}..${utf8_nfd}${utf8_nfd}\n${utf8_nfc}..${utf8_nfc}${utf8_nfc}\na..ij\n" >expected &&
1205 git log --format="%<(5,mtrunc)%s" -4 >actual &&
1206 test_cmp expected actual
1209 test_done