pretty: --format output should honor logOutputEncoding

One can set an alias
	$ git config [--global] alias.lg "log --graph --pretty=format:'%Cred%h%Creset
	-%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset'
	--abbrev-commit --date=local"

to see the log as a pretty tree (like *gitk* but in a terminal).

However, log messages written in an encoding i18n.commitEncoding which differs
from terminal encoding are shown corrupted even when i18n.logOutputEncoding
and terminal encoding are the same (e.g. log messages committed on a Cygwin box
with Windows-1251 encoding seen on a Linux box with a UTF-8 encoding and vice versa).

To simplify an example we can say the following two commands are expected
to give the same output to a terminal:

	$ git log --oneline --no-color
	$ git log --pretty=format:'%h %s'

However, the former pays attention to i18n.logOutputEncoding
configuration, while the latter does not when it formats "%s".

The same corruption is true for
	$ git diff --submodule=log
and
	$ git rev-list --pretty=format:%s HEAD
and
	$ git reset --hard

This patch makes pretty --format honor logOutputEncoding when it formats
log message.

Signed-off-by: Alexey Shumkin <Alex.Crezoff@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Alexey Shumkin 2013-06-26 14:19:50 +04:00 committed by Junio C Hamano
parent de6029a2d7
commit ecaee8050c
9 changed files with 35 additions and 28 deletions

View File

@ -93,10 +93,12 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
static void print_new_head_line(struct commit *commit) static void print_new_head_line(struct commit *commit)
{ {
const char *hex, *body; const char *hex, *body;
char *msg;
hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
printf(_("HEAD is now at %s"), hex); printf(_("HEAD is now at %s"), hex);
body = strstr(commit->buffer, "\n\n"); msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
body = strstr(msg, "\n\n");
if (body) { if (body) {
const char *eol; const char *eol;
size_t len; size_t len;
@ -107,6 +109,7 @@ static void print_new_head_line(struct commit *commit)
} }
else else
printf("\n"); printf("\n");
logmsg_free(msg, commit);
} }
static void update_index_from_diff(struct diff_queue_struct *q, static void update_index_from_diff(struct diff_queue_struct *q,

View File

@ -111,6 +111,7 @@ static void show_commit(struct commit *commit, void *data)
ctx.date_mode = revs->date_mode; ctx.date_mode = revs->date_mode;
ctx.date_mode_explicit = revs->date_mode_explicit; ctx.date_mode_explicit = revs->date_mode_explicit;
ctx.fmt = revs->commit_format; ctx.fmt = revs->commit_format;
ctx.output_encoding = get_log_output_encoding();
pretty_print_commit(&ctx, commit, &buf); pretty_print_commit(&ctx, commit, &buf);
if (revs->graph) { if (revs->graph) {
if (buf.len) { if (buf.len) {

View File

@ -137,6 +137,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
ctx.subject = ""; ctx.subject = "";
ctx.after_subject = ""; ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL; ctx.date_mode = DATE_NORMAL;
ctx.output_encoding = get_log_output_encoding();
pretty_print_commit(&ctx, commit, &ufbuf); pretty_print_commit(&ctx, commit, &ufbuf);
buffer = ufbuf.buf; buffer = ufbuf.buf;
} else if (*buffer) { } else if (*buffer) {

View File

@ -616,6 +616,7 @@ void show_log(struct rev_info *opt)
ctx.fmt = opt->commit_format; ctx.fmt = opt->commit_format;
ctx.mailmap = opt->mailmap; ctx.mailmap = opt->mailmap;
ctx.color = opt->diffopt.use_color; ctx.color = opt->diffopt.use_color;
ctx.output_encoding = get_log_output_encoding();
pretty_print_commit(&ctx, commit, &msgbuf); pretty_print_commit(&ctx, commit, &msgbuf);
if (opt->add_signoff) if (opt->add_signoff)

View File

@ -226,6 +226,7 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,
while ((commit = get_revision(rev))) { while ((commit = get_revision(rev))) {
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};
ctx.date_mode = rev->date_mode; ctx.date_mode = rev->date_mode;
ctx.output_encoding = get_log_output_encoding();
strbuf_setlen(&sb, 0); strbuf_setlen(&sb, 0);
strbuf_addstr(&sb, line_prefix); strbuf_addstr(&sb, line_prefix);
if (commit->object.flags & SYMMETRIC_LEFT) { if (commit->object.flags & SYMMETRIC_LEFT) {

View File

@ -94,7 +94,7 @@ test_expect_success 'diff.submodule does not affect plumbing' '
commit_file sm1 && commit_file sm1 &&
head2=$(add_file sm1 foo3) head2=$(add_file sm1 foo3)
test_expect_failure 'modified submodule(forward)' ' test_expect_success 'modified submodule(forward)' '
git diff-index -p --submodule=log HEAD >actual && git diff-index -p --submodule=log HEAD >actual &&
cat >expected <<-EOF && cat >expected <<-EOF &&
Submodule sm1 $head1..$head2: Submodule sm1 $head1..$head2:
@ -103,7 +103,7 @@ test_expect_failure 'modified submodule(forward)' '
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'modified submodule(forward)' ' test_expect_success 'modified submodule(forward)' '
git diff --submodule=log >actual && git diff --submodule=log >actual &&
cat >expected <<-EOF && cat >expected <<-EOF &&
Submodule sm1 $head1..$head2: Submodule sm1 $head1..$head2:
@ -112,7 +112,7 @@ test_expect_failure 'modified submodule(forward)' '
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'modified submodule(forward) --submodule' ' test_expect_success 'modified submodule(forward) --submodule' '
git diff --submodule >actual && git diff --submodule >actual &&
cat >expected <<-EOF && cat >expected <<-EOF &&
Submodule sm1 $head1..$head2: Submodule sm1 $head1..$head2:
@ -143,7 +143,7 @@ head3=$(
git rev-parse --short --verify HEAD git rev-parse --short --verify HEAD
) )
test_expect_failure 'modified submodule(backward)' ' test_expect_success 'modified submodule(backward)' '
git diff-index -p --submodule=log HEAD >actual && git diff-index -p --submodule=log HEAD >actual &&
cat >expected <<-EOF && cat >expected <<-EOF &&
Submodule sm1 $head2..$head3 (rewind): Submodule sm1 $head2..$head3 (rewind):
@ -154,7 +154,7 @@ test_expect_failure 'modified submodule(backward)' '
' '
head4=$(add_file sm1 foo4 foo5) head4=$(add_file sm1 foo4 foo5)
test_expect_failure 'modified submodule(backward and forward)' ' test_expect_success 'modified submodule(backward and forward)' '
git diff-index -p --submodule=log HEAD >actual && git diff-index -p --submodule=log HEAD >actual &&
cat >expected <<-EOF && cat >expected <<-EOF &&
Submodule sm1 $head2...$head4: Submodule sm1 $head2...$head4:

View File

@ -69,7 +69,7 @@ test_expect_success 'alias user-defined tformat with %s (iso-8859-1 encoding)' '
test_cmp expected-s actual-s test_cmp expected-s actual-s
' '
test_expect_failure 'alias user-defined tformat with %s (utf-8 encoding)' ' test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
git log --oneline >expected-s && git log --oneline >expected-s &&
git log --pretty="tformat:%h %s" >actual-s && git log --pretty="tformat:%h %s" >actual-s &&
test_cmp expected-s actual-s test_cmp expected-s actual-s
@ -108,19 +108,19 @@ test_expect_success 'alias loop' '
test_must_fail git log --pretty=test-foo test_must_fail git log --pretty=test-foo
' '
test_expect_failure 'NUL separation' ' test_expect_success 'NUL separation' '
printf "add bar\0$(commit_msg)" >expected && printf "add bar\0$(commit_msg)" >expected &&
git log -z --pretty="format:%s" >actual && git log -z --pretty="format:%s" >actual &&
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'NUL termination' ' test_expect_success 'NUL termination' '
printf "add bar\0$(commit_msg)\0" >expected && printf "add bar\0$(commit_msg)\0" >expected &&
git log -z --pretty="tformat:%s" >actual && git log -z --pretty="tformat:%s" >actual &&
test_cmp expected actual test_cmp expected actual
' '
test_expect_failure 'NUL separation with --stat' ' test_expect_success 'NUL separation with --stat' '
stat0_part=$(git diff --stat HEAD^ HEAD) && stat0_part=$(git diff --stat HEAD^ HEAD) &&
stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) && stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected && printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
@ -145,7 +145,7 @@ test_expect_success 'setup more commits' '
head4=$(git rev-parse --verify --short HEAD~3) head4=$(git rev-parse --verify --short HEAD~3)
' '
test_expect_failure 'left alignment formatting' " test_expect_success 'left alignment formatting' "
git log --pretty='format:%<(40)%s' >actual && git log --pretty='format:%<(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -158,7 +158,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left alignment formatting at the nth column' " test_expect_success 'left alignment formatting at the nth column' "
git log --pretty='format:%h %<|(40)%s' >actual && git log --pretty='format:%h %<|(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -171,7 +171,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left alignment formatting with no padding' " test_expect_success 'left alignment formatting with no padding' "
git log --pretty='format:%<(1)%s' >actual && git log --pretty='format:%<(1)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -184,7 +184,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left alignment formatting with trunc' " test_expect_success 'left alignment formatting with trunc' "
git log --pretty='format:%<(10,trunc)%s' >actual && git log --pretty='format:%<(10,trunc)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -197,7 +197,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left alignment formatting with ltrunc' " test_expect_success 'left alignment formatting with ltrunc' "
git log --pretty='format:%<(10,ltrunc)%s' >actual && git log --pretty='format:%<(10,ltrunc)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -210,7 +210,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left alignment formatting with mtrunc' " test_expect_success 'left alignment formatting with mtrunc' "
git log --pretty='format:%<(10,mtrunc)%s' >actual && git log --pretty='format:%<(10,mtrunc)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -223,7 +223,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'right alignment formatting' " test_expect_success 'right alignment formatting' "
git log --pretty='format:%>(40)%s' >actual && git log --pretty='format:%>(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -236,7 +236,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'right alignment formatting at the nth column' " test_expect_success 'right alignment formatting at the nth column' "
git log --pretty='format:%h %>|(40)%s' >actual && git log --pretty='format:%h %>|(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -249,7 +249,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'right alignment formatting with no padding' " test_expect_success 'right alignment formatting with no padding' "
git log --pretty='format:%>(1)%s' >actual && git log --pretty='format:%>(1)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -262,7 +262,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'center alignment formatting' " test_expect_success 'center alignment formatting' "
git log --pretty='format:%><(40)%s' >actual && git log --pretty='format:%><(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -275,7 +275,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'center alignment formatting at the nth column' " test_expect_success 'center alignment formatting at the nth column' "
git log --pretty='format:%h %><|(40)%s' >actual && git log --pretty='format:%h %><|(40)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -288,7 +288,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'center alignment formatting with no padding' " test_expect_success 'center alignment formatting with no padding' "
git log --pretty='format:%><(1)%s' >actual && git log --pretty='format:%><(1)%s' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end
echo >>actual && echo >>actual &&
@ -301,7 +301,7 @@ EOF
test_cmp expected actual test_cmp expected actual
" "
test_expect_failure 'left/right alignment formatting with stealing' " test_expect_success 'left/right alignment formatting with stealing' "
git commit --amend -m short --author 'long long long <long@me.com>' && git commit --amend -m short --author 'long long long <long@me.com>' &&
git log --pretty='format:%<(10,trunc)%s%>>(10,ltrunc)% an' >actual && git log --pretty='format:%<(10,trunc)%s%>>(10,ltrunc)% an' >actual &&
# complete the incomplete line at the end # complete the incomplete line at the end

View File

@ -141,7 +141,7 @@ commit $head1
iso-8859-1 iso-8859-1
EOF EOF
test_format failure subject %s <<EOF test_format subject %s <<EOF
commit $head2 commit $head2
$changed $changed
commit $head1 commit $head1
@ -153,7 +153,7 @@ commit $head2
commit $head1 commit $head1
EOF EOF
test_format failure raw-body %B <<EOF test_format raw-body %B <<EOF
commit $head2 commit $head2
$changed $changed
@ -247,7 +247,7 @@ commit $head1
iso-8859-1 iso-8859-1
EOF EOF
test_format failure complex-subject %s <<EOF test_format complex-subject %s <<EOF
commit $head3 commit $head3
Test printing of complex bodies Test printing of complex bodies
commit $head2 commit $head2
@ -256,7 +256,7 @@ commit $head1
$added $added
EOF EOF
test_format failure complex-body %b <<EOF test_format complex-body %b <<EOF
commit $head3 commit $head3
This commit message is much longer than the others, This commit message is much longer than the others,
and it will be encoded in iso8859-1. We should therefore and it will be encoded in iso8859-1. We should therefore

View File

@ -55,7 +55,7 @@ check_changes () {
done | test_cmp .cat_expect - done | test_cmp .cat_expect -
} }
test_expect_failure 'reset --hard message' ' test_expect_success 'reset --hard message' '
hex=$(git log -1 --format="%h") && hex=$(git log -1 --format="%h") &&
git reset --hard > .actual && git reset --hard > .actual &&
echo HEAD is now at $hex $(commit_msg) > .expected && echo HEAD is now at $hex $(commit_msg) > .expected &&