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