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# Tested non-UTF-8 encoding
13test_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.
18added_utf8_part=$(printf "\303\274")
19added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
20added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
21added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
22# same but "changed"
23changed_utf8_part=$(printf "\303\244")
24changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
25changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
26changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
27
28# Count of char to truncate
29# Number is chosen so, that non-ACSII characters
30# (see $added_utf8_part and $changed_utf8_part)
31# fall into truncated parts of appropriate words both from left and right
32truncate_count=20
33
34test_expect_success 'setup' '
35 : >foo &&
36 git add foo &&
37 git config i18n.commitEncoding $test_encoding &&
38 echo "$added_iso88591" | git commit -F - &&
39 head1=$(git rev-parse --verify HEAD) &&
40 head1_short=$(git rev-parse --verify --short $head1) &&
41 tree1=$(git rev-parse --verify HEAD:) &&
42 tree1_short=$(git rev-parse --verify --short $tree1) &&
43 echo "$changed" > foo &&
44 echo "$changed_iso88591" | git commit -a -F - &&
45 head2=$(git rev-parse --verify HEAD) &&
46 head2_short=$(git rev-parse --verify --short $head2) &&
47 tree2=$(git rev-parse --verify HEAD:) &&
48 tree2_short=$(git rev-parse --verify --short $tree2)
49 git config --unset i18n.commitEncoding
50'
51
52# usage: test_format name format_string [failure] <expected_output
53test_format () {
54 cat >expect.$1
55 test_expect_${3:-success} "format $1" "
56 git rev-list --pretty=format:'$2' master >output.$1 &&
57 test_cmp expect.$1 output.$1
58 "
59}
60
61# Feed to --format to provide predictable colored sequences.
62AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
63has_color () {
64 printf '\033[31mfoo\033[m\n' >expect &&
65 test_cmp expect "$1"
66}
67
68has_no_color () {
69 echo foo >expect &&
70 test_cmp expect "$1"
71}
72
73test_format percent %%h <<EOF
74commit $head2
75%h
76commit $head1
77%h
78EOF
79
80test_format hash %H%n%h <<EOF
81commit $head2
82$head2
83$head2_short
84commit $head1
85$head1
86$head1_short
87EOF
88
89test_format tree %T%n%t <<EOF
90commit $head2
91$tree2
92$tree2_short
93commit $head1
94$tree1
95$tree1_short
96EOF
97
98test_format parents %P%n%p <<EOF
99commit $head2
100$head1
101$head1_short
102commit $head1
103
104
105EOF
106
107# we don't test relative here
108test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
109commit $head2
110A U Thor
111author@example.com
112Thu Apr 7 15:13:13 2005 -0700
113Thu, 7 Apr 2005 15:13:13 -0700
1141112911993
115commit $head1
116A U Thor
117author@example.com
118Thu Apr 7 15:13:13 2005 -0700
119Thu, 7 Apr 2005 15:13:13 -0700
1201112911993
121EOF
122
123test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
124commit $head2
125C O Mitter
126committer@example.com
127Thu Apr 7 15:13:13 2005 -0700
128Thu, 7 Apr 2005 15:13:13 -0700
1291112911993
130commit $head1
131C O Mitter
132committer@example.com
133Thu Apr 7 15:13:13 2005 -0700
134Thu, 7 Apr 2005 15:13:13 -0700
1351112911993
136EOF
137
138test_format encoding %e <<EOF
139commit $head2
140$test_encoding
141commit $head1
142$test_encoding
143EOF
144
145test_format subject %s <<EOF
146commit $head2
147$changed
148commit $head1
149$added
150EOF
151
152test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
153commit $head2
154changed (ge${changed_utf8_part}ndert)..
155commit $head1
156added (hinzugef${added_utf8_part}gt..
157EOF
158
159test_format body %b <<EOF
160commit $head2
161commit $head1
162EOF
163
164test_format raw-body %B <<EOF
165commit $head2
166$changed
167
168commit $head1
169$added
170
171EOF
172
173test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
174commit $head2
175\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
176commit $head1
177\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
178EOF
179
180test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
181commit $head2
182\e[1;31;43mfoo\e[m
183commit $head1
184\e[1;31;43mfoo\e[m
185EOF
186
187test_expect_success '%C(auto) does not enable color by default' '
188 git log --format=$AUTO_COLOR -1 >actual &&
189 has_no_color actual
190'
191
192test_expect_success '%C(auto) enables colors for color.diff' '
193 git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
194 has_color actual
195'
196
197test_expect_success '%C(auto) enables colors for color.ui' '
198 git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
199 has_color actual
200'
201
202test_expect_success '%C(auto) respects --color' '
203 git log --format=$AUTO_COLOR -1 --color >actual &&
204 has_color actual
205'
206
207test_expect_success '%C(auto) respects --no-color' '
208 git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
209 has_no_color actual
210'
211
212test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
213 test_terminal env TERM=vt100 \
214 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
215 has_color actual
216'
217
218test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
219 (
220 TERM=vt100 && export TERM &&
221 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
222 has_no_color actual
223 )
224'
225
226iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
227Test printing of complex bodies
228
229This commit message is much longer than the others,
230and it will be encoded in $test_encoding. We should therefore
231include an ISO8859 character: ¡bueno!
232EOF
233
234test_expect_success 'setup complex body' '
235 git config i18n.commitencoding $test_encoding &&
236 echo change2 >foo && git commit -a -F commit-msg &&
237 head3=$(git rev-parse --verify HEAD) &&
238 head3_short=$(git rev-parse --short $head3)
239'
240
241test_format complex-encoding %e <<EOF
242commit $head3
243$test_encoding
244commit $head2
245$test_encoding
246commit $head1
247$test_encoding
248EOF
249
250test_format complex-subject %s <<EOF
251commit $head3
252Test printing of complex bodies
253commit $head2
254$changed_iso88591
255commit $head1
256$added_iso88591
257EOF
258
259test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
260commit $head3
261Test printing of c..
262commit $head2
263changed (ge${changed_utf8_part_iso88591}ndert)..
264commit $head1
265added (hinzugef${added_utf8_part_iso88591}gt..
266EOF
267
268test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
269commit $head3
270Test prin..ex bodies
271commit $head2
272changed (..dert) foo
273commit $head1
274added (hi..f${added_utf8_part_iso88591}gt) foo
275EOF
276
277test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
278commit $head3
279.. of complex bodies
280commit $head2
281..ged (ge${changed_utf8_part_iso88591}ndert) foo
282commit $head1
283.. (hinzugef${added_utf8_part_iso88591}gt) foo
284EOF
285
286test_expect_success 'prepare expected messages (for test %b)' '
287 cat <<-EOF >expected.utf-8 &&
288 commit $head3
289 This commit message is much longer than the others,
290 and it will be encoded in $test_encoding. We should therefore
291 include an ISO8859 character: ¡bueno!
292
293 commit $head2
294 commit $head1
295 EOF
296 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
297'
298
299test_format complex-body %b <expected.ISO8859-1
300
301# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
302# so unset i18n.commitEncoding to test encoding conversion
303git config --unset i18n.commitEncoding
304
305test_format complex-subject-commitencoding-unset %s <<EOF
306commit $head3
307Test printing of complex bodies
308commit $head2
309$changed
310commit $head1
311$added
312EOF
313
314test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
315commit $head3
316Test printing of c..
317commit $head2
318changed (ge${changed_utf8_part}ndert)..
319commit $head1
320added (hinzugef${added_utf8_part}gt..
321EOF
322
323test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
324commit $head3
325Test prin..ex bodies
326commit $head2
327changed (..dert) foo
328commit $head1
329added (hi..f${added_utf8_part}gt) foo
330EOF
331
332test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
333commit $head3
334.. of complex bodies
335commit $head2
336..ged (ge${changed_utf8_part}ndert) foo
337commit $head1
338.. (hinzugef${added_utf8_part}gt) foo
339EOF
340
341test_format complex-body-commitencoding-unset %b <expected.utf-8
342
343test_expect_success '%x00 shows NUL' '
344 echo >expect commit $head3 &&
345 echo >>expect fooQbar &&
346 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
347 nul_to_q <actual.nul >actual &&
348 test_cmp expect actual
349'
350
351test_expect_success '%ad respects --date=' '
352 echo 2005-04-07 >expect.ad-short &&
353 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
354 test_cmp expect.ad-short output.ad-short
355'
356
357test_expect_success 'empty email' '
358 test_tick &&
359 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
360 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
361 test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
362 echo "Eh? $A" >failure
363 false
364 }
365'
366
367test_expect_success 'del LF before empty (1)' '
368 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
369 test_line_count = 2 actual
370'
371
372test_expect_success 'del LF before empty (2)' '
373 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
374 test_line_count = 6 actual &&
375 grep "^$" actual
376'
377
378test_expect_success 'add LF before non-empty (1)' '
379 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
380 test_line_count = 2 actual
381'
382
383test_expect_success 'add LF before non-empty (2)' '
384 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
385 test_line_count = 6 actual &&
386 grep "^$" actual
387'
388
389test_expect_success 'add SP before non-empty (1)' '
390 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
391 test $(wc -w <actual) = 3
392'
393
394test_expect_success 'add SP before non-empty (2)' '
395 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
396 test $(wc -w <actual) = 6
397'
398
399test_expect_success '--abbrev' '
400 echo SHORT SHORT SHORT >expect2 &&
401 echo LONG LONG LONG >expect3 &&
402 git log -1 --format="%h %h %h" HEAD >actual1 &&
403 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
404 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
405 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
406 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
407 test_cmp expect2 fuzzy2 &&
408 test_cmp expect3 fuzzy3 &&
409 ! test_cmp actual1 actual2
410'
411
412test_expect_success '%H is not affected by --abbrev-commit' '
413 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
414 len=$(wc -c <actual) &&
415 test $len = 41
416'
417
418test_expect_success '%h is not affected by --abbrev-commit' '
419 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
420 len=$(wc -c <actual) &&
421 test $len = 21
422'
423
424test_expect_success '"%h %gD: %gs" is same as git-reflog' '
425 git reflog >expect &&
426 git log -g --format="%h %gD: %gs" >actual &&
427 test_cmp expect actual
428'
429
430test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
431 git reflog --date=raw >expect &&
432 git log -g --format="%h %gD: %gs" --date=raw >actual &&
433 test_cmp expect actual
434'
435
436test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
437 git reflog --abbrev=13 --date=raw >expect &&
438 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
439 test_cmp expect actual
440'
441
442test_expect_success '%gd shortens ref name' '
443 echo "master@{0}" >expect.gd-short &&
444 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
445 test_cmp expect.gd-short actual.gd-short
446'
447
448test_expect_success 'reflog identity' '
449 echo "C O Mitter:committer@example.com" >expect &&
450 git log -g -1 --format="%gn:%ge" >actual &&
451 test_cmp expect actual
452'
453
454test_expect_success 'oneline with empty message' '
455 git commit -m "dummy" --allow-empty &&
456 git commit -m "dummy" --allow-empty &&
457 git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
458 git rev-list --oneline HEAD >test.txt &&
459 test_line_count = 5 test.txt &&
460 git rev-list --oneline --graph HEAD >testg.txt &&
461 test_line_count = 5 testg.txt
462'
463
464test_expect_success 'single-character name is parsed correctly' '
465 git commit --author="a <a@example.com>" --allow-empty -m foo &&
466 echo "a <a@example.com>" >expect &&
467 git log -1 --format="%an <%ae>" >actual &&
468 test_cmp expect actual
469'
470
471test_expect_success 'unused %G placeholders are passed through' '
472 echo "%GX %G" >expect &&
473 git log -1 --format="%GX %G" >actual &&
474 test_cmp expect actual
475'
476
477test_done