interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t6006-rev-list-format.sh

642 lines · 16081 bytes Raw Blame History
1#!/bin/sh
2
3# Copyright (c) 2009 Jens Lehmann
4# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5
6test_description='git rev-list --pretty=format test'
7
8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11. ./test-lib.sh
12. "$TEST_DIRECTORY"/lib-terminal.sh
13
14test_tick
15
16if test_have_prereq ICONV
17then
18 # Tested non-UTF-8 encoding
19 test_encoding="ISO8859-1"
20
21 # String "added" in German
22 # (translated with Google Translate),
23 # encoded in UTF-8, used as a commit log message below.
24 added_utf8_part=$(printf "\303\274")
25 added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
26 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
27 added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
28 # same but "changed"
29 changed_utf8_part=$(printf "\303\244")
30 changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
31 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
32 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
33else
34 # Tested non-UTF-8 encoding
35 test_encoding="UTF-8"
36
37 # String "added" in German
38 # (translated with Google Translate),
39 # encoded in UTF-8, used as a commit log message below.
40 added_utf8_part="u"
41 added_utf8_part_iso88591="u"
42 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
43 added_iso88591="$added"
44 # same but "changed"
45 changed_utf8_part="a"
46 changed_utf8_part_iso88591="a"
47 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
48 changed_iso88591="$changed"
49fi
50
51# Count of char to truncate
52# Number is chosen so, that non-ACSII characters
53# (see $added_utf8_part and $changed_utf8_part)
54# fall into truncated parts of appropriate words both from left and right
55truncate_count=20
56
57test_expect_success 'setup' '
58 : >foo &&
59 git add foo &&
60 git config i18n.commitEncoding $test_encoding &&
61 echo "$added_iso88591" | git commit -F - &&
62 head1=$(git rev-parse --verify HEAD) &&
63 head1_short=$(git rev-parse --verify --short $head1) &&
64 head1_short4=$(git rev-parse --verify --short=4 $head1) &&
65 tree1=$(git rev-parse --verify HEAD:) &&
66 tree1_short=$(git rev-parse --verify --short $tree1) &&
67 echo "$changed" > foo &&
68 echo "$changed_iso88591" | git commit -a -F - &&
69 head2=$(git rev-parse --verify HEAD) &&
70 head2_short=$(git rev-parse --verify --short $head2) &&
71 head2_short4=$(git rev-parse --verify --short=4 $head2) &&
72 tree2=$(git rev-parse --verify HEAD:) &&
73 tree2_short=$(git rev-parse --verify --short $tree2) &&
74 git config --unset i18n.commitEncoding
75'
76
77# usage: test_format [argument...] name format_string [success|failure] [prereq] <expected_output
78test_format () {
79 local args=
80 while true
81 do
82 case "$1" in
83 --*)
84 args="$args $1"
85 shift;;
86 *)
87 break;;
88 esac
89 done
90 cat >expect.$1
91 test_expect_${3:-success} $4 "format $1" "
92 git rev-list $args --pretty=format:'$2' main >output.$1 &&
93 test_cmp expect.$1 output.$1
94 "
95}
96
97# usage: test_pretty [argument...] name format_name [failure] <expected_output
98test_pretty () {
99 local args=
100 while true
101 do
102 case "$1" in
103 --*)
104 args="$args $1"
105 shift;;
106 *)
107 break;;
108 esac
109 done
110 cat >expect.$1
111 test_expect_${3:-success} "pretty $1 (without --no-commit-header)" "
112 git rev-list $args --pretty='$2' main >output.$1 &&
113 test_cmp expect.$1 output.$1
114 "
115 test_expect_${3:-success} "pretty $1 (with --no-commit-header)" "
116 git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
117 test_cmp expect.$1 output.$1
118 "
119}
120
121# Feed to --format to provide predictable colored sequences.
122BASIC_COLOR='%Credfoo%Creset'
123COLOR='%C(red)foo%C(reset)'
124AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
125ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
126has_color () {
127 test_decode_color <"$1" >decoded &&
128 echo "<RED>foo<RESET>" >expect &&
129 test_cmp expect decoded
130}
131
132has_no_color () {
133 echo foo >expect &&
134 test_cmp expect "$1"
135}
136
137test_format percent %%h <<EOF
138commit $head2
139%h
140commit $head1
141%h
142EOF
143
144test_format hash %H%n%h <<EOF
145commit $head2
146$head2
147$head2_short
148commit $head1
149$head1
150$head1_short
151EOF
152
153test_format --no-commit-header hash-no-header %H%n%h <<EOF
154$head2
155$head2_short
156$head1
157$head1_short
158EOF
159
160test_format --abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev %H%n%h <<EOF
161$head2
162$head2_short4
163$head1
164$head1_short4
165EOF
166
167test_format tree %T%n%t <<EOF
168commit $head2
169$tree2
170$tree2_short
171commit $head1
172$tree1
173$tree1_short
174EOF
175
176test_format parents %P%n%p <<EOF
177commit $head2
178$head1
179$head1_short
180commit $head1
181
182
183EOF
184
185# we don't test relative here
186test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
187commit $head2
188$GIT_AUTHOR_NAME
189$GIT_AUTHOR_EMAIL
190$TEST_AUTHOR_LOCALNAME
191Thu Apr 7 15:13:13 2005 -0700
192Thu, 7 Apr 2005 15:13:13 -0700
1931112911993
194commit $head1
195$GIT_AUTHOR_NAME
196$GIT_AUTHOR_EMAIL
197$TEST_AUTHOR_LOCALNAME
198Thu Apr 7 15:13:13 2005 -0700
199Thu, 7 Apr 2005 15:13:13 -0700
2001112911993
201EOF
202
203test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
204commit $head2
205$GIT_COMMITTER_NAME
206$GIT_COMMITTER_EMAIL
207$TEST_COMMITTER_LOCALNAME
208Thu Apr 7 15:13:13 2005 -0700
209Thu, 7 Apr 2005 15:13:13 -0700
2101112911993
211commit $head1
212$GIT_COMMITTER_NAME
213$GIT_COMMITTER_EMAIL
214$TEST_COMMITTER_LOCALNAME
215Thu Apr 7 15:13:13 2005 -0700
216Thu, 7 Apr 2005 15:13:13 -0700
2171112911993
218EOF
219
220test_format encoding %e success ICONV <<EOF
221commit $head2
222$test_encoding
223commit $head1
224$test_encoding
225EOF
226
227test_format subject %s <<EOF
228commit $head2
229$changed
230commit $head1
231$added
232EOF
233
234test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
235commit $head2
236changed (ge${changed_utf8_part}ndert)..
237commit $head1
238added (hinzugef${added_utf8_part}gt..
239EOF
240
241test_format body %b <<EOF
242commit $head2
243commit $head1
244EOF
245
246test_format raw-body %B <<EOF
247commit $head2
248$changed
249
250commit $head1
251$added
252
253EOF
254
255test_format --no-commit-header raw-body-no-header %B <<EOF
256$changed
257
258$added
259
260EOF
261
262test_pretty oneline oneline <<EOF
263$head2 $changed
264$head1 $added
265EOF
266
267test_pretty short short <<EOF
268commit $head2
269Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
270
271 $changed
272
273commit $head1
274Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
275
276 $added
277
278EOF
279
280test_expect_success 'basic colors' '
281 cat >expect <<-EOF &&
282 commit $head2
283 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
284 EOF
285 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
286 git rev-list --color --format="$format" -1 main >actual.raw &&
287 test_decode_color <actual.raw >actual &&
288 test_cmp expect actual
289'
290
291test_expect_success '%S is not a placeholder for rev-list yet' '
292 git rev-list --format="%S" -1 main | grep "%S"
293'
294
295test_expect_success 'advanced colors' '
296 cat >expect <<-EOF &&
297 commit $head2
298 <BOLD;RED;BYELLOW>foo<RESET>
299 EOF
300 format="%C(red yellow bold)foo%C(reset)" &&
301 git rev-list --color --format="$format" -1 main >actual.raw &&
302 test_decode_color <actual.raw >actual &&
303 test_cmp expect actual
304'
305
306for spec in \
307 "%Cred:$BASIC_COLOR" \
308 "%C(...):$COLOR" \
309 "%C(auto,...):$AUTO_COLOR"
310do
311 desc=${spec%%:*}
312 color=${spec#*:}
313 test_expect_success "$desc does not enable color by default" '
314 git log --format=$color -1 >actual &&
315 has_no_color actual
316 '
317
318 test_expect_success "$desc enables colors for color.diff" '
319 git -c color.diff=always log --format=$color -1 >actual &&
320 has_color actual
321 '
322
323 test_expect_success "$desc enables colors for color.ui" '
324 git -c color.ui=always log --format=$color -1 >actual &&
325 has_color actual
326 '
327
328 test_expect_success "$desc respects --color" '
329 git log --format=$color -1 --color >actual &&
330 has_color actual
331 '
332
333 test_expect_success "$desc respects --no-color" '
334 git -c color.ui=always log --format=$color -1 --no-color >actual &&
335 has_no_color actual
336 '
337
338 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
339 test_terminal git log --format=$color -1 --color=auto >actual &&
340 has_color actual
341 '
342
343 test_expect_success "$desc respects --color=auto (stdout not tty)" '
344 (
345 TERM=vt100 && export TERM &&
346 git log --format=$color -1 --color=auto >actual &&
347 has_no_color actual
348 )
349 '
350done
351
352test_expect_success '%C(always,...) enables color even without tty' '
353 git log --format=$ALWAYS_COLOR -1 >actual &&
354 has_color actual
355'
356
357test_expect_success '%C(auto) respects --color' '
358 git log --color --format="%C(auto)%H" -1 >actual.raw &&
359 test_decode_color <actual.raw >actual &&
360 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
361 test_cmp expect actual
362'
363
364test_expect_success '%C(auto) respects --no-color' '
365 git log --no-color --format="%C(auto)%H" -1 >actual &&
366 git rev-parse HEAD >expect &&
367 test_cmp expect actual
368'
369
370test_expect_success 'rev-list %C(auto,...) respects --color' '
371 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
372 -1 HEAD >actual.raw &&
373 test_decode_color <actual.raw >actual &&
374 cat >expect <<-EOF &&
375 commit $(git rev-parse HEAD)
376 <GREEN>foo<RESET>
377 EOF
378 test_cmp expect actual
379'
380
381test_expect_success 'setup complex body' '
382 message=$(cat <<-EOF
383 Test printing of complex bodies
384
385 This commit message is much longer than the others,
386 and it will be encoded in $test_encoding. We should therefore
387 include an ISO8859 character: ¡bueno!
388 EOF
389 ) &&
390
391 if test_have_prereq ICONV
392 then
393 echo "$message" | iconv -f utf-8 -t $test_encoding >commit-msg
394 else
395 echo "$message" >commit-msg
396 fi &&
397
398 git config i18n.commitencoding $test_encoding &&
399 echo change2 >foo && git commit -a -F commit-msg &&
400 head3=$(git rev-parse --verify HEAD) &&
401 head3_short=$(git rev-parse --short $head3)
402'
403
404test_format complex-encoding %e success ICONV <<EOF
405commit $head3
406$test_encoding
407commit $head2
408$test_encoding
409commit $head1
410$test_encoding
411EOF
412
413test_format complex-subject %s <<EOF
414commit $head3
415Test printing of complex bodies
416commit $head2
417$changed_iso88591
418commit $head1
419$added_iso88591
420EOF
421
422test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
423commit $head3
424Test printing of c..
425commit $head2
426changed (ge${changed_utf8_part_iso88591}ndert)..
427commit $head1
428added (hinzugef${added_utf8_part_iso88591}gt..
429EOF
430
431test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
432commit $head3
433Test prin..ex bodies
434commit $head2
435changed (..dert) foo
436commit $head1
437added (hi..f${added_utf8_part_iso88591}gt) foo
438EOF
439
440test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
441commit $head3
442.. of complex bodies
443commit $head2
444..ged (ge${changed_utf8_part_iso88591}ndert) foo
445commit $head1
446.. (hinzugef${added_utf8_part_iso88591}gt) foo
447EOF
448
449test_expect_success 'setup expected messages (for test %b)' '
450 cat <<-EOF >expected.utf-8 &&
451 commit $head3
452 This commit message is much longer than the others,
453 and it will be encoded in $test_encoding. We should therefore
454 include an ISO8859 character: ¡bueno!
455
456 commit $head2
457 commit $head1
458 EOF
459 if test_have_prereq ICONV
460 then
461 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
462 else
463 cp expected.utf-8 expected.ISO8859-1
464 fi
465'
466
467test_format complex-body %b <expected.ISO8859-1
468
469# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
470# so unset i18n.commitEncoding to test encoding conversion
471git config --unset i18n.commitEncoding
472
473test_format complex-subject-commitencoding-unset %s <<EOF
474commit $head3
475Test printing of complex bodies
476commit $head2
477$changed
478commit $head1
479$added
480EOF
481
482test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
483commit $head3
484Test printing of c..
485commit $head2
486changed (ge${changed_utf8_part}ndert)..
487commit $head1
488added (hinzugef${added_utf8_part}gt..
489EOF
490
491test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
492commit $head3
493Test prin..ex bodies
494commit $head2
495changed (..dert) foo
496commit $head1
497added (hi..f${added_utf8_part}gt) foo
498EOF
499
500test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
501commit $head3
502.. of complex bodies
503commit $head2
504..ged (ge${changed_utf8_part}ndert) foo
505commit $head1
506.. (hinzugef${added_utf8_part}gt) foo
507EOF
508
509test_format complex-body-commitencoding-unset %b <expected.utf-8
510
511test_expect_success '%x00 shows NUL' '
512 echo >expect commit $head3 &&
513 echo >>expect fooQbar &&
514 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
515 nul_to_q <actual.nul >actual &&
516 test_cmp expect actual
517'
518
519test_expect_success '%ad respects --date=' '
520 echo 2005-04-07 >expect.ad-short &&
521 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
522 test_cmp expect.ad-short output.ad-short
523'
524
525test_expect_success 'empty email' '
526 test_tick &&
527 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
528 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
529 test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
530'
531
532test_expect_success 'del LF before empty (1)' '
533 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
534 test_line_count = 2 actual
535'
536
537test_expect_success 'del LF before empty (2)' '
538 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
539 test_line_count = 6 actual &&
540 test_grep "^$" actual
541'
542
543test_expect_success 'add LF before non-empty (1)' '
544 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
545 test_line_count = 2 actual
546'
547
548test_expect_success 'add LF before non-empty (2)' '
549 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
550 test_line_count = 6 actual &&
551 test_grep "^$" actual
552'
553
554test_expect_success 'add SP before non-empty (1)' '
555 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
556 test $(wc -w <actual) = 3
557'
558
559test_expect_success 'add SP before non-empty (2)' '
560 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
561 test $(wc -w <actual) = 6
562'
563
564test_expect_success '--abbrev' '
565 echo SHORT SHORT SHORT >expect2 &&
566 echo LONG LONG LONG >expect3 &&
567 git log -1 --format="%h %h %h" HEAD >actual1 &&
568 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
569 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
570 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
571 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
572 test_cmp expect2 fuzzy2 &&
573 test_cmp expect3 fuzzy3 &&
574 ! test_cmp actual1 actual2
575'
576
577test_expect_success '%H is not affected by --abbrev-commit' '
578 expected=$(($(test_oid hexsz) + 1)) &&
579 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
580 len=$(wc -c <actual) &&
581 test $len = $expected
582'
583
584test_expect_success '%h is not affected by --abbrev-commit' '
585 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
586 len=$(wc -c <actual) &&
587 test $len = 21
588'
589
590test_expect_success '"%h %gD: %gs" is same as git-reflog' '
591 git reflog >expect &&
592 git log -g --format="%h %gD: %gs" >actual &&
593 test_cmp expect actual
594'
595
596test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
597 git reflog --date=raw >expect &&
598 git log -g --format="%h %gD: %gs" --date=raw >actual &&
599 test_cmp expect actual
600'
601
602test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
603 git reflog --abbrev=13 --date=raw >expect &&
604 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
605 test_cmp expect actual
606'
607
608test_expect_success '%gd shortens ref name' '
609 echo "main@{0}" >expect.gd-short &&
610 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
611 test_cmp expect.gd-short actual.gd-short
612'
613
614test_expect_success 'reflog identity' '
615 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
616 git log -g -1 --format="%gn:%ge" >actual &&
617 test_cmp expect actual
618'
619
620test_expect_success 'oneline with empty message' '
621 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
622 git commit --allow-empty --allow-empty-message &&
623 git rev-list --oneline HEAD >test.txt &&
624 test_line_count = 5 test.txt &&
625 git rev-list --oneline --graph HEAD >testg.txt &&
626 test_line_count = 5 testg.txt
627'
628
629test_expect_success 'single-character name is parsed correctly' '
630 git commit --author="a <a@example.com>" --allow-empty -m foo &&
631 echo "a <a@example.com>" >expect &&
632 git log -1 --format="%an <%ae>" >actual &&
633 test_cmp expect actual
634'
635
636test_expect_success 'unused %G placeholders are passed through' '
637 echo "%GX %G" >expect &&
638 git log -1 --format="%GX %G" >actual &&
639 test_cmp expect actual
640'
641
642test_done