Raw
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
16 if test_have_prereq ICONV
17 then
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)
33 else
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"
49 fi
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
55 truncate_count=20
56
57 test_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
78 test_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
98 test_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.
122 BASIC_COLOR='%Credfoo%Creset'
123 COLOR='%C(red)foo%C(reset)'
124 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
125 ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
126 has_color () {
127 test_decode_color <"$1" >decoded &&
128 echo "<RED>foo<RESET>" >expect &&
129 test_cmp expect decoded
130 }
131
132 has_no_color () {
133 echo foo >expect &&
134 test_cmp expect "$1"
135 }
136
137 test_format percent %%h <<EOF
138 commit $head2
139 %h
140 commit $head1
141 %h
142 EOF
143
144 test_format hash %H%n%h <<EOF
145 commit $head2
146 $head2
147 $head2_short
148 commit $head1
149 $head1
150 $head1_short
151 EOF
152
153 test_format --no-commit-header hash-no-header %H%n%h <<EOF
154 $head2
155 $head2_short
156 $head1
157 $head1_short
158 EOF
159
160 test_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
165 EOF
166
167 test_format tree %T%n%t <<EOF
168 commit $head2
169 $tree2
170 $tree2_short
171 commit $head1
172 $tree1
173 $tree1_short
174 EOF
175
176 test_format parents %P%n%p <<EOF
177 commit $head2
178 $head1
179 $head1_short
180 commit $head1
181
182
183 EOF
184
185 # we don't test relative here
186 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
187 commit $head2
188 $GIT_AUTHOR_NAME
189 $GIT_AUTHOR_EMAIL
190 $TEST_AUTHOR_LOCALNAME
191 Thu Apr 7 15:13:13 2005 -0700
192 Thu, 7 Apr 2005 15:13:13 -0700
193 1112911993
194 commit $head1
195 $GIT_AUTHOR_NAME
196 $GIT_AUTHOR_EMAIL
197 $TEST_AUTHOR_LOCALNAME
198 Thu Apr 7 15:13:13 2005 -0700
199 Thu, 7 Apr 2005 15:13:13 -0700
200 1112911993
201 EOF
202
203 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
204 commit $head2
205 $GIT_COMMITTER_NAME
206 $GIT_COMMITTER_EMAIL
207 $TEST_COMMITTER_LOCALNAME
208 Thu Apr 7 15:13:13 2005 -0700
209 Thu, 7 Apr 2005 15:13:13 -0700
210 1112911993
211 commit $head1
212 $GIT_COMMITTER_NAME
213 $GIT_COMMITTER_EMAIL
214 $TEST_COMMITTER_LOCALNAME
215 Thu Apr 7 15:13:13 2005 -0700
216 Thu, 7 Apr 2005 15:13:13 -0700
217 1112911993
218 EOF
219
220 test_format encoding %e success ICONV <<EOF
221 commit $head2
222 $test_encoding
223 commit $head1
224 $test_encoding
225 EOF
226
227 test_format subject %s <<EOF
228 commit $head2
229 $changed
230 commit $head1
231 $added
232 EOF
233
234 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
235 commit $head2
236 changed (ge${changed_utf8_part}ndert)..
237 commit $head1
238 added (hinzugef${added_utf8_part}gt..
239 EOF
240
241 test_format body %b <<EOF
242 commit $head2
243 commit $head1
244 EOF
245
246 test_format raw-body %B <<EOF
247 commit $head2
248 $changed
249
250 commit $head1
251 $added
252
253 EOF
254
255 test_format --no-commit-header raw-body-no-header %B <<EOF
256 $changed
257
258 $added
259
260 EOF
261
262 test_pretty oneline oneline <<EOF
263 $head2 $changed
264 $head1 $added
265 EOF
266
267 test_pretty short short <<EOF
268 commit $head2
269 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
270
271 $changed
272
273 commit $head1
274 Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
275
276 $added
277
278 EOF
279
280 test_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
291 test_expect_success '%S is not a placeholder for rev-list yet' '
292 git rev-list --format="%S" -1 main | grep "%S"
293 '
294
295 test_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
306 for spec in \
307 "%Cred:$BASIC_COLOR" \
308 "%C(...):$COLOR" \
309 "%C(auto,...):$AUTO_COLOR"
310 do
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 '
350 done
351
352 test_expect_success '%C(always,...) enables color even without tty' '
353 git log --format=$ALWAYS_COLOR -1 >actual &&
354 has_color actual
355 '
356
357 test_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
364 test_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
370 test_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
381 test_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
404 test_format complex-encoding %e success ICONV <<EOF
405 commit $head3
406 $test_encoding
407 commit $head2
408 $test_encoding
409 commit $head1
410 $test_encoding
411 EOF
412
413 test_format complex-subject %s <<EOF
414 commit $head3
415 Test printing of complex bodies
416 commit $head2
417 $changed_iso88591
418 commit $head1
419 $added_iso88591
420 EOF
421
422 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
423 commit $head3
424 Test printing of c..
425 commit $head2
426 changed (ge${changed_utf8_part_iso88591}ndert)..
427 commit $head1
428 added (hinzugef${added_utf8_part_iso88591}gt..
429 EOF
430
431 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
432 commit $head3
433 Test prin..ex bodies
434 commit $head2
435 changed (..dert) foo
436 commit $head1
437 added (hi..f${added_utf8_part_iso88591}gt) foo
438 EOF
439
440 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
441 commit $head3
442 .. of complex bodies
443 commit $head2
444 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
445 commit $head1
446 .. (hinzugef${added_utf8_part_iso88591}gt) foo
447 EOF
448
449 test_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
467 test_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
471 git config --unset i18n.commitEncoding
472
473 test_format complex-subject-commitencoding-unset %s <<EOF
474 commit $head3
475 Test printing of complex bodies
476 commit $head2
477 $changed
478 commit $head1
479 $added
480 EOF
481
482 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
483 commit $head3
484 Test printing of c..
485 commit $head2
486 changed (ge${changed_utf8_part}ndert)..
487 commit $head1
488 added (hinzugef${added_utf8_part}gt..
489 EOF
490
491 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
492 commit $head3
493 Test prin..ex bodies
494 commit $head2
495 changed (..dert) foo
496 commit $head1
497 added (hi..f${added_utf8_part}gt) foo
498 EOF
499
500 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
501 commit $head3
502 .. of complex bodies
503 commit $head2
504 ..ged (ge${changed_utf8_part}ndert) foo
505 commit $head1
506 .. (hinzugef${added_utf8_part}gt) foo
507 EOF
508
509 test_format complex-body-commitencoding-unset %b <expected.utf-8
510
511 test_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
519 test_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
525 test_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
532 test_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
537 test_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
543 test_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
548 test_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
554 test_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
559 test_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
564 test_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
577 test_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
584 test_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
590 test_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
596 test_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
602 test_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
608 test_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
614 test_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
620 test_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
629 test_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
636 test_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
642 test_done