2933c637090e7efd8726847c0e782d71a4fe782b
1#!/bin/sh
2#
3# Copyright (c) 2010, Will Palmer
4# Copyright (c) 2011, Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5#
6
7test_description='Test pretty formats'
8. ./test-lib.sh
9
10commit_msg () {
11 # String "initial. initial" partly in German
12 # (translated with Google Translate),
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
31test_expect_success 'set up basic repos' '
32 >foo &&
33 >bar &&
34 git add foo &&
35 test_tick &&
36 git config i18n.commitEncoding iso8859-1 &&
37 git commit -m "$(commit_msg iso8859-1)" &&
38 git add bar &&
39 test_tick &&
40 git commit -m "add bar" &&
41 git config --unset i18n.commitEncoding
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
65test_expect_success 'alias user-defined tformat with %s (iso8859-1 encoding)' '
66 git config i18n.logOutputEncoding iso8859-1 &&
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
73test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
74 git log --oneline >expected-s &&
75 git log --pretty="tformat:%h %s" >actual-s &&
76 test_cmp expected-s actual-s
77'
78
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
86test_expect_success 'alias non-existent format' '
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
112test_expect_success 'NUL separation' '
113 printf "add bar\0$(commit_msg)" >expected &&
114 git log -z --pretty="format:%s" >actual &&
115 test_cmp expected actual
116'
117
118test_expect_success 'NUL termination' '
119 printf "add bar\0$(commit_msg)\0" >expected &&
120 git log -z --pretty="tformat:%s" >actual &&
121 test_cmp expected actual
122'
123
124test_expect_success 'NUL separation with --stat' '
125 stat0_part=$(git diff --stat HEAD^ HEAD) &&
126 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
127 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
128 git log -z --stat --pretty="format:%s" >actual &&
129 test_i18ncmp expected actual
130'
131
132test_expect_failure 'NUL termination with --stat' '
133 stat0_part=$(git diff --stat HEAD^ HEAD) &&
134 stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
135 printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n0" >expected &&
136 git log -z --stat --pretty="tformat:%s" >actual &&
137 test_i18ncmp expected actual
138'
139
140test_expect_success 'setup more commits' '
141 test_commit "message one" one one message-one &&
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)
147'
148
149test_expect_success 'left alignment formatting' '
150 git log --pretty="format:%<(40)%s" >actual &&
151 # complete the incomplete line at the end
152 echo >>actual &&
153 qz_to_tab_space <<EOF >expected &&
154message two Z
155message one Z
156add bar Z
157$(commit_msg) Z
158EOF
159 test_cmp expected actual
160'
161
162test_expect_success 'left alignment formatting at the nth column' '
163 git log --pretty="format:%h %<|(40)%s" >actual &&
164 # complete the incomplete line at the end
165 echo >>actual &&
166 qz_to_tab_space <<EOF >expected &&
167$head1 message two Z
168$head2 message one Z
169$head3 add bar Z
170$head4 $(commit_msg) Z
171EOF
172 test_cmp expected actual
173'
174
175test_expect_success 'left alignment formatting with no padding' '
176 git log --pretty="format:%<(1)%s" >actual &&
177 # complete the incomplete line at the end
178 echo >>actual &&
179 cat <<EOF >expected &&
180message two
181message one
182add bar
183$(commit_msg)
184EOF
185 test_cmp expected actual
186'
187
188test_expect_success 'left alignment formatting with trunc' '
189 git log --pretty="format:%<(10,trunc)%s" >actual &&
190 # complete the incomplete line at the end
191 echo >>actual &&
192 qz_to_tab_space <<EOF >expected &&
193message ..
194message ..
195add bar Z
196$(commit_msg "" "8" "..*$")
197EOF
198 test_cmp expected actual
199'
200
201test_expect_success 'left alignment formatting with ltrunc' '
202 git log --pretty="format:%<(10,ltrunc)%s" >actual &&
203 # complete the incomplete line at the end
204 echo >>actual &&
205 qz_to_tab_space <<EOF >expected &&
206..sage two
207..sage one
208add bar Z
209$(commit_msg "" "0" ".\{11\}")
210EOF
211 test_cmp expected actual
212'
213
214test_expect_success 'left alignment formatting with mtrunc' '
215 git log --pretty="format:%<(10,mtrunc)%s" >actual &&
216 # complete the incomplete line at the end
217 echo >>actual &&
218 qz_to_tab_space <<EOF >expected &&
219mess.. two
220mess.. one
221add bar Z
222$(commit_msg "" "4" ".\{11\}")
223EOF
224 test_cmp expected actual
225'
226
227test_expect_success 'right alignment formatting' '
228 git log --pretty="format:%>(40)%s" >actual &&
229 # complete the incomplete line at the end
230 echo >>actual &&
231 qz_to_tab_space <<EOF >expected &&
232Z message two
233Z message one
234Z add bar
235Z $(commit_msg)
236EOF
237 test_cmp expected actual
238'
239
240test_expect_success 'right alignment formatting at the nth column' '
241 git log --pretty="format:%h %>|(40)%s" >actual &&
242 # complete the incomplete line at the end
243 echo >>actual &&
244 qz_to_tab_space <<EOF >expected &&
245$head1 message two
246$head2 message one
247$head3 add bar
248$head4 $(commit_msg)
249EOF
250 test_cmp expected actual
251'
252
253test_expect_success 'right alignment formatting with no padding' '
254 git log --pretty="format:%>(1)%s" >actual &&
255 # complete the incomplete line at the end
256 echo >>actual &&
257 cat <<EOF >expected &&
258message two
259message one
260add bar
261$(commit_msg)
262EOF
263 test_cmp expected actual
264'
265
266test_expect_success 'center alignment formatting' '
267 git log --pretty="format:%><(40)%s" >actual &&
268 # complete the incomplete line at the end
269 echo >>actual &&
270 qz_to_tab_space <<EOF >expected &&
271Z message two Z
272Z message one Z
273Z add bar Z
274Z $(commit_msg) Z
275EOF
276 test_cmp expected actual
277'
278
279test_expect_success 'center alignment formatting at the nth column' '
280 git log --pretty="format:%h %><|(40)%s" >actual &&
281 # complete the incomplete line at the end
282 echo >>actual &&
283 qz_to_tab_space <<EOF >expected &&
284$head1 message two Z
285$head2 message one Z
286$head3 add bar Z
287$head4 $(commit_msg) Z
288EOF
289 test_cmp expected actual
290'
291
292test_expect_success 'center alignment formatting with no padding' '
293 git log --pretty="format:%><(1)%s" >actual &&
294 # complete the incomplete line at the end
295 echo >>actual &&
296 cat <<EOF >expected &&
297message two
298message one
299add bar
300$(commit_msg)
301EOF
302 test_cmp expected actual
303'
304
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 &&
308 # complete the incomplete line at the end
309 echo >>actual &&
310 cat <<EOF >expected &&
311short long long long
312message .. A U Thor
313add bar A U Thor
314$(commit_msg "" "8" "..*$") A U Thor
315EOF
316 test_cmp expected actual
317'
318
319test_done