debian: prepare for upload
[git/debian.git] / t / t6006-rev-list-format.sh
blobbc95da8a5f72c1b9a9e523f82ce4404f92679d5e
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 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
11 test_tick
12 # Tested non-UTF-8 encoding
13 test_encoding="ISO8859-1"
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)
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
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
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
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
72 has_no_color () {
73 echo foo >expect &&
74 test_cmp expect "$1"
77 test_format percent %%h <<EOF
78 commit $head2
80 commit $head1
82 EOF
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
93 test_format tree %T%n%t <<EOF
94 commit $head2
95 $tree2
96 $tree2_short
97 commit $head1
98 $tree1
99 $tree1_short
102 test_format parents %P%n%p <<EOF
103 commit $head2
104 $head1
105 $head1_short
106 commit $head1
111 # we don't test relative here
112 test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
113 commit $head2
114 $GIT_AUTHOR_NAME
115 $GIT_AUTHOR_EMAIL
116 $TEST_AUTHOR_LOCALNAME
117 Thu Apr 7 15:13:13 2005 -0700
118 Thu, 7 Apr 2005 15:13:13 -0700
119 1112911993
120 commit $head1
121 $GIT_AUTHOR_NAME
122 $GIT_AUTHOR_EMAIL
123 $TEST_AUTHOR_LOCALNAME
124 Thu Apr 7 15:13:13 2005 -0700
125 Thu, 7 Apr 2005 15:13:13 -0700
126 1112911993
129 test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
130 commit $head2
131 $GIT_COMMITTER_NAME
132 $GIT_COMMITTER_EMAIL
133 $TEST_COMMITTER_LOCALNAME
134 Thu Apr 7 15:13:13 2005 -0700
135 Thu, 7 Apr 2005 15:13:13 -0700
136 1112911993
137 commit $head1
138 $GIT_COMMITTER_NAME
139 $GIT_COMMITTER_EMAIL
140 $TEST_COMMITTER_LOCALNAME
141 Thu Apr 7 15:13:13 2005 -0700
142 Thu, 7 Apr 2005 15:13:13 -0700
143 1112911993
146 test_format encoding %e <<EOF
147 commit $head2
148 $test_encoding
149 commit $head1
150 $test_encoding
153 test_format subject %s <<EOF
154 commit $head2
155 $changed
156 commit $head1
157 $added
160 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
161 commit $head2
162 changed (ge${changed_utf8_part}ndert)..
163 commit $head1
164 added (hinzugef${added_utf8_part}gt..
167 test_format body %b <<EOF
168 commit $head2
169 commit $head1
172 test_format raw-body %B <<EOF
173 commit $head2
174 $changed
176 commit $head1
177 $added
181 test_expect_success 'basic colors' '
182 cat >expect <<-EOF &&
183 commit $head2
184 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
186 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
187 git rev-list --color --format="$format" -1 master >actual.raw &&
188 test_decode_color <actual.raw >actual &&
189 test_cmp expect actual
192 test_expect_success '%S is not a placeholder for rev-list yet' '
193 git rev-list --format="%S" -1 master | grep "%S"
196 test_expect_success 'advanced colors' '
197 cat >expect <<-EOF &&
198 commit $head2
199 <BOLD;RED;BYELLOW>foo<RESET>
201 format="%C(red yellow bold)foo%C(reset)" &&
202 git rev-list --color --format="$format" -1 master >actual.raw &&
203 test_decode_color <actual.raw >actual &&
204 test_cmp expect actual
207 for spec in \
208 "%Cred:$BASIC_COLOR" \
209 "%C(...):$COLOR" \
210 "%C(auto,...):$AUTO_COLOR"
212 desc=${spec%%:*}
213 color=${spec#*:}
214 test_expect_success "$desc does not enable color by default" '
215 git log --format=$color -1 >actual &&
216 has_no_color actual
219 test_expect_success "$desc enables colors for color.diff" '
220 git -c color.diff=always log --format=$color -1 >actual &&
221 has_color actual
224 test_expect_success "$desc enables colors for color.ui" '
225 git -c color.ui=always log --format=$color -1 >actual &&
226 has_color actual
229 test_expect_success "$desc respects --color" '
230 git log --format=$color -1 --color >actual &&
231 has_color actual
234 test_expect_success "$desc respects --no-color" '
235 git -c color.ui=always log --format=$color -1 --no-color >actual &&
236 has_no_color actual
239 test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
240 test_terminal git log --format=$color -1 --color=auto >actual &&
241 has_color actual
244 test_expect_success "$desc respects --color=auto (stdout not tty)" '
246 TERM=vt100 && export TERM &&
247 git log --format=$color -1 --color=auto >actual &&
248 has_no_color actual
251 done
253 test_expect_success '%C(always,...) enables color even without tty' '
254 git log --format=$ALWAYS_COLOR -1 >actual &&
255 has_color actual
258 test_expect_success '%C(auto) respects --color' '
259 git log --color --format="%C(auto)%H" -1 >actual.raw &&
260 test_decode_color <actual.raw >actual &&
261 echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
262 test_cmp expect actual
265 test_expect_success '%C(auto) respects --no-color' '
266 git log --no-color --format="%C(auto)%H" -1 >actual &&
267 git rev-parse HEAD >expect &&
268 test_cmp expect actual
271 test_expect_success 'rev-list %C(auto,...) respects --color' '
272 git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
273 -1 HEAD >actual.raw &&
274 test_decode_color <actual.raw >actual &&
275 cat >expect <<-EOF &&
276 commit $(git rev-parse HEAD)
277 <GREEN>foo<RESET>
279 test_cmp expect actual
282 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
283 Test printing of complex bodies
285 This commit message is much longer than the others,
286 and it will be encoded in $test_encoding. We should therefore
287 include an ISO8859 character: ¡bueno!
290 test_expect_success 'setup complex body' '
291 git config i18n.commitencoding $test_encoding &&
292 echo change2 >foo && git commit -a -F commit-msg &&
293 head3=$(git rev-parse --verify HEAD) &&
294 head3_short=$(git rev-parse --short $head3)
297 test_format complex-encoding %e <<EOF
298 commit $head3
299 $test_encoding
300 commit $head2
301 $test_encoding
302 commit $head1
303 $test_encoding
306 test_format complex-subject %s <<EOF
307 commit $head3
308 Test printing of complex bodies
309 commit $head2
310 $changed_iso88591
311 commit $head1
312 $added_iso88591
315 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
316 commit $head3
317 Test printing of c..
318 commit $head2
319 changed (ge${changed_utf8_part_iso88591}ndert)..
320 commit $head1
321 added (hinzugef${added_utf8_part_iso88591}gt..
324 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
325 commit $head3
326 Test prin..ex bodies
327 commit $head2
328 changed (..dert) foo
329 commit $head1
330 added (hi..f${added_utf8_part_iso88591}gt) foo
333 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
334 commit $head3
335 .. of complex bodies
336 commit $head2
337 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
338 commit $head1
339 .. (hinzugef${added_utf8_part_iso88591}gt) foo
342 test_expect_success 'prepare expected messages (for test %b)' '
343 cat <<-EOF >expected.utf-8 &&
344 commit $head3
345 This commit message is much longer than the others,
346 and it will be encoded in $test_encoding. We should therefore
347 include an ISO8859 character: ¡bueno!
349 commit $head2
350 commit $head1
352 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
355 test_format complex-body %b <expected.ISO8859-1
357 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
358 # so unset i18n.commitEncoding to test encoding conversion
359 git config --unset i18n.commitEncoding
361 test_format complex-subject-commitencoding-unset %s <<EOF
362 commit $head3
363 Test printing of complex bodies
364 commit $head2
365 $changed
366 commit $head1
367 $added
370 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
371 commit $head3
372 Test printing of c..
373 commit $head2
374 changed (ge${changed_utf8_part}ndert)..
375 commit $head1
376 added (hinzugef${added_utf8_part}gt..
379 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
380 commit $head3
381 Test prin..ex bodies
382 commit $head2
383 changed (..dert) foo
384 commit $head1
385 added (hi..f${added_utf8_part}gt) foo
388 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
389 commit $head3
390 .. of complex bodies
391 commit $head2
392 ..ged (ge${changed_utf8_part}ndert) foo
393 commit $head1
394 .. (hinzugef${added_utf8_part}gt) foo
397 test_format complex-body-commitencoding-unset %b <expected.utf-8
399 test_expect_success '%x00 shows NUL' '
400 echo >expect commit $head3 &&
401 echo >>expect fooQbar &&
402 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
403 nul_to_q <actual.nul >actual &&
404 test_cmp expect actual
407 test_expect_success '%ad respects --date=' '
408 echo 2005-04-07 >expect.ad-short &&
409 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
410 test_cmp expect.ad-short output.ad-short
413 test_expect_success 'empty email' '
414 test_tick &&
415 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
416 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
417 verbose test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
420 test_expect_success 'del LF before empty (1)' '
421 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
422 test_line_count = 2 actual
425 test_expect_success 'del LF before empty (2)' '
426 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
427 test_line_count = 6 actual &&
428 grep "^$" actual
431 test_expect_success 'add LF before non-empty (1)' '
432 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
433 test_line_count = 2 actual
436 test_expect_success 'add LF before non-empty (2)' '
437 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
438 test_line_count = 6 actual &&
439 grep "^$" actual
442 test_expect_success 'add SP before non-empty (1)' '
443 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
444 test $(wc -w <actual) = 3
447 test_expect_success 'add SP before non-empty (2)' '
448 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
449 test $(wc -w <actual) = 6
452 test_expect_success '--abbrev' '
453 echo SHORT SHORT SHORT >expect2 &&
454 echo LONG LONG LONG >expect3 &&
455 git log -1 --format="%h %h %h" HEAD >actual1 &&
456 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
457 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
458 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
459 sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
460 test_cmp expect2 fuzzy2 &&
461 test_cmp expect3 fuzzy3 &&
462 ! test_cmp actual1 actual2
465 test_expect_success '%H is not affected by --abbrev-commit' '
466 expected=$(($(test_oid hexsz) + 1)) &&
467 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
468 len=$(wc -c <actual) &&
469 test $len = $expected
472 test_expect_success '%h is not affected by --abbrev-commit' '
473 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
474 len=$(wc -c <actual) &&
475 test $len = 21
478 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
479 git reflog >expect &&
480 git log -g --format="%h %gD: %gs" >actual &&
481 test_cmp expect actual
484 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
485 git reflog --date=raw >expect &&
486 git log -g --format="%h %gD: %gs" --date=raw >actual &&
487 test_cmp expect actual
490 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
491 git reflog --abbrev=13 --date=raw >expect &&
492 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
493 test_cmp expect actual
496 test_expect_success '%gd shortens ref name' '
497 echo "master@{0}" >expect.gd-short &&
498 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
499 test_cmp expect.gd-short actual.gd-short
502 test_expect_success 'reflog identity' '
503 echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
504 git log -g -1 --format="%gn:%ge" >actual &&
505 test_cmp expect actual
508 test_expect_success 'oneline with empty message' '
509 git commit --allow-empty --cleanup=verbatim -m "$LF" &&
510 git commit --allow-empty --allow-empty-message &&
511 git rev-list --oneline HEAD >test.txt &&
512 test_line_count = 5 test.txt &&
513 git rev-list --oneline --graph HEAD >testg.txt &&
514 test_line_count = 5 testg.txt
517 test_expect_success 'single-character name is parsed correctly' '
518 git commit --author="a <a@example.com>" --allow-empty -m foo &&
519 echo "a <a@example.com>" >expect &&
520 git log -1 --format="%an <%ae>" >actual &&
521 test_cmp expect actual
524 test_expect_success 'unused %G placeholders are passed through' '
525 echo "%GX %G" >expect &&
526 git log -1 --format="%GX %G" >actual &&
527 test_cmp expect actual
530 test_done