pretty: share code between format_decoration and show_decorations
[gitweb.git] / pretty.c
index 41f04e669d3b9c38f49e0853053a6ba27b97357a..e0f93bab0d5a02afc7b4657e7a80e1427ceff5ce 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -594,6 +594,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
 }
 
 char *logmsg_reencode(const struct commit *commit,
+                     char **commit_encoding,
                      const char *output_encoding)
 {
        static const char *utf8 = "UTF-8";
@@ -615,9 +616,15 @@ char *logmsg_reencode(const struct commit *commit,
                            sha1_to_hex(commit->object.sha1), typename(type));
        }
 
-       if (!output_encoding || !*output_encoding)
+       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)) {
                /*
@@ -658,7 +665,8 @@ char *logmsg_reencode(const struct commit *commit,
        if (out)
                out = replace_encoding_header(out, output_encoding);
 
-       free(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.
@@ -766,21 +774,14 @@ struct format_commit_context {
        const struct pretty_print_context *pretty_ctx;
        unsigned commit_header_parsed:1;
        unsigned commit_message_parsed:1;
-       unsigned commit_signature_parsed:1;
-       struct {
-               char *gpg_output;
-               char *gpg_status;
-               char good_bad;
-               char *signer;
-               char *key;
-       } signature;
+       struct signature_check signature_check;
        char *message;
+       char *commit_encoding;
        size_t width, indent1, indent2;
 
        /* These offsets are relative to the start of the commit message. */
        struct chunk author;
        struct chunk committer;
-       struct chunk encoding;
        size_t message_off;
        size_t subject_off;
        size_t body_off;
@@ -827,9 +828,6 @@ static void parse_commit_header(struct format_commit_context *context)
                } else if (!prefixcmp(msg + i, "committer ")) {
                        context->committer.off = i + 10;
                        context->committer.len = eol - i - 10;
-               } else if (!prefixcmp(msg + i, "encoding ")) {
-                       context->encoding.off = i + 9;
-                       context->encoding.len = eol - i - 9;
                }
                i = eol;
        }
@@ -910,23 +908,6 @@ static void parse_commit_message(struct format_commit_context *c)
        c->commit_message_parsed = 1;
 }
 
-static void format_decoration(struct strbuf *sb, const struct commit *commit)
-{
-       struct name_decoration *d;
-       const char *prefix = " (";
-
-       load_ref_decorations(DECORATE_SHORT_REFS);
-       d = lookup_decoration(&name_decoration, &commit->object);
-       while (d) {
-               strbuf_addstr(sb, prefix);
-               prefix = ", ";
-               strbuf_addstr(sb, d->name);
-               d = d->next;
-       }
-       if (prefix[0] == ',')
-               strbuf_addch(sb, ')');
-}
-
 static void strbuf_wrap(struct strbuf *sb, size_t pos,
                        size_t width, size_t indent1, size_t indent2)
 {
@@ -956,64 +937,6 @@ static void rewrap_message_tail(struct strbuf *sb,
        c->indent2 = new_indent2;
 }
 
-static struct {
-       char result;
-       const char *check;
-} signature_check[] = {
-       { 'G', "\n[GNUPG:] GOODSIG " },
-       { 'B', "\n[GNUPG:] BADSIG " },
-};
-
-static void parse_signature_lines(struct format_commit_context *ctx)
-{
-       const char *buf = ctx->signature.gpg_status;
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
-               const char *found = strstr(buf, signature_check[i].check);
-               const char *next;
-               if (!found)
-                       continue;
-               ctx->signature.good_bad = signature_check[i].result;
-               found += strlen(signature_check[i].check);
-               ctx->signature.key = xmemdupz(found, 16);
-               found += 17;
-               next = strchrnul(found, '\n');
-               ctx->signature.signer = xmemdupz(found, next - found);
-               break;
-       }
-}
-
-static void parse_commit_signature(struct format_commit_context *ctx)
-{
-       struct strbuf payload = STRBUF_INIT;
-       struct strbuf signature = STRBUF_INIT;
-       struct strbuf gpg_output = STRBUF_INIT;
-       struct strbuf gpg_status = STRBUF_INIT;
-       int status;
-
-       ctx->commit_signature_parsed = 1;
-
-       if (parse_signed_commit(ctx->commit->object.sha1,
-                               &payload, &signature) <= 0)
-               goto out;
-       status = verify_signed_buffer(payload.buf, payload.len,
-                                     signature.buf, signature.len,
-                                     &gpg_output, &gpg_status);
-       if (status && !gpg_output.len)
-               goto out;
-       ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
-       ctx->signature.gpg_status = strbuf_detach(&gpg_status, NULL);
-       parse_signature_lines(ctx);
-
- out:
-       strbuf_release(&gpg_status);
-       strbuf_release(&gpg_output);
-       strbuf_release(&payload);
-       strbuf_release(&signature);
-}
-
-
 static int format_reflog_person(struct strbuf *sb,
                                char part,
                                struct reflog_walk_info *log,
@@ -1163,7 +1086,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
                strbuf_addstr(sb, get_revision_mark(NULL, commit));
                return 1;
        case 'd':
-               format_decoration(sb, commit);
+               load_ref_decorations(DECORATE_SHORT_REFS);
+               format_decorations(sb, commit, 0);
                return 1;
        case 'g':               /* reflog info */
                switch(placeholder[1]) {
@@ -1199,27 +1123,29 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
        }
 
        if (placeholder[0] == 'G') {
-               if (!c->commit_signature_parsed)
-                       parse_commit_signature(c);
+               if (!c->signature_check.result)
+                       check_commit_signature(c->commit, &(c->signature_check));
                switch (placeholder[1]) {
                case 'G':
-                       if (c->signature.gpg_output)
-                               strbuf_addstr(sb, c->signature.gpg_output);
+                       if (c->signature_check.gpg_output)
+                               strbuf_addstr(sb, c->signature_check.gpg_output);
                        break;
                case '?':
-                       switch (c->signature.good_bad) {
+                       switch (c->signature_check.result) {
                        case 'G':
                        case 'B':
-                               strbuf_addch(sb, c->signature.good_bad);
+                       case 'U':
+                       case 'N':
+                               strbuf_addch(sb, c->signature_check.result);
                        }
                        break;
                case 'S':
-                       if (c->signature.signer)
-                               strbuf_addstr(sb, c->signature.signer);
+                       if (c->signature_check.signer)
+                               strbuf_addstr(sb, c->signature_check.signer);
                        break;
                case 'K':
-                       if (c->signature.key)
-                               strbuf_addstr(sb, c->signature.key);
+                       if (c->signature_check.key)
+                               strbuf_addstr(sb, c->signature_check.key);
                        break;
                }
                return 2;
@@ -1240,7 +1166,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
                                   msg + c->committer.off, c->committer.len,
                                   c->pretty_ctx->date_mode);
        case 'e':       /* encoding */
-               strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
+               if (c->commit_encoding)
+                       strbuf_addstr(sb, c->commit_encoding);
                return 1;
        case 'B':       /* raw body */
                /* message_off is always left at the initial newline */
@@ -1351,14 +1278,17 @@ void format_commit_message(const struct commit *commit,
        context.commit = commit;
        context.pretty_ctx = pretty_ctx;
        context.wrap_start = sb->len;
-       context.message = logmsg_reencode(commit, output_enc);
+       context.message = logmsg_reencode(commit,
+                                         &context.commit_encoding,
+                                         output_enc);
 
        strbuf_expand(sb, format, format_commit_item, &context);
        rewrap_message_tail(sb, &context, 0, 0, 0);
 
+       free(context.commit_encoding);
        logmsg_free(context.message, commit);
-       free(context.signature.gpg_output);
-       free(context.signature.signer);
+       free(context.signature_check.gpg_output);
+       free(context.signature_check.signer);
 }
 
 static void pp_header(const struct pretty_print_context *pp,
@@ -1514,7 +1444,7 @@ void pretty_print_commit(const struct pretty_print_context *pp,
        }
 
        encoding = get_log_output_encoding();
-       msg = reencoded = logmsg_reencode(commit, encoding);
+       msg = reencoded = logmsg_reencode(commit, NULL, encoding);
 
        if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
                indent = 0;