Merge branch 'jc/same-encoding'
authorJunio C Hamano <gitster@pobox.com>
Thu, 15 Nov 2012 18:24:05 +0000 (10:24 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 15 Nov 2012 18:24:05 +0000 (10:24 -0800)
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()

Conflicts:
builtin/mailinfo.c

builtin/mailinfo.c
notes.c
pretty.c
sequencer.c
utf8.c
utf8.h
index da231400b327b86a18a054075f6aee4749846932..24a772d8e1b7355a58088d784fdc18cd54302b7d 100644 (file)
@@ -483,7 +483,8 @@ static void convert_to_utf8(struct strbuf *line, const char *charset)
 
        if (!charset || !*charset)
                return;
-       if (!strcasecmp(metainfo_charset, charset))
+
+       if (same_encoding(metainfo_charset, charset))
                return;
        out = reencode_string(line->buf, metainfo_charset, charset);
        if (!out)
diff --git a/notes.c b/notes.c
index bc454e1eab2b0e1d264cee7bb1f84bdb3bb14237..ee8f01f1d5c1dcb39c40eee443eddaedde380d20 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1231,7 +1231,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
        }
 
        if (output_encoding && *output_encoding &&
-                       strcmp(utf8, output_encoding)) {
+           !is_encoding_utf8(output_encoding)) {
                char *reencoded = reencode_string(msg, output_encoding, utf8);
                if (reencoded) {
                        free(msg);
index 413e7587b6f67326a68a7679ca5496cd1249fbcd..dba682828c2e005b71c0ccbb325fb666915984bc 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -571,7 +571,7 @@ char *logmsg_reencode(const struct commit *commit,
                return NULL;
        encoding = get_header(commit, "encoding");
        use_encoding = encoding ? encoding : utf8;
-       if (!strcmp(use_encoding, output_encoding))
+       if (same_encoding(use_encoding, output_encoding))
                if (encoding) /* we'll strip encoding header later */
                        out = xstrdup(commit->buffer);
                else
index be0cb8b1076dc29bee5a77fbbde155514723c6a8..22604902aa4c4dd146f562c4841c344a773a9bda 100644 (file)
@@ -60,7 +60,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
 
        out->reencoded_message = NULL;
        out->message = commit->buffer;
-       if (strcmp(encoding, git_commit_encoding))
+       if (same_encoding(encoding, git_commit_encoding))
                out->reencoded_message = reencode_string(commit->buffer,
                                        git_commit_encoding, encoding);
        if (out->reencoded_message)
diff --git a/utf8.c b/utf8.c
index 28791a7c3174924967182d54c8b4a7f9600c87bf..5c61bbe1131e7bbdd939c8b815bd5222b872e3fb 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -423,6 +423,13 @@ int is_encoding_utf8(const char *name)
        return 0;
 }
 
+int same_encoding(const char *src, const char *dst)
+{
+       if (is_encoding_utf8(src) && is_encoding_utf8(dst))
+               return 1;
+       return !strcasecmp(src, dst);
+}
+
 /*
  * Given a buffer and its encoding, return it re-encoded
  * with iconv.  If the conversion fails, returns NULL.
diff --git a/utf8.h b/utf8.h
index 3c0ae7624e027a802c38c4afb9fe5b0a590e093d..93ef60042c2fad93184b573f30e771ecddc842be 100644 (file)
--- a/utf8.h
+++ b/utf8.h
@@ -7,6 +7,7 @@ int utf8_width(const char **start, size_t *remainder_p);
 int utf8_strwidth(const char *string);
 int is_utf8(const char *text);
 int is_encoding_utf8(const char *name);
+int same_encoding(const char *, const char *);
 
 int strbuf_add_wrapped_text(struct strbuf *buf,
                const char *text, int indent, int indent2, int width);