char *logmsg_reencode(const struct commit *commit, const char *output_encoding) { static const char *utf8 = "UTF-8"; const char *use_encoding; char *encoding; char *out; if (!output_encoding || !*output_encoding) return NULL; encoding = get_header(commit, "encoding"); use_encoding = encoding ? encoding : utf8; if (same_encoding(use_encoding, output_encoding)) if (encoding) /* we'll strip encoding header later */ out = xstrdup(commit->buffer); else return NULL; /* nothing to do */ else out = reencode_string(commit->buffer, output_encoding, use_encoding); if (out) out = replace_encoding_header(out, output_encoding); free(encoding); return out; }
void format_commit_message(const struct commit *commit, const char *format, struct strbuf *sb, const struct pretty_print_context *pretty_ctx) { struct format_commit_context context; const char *output_enc = pretty_ctx->output_encoding; const char *utf8 = "UTF-8"; memset(&context, 0, sizeof(context)); context.commit = commit; context.pretty_ctx = pretty_ctx; 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.commit_encoding, utf8); strbuf_expand(sb, format, format_commit_item, &context); rewrap_message_tail(sb, &context, 0, 0, 0); /* then convert a commit message to an actual output encoding */ if (output_enc) { if (same_encoding(utf8, output_enc)) output_enc = NULL; } else { if (context.commit_encoding && !same_encoding(context.commit_encoding, utf8)) output_enc = context.commit_encoding; } if (output_enc) { int outsz; char *out = reencode_string_len(sb->buf, sb->len, output_enc, utf8, &outsz); if (out) strbuf_attach(sb, out, outsz, outsz + 1); } free(context.commit_encoding); unuse_commit_buffer(commit, context.message); free(context.signature_check.gpg_output); free(context.signature_check.signer); }
int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) { char *out; int len; if (same_encoding(from, to)) return 0; out = reencode_string_len(sb->buf, sb->len, to, from, &len); if (!out) return -1; strbuf_attach(sb, out, len, len); return 0; }
static int convert_to_utf8(struct mailinfo *mi, struct strbuf *line, const char *charset) { char *out; if (!mi->metainfo_charset || !charset || !*charset) return 0; if (same_encoding(mi->metainfo_charset, charset)) return 0; out = reencode_string(line->buf, mi->metainfo_charset, charset); if (!out) { mi->input_error = -1; return error("cannot convert from %s to %s", charset, mi->metainfo_charset); } strbuf_attach(line, out, strlen(out), strlen(out)); return 0; }
static int get_message(struct commit *commit, struct commit_message *out) { const char *encoding; const char *abbrev, *subject; int abbrev_len, subject_len; char *q; if (!commit->buffer) return -1; encoding = get_encoding(commit->buffer); if (!encoding) encoding = "UTF-8"; if (!git_commit_encoding) git_commit_encoding = "UTF-8"; out->reencoded_message = NULL; out->message = commit->buffer; if (same_encoding(encoding, git_commit_encoding)) out->reencoded_message = reencode_string(commit->buffer, git_commit_encoding, encoding); if (out->reencoded_message) out->message = out->reencoded_message; abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); abbrev_len = strlen(abbrev); subject_len = find_commit_subject(out->message, &subject); out->parent_label = xmalloc(strlen("parent of ") + abbrev_len + strlen("... ") + subject_len + 1); q = out->parent_label; q = mempcpy(q, "parent of ", strlen("parent of ")); out->label = q; q = mempcpy(q, abbrev, abbrev_len); q = mempcpy(q, "... ", strlen("... ")); out->subject = q; q = mempcpy(q, subject, subject_len); *q = '\0'; return 0; }
const char *logmsg_reencode(const struct commit *commit, char **commit_encoding, const char *output_encoding) { static const char *utf8 = "UTF-8"; const char *use_encoding; char *encoding; const char *msg = get_commit_buffer(commit, NULL); char *out; if (!output_encoding || !*output_encoding) { if (commit_encoding) *commit_encoding = get_header(commit, msg, "encoding"); return msg; } encoding = get_header(commit, msg, "encoding"); if (commit_encoding) *commit_encoding = encoding; use_encoding = encoding ? encoding : utf8; if (same_encoding(use_encoding, output_encoding)) { /* * No encoding work to be done. If we have no encoding header * at all, then there's nothing to do, and we can return the * message verbatim (whether newly allocated or not). */ if (!encoding) return msg; /* * Otherwise, we still want to munge the encoding header in the * result, which will be done by modifying the buffer. If we * are using a fresh copy, we can reuse it. But if we are using * the cached copy from get_commit_buffer, we need to duplicate it * to avoid munging the cached copy. */ if (msg == get_cached_commit_buffer(commit, NULL)) out = xstrdup(msg); else out = (char *)msg; } else { /* * There's actual encoding work to do. Do the reencoding, which * still leaves the header to be replaced in the next step. At * this point, we are done with msg. If we allocated a fresh * copy, we can free it. */ out = reencode_string(msg, output_encoding, use_encoding); if (out) unuse_commit_buffer(commit, msg); } /* * This replacement actually consumes the buffer we hand it, so we do * not have to worry about freeing the old "out" here. */ if (out) out = replace_encoding_header(out, output_encoding); if (!commit_encoding) free(encoding); /* * If the re-encoding failed, out might be NULL here; in that * case we just return the commit message verbatim. */ return out ? out : msg; }