The twelfth batch
[git/gitster.git] / t / t6006-rev-list-format.sh
blobb0ec2fe865b5301f8c5fdc1aa5a040ef97f2417f
1 #!/bin/sh
3 # Copyright (c) 2009 Jens Lehmann
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
6 test_description='git rev-list --pretty=format test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-terminal.sh
15 test_tick
17 if test_have_prereq ICONV
18 then
19 # Tested non-UTF-8 encoding
20 test_encoding="ISO8859-1"
22 # String "added" in German
23 # (translated with Google Translate),
24 # encoded in UTF-8, used as a commit log message below.
25 added_utf8_part=$(printf "\303\274")
26 added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
27 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
28 added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
29 # same but "changed"
30 changed_utf8_part=$(printf "\303\244")
31 changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
32 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
33 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
34 else
35 # Tested non-UTF-8 encoding
36 test_encoding="UTF-8"
38 # String "added" in German
39 # (translated with Google Translate),
40 # encoded in UTF-8, used as a commit log message below.
41 added_utf8_part="u"
42 added_utf8_part_iso88591="u"
43 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
44 added_iso88591="$added"
45 # same but "changed"
46 changed_utf8_part="a"
47 changed_utf8_part_iso88591="a"
48 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
49 changed_iso88591="$changed"
52 # Count of char to truncate
53 # Number is chosen so, that non-ACSII characters
54 # (see $added_utf8_part and $changed_utf8_part)
55 # fall into truncated parts of appropriate words both from left and right
56 truncate_count=20
58 test_expect_success 'setup' '
59 : >foo &&
60 git add foo &&
61 git config i18n.commitEncoding $test_encoding &&
62 echo "$added_iso88591" | git commit -F - &&
63 head1=$(git rev-parse --verify HEAD) &&
64 head1_short=$(git rev-parse --verify --short $head1) &&
65 head1_short4=$(git rev-parse --verify --short=4 $head1) &&
66 tree1=$(git rev-parse --verify HEAD:) &&
67 tree1_short=$(git rev-parse --verify --short $tree1) &&
68 echo "$changed" > foo &&
69 echo "$changed_iso88591" | git commit -a -F - &&
70 head2=$(git rev-parse --verify HEAD) &&
71 head2_short=$(git rev-parse --verify --short $head2) &&
72 head2_short4=$(git rev-parse --verify --short=4 $head2) &&
73 tree2=$(git rev-parse --verify HEAD:) &&
74 tree2_short=$(git rev-parse --verify --short $tree2) &&
75 git config --unset i18n.commitEncoding
78 # usage: test_format [argument...] name format_string [success|failure] [prereq] <expected_output
79 test_format () {
80 local args=
81 while true
83 case "$1" in
84 --*)
85 args="$args $1"
86 shift;;
88 break;;
89 esac
90 done
91 cat >expect.$1
92 test_expect_${3:-success} $4 "format $1" "
93 git rev-list $args --pretty=format:'$2' main >output.$1 &&
94 test_cmp expect.$1 output.$1
98 # usage: test_pretty [argument...] name format_name [failure] <expected_output
99 test_pretty () {
100 local args=
101 while true
103 case "$1" in
104 --*)
105 args="$args $1"
106 shift;;
108 break;;
109 esac
110 done
111 cat >expect.$1
112 test_expect_${3:-success} "pretty $1 (without --no-commit-header)" "
113 git rev-list $args --pretty='$2' main >output.$1 &&
114 test_cmp expect.$1 output.$1
116 test_expect_${3:-success} "pretty $1 (with --no-commit-header)" "
117 git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
118 test_cmp expect.$1 output.$1
122 # Feed to --format to provide predictable colored sequences.
123 BASIC_COLOR='%Credfoo%Creset'
124 COLOR='%C(red)foo%C(reset)'
125 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
126 ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
127 has_color () {
128 test_decode_color <"$1" >decoded &&
129 echo "<RED>foo<RESET>" >expect &&
130 test_cmp expect decoded
133 has_no_color () {
134 echo foo >expect &&
135 test_cmp expect "$1"
138 test_format percent %%h <<EOF
139 commit $head2
141 commit $head1
145 test_format hash %H%n%h <<EOF
146 commit $head2
147 $head2
148 $head2_short
149 commit $head1
150 $head1
151 $head1_short
154 test_format --no-commit-header hash-no-header %H%n%h <<EOF
155 $head2
156 $head2_short
157 $head1
158 $head1_short
161 test_format --abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev %H%n%h <<EOF
162 $head2
163 $head2_short4
164 $head1
165 $head1_short4
168 test_format tree %T%n%t <<EOF
169 commit $head2
170 $tree2
171 $tree2_short
172 commit $head1
173 $tree1
174 $tree1_short
177 test_format parents %P%n%p <<EOF
178 commit $head2
179 $head1
180 $head1_short
181 commit $head1
186 # we don't test relative here
187 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
188 commit $head2
189 $GIT_AUTHOR_NAME
190 $GIT_AUTHOR_EMAIL
191 $TEST_AUTHOR_LOCALNAME
192 Thu Apr 7 15:13:13 2005 -0700
193 Thu, 7 Apr 2005 15:13:13 -0700
194 1112911993
195 commit $head1
196 $GIT_AUTHOR_NAME
197 $GIT_AUTHOR_EMAIL
198 $TEST_AUTHOR_LOCALNAME
199 Thu Apr 7 15:13:13 2005 -0700
200 Thu, 7 Apr 2005 15:13:13 -0700
201 1112911993
204 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
205 commit $head2
206 $GIT_COMMITTER_NAME
207 $GIT_COMMITTER_EMAIL
208 $TEST_COMMITTER_LOCALNAME
209 Thu Apr 7 15:13:13 2005 -0700
210 Thu, 7 Apr 2005 15:13:13 -0700
211 1112911993
212 commit $head1
213 $GIT_COMMITTER_NAME
214 $GIT_COMMITTER_EMAIL
215 $TEST_COMMITTER_LOCALNAME
216 Thu Apr 7 15:13:13 2005 -0700
217 Thu, 7 Apr 2005 15:13:13 -0700
218 1112911993
221 test_format encoding %e success ICONV <<EOF
222 commit $head2
223 $test_encoding
224 commit $head1
225 $test_encoding
228 test_format subject %s <<EOF
229 commit $head2
230 $changed
231 commit $head1
232 $added
235 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
236 commit $head2
237 changed (ge${changed_utf8_part}ndert)..
238 commit $head1
239 added (hinzugef${added_utf8_part}gt..
242 test_format body %b <<EOF
243 commit $head2
244 commit $head1
247 test_format raw-body %B <<EOF
248 commit $head2
249 $changed
251 commit $head1
252 $added
256 test_format --no-commit-header raw-body-no-header %B <<EOF
257 $changed
259 $added
263 test_pretty oneline oneline <<EOF
264 $head2 $changed
265 $head1 $added
268 test_pretty short short <<EOF
269 commit $head2
270 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
272 $changed
274 commit $head1
275 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
277 $added
281 test_expect_success 'basic colors' '
282 cat >expect <<-EOF &&
283 commit $head2
284 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
286 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
287 git rev-list --color --format="$format" -1 main >actual.raw &&
288 test_decode_color <actual.raw >actual &&
289 test_cmp expect actual
292 test_expect_success '%S is not a placeholder for rev-list yet' '
293 git rev-list --format="%S" -1 main | grep "%S"
296 test_expect_success 'advanced colors' '
297 cat >expect <<-EOF &&
298 commit $head2
299 <BOLD;RED;BYELLOW>foo<RESET>
301 format="%C(red yellow bold)foo%C(reset)" &&
302 git rev-list --color --format="$format" -1 main >actual.raw &&
303 test_decode_color <actual.raw >actual &&
304 test_cmp expect actual
307 for spec in \
308 "%Cred:$BASIC_COLOR" \
309 "%C(...):$COLOR" \
310 "%C(auto,...):$AUTO_COLOR"
312 desc=${spec%%:*}
313 color=${spec#*:}
314 test_expect_success "$desc does not enable color by default" '
315 git log --format=$color -1 >actual &&
316 has_no_color actual
319 test_expect_success "$desc enables colors for color.diff" '
320 git -c color.diff=always log --format=$color -1 >actual &&
321 has_color actual
324 test_expect_success "$desc enables colors for color.ui" '
325 git -c color.ui=always log --format=$color -1 >actual &&
326 has_color actual
329 test_expect_success "$desc respects --color" '
330 git log --format=$color -1 --color >actual &&
331 has_color actual
334 test_expect_success "$desc respects --no-color" '
335 git -c color.ui=always log --format=$color -1 --no-color >actual &&
336 has_no_color actual
339 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
340 test_terminal git log --format=$color -1 --color=auto >actual &&
341 has_color actual
344 test_expect_success "$desc respects --color=auto (stdout not tty)" '
346 TERM=vt100 && export TERM &&
347 git log --format=$color -1 --color=auto >actual &&
348 has_no_color actual
351 done
353 test_expect_success '%C(always,...) enables color even without tty' '
354 git log --format=$ALWAYS_COLOR -1 >actual &&
355 has_color actual
358 test_expect_success '%C(auto) respects --color' '
359 git log --color --format="%C(auto)%H" -1 >actual.raw &&
360 test_decode_color <actual.raw >actual &&
361 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
362 test_cmp expect actual
365 test_expect_success '%C(auto) respects --no-color' '
366 git log --no-color --format="%C(auto)%H" -1 >actual &&
367 git rev-parse HEAD >expect &&
368 test_cmp expect actual
371 test_expect_success 'rev-list %C(auto,...) respects --color' '
372 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
373 -1 HEAD >actual.raw &&
374 test_decode_color <actual.raw >actual &&
375 cat >expect <<-EOF &&
376 commit $(git rev-parse HEAD)
377 <GREEN>foo<RESET>
379 test_cmp expect actual
382 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
383 Test printing of complex bodies
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!
390 test_expect_success 'setup complex body' '
391 git config i18n.commitencoding $test_encoding &&
392 echo change2 >foo && git commit -a -F commit-msg &&
393 head3=$(git rev-parse --verify HEAD) &&
394 head3_short=$(git rev-parse --short $head3)
397 test_format complex-encoding %e success ICONV <<EOF
398 commit $head3
399 $test_encoding
400 commit $head2
401 $test_encoding
402 commit $head1
403 $test_encoding
406 test_format complex-subject %s <<EOF
407 commit $head3
408 Test printing of complex bodies
409 commit $head2
410 $changed_iso88591
411 commit $head1
412 $added_iso88591
415 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
416 commit $head3
417 Test printing of c..
418 commit $head2
419 changed (ge${changed_utf8_part_iso88591}ndert)..
420 commit $head1
421 added (hinzugef${added_utf8_part_iso88591}gt..
424 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
425 commit $head3
426 Test prin..ex bodies
427 commit $head2
428 changed (..dert) foo
429 commit $head1
430 added (hi..f${added_utf8_part_iso88591}gt) foo
433 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
434 commit $head3
435 .. of complex bodies
436 commit $head2
437 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
438 commit $head1
439 .. (hinzugef${added_utf8_part_iso88591}gt) foo
442 test_expect_success 'setup expected messages (for test %b)' '
443 cat <<-EOF >expected.utf-8 &&
444 commit $head3
445 This commit message is much longer than the others,
446 and it will be encoded in $test_encoding. We should therefore
447 include an ISO8859 character: ¡bueno!
449 commit $head2
450 commit $head1
452 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
455 test_format complex-body %b <expected.ISO8859-1
457 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
458 # so unset i18n.commitEncoding to test encoding conversion
459 git config --unset i18n.commitEncoding
461 test_format complex-subject-commitencoding-unset %s <<EOF
462 commit $head3
463 Test printing of complex bodies
464 commit $head2
465 $changed
466 commit $head1
467 $added
470 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
471 commit $head3
472 Test printing of c..
473 commit $head2
474 changed (ge${changed_utf8_part}ndert)..
475 commit $head1
476 added (hinzugef${added_utf8_part}gt..
479 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
480 commit $head3
481 Test prin..ex bodies
482 commit $head2
483 changed (..dert) foo
484 commit $head1
485 added (hi..f${added_utf8_part}gt) foo
488 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
489 commit $head3
490 .. of complex bodies
491 commit $head2
492 ..ged (ge${changed_utf8_part}ndert) foo
493 commit $head1
494 .. (hinzugef${added_utf8_part}gt) foo
497 test_format complex-body-commitencoding-unset %b <expected.utf-8
499 test_expect_success '%x00 shows NUL' '
500 echo >expect commit $head3 &&
501 echo >>expect fooQbar &&
502 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
503 nul_to_q <actual.nul >actual &&
504 test_cmp expect actual
507 test_expect_success '%ad respects --date=' '
508 echo 2005-04-07 >expect.ad-short &&
509 git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
510 test_cmp expect.ad-short output.ad-short
513 test_expect_success 'empty email' '
514 test_tick &&
515 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
516 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
517 test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
520 test_expect_success 'del LF before empty (1)' '
521 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
522 test_line_count = 2 actual
525 test_expect_success 'del LF before empty (2)' '
526 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
527 test_line_count = 6 actual &&
528 grep "^$" actual
531 test_expect_success 'add LF before non-empty (1)' '
532 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
533 test_line_count = 2 actual
536 test_expect_success 'add LF before non-empty (2)' '
537 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
538 test_line_count = 6 actual &&
539 grep "^$" actual
542 test_expect_success 'add SP before non-empty (1)' '
543 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
544 test $(wc -w <actual) = 3
547 test_expect_success 'add SP before non-empty (2)' '
548 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
549 test $(wc -w <actual) = 6
552 test_expect_success '--abbrev' '
553 echo SHORT SHORT SHORT >expect2 &&
554 echo LONG LONG LONG >expect3 &&
555 git log -1 --format="%h %h %h" HEAD >actual1 &&
556 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
557 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
558 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
559 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
560 test_cmp expect2 fuzzy2 &&
561 test_cmp expect3 fuzzy3 &&
562 ! test_cmp actual1 actual2
565 test_expect_success '%H is not affected by --abbrev-commit' '
566 expected=$(($(test_oid hexsz) + 1)) &&
567 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
568 len=$(wc -c <actual) &&
569 test $len = $expected
572 test_expect_success '%h is not affected by --abbrev-commit' '
573 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
574 len=$(wc -c <actual) &&
575 test $len = 21
578 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
579 git reflog >expect &&
580 git log -g --format="%h %gD: %gs" >actual &&
581 test_cmp expect actual
584 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
585 git reflog --date=raw >expect &&
586 git log -g --format="%h %gD: %gs" --date=raw >actual &&
587 test_cmp expect actual
590 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
591 git reflog --abbrev=13 --date=raw >expect &&
592 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
593 test_cmp expect actual
596 test_expect_success '%gd shortens ref name' '
597 echo "main@{0}" >expect.gd-short &&
598 git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
599 test_cmp expect.gd-short actual.gd-short
602 test_expect_success 'reflog identity' '
603 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
604 git log -g -1 --format="%gn:%ge" >actual &&
605 test_cmp expect actual
608 test_expect_success 'oneline with empty message' '
609 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
610 git commit --allow-empty --allow-empty-message &&
611 git rev-list --oneline HEAD >test.txt &&
612 test_line_count = 5 test.txt &&
613 git rev-list --oneline --graph HEAD >testg.txt &&
614 test_line_count = 5 testg.txt
617 test_expect_success 'single-character name is parsed correctly' '
618 git commit --author="a <a@example.com>" --allow-empty -m foo &&
619 echo "a <a@example.com>" >expect &&
620 git log -1 --format="%an <%ae>" >actual &&
621 test_cmp expect actual
624 test_expect_success 'unused %G placeholders are passed through' '
625 echo "%GX %G" >expect &&
626 git log -1 --format="%GX %G" >actual &&
627 test_cmp expect actual
630 test_done