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