pretty.c: format string with truncate respects logOutputEncoding

Pretty format string %<(N,[ml]trunc)>%s truncates subject to a given
length with an appropriate padding. This works for non-ASCII texts when
i18n.logOutputEncoding is UTF-8 only (independently of a printed commit
message encoding) but does not work when i18n.logOutputEncoding is NOT
UTF-8.

In 7e77df3 (pretty: two phase conversion for non utf-8 commits, 2013-04-19)
'format_commit_item' function assumes commit message to be in UTF-8.
And that was so until ecaee80 (pretty: --format output should honor
logOutputEncoding, 2013-06-26) where conversion to logOutputEncoding was
added before calling 'format_commit_message'.

Correct this by converting a commit message to UTF-8 first (as it
assumed in 7e77df3 (pretty: two phase conversion for non utf-8 commits,
2013-04-19)). Only after that convert a commit message to an actual
logOutputEncoding.

Signed-off-by: Alexey Shumkin <Alex.Crezoff@gmail.com>
Reviewed-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Alexey Shumkin 2014-05-21 17:20:07 +04:00 committed by Junio C Hamano
parent d928d81051
commit 7d509878b8
3 changed files with 13 additions and 8 deletions

View File

@ -1506,13 +1506,18 @@ void format_commit_message(const struct commit *commit,
context.commit = commit; context.commit = commit;
context.pretty_ctx = pretty_ctx; context.pretty_ctx = pretty_ctx;
context.wrap_start = sb->len; context.wrap_start = sb->len;
/*
* convert a commit message to UTF-8 first
* as far as 'format_commit_item' assumes it in UTF-8
*/
context.message = logmsg_reencode(commit, context.message = logmsg_reencode(commit,
&context.commit_encoding, &context.commit_encoding,
output_enc); utf8);
strbuf_expand(sb, format, format_commit_item, &context); strbuf_expand(sb, format, format_commit_item, &context);
rewrap_message_tail(sb, &context, 0, 0, 0); rewrap_message_tail(sb, &context, 0, 0, 0);
/* then convert a commit message to an actual output encoding */
if (output_enc) { if (output_enc) {
if (same_encoding(utf8, output_enc)) if (same_encoding(utf8, output_enc))
output_enc = NULL; output_enc = NULL;

View File

@ -220,7 +220,7 @@ EOF
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'left alignment formatting with trunc. i18n.logOutputEncoding' ' test_expect_success 'left alignment formatting with trunc. i18n.logOutputEncoding' '
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual && git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual &&
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected && qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
message .. message ..
@ -242,7 +242,7 @@ EOF
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'left alignment formatting with ltrunc. i18n.logOutputEncoding' ' test_expect_success 'left alignment formatting with ltrunc. i18n.logOutputEncoding' '
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual && git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected && qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
..sage two ..sage two
@ -264,7 +264,7 @@ EOF
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'left alignment formatting with mtrunc. i18n.logOutputEncoding' ' test_expect_success 'left alignment formatting with mtrunc. i18n.logOutputEncoding' '
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual && git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected && qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
mess.. two mess.. two
@ -420,7 +420,7 @@ initial... A U Thor
EOF EOF
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'left/right alignment formatting with stealing. i18n.logOutputEncoding' ' test_expect_success 'left/right alignment formatting with stealing. i18n.logOutputEncoding' '
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual && git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected && cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
short long long long short long long long

View File

@ -256,7 +256,7 @@ commit $head1
$added_iso88591 $added_iso88591
EOF EOF
test_format complex-subject-trunc "%<($truncate_count,trunc)%s" failure <<EOF test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
commit $head3 commit $head3
Test printing of c.. Test printing of c..
commit $head2 commit $head2
@ -265,7 +265,7 @@ commit $head1
added (hinzugef${added_utf8_part_iso88591}gt.. added (hinzugef${added_utf8_part_iso88591}gt..
EOF EOF
test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" failure <<EOF test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
commit $head3 commit $head3
Test prin..ex bodies Test prin..ex bodies
commit $head2 commit $head2
@ -274,7 +274,7 @@ commit $head1
added (hi..f${added_utf8_part_iso88591}gt) foo added (hi..f${added_utf8_part_iso88591}gt) foo
EOF EOF
test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" failure <<EOF test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
commit $head3 commit $head3
.. of complex bodies .. of complex bodies
commit $head2 commit $head2