Skip to content

Commit fff26a6

Browse files
committed
Merge branch 'jc/same-encoding' into maint
Various codepaths checked if two encoding names are the same using ad-hoc code and some of them ended up asking iconv() to convert between "utf8" and "UTF-8". The former is not a valid way to spell the encoding name, but often people use it by mistake, and we equated them in some but not all codepaths. Introduce a new helper function to make these codepaths consistent. * jc/same-encoding: reencode_string(): introduce and use same_encoding()
2 parents 6a40284 + 0e18bcd commit fff26a6

File tree

6 files changed

+13
-4
lines changed

6 files changed

+13
-4
lines changed

builtin/mailinfo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ static void convert_to_utf8(struct strbuf *line, const char *charset)
483483

484484
if (!charset || !*charset)
485485
return;
486-
if (!strcasecmp(metainfo_charset, charset))
486+
487+
if (same_encoding(metainfo_charset, charset))
487488
return;
488489
out = reencode_string(line->buf, metainfo_charset, charset);
489490
if (!out)

notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
12311231
}
12321232

12331233
if (output_encoding && *output_encoding &&
1234-
strcmp(utf8, output_encoding)) {
1234+
!is_encoding_utf8(output_encoding)) {
12351235
char *reencoded = reencode_string(msg, output_encoding, utf8);
12361236
if (reencoded) {
12371237
free(msg);

pretty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ char *logmsg_reencode(const struct commit *commit,
571571
return NULL;
572572
encoding = get_header(commit, "encoding");
573573
use_encoding = encoding ? encoding : utf8;
574-
if (!strcmp(use_encoding, output_encoding))
574+
if (same_encoding(use_encoding, output_encoding))
575575
if (encoding) /* we'll strip encoding header later */
576576
out = xstrdup(commit->buffer);
577577
else

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
6060

6161
out->reencoded_message = NULL;
6262
out->message = commit->buffer;
63-
if (strcmp(encoding, git_commit_encoding))
63+
if (same_encoding(encoding, git_commit_encoding))
6464
out->reencoded_message = reencode_string(commit->buffer,
6565
git_commit_encoding, encoding);
6666
if (out->reencoded_message)

utf8.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ int is_encoding_utf8(const char *name)
423423
return 0;
424424
}
425425

426+
int same_encoding(const char *src, const char *dst)
427+
{
428+
if (is_encoding_utf8(src) && is_encoding_utf8(dst))
429+
return 1;
430+
return !strcasecmp(src, dst);
431+
}
432+
426433
/*
427434
* Given a buffer and its encoding, return it re-encoded
428435
* with iconv. If the conversion fails, returns NULL.

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ int utf8_width(const char **start, size_t *remainder_p);
77
int utf8_strwidth(const char *string);
88
int is_utf8(const char *text);
99
int is_encoding_utf8(const char *name);
10+
int same_encoding(const char *, const char *);
1011

1112
int strbuf_add_wrapped_text(struct strbuf *buf,
1213
const char *text, int indent, int indent2, int width);

0 commit comments

Comments
 (0)