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