t4205, t6006, t7102: make functions better readable
[git/git.git] / t / t4205-log-pretty-formats.sh
CommitLineData
8028184e
WP
1#!/bin/sh
2#
3# Copyright (c) 2010, Will Palmer
de6029a2 4# Copyright (c) 2011, Alexey Shumkin (+ non-UTF-8 commit encoding tests)
8028184e
WP
5#
6
7test_description='Test pretty formats'
8. ./test-lib.sh
9
de6029a2 10commit_msg () {
17cc2ef1
AS
11 # String "initial. initial" partly in German
12 # (translated with Google Translate),
de6029a2
AS
13 # encoded in UTF-8, used as a commit log message below.
14 msg=$(printf "initial. anf\303\244nglich")
15 if test -n "$1"
16 then
17 msg=$(echo $msg | iconv -f utf-8 -t $1)
18 fi
19 if test -n "$2" -a -n "$3"
20 then
21 # cut string, replace cut part with two dots
22 # $2 - chars count from the beginning of the string
23 # $3 - "trailing" chars
24 # LC_ALL is set to make `sed` interpret "." as a UTF-8 char not a byte
25 # as it does with C locale
26 msg=$(echo $msg | LC_ALL=en_US.UTF-8 sed -e "s/^\(.\{$2\}\)$3/\1../")
27 fi
28 echo $msg
29}
30
8028184e
WP
31test_expect_success 'set up basic repos' '
32 >foo &&
33 >bar &&
34 git add foo &&
35 test_tick &&
14332bcc
AS
36 git config i18n.commitEncoding iso8859-1 &&
37 git commit -m "$(commit_msg iso8859-1)" &&
8028184e
WP
38 git add bar &&
39 test_tick &&
de6029a2
AS
40 git commit -m "add bar" &&
41 git config --unset i18n.commitEncoding
8028184e
WP
42'
43
44test_expect_success 'alias builtin format' '
45 git log --pretty=oneline >expected &&
46 git config pretty.test-alias oneline &&
47 git log --pretty=test-alias >actual &&
48 test_cmp expected actual
49'
50
51test_expect_success 'alias masking builtin format' '
52 git log --pretty=oneline >expected &&
53 git config pretty.oneline "%H" &&
54 git log --pretty=oneline >actual &&
55 test_cmp expected actual
56'
57
58test_expect_success 'alias user-defined format' '
59 git log --pretty="format:%h" >expected &&
60 git config pretty.test-alias "format:%h" &&
61 git log --pretty=test-alias >actual &&
62 test_cmp expected actual
63'
64
14332bcc
AS
65test_expect_success 'alias user-defined tformat with %s (iso8859-1 encoding)' '
66 git config i18n.logOutputEncoding iso8859-1 &&
de6029a2
AS
67 git log --oneline >expected-s &&
68 git log --pretty="tformat:%h %s" >actual-s &&
69 git config --unset i18n.logOutputEncoding &&
70 test_cmp expected-s actual-s
71'
72
ecaee805 73test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
de6029a2
AS
74 git log --oneline >expected-s &&
75 git log --pretty="tformat:%h %s" >actual-s &&
76 test_cmp expected-s actual-s
77'
78
8028184e
WP
79test_expect_success 'alias user-defined tformat' '
80 git log --pretty="tformat:%h" >expected &&
81 git config pretty.test-alias "tformat:%h" &&
82 git log --pretty=test-alias >actual &&
83 test_cmp expected actual
84'
85
7be8b3ba 86test_expect_success 'alias non-existent format' '
8028184e
WP
87 git config pretty.test-alias format-that-will-never-exist &&
88 test_must_fail git log --pretty=test-alias
89'
90
91test_expect_success 'alias of an alias' '
92 git log --pretty="tformat:%h" >expected &&
93 git config pretty.test-foo "tformat:%h" &&
94 git config pretty.test-bar test-foo &&
95 git log --pretty=test-bar >actual && test_cmp expected actual
96'
97
98test_expect_success 'alias masking an alias' '
99 git log --pretty=format:"Two %H" >expected &&
100 git config pretty.duplicate "format:One %H" &&
101 git config --add pretty.duplicate "format:Two %H" &&
102 git log --pretty=duplicate >actual &&
103 test_cmp expected actual
104'
105
106test_expect_success 'alias loop' '
107 git config pretty.test-foo test-bar &&
108 git config pretty.test-bar test-foo &&
109 test_must_fail git log --pretty=test-foo
110'
111
ecaee805 112test_expect_success 'NUL separation' '
de6029a2 113 printf "add bar\0$(commit_msg)" >expected &&
3e065308
JK
114 git log -z --pretty="format:%s" >actual &&
115 test_cmp expected actual
116'
117
ecaee805 118test_expect_success 'NUL termination' '
de6029a2 119 printf "add bar\0$(commit_msg)\0" >expected &&
3e065308
JK
120 git log -z --pretty="tformat:%s" >actual &&
121 test_cmp expected actual
122'
123
ecaee805 124test_expect_success 'NUL separation with --stat' '
fafd3829 125 stat0_part=$(git diff --stat HEAD^ HEAD) &&
74faaa16 126 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
de6029a2 127 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
fafd3829 128 git log -z --stat --pretty="format:%s" >actual &&
b354f11b 129 test_i18ncmp expected actual
fafd3829
JH
130'
131
132test_expect_failure 'NUL termination with --stat' '
133 stat0_part=$(git diff --stat HEAD^ HEAD) &&
74faaa16 134 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
de6029a2 135 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n0" >expected &&
fafd3829 136 git log -z --stat --pretty="tformat:%s" >actual &&
b354f11b 137 test_i18ncmp expected actual
fafd3829
JH
138'
139
a5752342
NTND
140test_expect_success 'setup more commits' '
141 test_commit "message one" one one message-one &&
a742f2a0
AS
142 test_commit "message two" two two message-two &&
143 head1=$(git rev-parse --verify --short HEAD~0) &&
144 head2=$(git rev-parse --verify --short HEAD~1) &&
145 head3=$(git rev-parse --verify --short HEAD~2) &&
146 head4=$(git rev-parse --verify --short HEAD~3)
a5752342
NTND
147'
148
f3445f78
AS
149test_expect_success 'left alignment formatting' '
150 git log --pretty="format:%<(40)%s" >actual &&
a5752342
NTND
151 # complete the incomplete line at the end
152 echo >>actual &&
f3445f78 153 qz_to_tab_space <<EOF >expected &&
a5752342
NTND
154message two Z
155message one Z
156add bar Z
de6029a2 157$(commit_msg) Z
a5752342
NTND
158EOF
159 test_cmp expected actual
f3445f78 160'
a5752342 161
f3445f78
AS
162test_expect_success 'left alignment formatting at the nth column' '
163 git log --pretty="format:%h %<|(40)%s" >actual &&
a5752342
NTND
164 # complete the incomplete line at the end
165 echo >>actual &&
f3445f78 166 qz_to_tab_space <<EOF >expected &&
a742f2a0
AS
167$head1 message two Z
168$head2 message one Z
169$head3 add bar Z
de6029a2 170$head4 $(commit_msg) Z
a5752342
NTND
171EOF
172 test_cmp expected actual
f3445f78 173'
a5752342 174
f3445f78
AS
175test_expect_success 'left alignment formatting with no padding' '
176 git log --pretty="format:%<(1)%s" >actual &&
a5752342
NTND
177 # complete the incomplete line at the end
178 echo >>actual &&
f3445f78 179 cat <<EOF >expected &&
a5752342
NTND
180message two
181message one
182add bar
de6029a2 183$(commit_msg)
a5752342
NTND
184EOF
185 test_cmp expected actual
f3445f78 186'
a5752342 187
f3445f78
AS
188test_expect_success 'left alignment formatting with trunc' '
189 git log --pretty="format:%<(10,trunc)%s" >actual &&
a7f01c6b
NTND
190 # complete the incomplete line at the end
191 echo >>actual &&
f3445f78 192 qz_to_tab_space <<EOF >expected &&
a7f01c6b
NTND
193message ..
194message ..
195add bar Z
7c375214 196$(commit_msg "" "8" "..*$")
a7f01c6b
NTND
197EOF
198 test_cmp expected actual
f3445f78 199'
a7f01c6b 200
f3445f78
AS
201test_expect_success 'left alignment formatting with ltrunc' '
202 git log --pretty="format:%<(10,ltrunc)%s" >actual &&
a7f01c6b
NTND
203 # complete the incomplete line at the end
204 echo >>actual &&
f3445f78 205 qz_to_tab_space <<EOF >expected &&
a7f01c6b
NTND
206..sage two
207..sage one
208add bar Z
de6029a2 209$(commit_msg "" "0" ".\{11\}")
a7f01c6b
NTND
210EOF
211 test_cmp expected actual
f3445f78 212'
a7f01c6b 213
f3445f78
AS
214test_expect_success 'left alignment formatting with mtrunc' '
215 git log --pretty="format:%<(10,mtrunc)%s" >actual &&
a7f01c6b
NTND
216 # complete the incomplete line at the end
217 echo >>actual &&
f3445f78 218 qz_to_tab_space <<EOF >expected &&
a7f01c6b
NTND
219mess.. two
220mess.. one
221add bar Z
de6029a2 222$(commit_msg "" "4" ".\{11\}")
a7f01c6b
NTND
223EOF
224 test_cmp expected actual
f3445f78 225'
a7f01c6b 226
f3445f78
AS
227test_expect_success 'right alignment formatting' '
228 git log --pretty="format:%>(40)%s" >actual &&
a5752342
NTND
229 # complete the incomplete line at the end
230 echo >>actual &&
f3445f78 231 qz_to_tab_space <<EOF >expected &&
a5752342
NTND
232Z message two
233Z message one
234Z add bar
de6029a2 235Z $(commit_msg)
a5752342
NTND
236EOF
237 test_cmp expected actual
f3445f78 238'
a5752342 239
f3445f78
AS
240test_expect_success 'right alignment formatting at the nth column' '
241 git log --pretty="format:%h %>|(40)%s" >actual &&
a5752342
NTND
242 # complete the incomplete line at the end
243 echo >>actual &&
f3445f78 244 qz_to_tab_space <<EOF >expected &&
a742f2a0
AS
245$head1 message two
246$head2 message one
247$head3 add bar
de6029a2 248$head4 $(commit_msg)
a5752342
NTND
249EOF
250 test_cmp expected actual
f3445f78 251'
a5752342 252
f3445f78
AS
253test_expect_success 'right alignment formatting with no padding' '
254 git log --pretty="format:%>(1)%s" >actual &&
a5752342
NTND
255 # complete the incomplete line at the end
256 echo >>actual &&
f3445f78 257 cat <<EOF >expected &&
a5752342
NTND
258message two
259message one
260add bar
de6029a2 261$(commit_msg)
a5752342
NTND
262EOF
263 test_cmp expected actual
f3445f78 264'
a5752342 265
f3445f78
AS
266test_expect_success 'center alignment formatting' '
267 git log --pretty="format:%><(40)%s" >actual &&
a5752342
NTND
268 # complete the incomplete line at the end
269 echo >>actual &&
f3445f78 270 qz_to_tab_space <<EOF >expected &&
a5752342
NTND
271Z message two Z
272Z message one Z
273Z add bar Z
de6029a2 274Z $(commit_msg) Z
a5752342
NTND
275EOF
276 test_cmp expected actual
f3445f78 277'
a5752342 278
f3445f78
AS
279test_expect_success 'center alignment formatting at the nth column' '
280 git log --pretty="format:%h %><|(40)%s" >actual &&
a5752342
NTND
281 # complete the incomplete line at the end
282 echo >>actual &&
f3445f78 283 qz_to_tab_space <<EOF >expected &&
a742f2a0
AS
284$head1 message two Z
285$head2 message one Z
286$head3 add bar Z
de6029a2 287$head4 $(commit_msg) Z
a5752342
NTND
288EOF
289 test_cmp expected actual
f3445f78 290'
a5752342 291
f3445f78
AS
292test_expect_success 'center alignment formatting with no padding' '
293 git log --pretty="format:%><(1)%s" >actual &&
a5752342
NTND
294 # complete the incomplete line at the end
295 echo >>actual &&
f3445f78 296 cat <<EOF >expected &&
a5752342
NTND
297message two
298message one
299add bar
de6029a2 300$(commit_msg)
a5752342
NTND
301EOF
302 test_cmp expected actual
f3445f78 303'
a5752342 304
f3445f78
AS
305test_expect_success 'left/right alignment formatting with stealing' '
306 git commit --amend -m short --author "long long long <long@me.com>" &&
307 git log --pretty="format:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
1640632b
NTND
308 # complete the incomplete line at the end
309 echo >>actual &&
f3445f78 310 cat <<EOF >expected &&
1640632b
NTND
311short long long long
312message .. A U Thor
313add bar A U Thor
7c375214 314$(commit_msg "" "8" "..*$") A U Thor
1640632b
NTND
315EOF
316 test_cmp expected actual
f3445f78 317'
1640632b 318
8028184e 319test_done