Merge branch 'mc/git-p4-prepare-p4-only'
[alt-git.git] / t / t6006-rev-list-format.sh
blobc277db64f75ac9f9c0cd617262824d76959fbfbd
1 #!/bin/sh
3 # Copyright (c) 2009 Jens Lehmann
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
6 test_description='git rev-list --pretty=format test'
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
11 test_tick
12 # Tested non-UTF-8 encoding
13 test_encoding="ISO8859-1"
15 # String "added" in German
16 # (translated with Google Translate),
17 # encoded in UTF-8, used as a commit log message below.
18 added_utf8_part=$(printf "\303\274")
19 added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
20 added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
21 added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
22 # same but "changed"
23 changed_utf8_part=$(printf "\303\244")
24 changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
25 changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
26 changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
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
32 truncate_count=20
34 test_expect_success 'setup' '
35 : >foo &&
36 git add foo &&
37 git config i18n.commitEncoding $test_encoding &&
38 git commit -m "$added_iso88591" &&
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 git commit -a -m "$changed_iso88591" &&
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
52 # usage: test_format name format_string [failure] <expected_output
53 test_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
61 # Feed to --format to provide predictable colored sequences.
62 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
63 has_color () {
64 printf '\033[31mfoo\033[m\n' >expect &&
65 test_cmp expect "$1"
68 has_no_color () {
69 echo foo >expect &&
70 test_cmp expect "$1"
73 test_format percent %%h <<EOF
74 commit $head2
76 commit $head1
78 EOF
80 test_format hash %H%n%h <<EOF
81 commit $head2
82 $head2
83 $head2_short
84 commit $head1
85 $head1
86 $head1_short
87 EOF
89 test_format tree %T%n%t <<EOF
90 commit $head2
91 $tree2
92 $tree2_short
93 commit $head1
94 $tree1
95 $tree1_short
96 EOF
98 test_format parents %P%n%p <<EOF
99 commit $head2
100 $head1
101 $head1_short
102 commit $head1
107 # we don't test relative here
108 test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
109 commit $head2
110 A U Thor
111 author@example.com
112 Thu Apr 7 15:13:13 2005 -0700
113 Thu, 7 Apr 2005 15:13:13 -0700
114 1112911993
115 commit $head1
116 A U Thor
117 author@example.com
118 Thu Apr 7 15:13:13 2005 -0700
119 Thu, 7 Apr 2005 15:13:13 -0700
120 1112911993
123 test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
124 commit $head2
125 C O Mitter
126 committer@example.com
127 Thu Apr 7 15:13:13 2005 -0700
128 Thu, 7 Apr 2005 15:13:13 -0700
129 1112911993
130 commit $head1
131 C O Mitter
132 committer@example.com
133 Thu Apr 7 15:13:13 2005 -0700
134 Thu, 7 Apr 2005 15:13:13 -0700
135 1112911993
138 test_format encoding %e <<EOF
139 commit $head2
140 $test_encoding
141 commit $head1
142 $test_encoding
145 test_format subject %s <<EOF
146 commit $head2
147 $changed
148 commit $head1
149 $added
152 test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
153 commit $head2
154 changed (ge${changed_utf8_part}ndert)..
155 commit $head1
156 added (hinzugef${added_utf8_part}gt..
159 test_format body %b <<EOF
160 commit $head2
161 commit $head1
164 test_format raw-body %B <<EOF
165 commit $head2
166 $changed
168 commit $head1
169 $added
173 test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
174 commit $head2
175 \e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
176 commit $head1
177 \e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
180 test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
181 commit $head2
182 \e[1;31;43mfoo\e[m
183 commit $head1
184 \e[1;31;43mfoo\e[m
187 test_expect_success '%C(auto) does not enable color by default' '
188 git log --format=$AUTO_COLOR -1 >actual &&
189 has_no_color actual
192 test_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
197 test_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
202 test_expect_success '%C(auto) respects --color' '
203 git log --format=$AUTO_COLOR -1 --color >actual &&
204 has_color actual
207 test_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
212 test_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
218 test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
220 TERM=vt100 && export TERM &&
221 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
222 has_no_color actual
226 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
227 Test printing of complex bodies
229 This commit message is much longer than the others,
230 and it will be encoded in $test_encoding. We should therefore
231 include an ISO8859 character: ¡bueno!
234 test_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)
241 test_format complex-encoding %e <<EOF
242 commit $head3
243 $test_encoding
244 commit $head2
245 $test_encoding
246 commit $head1
247 $test_encoding
250 test_format complex-subject %s <<EOF
251 commit $head3
252 Test printing of complex bodies
253 commit $head2
254 $changed_iso88591
255 commit $head1
256 $added_iso88591
259 test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
260 commit $head3
261 Test printing of c..
262 commit $head2
263 changed (ge${changed_utf8_part_iso88591}ndert)..
264 commit $head1
265 added (hinzugef${added_utf8_part_iso88591}gt..
268 test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
269 commit $head3
270 Test prin..ex bodies
271 commit $head2
272 changed (..dert) foo
273 commit $head1
274 added (hi..f${added_utf8_part_iso88591}gt) foo
277 test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
278 commit $head3
279 .. of complex bodies
280 commit $head2
281 ..ged (ge${changed_utf8_part_iso88591}ndert) foo
282 commit $head1
283 .. (hinzugef${added_utf8_part_iso88591}gt) foo
286 test_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!
293 commit $head2
294 commit $head1
296 iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
299 test_format complex-body %b <expected.ISO8859-1
301 # Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
302 # so unset i18n.commitEncoding to test encoding conversion
303 git config --unset i18n.commitEncoding
305 test_format complex-subject-commitencoding-unset %s <<EOF
306 commit $head3
307 Test printing of complex bodies
308 commit $head2
309 $changed
310 commit $head1
311 $added
314 test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
315 commit $head3
316 Test printing of c..
317 commit $head2
318 changed (ge${changed_utf8_part}ndert)..
319 commit $head1
320 added (hinzugef${added_utf8_part}gt..
323 test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
324 commit $head3
325 Test prin..ex bodies
326 commit $head2
327 changed (..dert) foo
328 commit $head1
329 added (hi..f${added_utf8_part}gt) foo
332 test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
333 commit $head3
334 .. of complex bodies
335 commit $head2
336 ..ged (ge${changed_utf8_part}ndert) foo
337 commit $head1
338 .. (hinzugef${added_utf8_part}gt) foo
341 test_format complex-body-commitencoding-unset %b <expected.utf-8
343 test_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
351 test_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
357 test_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
367 test_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
372 test_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
378 test_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
383 test_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
389 test_expect_success 'add SP before non-empty (1)' '
390 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
391 test $(wc -w <actual) = 3
394 test_expect_success 'add SP before non-empty (2)' '
395 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
396 test $(wc -w <actual) = 6
399 test_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
412 test_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
418 test_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
424 test_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
430 test_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
436 test_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
442 test_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
448 test_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
454 test_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
464 test_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
471 test_done