Skip to content

Commit ad5d893

Browse files
committed
Merge branch 'as/pretty-truncate' into maint
The "%<(10,trunc)%s" pretty format specifier in the log family of commands is used to truncate the string to a given length (e.g. 10 in the example) with padding to column-align the output, but did not take into account that number of bytes and number of display columns are different. * as/pretty-truncate: pretty.c: format string with truncate respects logOutputEncoding t4205, t6006: add tests that fail with i18n.logOutputEncoding set t4205 (log-pretty-format): use `tformat` rather than `format` t4041, t4205, t6006, t7102: don't hardcode tested encoding value t4205 (log-pretty-formats): don't hardcode SHA-1 in expected outputs
2 parents 91043fc + 7d50987 commit ad5d893

File tree

5 files changed

+282
-72
lines changed

5 files changed

+282
-72
lines changed

pretty.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,13 +1506,18 @@ void format_commit_message(const struct commit *commit,
15061506
context.commit = commit;
15071507
context.pretty_ctx = pretty_ctx;
15081508
context.wrap_start = sb->len;
1509+
/*
1510+
* convert a commit message to UTF-8 first
1511+
* as far as 'format_commit_item' assumes it in UTF-8
1512+
*/
15091513
context.message = logmsg_reencode(commit,
15101514
&context.commit_encoding,
1511-
output_enc);
1515+
utf8);
15121516

15131517
strbuf_expand(sb, format, format_commit_item, &context);
15141518
rewrap_message_tail(sb, &context, 0, 0, 0);
15151519

1520+
/* then convert a commit message to an actual output encoding */
15161521
if (output_enc) {
15171522
if (same_encoding(utf8, output_enc))
15181523
output_enc = NULL;

t/t4041-diff-submodule-option.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This test tries to verify the sanity of the --submodule option of git diff.
1111

1212
. ./test-lib.sh
1313

14+
# Tested non-UTF-8 encoding
15+
test_encoding="ISO8859-1"
16+
1417
# String "added" in German (translated with Google Translate), encoded in UTF-8,
1518
# used in sample commit log messages in add_file() function below.
1619
added=$(printf "hinzugef\303\274gt")
@@ -23,8 +26,8 @@ add_file () {
2326
echo "$name" >"$name" &&
2427
git add "$name" &&
2528
test_tick &&
26-
msg_added_iso88591=$(echo "Add $name ($added $name)" | iconv -f utf-8 -t iso8859-1) &&
27-
git -c 'i18n.commitEncoding=iso8859-1' commit -m "$msg_added_iso88591"
29+
msg_added_iso88591=$(echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding) &&
30+
git -c "i18n.commitEncoding=$test_encoding" commit -m "$msg_added_iso88591"
2831
done >/dev/null &&
2932
git rev-parse --short --verify HEAD
3033
)

t/t4205-log-pretty-formats.sh

Lines changed: 171 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
test_description='Test pretty formats'
88
. ./test-lib.sh
99

10+
# Tested non-UTF-8 encoding
11+
test_encoding="ISO8859-1"
12+
1013
sample_utf8_part=$(printf "f\303\244ng")
1114

1215
commit_msg () {
@@ -27,8 +30,8 @@ test_expect_success 'set up basic repos' '
2730
>bar &&
2831
git add foo &&
2932
test_tick &&
30-
git config i18n.commitEncoding iso8859-1 &&
31-
git commit -m "$(commit_msg iso8859-1)" &&
33+
git config i18n.commitEncoding $test_encoding &&
34+
git commit -m "$(commit_msg $test_encoding)" &&
3235
git add bar &&
3336
test_tick &&
3437
git commit -m "add bar" &&
@@ -56,8 +59,8 @@ test_expect_success 'alias user-defined format' '
5659
test_cmp expected actual
5760
'
5861

59-
test_expect_success 'alias user-defined tformat with %s (iso8859-1 encoding)' '
60-
git config i18n.logOutputEncoding iso8859-1 &&
62+
test_expect_success 'alias user-defined tformat with %s (ISO8859-1 encoding)' '
63+
git config i18n.logOutputEncoding $test_encoding &&
6164
git log --oneline >expected-s &&
6265
git log --pretty="tformat:%h %s" >actual-s &&
6366
git config --unset i18n.logOutputEncoding &&
@@ -141,9 +144,7 @@ test_expect_success 'setup more commits' '
141144
'
142145

143146
test_expect_success 'left alignment formatting' '
144-
git log --pretty="format:%<(40)%s" >actual &&
145-
# complete the incomplete line at the end
146-
echo >>actual &&
147+
git log --pretty="tformat:%<(40)%s" >actual &&
147148
qz_to_tab_space <<EOF >expected &&
148149
message two Z
149150
message one Z
@@ -153,10 +154,19 @@ EOF
153154
test_cmp expected actual
154155
'
155156

157+
test_expect_success 'left alignment formatting. i18n.logOutputEncoding' '
158+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(40)%s" >actual &&
159+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
160+
message two Z
161+
message one Z
162+
add bar Z
163+
$(commit_msg) Z
164+
EOF
165+
test_cmp expected actual
166+
'
167+
156168
test_expect_success 'left alignment formatting at the nth column' '
157-
git log --pretty="format:%h %<|(40)%s" >actual &&
158-
# complete the incomplete line at the end
159-
echo >>actual &&
169+
git log --pretty="tformat:%h %<|(40)%s" >actual &&
160170
qz_to_tab_space <<EOF >expected &&
161171
$head1 message two Z
162172
$head2 message one Z
@@ -166,10 +176,19 @@ EOF
166176
test_cmp expected actual
167177
'
168178

179+
test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
180+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
181+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
182+
$head1 message two Z
183+
$head2 message one Z
184+
$head3 add bar Z
185+
$head4 $(commit_msg) Z
186+
EOF
187+
test_cmp expected actual
188+
'
189+
169190
test_expect_success 'left alignment formatting with no padding' '
170-
git log --pretty="format:%<(1)%s" >actual &&
171-
# complete the incomplete line at the end
172-
echo >>actual &&
191+
git log --pretty="tformat:%<(1)%s" >actual &&
173192
cat <<EOF >expected &&
174193
message two
175194
message one
@@ -179,10 +198,19 @@ EOF
179198
test_cmp expected actual
180199
'
181200

201+
test_expect_success 'left alignment formatting with no padding. i18n.logOutputEncoding' '
202+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(1)%s" >actual &&
203+
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
204+
message two
205+
message one
206+
add bar
207+
$(commit_msg)
208+
EOF
209+
test_cmp expected actual
210+
'
211+
182212
test_expect_success 'left alignment formatting with trunc' '
183-
git log --pretty="format:%<(10,trunc)%s" >actual &&
184-
# complete the incomplete line at the end
185-
echo >>actual &&
213+
git log --pretty="tformat:%<(10,trunc)%s" >actual &&
186214
qz_to_tab_space <<EOF >expected &&
187215
message ..
188216
message ..
@@ -192,10 +220,19 @@ EOF
192220
test_cmp expected actual
193221
'
194222

223+
test_expect_success 'left alignment formatting with trunc. i18n.logOutputEncoding' '
224+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual &&
225+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
226+
message ..
227+
message ..
228+
add bar Z
229+
initial...
230+
EOF
231+
test_cmp expected actual
232+
'
233+
195234
test_expect_success 'left alignment formatting with ltrunc' '
196-
git log --pretty="format:%<(10,ltrunc)%s" >actual &&
197-
# complete the incomplete line at the end
198-
echo >>actual &&
235+
git log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
199236
qz_to_tab_space <<EOF >expected &&
200237
..sage two
201238
..sage one
@@ -205,10 +242,19 @@ EOF
205242
test_cmp expected actual
206243
'
207244

245+
test_expect_success 'left alignment formatting with ltrunc. i18n.logOutputEncoding' '
246+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
247+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
248+
..sage two
249+
..sage one
250+
add bar Z
251+
..${sample_utf8_part}lich
252+
EOF
253+
test_cmp expected actual
254+
'
255+
208256
test_expect_success 'left alignment formatting with mtrunc' '
209-
git log --pretty="format:%<(10,mtrunc)%s" >actual &&
210-
# complete the incomplete line at the end
211-
echo >>actual &&
257+
git log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
212258
qz_to_tab_space <<EOF >expected &&
213259
mess.. two
214260
mess.. one
@@ -218,10 +264,19 @@ EOF
218264
test_cmp expected actual
219265
'
220266

267+
test_expect_success 'left alignment formatting with mtrunc. i18n.logOutputEncoding' '
268+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
269+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
270+
mess.. two
271+
mess.. one
272+
add bar Z
273+
init..lich
274+
EOF
275+
test_cmp expected actual
276+
'
277+
221278
test_expect_success 'right alignment formatting' '
222-
git log --pretty="format:%>(40)%s" >actual &&
223-
# complete the incomplete line at the end
224-
echo >>actual &&
279+
git log --pretty="tformat:%>(40)%s" >actual &&
225280
qz_to_tab_space <<EOF >expected &&
226281
Z message two
227282
Z message one
@@ -231,10 +286,19 @@ EOF
231286
test_cmp expected actual
232287
'
233288

289+
test_expect_success 'right alignment formatting. i18n.logOutputEncoding' '
290+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(40)%s" >actual &&
291+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
292+
Z message two
293+
Z message one
294+
Z add bar
295+
Z $(commit_msg)
296+
EOF
297+
test_cmp expected actual
298+
'
299+
234300
test_expect_success 'right alignment formatting at the nth column' '
235-
git log --pretty="format:%h %>|(40)%s" >actual &&
236-
# complete the incomplete line at the end
237-
echo >>actual &&
301+
git log --pretty="tformat:%h %>|(40)%s" >actual &&
238302
qz_to_tab_space <<EOF >expected &&
239303
$head1 message two
240304
$head2 message one
@@ -244,10 +308,19 @@ EOF
244308
test_cmp expected actual
245309
'
246310

311+
test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
312+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
313+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
314+
$head1 message two
315+
$head2 message one
316+
$head3 add bar
317+
$head4 $(commit_msg)
318+
EOF
319+
test_cmp expected actual
320+
'
321+
247322
test_expect_success 'right alignment formatting with no padding' '
248-
git log --pretty="format:%>(1)%s" >actual &&
249-
# complete the incomplete line at the end
250-
echo >>actual &&
323+
git log --pretty="tformat:%>(1)%s" >actual &&
251324
cat <<EOF >expected &&
252325
message two
253326
message one
@@ -257,10 +330,19 @@ EOF
257330
test_cmp expected actual
258331
'
259332

333+
test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
334+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
335+
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
336+
message two
337+
message one
338+
add bar
339+
$(commit_msg)
340+
EOF
341+
test_cmp expected actual
342+
'
343+
260344
test_expect_success 'center alignment formatting' '
261-
git log --pretty="format:%><(40)%s" >actual &&
262-
# complete the incomplete line at the end
263-
echo >>actual &&
345+
git log --pretty="tformat:%><(40)%s" >actual &&
264346
qz_to_tab_space <<EOF >expected &&
265347
Z message two Z
266348
Z message one Z
@@ -270,10 +352,18 @@ EOF
270352
test_cmp expected actual
271353
'
272354

355+
test_expect_success 'center alignment formatting. i18n.logOutputEncoding' '
356+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(40)%s" >actual &&
357+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
358+
Z message two Z
359+
Z message one Z
360+
Z add bar Z
361+
Z $(commit_msg) Z
362+
EOF
363+
test_cmp expected actual
364+
'
273365
test_expect_success 'center alignment formatting at the nth column' '
274-
git log --pretty="format:%h %><|(40)%s" >actual &&
275-
# complete the incomplete line at the end
276-
echo >>actual &&
366+
git log --pretty="tformat:%h %><|(40)%s" >actual &&
277367
qz_to_tab_space <<EOF >expected &&
278368
$head1 message two Z
279369
$head2 message one Z
@@ -283,10 +373,19 @@ EOF
283373
test_cmp expected actual
284374
'
285375

376+
test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
377+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
378+
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
379+
$head1 message two Z
380+
$head2 message one Z
381+
$head3 add bar Z
382+
$head4 $(commit_msg) Z
383+
EOF
384+
test_cmp expected actual
385+
'
386+
286387
test_expect_success 'center alignment formatting with no padding' '
287-
git log --pretty="format:%><(1)%s" >actual &&
288-
# complete the incomplete line at the end
289-
echo >>actual &&
388+
git log --pretty="tformat:%><(1)%s" >actual &&
290389
cat <<EOF >expected &&
291390
message two
292391
message one
@@ -296,11 +395,23 @@ EOF
296395
test_cmp expected actual
297396
'
298397

398+
# save HEAD's SHA-1 digest (with no abbreviations) to use it below
399+
# as far as the next test amends HEAD
400+
old_head1=$(git rev-parse --verify HEAD~0)
401+
test_expect_success 'center alignment formatting with no padding. i18n.logOutputEncoding' '
402+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(1)%s" >actual &&
403+
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
404+
message two
405+
message one
406+
add bar
407+
$(commit_msg)
408+
EOF
409+
test_cmp expected actual
410+
'
411+
299412
test_expect_success 'left/right alignment formatting with stealing' '
300413
git commit --amend -m short --author "long long long <[email protected]>" &&
301-
git log --pretty="format:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
302-
# complete the incomplete line at the end
303-
echo >>actual &&
414+
git log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
304415
cat <<EOF >expected &&
305416
short long long long
306417
message .. A U Thor
@@ -309,6 +420,20 @@ initial... A U Thor
309420
EOF
310421
test_cmp expected actual
311422
'
423+
test_expect_success 'left/right alignment formatting with stealing. i18n.logOutputEncoding' '
424+
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
425+
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
426+
short long long long
427+
message .. A U Thor
428+
add bar A U Thor
429+
initial... A U Thor
430+
EOF
431+
test_cmp expected actual
432+
'
433+
434+
# get new digests (with no abbreviations)
435+
head1=$(git rev-parse --verify HEAD~0) &&
436+
head2=$(git rev-parse --verify HEAD~1) &&
312437

313438
test_expect_success 'log decoration properly follows tag chain' '
314439
git tag -a tag1 -m tag1 &&
@@ -317,9 +442,9 @@ test_expect_success 'log decoration properly follows tag chain' '
317442
git commit --amend -m shorter &&
318443
git log --no-walk --tags --pretty="%H %d" --decorate=full >actual &&
319444
cat <<EOF >expected &&
320-
6a908c10688b2503073c39c9ba26322c73902bb5 (tag: refs/tags/tag2)
321-
9f716384d92283fb915a4eee5073f030638e05f9 (tag: refs/tags/message-one)
322-
b87e4cccdb77336ea79d89224737be7ea8e95367 (tag: refs/tags/message-two)
445+
$head1 (tag: refs/tags/tag2)
446+
$head2 (tag: refs/tags/message-one)
447+
$old_head1 (tag: refs/tags/message-two)
323448
EOF
324449
sort actual >actual1 &&
325450
test_cmp expected actual1

0 commit comments

Comments
 (0)