1#!/bin/sh
2
3# Copyright (c) 2009 Jens Lehmann
4# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5
6test_description='git rev-list --pretty=format test'
7
8. ./test-lib.sh
9. "$TEST_DIRECTORY"/lib-terminal.sh
10
11test_tick
12# String "added" in German
13# (translated with Google Translate),
14# encoded in UTF-8, used as a commit log message below.
15added=$(printf "added (hinzugef\303\274gt) foo")
16added_iso88591=$(echo "$added" | iconv -f utf-8 -t iso8859-1)
17# same but "changed"
18changed=$(printf "changed (ge\303\244ndert) foo")
19changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t iso8859-1)
20
21test_expect_success 'setup' '
22 : >foo &&
23 git add foo &&
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
37'
38
39# usage: test_format name format_string [failure] <expected_output
40test_format () {
41 cat >expect.$1
42 test_expect_${3:-success} "format $1" "
43 git rev-list --pretty=format:'$2' master >output.$1 &&
44 test_cmp expect.$1 output.$1
45 "
46}
47
48# Feed to --format to provide predictable colored sequences.
49AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
50has_color () {
51 printf '\033[31mfoo\033[m\n' >expect &&
52 test_cmp expect "$1"
53}
54
55has_no_color () {
56 echo foo >expect &&
57 test_cmp expect "$1"
58}
59
60test_format percent %%h <<EOF
61commit $head2
62%h
63commit $head1
64%h
65EOF
66
67test_format hash %H%n%h <<EOF
68commit $head2
69$head2
70$head2_short
71commit $head1
72$head1
73$head1_short
74EOF
75
76test_format tree %T%n%t <<EOF
77commit $head2
78$tree2
79$tree2_short
80commit $head1
81$tree1
82$tree1_short
83EOF
84
85test_format parents %P%n%p <<EOF
86commit $head2
87$head1
88$head1_short
89commit $head1
90
91
92EOF
93
94# we don't test relative here
95test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
96commit $head2
97A U Thor
98author@example.com
99Thu Apr 7 15:13:13 2005 -0700
100Thu, 7 Apr 2005 15:13:13 -0700
1011112911993
102commit $head1
103A U Thor
104author@example.com
105Thu Apr 7 15:13:13 2005 -0700
106Thu, 7 Apr 2005 15:13:13 -0700
1071112911993
108EOF
109
110test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
111commit $head2
112C O Mitter
113committer@example.com
114Thu Apr 7 15:13:13 2005 -0700
115Thu, 7 Apr 2005 15:13:13 -0700
1161112911993
117commit $head1
118C O Mitter
119committer@example.com
120Thu Apr 7 15:13:13 2005 -0700
121Thu, 7 Apr 2005 15:13:13 -0700
1221112911993
123EOF
124
125test_format encoding %e <<EOF
126commit $head2
127iso8859-1
128commit $head1
129iso8859-1
130EOF
131
132test_format subject %s <<EOF
133commit $head2
134$changed
135commit $head1
136$added
137EOF
138
139test_format body %b <<EOF
140commit $head2
141commit $head1
142EOF
143
144test_format raw-body %B <<EOF
145commit $head2
146$changed
147
148commit $head1
149$added
150
151EOF
152
153test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
154commit $head2
155\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
156commit $head1
157\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
158EOF
159
160test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
161commit $head2
162\e[1;31;43mfoo\e[m
163commit $head1
164\e[1;31;43mfoo\e[m
165EOF
166
167test_expect_success '%C(auto) does not enable color by default' '
168 git log --format=$AUTO_COLOR -1 >actual &&
169 has_no_color actual
170'
171
172test_expect_success '%C(auto) enables colors for color.diff' '
173 git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
174 has_color actual
175'
176
177test_expect_success '%C(auto) enables colors for color.ui' '
178 git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
179 has_color actual
180'
181
182test_expect_success '%C(auto) respects --color' '
183 git log --format=$AUTO_COLOR -1 --color >actual &&
184 has_color actual
185'
186
187test_expect_success '%C(auto) respects --no-color' '
188 git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
189 has_no_color actual
190'
191
192test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
193 (
194 TERM=vt100 && export TERM &&
195 test_terminal \
196 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
197 has_color actual
198 )
199'
200
201test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
202 (
203 TERM=vt100 && export TERM &&
204 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
205 has_no_color actual
206 )
207'
208
209iconv -f utf-8 -t iso8859-1 > commit-msg <<EOF
210Test printing of complex bodies
211
212This commit message is much longer than the others,
213and it will be encoded in iso8859-1. We should therefore
214include an iso8859 character: ¡bueno!
215EOF
216
217test_expect_success 'setup complex body' '
218 git config i18n.commitencoding iso8859-1 &&
219 echo change2 >foo && git commit -a -F commit-msg &&
220 head3=$(git rev-parse --verify HEAD) &&
221 head3_short=$(git rev-parse --short $head3) &&
222 # unset commit encoding config
223 # otherwise %e does not print encoding value
224 # and following test fails
225 git config --unset i18n.commitEncoding
226
227'
228
229test_format complex-encoding %e <<EOF
230commit $head3
231iso8859-1
232commit $head2
233iso8859-1
234commit $head1
235iso8859-1
236EOF
237
238test_format complex-subject %s <<EOF
239commit $head3
240Test printing of complex bodies
241commit $head2
242$changed
243commit $head1
244$added
245EOF
246
247test_format complex-body %b <<EOF
248commit $head3
249This commit message is much longer than the others,
250and it will be encoded in iso8859-1. We should therefore
251include an iso8859 character: ¡bueno!
252
253commit $head2
254commit $head1
255EOF
256
257test_expect_success '%x00 shows NUL' '
258 echo >expect commit $head3 &&
259 echo >>expect fooQbar &&
260 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
261 nul_to_q <actual.nul >actual &&
262 test_cmp expect actual
263'
264
265test_expect_success '%ad respects --date=' '
266 echo 2005-04-07 >expect.ad-short &&
267 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
268 test_cmp expect.ad-short output.ad-short
269'
270
271test_expect_success 'empty email' '
272 test_tick &&
273 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
274 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
275 test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
276 echo "Eh? $A" >failure
277 false
278 }
279'
280
281test_expect_success 'del LF before empty (1)' '
282 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
283 test_line_count = 2 actual
284'
285
286test_expect_success 'del LF before empty (2)' '
287 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
288 test_line_count = 6 actual &&
289 grep "^$" actual
290'
291
292test_expect_success 'add LF before non-empty (1)' '
293 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
294 test_line_count = 2 actual
295'
296
297test_expect_success 'add LF before non-empty (2)' '
298 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
299 test_line_count = 6 actual &&
300 grep "^$" actual
301'
302
303test_expect_success 'add SP before non-empty (1)' '
304 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
305 test $(wc -w <actual) = 3
306'
307
308test_expect_success 'add SP before non-empty (2)' '
309 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
310 test $(wc -w <actual) = 6
311'
312
313test_expect_success '--abbrev' '
314 echo SHORT SHORT SHORT >expect2 &&
315 echo LONG LONG LONG >expect3 &&
316 git log -1 --format="%h %h %h" HEAD >actual1 &&
317 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
318 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
319 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
320 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
321 test_cmp expect2 fuzzy2 &&
322 test_cmp expect3 fuzzy3 &&
323 ! test_cmp actual1 actual2
324'
325
326test_expect_success '%H is not affected by --abbrev-commit' '
327 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
328 len=$(wc -c <actual) &&
329 test $len = 41
330'
331
332test_expect_success '%h is not affected by --abbrev-commit' '
333 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
334 len=$(wc -c <actual) &&
335 test $len = 21
336'
337
338test_expect_success '"%h %gD: %gs" is same as git-reflog' '
339 git reflog >expect &&
340 git log -g --format="%h %gD: %gs" >actual &&
341 test_cmp expect actual
342'
343
344test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
345 git reflog --date=raw >expect &&
346 git log -g --format="%h %gD: %gs" --date=raw >actual &&
347 test_cmp expect actual
348'
349
350test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
351 git reflog --abbrev=13 --date=raw >expect &&
352 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
353 test_cmp expect actual
354'
355
356test_expect_success '%gd shortens ref name' '
357 echo "master@{0}" >expect.gd-short &&
358 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
359 test_cmp expect.gd-short actual.gd-short
360'
361
362test_expect_success 'reflog identity' '
363 echo "C O Mitter:committer@example.com" >expect &&
364 git log -g -1 --format="%gn:%ge" >actual &&
365 test_cmp expect actual
366'
367
368test_expect_success 'oneline with empty message' '
369 git commit -m "dummy" --allow-empty &&
370 git commit -m "dummy" --allow-empty &&
371 git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
372 git rev-list --oneline HEAD >test.txt &&
373 test_line_count = 5 test.txt &&
374 git rev-list --oneline --graph HEAD >testg.txt &&
375 test_line_count = 5 testg.txt
376'
377
378test_expect_success 'single-character name is parsed correctly' '
379 git commit --author="a <a@example.com>" --allow-empty -m foo &&
380 echo "a <a@example.com>" >expect &&
381 git log -1 --format="%an <%ae>" >actual &&
382 test_cmp expect actual
383'
384
385test_done