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