Skip to content

Commit ecaee80

Browse files
ashumkingitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent de6029a commit ecaee80

File tree

9 files changed

+35
-28
lines changed

9 files changed

+35
-28
lines changed

builtin/reset.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
9393
static void print_new_head_line(struct commit *commit)
9494
{
9595
const char *hex, *body;
96+
char *msg;
9697

9798
hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
9899
printf(_("HEAD is now at %s"), hex);
99-
body = strstr(commit->buffer, "\n\n");
100+
msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
101+
body = strstr(msg, "\n\n");
100102
if (body) {
101103
const char *eol;
102104
size_t len;
@@ -107,6 +109,7 @@ static void print_new_head_line(struct commit *commit)
107109
}
108110
else
109111
printf("\n");
112+
logmsg_free(msg, commit);
110113
}
111114

112115
static void update_index_from_diff(struct diff_queue_struct *q,

builtin/rev-list.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static void show_commit(struct commit *commit, void *data)
111111
ctx.date_mode = revs->date_mode;
112112
ctx.date_mode_explicit = revs->date_mode_explicit;
113113
ctx.fmt = revs->commit_format;
114+
ctx.output_encoding = get_log_output_encoding();
114115
pretty_print_commit(&ctx, commit, &buf);
115116
if (revs->graph) {
116117
if (buf.len) {

builtin/shortlog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
137137
ctx.subject = "";
138138
ctx.after_subject = "";
139139
ctx.date_mode = DATE_NORMAL;
140+
ctx.output_encoding = get_log_output_encoding();
140141
pretty_print_commit(&ctx, commit, &ufbuf);
141142
buffer = ufbuf.buf;
142143
} else if (*buffer) {

log-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ void show_log(struct rev_info *opt)
616616
ctx.fmt = opt->commit_format;
617617
ctx.mailmap = opt->mailmap;
618618
ctx.color = opt->diffopt.use_color;
619+
ctx.output_encoding = get_log_output_encoding();
619620
pretty_print_commit(&ctx, commit, &msgbuf);
620621

621622
if (opt->add_signoff)

submodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,
226226
while ((commit = get_revision(rev))) {
227227
struct pretty_print_context ctx = {0};
228228
ctx.date_mode = rev->date_mode;
229+
ctx.output_encoding = get_log_output_encoding();
229230
strbuf_setlen(&sb, 0);
230231
strbuf_addstr(&sb, line_prefix);
231232
if (commit->object.flags & SYMMETRIC_LEFT) {

t/t4041-diff-submodule-option.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test_expect_success 'diff.submodule does not affect plumbing' '
9494
commit_file sm1 &&
9595
head2=$(add_file sm1 foo3)
9696

97-
test_expect_failure 'modified submodule(forward)' '
97+
test_expect_success 'modified submodule(forward)' '
9898
git diff-index -p --submodule=log HEAD >actual &&
9999
cat >expected <<-EOF &&
100100
Submodule sm1 $head1..$head2:
@@ -103,7 +103,7 @@ test_expect_failure 'modified submodule(forward)' '
103103
test_cmp expected actual
104104
'
105105

106-
test_expect_failure 'modified submodule(forward)' '
106+
test_expect_success 'modified submodule(forward)' '
107107
git diff --submodule=log >actual &&
108108
cat >expected <<-EOF &&
109109
Submodule sm1 $head1..$head2:
@@ -112,7 +112,7 @@ test_expect_failure 'modified submodule(forward)' '
112112
test_cmp expected actual
113113
'
114114

115-
test_expect_failure 'modified submodule(forward) --submodule' '
115+
test_expect_success 'modified submodule(forward) --submodule' '
116116
git diff --submodule >actual &&
117117
cat >expected <<-EOF &&
118118
Submodule sm1 $head1..$head2:
@@ -143,7 +143,7 @@ head3=$(
143143
git rev-parse --short --verify HEAD
144144
)
145145

146-
test_expect_failure 'modified submodule(backward)' '
146+
test_expect_success 'modified submodule(backward)' '
147147
git diff-index -p --submodule=log HEAD >actual &&
148148
cat >expected <<-EOF &&
149149
Submodule sm1 $head2..$head3 (rewind):
@@ -154,7 +154,7 @@ test_expect_failure 'modified submodule(backward)' '
154154
'
155155

156156
head4=$(add_file sm1 foo4 foo5)
157-
test_expect_failure 'modified submodule(backward and forward)' '
157+
test_expect_success 'modified submodule(backward and forward)' '
158158
git diff-index -p --submodule=log HEAD >actual &&
159159
cat >expected <<-EOF &&
160160
Submodule sm1 $head2...$head4:

t/t4205-log-pretty-formats.sh

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test_expect_success 'alias user-defined tformat with %s (iso-8859-1 encoding)' '
6969
test_cmp expected-s actual-s
7070
'
7171

72-
test_expect_failure 'alias user-defined tformat with %s (utf-8 encoding)' '
72+
test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
7373
git log --oneline >expected-s &&
7474
git log --pretty="tformat:%h %s" >actual-s &&
7575
test_cmp expected-s actual-s
@@ -108,19 +108,19 @@ test_expect_success 'alias loop' '
108108
test_must_fail git log --pretty=test-foo
109109
'
110110

111-
test_expect_failure 'NUL separation' '
111+
test_expect_success 'NUL separation' '
112112
printf "add bar\0$(commit_msg)" >expected &&
113113
git log -z --pretty="format:%s" >actual &&
114114
test_cmp expected actual
115115
'
116116

117-
test_expect_failure 'NUL termination' '
117+
test_expect_success 'NUL termination' '
118118
printf "add bar\0$(commit_msg)\0" >expected &&
119119
git log -z --pretty="tformat:%s" >actual &&
120120
test_cmp expected actual
121121
'
122122

123-
test_expect_failure 'NUL separation with --stat' '
123+
test_expect_success 'NUL separation with --stat' '
124124
stat0_part=$(git diff --stat HEAD^ HEAD) &&
125125
stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
126126
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' '
145145
head4=$(git rev-parse --verify --short HEAD~3)
146146
'
147147

148-
test_expect_failure 'left alignment formatting' "
148+
test_expect_success 'left alignment formatting' "
149149
git log --pretty='format:%<(40)%s' >actual &&
150150
# complete the incomplete line at the end
151151
echo >>actual &&
@@ -158,7 +158,7 @@ EOF
158158
test_cmp expected actual
159159
"
160160

161-
test_expect_failure 'left alignment formatting at the nth column' "
161+
test_expect_success 'left alignment formatting at the nth column' "
162162
git log --pretty='format:%h %<|(40)%s' >actual &&
163163
# complete the incomplete line at the end
164164
echo >>actual &&
@@ -171,7 +171,7 @@ EOF
171171
test_cmp expected actual
172172
"
173173

174-
test_expect_failure 'left alignment formatting with no padding' "
174+
test_expect_success 'left alignment formatting with no padding' "
175175
git log --pretty='format:%<(1)%s' >actual &&
176176
# complete the incomplete line at the end
177177
echo >>actual &&
@@ -184,7 +184,7 @@ EOF
184184
test_cmp expected actual
185185
"
186186

187-
test_expect_failure 'left alignment formatting with trunc' "
187+
test_expect_success 'left alignment formatting with trunc' "
188188
git log --pretty='format:%<(10,trunc)%s' >actual &&
189189
# complete the incomplete line at the end
190190
echo >>actual &&
@@ -197,7 +197,7 @@ EOF
197197
test_cmp expected actual
198198
"
199199

200-
test_expect_failure 'left alignment formatting with ltrunc' "
200+
test_expect_success 'left alignment formatting with ltrunc' "
201201
git log --pretty='format:%<(10,ltrunc)%s' >actual &&
202202
# complete the incomplete line at the end
203203
echo >>actual &&
@@ -210,7 +210,7 @@ EOF
210210
test_cmp expected actual
211211
"
212212

213-
test_expect_failure 'left alignment formatting with mtrunc' "
213+
test_expect_success 'left alignment formatting with mtrunc' "
214214
git log --pretty='format:%<(10,mtrunc)%s' >actual &&
215215
# complete the incomplete line at the end
216216
echo >>actual &&
@@ -223,7 +223,7 @@ EOF
223223
test_cmp expected actual
224224
"
225225

226-
test_expect_failure 'right alignment formatting' "
226+
test_expect_success 'right alignment formatting' "
227227
git log --pretty='format:%>(40)%s' >actual &&
228228
# complete the incomplete line at the end
229229
echo >>actual &&
@@ -236,7 +236,7 @@ EOF
236236
test_cmp expected actual
237237
"
238238

239-
test_expect_failure 'right alignment formatting at the nth column' "
239+
test_expect_success 'right alignment formatting at the nth column' "
240240
git log --pretty='format:%h %>|(40)%s' >actual &&
241241
# complete the incomplete line at the end
242242
echo >>actual &&
@@ -249,7 +249,7 @@ EOF
249249
test_cmp expected actual
250250
"
251251

252-
test_expect_failure 'right alignment formatting with no padding' "
252+
test_expect_success 'right alignment formatting with no padding' "
253253
git log --pretty='format:%>(1)%s' >actual &&
254254
# complete the incomplete line at the end
255255
echo >>actual &&
@@ -262,7 +262,7 @@ EOF
262262
test_cmp expected actual
263263
"
264264

265-
test_expect_failure 'center alignment formatting' "
265+
test_expect_success 'center alignment formatting' "
266266
git log --pretty='format:%><(40)%s' >actual &&
267267
# complete the incomplete line at the end
268268
echo >>actual &&
@@ -275,7 +275,7 @@ EOF
275275
test_cmp expected actual
276276
"
277277

278-
test_expect_failure 'center alignment formatting at the nth column' "
278+
test_expect_success 'center alignment formatting at the nth column' "
279279
git log --pretty='format:%h %><|(40)%s' >actual &&
280280
# complete the incomplete line at the end
281281
echo >>actual &&
@@ -288,7 +288,7 @@ EOF
288288
test_cmp expected actual
289289
"
290290

291-
test_expect_failure 'center alignment formatting with no padding' "
291+
test_expect_success 'center alignment formatting with no padding' "
292292
git log --pretty='format:%><(1)%s' >actual &&
293293
# complete the incomplete line at the end
294294
echo >>actual &&
@@ -301,7 +301,7 @@ EOF
301301
test_cmp expected actual
302302
"
303303

304-
test_expect_failure 'left/right alignment formatting with stealing' "
304+
test_expect_success 'left/right alignment formatting with stealing' "
305305
git commit --amend -m short --author 'long long long <[email protected]>' &&
306306
git log --pretty='format:%<(10,trunc)%s%>>(10,ltrunc)% an' >actual &&
307307
# complete the incomplete line at the end

t/t6006-rev-list-format.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ commit $head1
141141
iso-8859-1
142142
EOF
143143

144-
test_format failure subject %s <<EOF
144+
test_format subject %s <<EOF
145145
commit $head2
146146
$changed
147147
commit $head1
@@ -153,7 +153,7 @@ commit $head2
153153
commit $head1
154154
EOF
155155

156-
test_format failure raw-body %B <<EOF
156+
test_format raw-body %B <<EOF
157157
commit $head2
158158
$changed
159159
@@ -247,7 +247,7 @@ commit $head1
247247
iso-8859-1
248248
EOF
249249

250-
test_format failure complex-subject %s <<EOF
250+
test_format complex-subject %s <<EOF
251251
commit $head3
252252
Test printing of complex bodies
253253
commit $head2
@@ -256,7 +256,7 @@ commit $head1
256256
$added
257257
EOF
258258

259-
test_format failure complex-body %b <<EOF
259+
test_format complex-body %b <<EOF
260260
commit $head3
261261
This commit message is much longer than the others,
262262
and it will be encoded in iso8859-1. We should therefore

t/t7102-reset.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ check_changes () {
5555
done | test_cmp .cat_expect -
5656
}
5757

58-
test_expect_failure 'reset --hard message' '
58+
test_expect_success 'reset --hard message' '
5959
hex=$(git log -1 --format="%h") &&
6060
git reset --hard > .actual &&
6161
echo HEAD is now at $hex $(commit_msg) > .expected &&

0 commit comments

Comments
 (0)