]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6006-rev-list-format.sh
tests: use "env" to run commands with temporary env-var settings
[thirdparty/git.git] / t / t6006-rev-list-format.sh
CommitLineData
fa21b602
JK
1#!/bin/sh
2
de6029a2
AS
3# Copyright (c) 2009 Jens Lehmann
4# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5
5be60078 6test_description='git rev-list --pretty=format test'
fa21b602
JK
7
8. ./test-lib.sh
30825178 9. "$TEST_DIRECTORY"/lib-terminal.sh
fa21b602
JK
10
11test_tick
17cc2ef1
AS
12# String "added" in German
13# (translated with Google Translate),
14# encoded in UTF-8, used as a commit log message below.
de6029a2 15added=$(printf "added (hinzugef\303\274gt) foo")
14332bcc 16added_iso88591=$(echo "$added" | iconv -f utf-8 -t iso8859-1)
de6029a2
AS
17# same but "changed"
18changed=$(printf "changed (ge\303\244ndert) foo")
14332bcc 19changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t iso8859-1)
de6029a2 20
fa21b602 21test_expect_success 'setup' '
77a6815d
AS
22 : >foo &&
23 git add foo &&
14332bcc 24 git config i18n.commitEncoding iso8859-1 &&
de6029a2 25 git commit -m "$added_iso88591" &&
77a6815d
AS
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) &&
de6029a2
AS
30 echo "$changed" > foo &&
31 git commit -a -m "$changed_iso88591" &&
77a6815d
AS
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)
de6029a2 36 git config --unset i18n.commitEncoding
fa21b602
JK
37'
38
17cc2ef1 39# usage: test_format name format_string [failure] <expected_output
2581ad5e 40test_format () {
fa21b602 41 cat >expect.$1
17cc2ef1
AS
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 "
fa21b602
JK
46}
47
30825178
JH
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
77a6815d
AS
60test_format percent %%h <<EOF
61commit $head2
0a0416a3 62%h
77a6815d 63commit $head1
0a0416a3
JK
64%h
65EOF
66
77a6815d
AS
67test_format hash %H%n%h <<EOF
68commit $head2
69$head2
70$head2_short
71commit $head1
72$head1
73$head1_short
fa21b602
JK
74EOF
75
77a6815d
AS
76test_format tree %T%n%t <<EOF
77commit $head2
78$tree2
79$tree2_short
80commit $head1
81$tree1
82$tree1_short
fa21b602
JK
83EOF
84
77a6815d
AS
85test_format parents %P%n%p <<EOF
86commit $head2
87$head1
88$head1_short
89commit $head1
542e165c
JH
90
91
fa21b602
JK
92EOF
93
94# we don't test relative here
77a6815d
AS
95test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
96commit $head2
fa21b602
JK
97A U Thor
98author@example.com
99Thu Apr 7 15:13:13 2005 -0700
100Thu, 7 Apr 2005 15:13:13 -0700
1011112911993
77a6815d 102commit $head1
fa21b602
JK
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
77a6815d
AS
110test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
111commit $head2
fa21b602
JK
112C O Mitter
113committer@example.com
114Thu Apr 7 15:13:13 2005 -0700
115Thu, 7 Apr 2005 15:13:13 -0700
1161112911993
77a6815d 117commit $head1
fa21b602
JK
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
77a6815d
AS
125test_format encoding %e <<EOF
126commit $head2
14332bcc 127iso8859-1
77a6815d 128commit $head1
14332bcc 129iso8859-1
fa21b602
JK
130EOF
131
ecaee805 132test_format subject %s <<EOF
77a6815d 133commit $head2
de6029a2 134$changed
77a6815d 135commit $head1
de6029a2 136$added
fa21b602
JK
137EOF
138
77a6815d
AS
139test_format body %b <<EOF
140commit $head2
141commit $head1
fa21b602
JK
142EOF
143
ecaee805 144test_format raw-body %B <<EOF
77a6815d 145commit $head2
de6029a2 146$changed
1367b12a 147
77a6815d 148commit $head1
de6029a2 149$added
1367b12a
EB
150
151EOF
152
77a6815d
AS
153test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
154commit $head2
fa21b602 155\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
77a6815d 156commit $head1
fa21b602
JK
157\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
158EOF
159
77a6815d
AS
160test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
161commit $head2
c002922a 162\e[1;31;43mfoo\e[m
77a6815d 163commit $head1
c002922a
JK
164\e[1;31;43mfoo\e[m
165EOF
166
30825178
JH
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)' '
512477b1
DT
193 test_terminal env TERM=vt100 \
194 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
195 has_color actual
30825178
JH
196'
197
198test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
199 (
200 TERM=vt100 && export TERM &&
201 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
202 has_no_color actual
203 )
204'
205
de6029a2 206iconv -f utf-8 -t iso8859-1 > commit-msg <<EOF
03bcaaca
JK
207Test printing of complex bodies
208
209This commit message is much longer than the others,
210and it will be encoded in iso8859-1. We should therefore
de6029a2 211include an iso8859 character: ¡bueno!
03bcaaca 212EOF
77a6815d 213
03bcaaca 214test_expect_success 'setup complex body' '
77a6815d
AS
215 git config i18n.commitencoding iso8859-1 &&
216 echo change2 >foo && git commit -a -F commit-msg &&
217 head3=$(git rev-parse --verify HEAD) &&
0fe6df3c 218 head3_short=$(git rev-parse --short $head3)
03bcaaca
JK
219'
220
77a6815d
AS
221test_format complex-encoding %e <<EOF
222commit $head3
03bcaaca 223iso8859-1
77a6815d 224commit $head2
14332bcc 225iso8859-1
77a6815d 226commit $head1
14332bcc 227iso8859-1
03bcaaca
JK
228EOF
229
ecaee805 230test_format complex-subject %s <<EOF
77a6815d 231commit $head3
03bcaaca 232Test printing of complex bodies
77a6815d 233commit $head2
0fe6df3c 234$changed_iso88591
77a6815d 235commit $head1
0fe6df3c 236$added_iso88591
03bcaaca
JK
237EOF
238
0fe6df3c
AS
239test_expect_success 'prepare expected messages (for test %b)' '
240 cat <<-EOF >expected.utf-8 &&
241 commit $head3
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!
245
246 commit $head2
247 commit $head1
248 EOF
249 iconv -f utf-8 -t iso8859-1 expected.utf-8 >expected.iso8859-1
250'
251
252test_format complex-body %b <expected.iso8859-1
03bcaaca 253
0fe6df3c
AS
254# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
255# so unset i18n.commitEncoding to test encoding conversion
256git config --unset i18n.commitEncoding
257
258test_format complex-subject-commitencoding-unset %s <<EOF
259commit $head3
260Test printing of complex bodies
77a6815d 261commit $head2
0fe6df3c 262$changed
77a6815d 263commit $head1
0fe6df3c 264$added
03bcaaca
JK
265EOF
266
0fe6df3c
AS
267test_format complex-body-commitencoding-unset %b <expected.utf-8
268
9130ac9f 269test_expect_success '%x00 shows NUL' '
77a6815d 270 echo >expect commit $head3 &&
9130ac9f
JK
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
275'
276
d36f8679
JK
277test_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
281'
282
f7ab5c79
JH
283test_expect_success 'empty email' '
284 test_tick &&
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
289 false
290 }
291'
292
9fa708da
JH
293test_expect_success 'del LF before empty (1)' '
294 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
3fb0459b 295 test_line_count = 2 actual
9fa708da
JH
296'
297
298test_expect_success 'del LF before empty (2)' '
299 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
3fb0459b 300 test_line_count = 6 actual &&
9fa708da
JH
301 grep "^$" actual
302'
303
304test_expect_success 'add LF before non-empty (1)' '
305 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
3fb0459b 306 test_line_count = 2 actual
9fa708da
JH
307'
308
309test_expect_success 'add LF before non-empty (2)' '
310 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
3fb0459b 311 test_line_count = 6 actual &&
9fa708da
JH
312 grep "^$" actual
313'
314
7b88176e
MG
315test_expect_success 'add SP before non-empty (1)' '
316 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
de6029a2 317 test $(wc -w <actual) = 3
7b88176e
MG
318'
319
320test_expect_success 'add SP before non-empty (2)' '
321 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
de6029a2 322 test $(wc -w <actual) = 6
7b88176e
MG
323'
324
c1977021
WP
325test_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
336'
337
338test_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) &&
341 test $len = 41
342'
343
344test_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) &&
347 test $len = 21
348'
349
8f8f5476
TR
350test_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
354'
355
356test_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
360'
361
c1977021
WP
362test_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
366'
367
8f8f5476
TR
368test_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
372'
373
cd1957f5
JK
374test_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
378'
379
1fb5fdd2
EFL
380test_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^^.. &&
f02dd06e 384 git rev-list --oneline HEAD >test.txt &&
3fb0459b
SL
385 test_line_count = 5 test.txt &&
386 git rev-list --oneline --graph HEAD >testg.txt &&
387 test_line_count = 5 testg.txt
1fb5fdd2
EFL
388'
389
d9955fd6
JK
390test_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
395'
396
fa21b602 397test_done