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'
9 . "$TEST_DIRECTORY"/lib-terminal.sh
12 # String "added" in German
13 # (translated with Google Translate),
14 # encoded in UTF-8, used as a commit log message below.
15 added=$(printf "added (hinzugef\303\274gt) foo")
16 added_iso88591=$(echo "$added" | iconv -f utf-8 -t iso8859-1)
18 changed=$(printf "changed (ge\303\244ndert) foo")
19 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t iso8859-1)
21 test_expect_success 'setup' '
24 git config i18n.commitEncoding iso8859-1 &&
25 git commit -m "$added_iso88591" &&
26 head1=$(git rev-parse --verify HEAD) &&
27 head1_short=$(git rev-parse --verify --short $head1) &&
28 tree1=$(git rev-parse --verify HEAD:) &&
29 tree1_short=$(git rev-parse --verify --short $tree1) &&
30 echo "$changed" > foo &&
31 git commit -a -m "$changed_iso88591" &&
32 head2=$(git rev-parse --verify HEAD) &&
33 head2_short=$(git rev-parse --verify --short $head2) &&
34 tree2=$(git rev-parse --verify HEAD:) &&
35 tree2_short=$(git rev-parse --verify --short $tree2)
36 git config --unset i18n.commitEncoding
39 # usage: test_format name format_string [failure] <expected_output
42 test_expect_${3:-success} "format $1" "
43 git rev-list --pretty=format:'$2' master >output.$1 &&
44 test_cmp expect.$1 output.$1
48 # Feed to --format to provide predictable colored sequences.
49 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
51 printf '\033[31mfoo\033[m\n' >expect &&
60 test_format percent %%h <<EOF
67 test_format hash %H%n%h <<EOF
76 test_format tree %T%n%t <<EOF
85 test_format parents %P%n%p <<EOF
94 # we don't test relative here
95 test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
99 Thu Apr 7 15:13:13 2005 -0700
100 Thu, 7 Apr 2005 15:13:13 -0700
105 Thu Apr 7 15:13:13 2005 -0700
106 Thu, 7 Apr 2005 15:13:13 -0700
110 test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
113 committer@example.com
114 Thu Apr 7 15:13:13 2005 -0700
115 Thu, 7 Apr 2005 15:13:13 -0700
119 committer@example.com
120 Thu Apr 7 15:13:13 2005 -0700
121 Thu, 7 Apr 2005 15:13:13 -0700
125 test_format encoding %e <<EOF
132 test_format subject %s <<EOF
139 test_format body %b <<EOF
144 test_format raw-body %B <<EOF
153 test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
155 \e[31mfoo
\e[32mbar
\e[34mbaz
\e[mxyzzy
157 \e[31mfoo
\e[32mbar
\e[34mbaz
\e[mxyzzy
160 test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
167 test_expect_success '%C(auto) does not enable color by default' '
168 git log --format=$AUTO_COLOR -1 >actual &&
172 test_expect_success '%C(auto) enables colors for color.diff' '
173 git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
177 test_expect_success '%C(auto) enables colors for color.ui' '
178 git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
182 test_expect_success '%C(auto) respects --color' '
183 git log --format=$AUTO_COLOR -1 --color >actual &&
187 test_expect_success '%C(auto) respects --no-color' '
188 git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
192 test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
193 test_terminal env TERM=vt100 \
194 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
198 test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
200 TERM=vt100 && export TERM &&
201 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
206 iconv -f utf-8 -t iso8859-1 > commit-msg <<EOF
207 Test printing of complex bodies
209 This commit message is much longer than the others,
210 and it will be encoded in iso8859-1. We should therefore
211 include an iso8859 character: ¡bueno!
214 test_expect_success 'setup complex body' '
215 git config i18n.commitencoding iso8859-1 &&
216 echo change2 >foo && git commit -a -F commit-msg &&
217 head3=$(git rev-parse --verify HEAD) &&
218 head3_short=$(git rev-parse --short $head3)
221 test_format complex-encoding %e <<EOF
230 test_format complex-subject %s <<EOF
232 Test printing of complex bodies
239 test_expect_success 'prepare expected messages (for test %b)' '
240 cat <<-EOF >expected.utf-8 &&
242 This commit message is much longer than the others,
243 and it will be encoded in iso8859-1. We should therefore
244 include an iso8859 character: ¡bueno!
249 iconv -f utf-8 -t iso8859-1 expected.utf-8 >expected.iso8859-1
252 test_format complex-body %b <expected.iso8859-1
254 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
255 # so unset i18n.commitEncoding to test encoding conversion
256 git config --unset i18n.commitEncoding
258 test_format complex-subject-commitencoding-unset %s <<EOF
260 Test printing of complex bodies
267 test_format complex-body-commitencoding-unset %b <expected.utf-8
269 test_expect_success '%x00 shows NUL' '
270 echo >expect commit $head3 &&
271 echo >>expect fooQbar &&
272 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
273 nul_to_q <actual.nul >actual &&
274 test_cmp expect actual
277 test_expect_success '%ad respects --date=' '
278 echo 2005-04-07 >expect.ad-short &&
279 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
280 test_cmp expect.ad-short output.ad-short
283 test_expect_success 'empty email' '
285 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
286 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
287 test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
288 echo "Eh? $A" >failure
293 test_expect_success 'del LF before empty (1)' '
294 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
295 test_line_count = 2 actual
298 test_expect_success 'del LF before empty (2)' '
299 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
300 test_line_count = 6 actual &&
304 test_expect_success 'add LF before non-empty (1)' '
305 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
306 test_line_count = 2 actual
309 test_expect_success 'add LF before non-empty (2)' '
310 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
311 test_line_count = 6 actual &&
315 test_expect_success 'add SP before non-empty (1)' '
316 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
317 test $(wc -w <actual) = 3
320 test_expect_success 'add SP before non-empty (2)' '
321 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
322 test $(wc -w <actual) = 6
325 test_expect_success '--abbrev' '
326 echo SHORT SHORT SHORT >expect2 &&
327 echo LONG LONG LONG >expect3 &&
328 git log -1 --format="%h %h %h" HEAD >actual1 &&
329 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
330 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
331 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
332 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
333 test_cmp expect2 fuzzy2 &&
334 test_cmp expect3 fuzzy3 &&
335 ! test_cmp actual1 actual2
338 test_expect_success '%H is not affected by --abbrev-commit' '
339 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
340 len=$(wc -c <actual) &&
344 test_expect_success '%h is not affected by --abbrev-commit' '
345 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
346 len=$(wc -c <actual) &&
350 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
351 git reflog >expect &&
352 git log -g --format="%h %gD: %gs" >actual &&
353 test_cmp expect actual
356 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
357 git reflog --date=raw >expect &&
358 git log -g --format="%h %gD: %gs" --date=raw >actual &&
359 test_cmp expect actual
362 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
363 git reflog --abbrev=13 --date=raw >expect &&
364 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
365 test_cmp expect actual
368 test_expect_success '%gd shortens ref name' '
369 echo "master@{0}" >expect.gd-short &&
370 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
371 test_cmp expect.gd-short actual.gd-short
374 test_expect_success 'reflog identity' '
375 echo "C O Mitter:committer@example.com" >expect &&
376 git log -g -1 --format="%gn:%ge" >actual &&
377 test_cmp expect actual
380 test_expect_success 'oneline with empty message' '
381 git commit -m "dummy" --allow-empty &&
382 git commit -m "dummy" --allow-empty &&
383 git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
384 git rev-list --oneline HEAD >test.txt &&
385 test_line_count = 5 test.txt &&
386 git rev-list --oneline --graph HEAD >testg.txt &&
387 test_line_count = 5 testg.txt
390 test_expect_success 'single-character name is parsed correctly' '
391 git commit --author="a <a@example.com>" --allow-empty -m foo &&
392 echo "a <a@example.com>" >expect &&
393 git log -1 --format="%an <%ae>" >actual &&
394 test_cmp expect actual