tree-diff: move all action-taking code out of compare_tree_entry()
[gitweb.git] / log-tree.c
index 3d888238711364338a83e7d81bf27f555352bdca..08970bf46e49f6447ada24e81e5792ea9f92f419 100644 (file)
@@ -9,6 +9,8 @@
 #include "string-list.h"
 #include "color.h"
 #include "gpg-interface.h"
+#include "sequencer.h"
+#include "line-log.h"
 
 struct decoration name_decoration = { "object names" };
 
@@ -96,7 +98,7 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
        struct object *obj;
        enum decoration_type type = DECORATION_NONE;
 
-       if (!prefixcmp(refname, "refs/replace/")) {
+       if (starts_with(refname, "refs/replace/")) {
                unsigned char original_sha1[20];
                if (!read_replace_refs)
                        return 0;
@@ -114,11 +116,11 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
        if (!obj)
                return 0;
 
-       if (!prefixcmp(refname, "refs/heads/"))
+       if (starts_with(refname, "refs/heads/"))
                type = DECORATION_REF_LOCAL;
-       else if (!prefixcmp(refname, "refs/remotes/"))
+       else if (starts_with(refname, "refs/remotes/"))
                type = DECORATION_REF_REMOTE;
-       else if (!prefixcmp(refname, "refs/tags/"))
+       else if (starts_with(refname, "refs/tags/"))
                type = DECORATION_REF_TAG;
        else if (!strcmp(refname, "refs/stash"))
                type = DECORATION_REF_STASH;
@@ -132,6 +134,8 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
                obj = ((struct tag *)obj)->tagged;
                if (!obj)
                        break;
+               if (!obj->parsed)
+                       parse_object(obj->sha1);
                add_name_decoration(DECORATION_REF_TAG, refname, obj);
        }
        return 0;
@@ -174,119 +178,52 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
        }
 }
 
-void show_decorations(struct rev_info *opt, struct commit *commit)
+/*
+ * The caller makes sure there is no funny color before
+ * calling. format_decorations makes sure the same after return.
+ */
+void format_decorations(struct strbuf *sb,
+                       const struct commit *commit,
+                       int use_color)
 {
        const char *prefix;
        struct name_decoration *decoration;
        const char *color_commit =
-               diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
+               diff_get_color(use_color, DIFF_COMMIT);
        const char *color_reset =
-               decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
+               decorate_get_color(use_color, DECORATION_NONE);
 
-       if (opt->show_source && commit->util)
-               printf("\t%s", (char *) commit->util);
-       if (!opt->show_decorations)
-               return;
        decoration = lookup_decoration(&name_decoration, &commit->object);
        if (!decoration)
                return;
        prefix = " (";
        while (decoration) {
-               printf("%s", prefix);
-               fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
-                     stdout);
+               strbuf_addstr(sb, color_commit);
+               strbuf_addstr(sb, prefix);
+               strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
                if (decoration->type == DECORATION_REF_TAG)
-                       fputs("tag: ", stdout);
-               printf("%s", decoration->name);
-               fputs(color_reset, stdout);
-               fputs(color_commit, stdout);
+                       strbuf_addstr(sb, "tag: ");
+               strbuf_addstr(sb, decoration->name);
+               strbuf_addstr(sb, color_reset);
                prefix = ", ";
                decoration = decoration->next;
        }
-       putchar(')');
+       strbuf_addstr(sb, color_commit);
+       strbuf_addch(sb, ')');
+       strbuf_addstr(sb, color_reset);
 }
 
-/*
- * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
- * Signed-off-by: and Acked-by: lines.
- */
-static int detect_any_signoff(char *letter, int size)
-{
-       char *cp;
-       int seen_colon = 0;
-       int seen_at = 0;
-       int seen_name = 0;
-       int seen_head = 0;
-
-       cp = letter + size;
-       while (letter <= --cp && *cp == '\n')
-               continue;
-
-       while (letter <= cp) {
-               char ch = *cp--;
-               if (ch == '\n')
-                       break;
-
-               if (!seen_at) {
-                       if (ch == '@')
-                               seen_at = 1;
-                       continue;
-               }
-               if (!seen_colon) {
-                       if (ch == '@')
-                               return 0;
-                       else if (ch == ':')
-                               seen_colon = 1;
-                       else
-                               seen_name = 1;
-                       continue;
-               }
-               if (('A' <= ch && ch <= 'Z') ||
-                   ('a' <= ch && ch <= 'z') ||
-                   ch == '-') {
-                       seen_head = 1;
-                       continue;
-               }
-               /* no empty last line doesn't match */
-               return 0;
-       }
-       return seen_head && seen_name;
-}
-
-static void append_signoff(struct strbuf *sb, const char *signoff)
+void show_decorations(struct rev_info *opt, struct commit *commit)
 {
-       static const char signed_off_by[] = "Signed-off-by: ";
-       size_t signoff_len = strlen(signoff);
-       int has_signoff = 0;
-       char *cp;
-
-       cp = sb->buf;
+       struct strbuf sb = STRBUF_INIT;
 
-       /* First see if we already have the sign-off by the signer */
-       while ((cp = strstr(cp, signed_off_by))) {
-
-               has_signoff = 1;
-
-               cp += strlen(signed_off_by);
-               if (cp + signoff_len >= sb->buf + sb->len)
-                       break;
-               if (strncmp(cp, signoff, signoff_len))
-                       continue;
-               if (!isspace(cp[signoff_len]))
-                       continue;
-               /* we already have him */
+       if (opt->show_source && commit->util)
+               printf("\t%s", (char *) commit->util);
+       if (!opt->show_decorations)
                return;
-       }
-
-       if (!has_signoff)
-               has_signoff = detect_any_signoff(sb->buf, sb->len);
-
-       if (!has_signoff)
-               strbuf_addch(sb, '\n');
-
-       strbuf_addstr(sb, signed_off_by);
-       strbuf_add(sb, signoff, signoff_len);
-       strbuf_addch(sb, '\n');
+       format_decorations(&sb, commit, opt->diffopt.use_color);
+       fputs(sb.buf, stdout);
+       strbuf_release(&sb);
 }
 
 static unsigned int digits_in_number(unsigned int number)
@@ -622,8 +559,8 @@ void show_log(struct rev_info *opt)
                        printf(" (from %s)",
                               find_unique_abbrev(parent->object.sha1,
                                                  abbrev_commit));
+               fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
                show_decorations(opt, commit);
-               printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
                if (opt->commit_format == CMIT_FMT_ONELINE) {
                        putchar(' ');
                } else {
@@ -669,8 +606,10 @@ void show_log(struct rev_info *opt)
        /*
         * And then the pretty-printed message itself
         */
-       if (ctx.need_8bit_cte >= 0)
-               ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
+       if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
+               ctx.need_8bit_cte =
+                       has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
+                                              getenv("GIT_COMMITTER_EMAIL")));
        ctx.date_mode = opt->date_mode;
        ctx.date_mode_explicit = opt->date_mode_explicit;
        ctx.abbrev = opt->diffopt.abbrev;
@@ -680,10 +619,13 @@ void show_log(struct rev_info *opt)
        ctx.fmt = opt->commit_format;
        ctx.mailmap = opt->mailmap;
        ctx.color = opt->diffopt.use_color;
+       ctx.output_encoding = get_log_output_encoding();
+       if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
+               ctx.from_ident = &opt->from_ident;
        pretty_print_commit(&ctx, commit, &msgbuf);
 
        if (opt->add_signoff)
-               append_signoff(&msgbuf, opt->add_signoff);
+               append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
 
        if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
            ctx.notes_message && *ctx.notes_message) {
@@ -789,13 +731,16 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
 {
        int showed_log;
        struct commit_list *parents;
-       unsigned const char *sha1 = commit->object.sha1;
+       unsigned const char *sha1;
 
        if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
                return 0;
 
+       parse_commit_or_die(commit);
+       sha1 = commit->tree->object.sha1;
+
        /* Root commit? */
-       parents = commit->parents;
+       parents = get_saved_parents(opt, commit);
        if (!parents) {
                if (opt->show_root_diff) {
                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
@@ -816,7 +761,9 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
                         * parent, showing summary diff of the others
                         * we merged _in_.
                         */
-                       diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
+                       parse_commit_or_die(parents->item);
+                       diff_tree_sha1(parents->item->tree->object.sha1,
+                                      sha1, "", &opt->diffopt);
                        log_tree_diff_flush(opt);
                        return !opt->loginfo;
                }
@@ -829,7 +776,9 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
        for (;;) {
                struct commit *parent = parents->item;
 
-               diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
+               parse_commit_or_die(parent);
+               diff_tree_sha1(parent->tree->object.sha1,
+                              sha1, "", &opt->diffopt);
                log_tree_diff_flush(opt);
 
                showed_log |= !opt->loginfo;
@@ -853,6 +802,9 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
        log.parent = NULL;
        opt->loginfo = &log;
 
+       if (opt->line_level_traverse)
+               return line_log_print(opt, commit);
+
        shown = log_tree_diff(opt, commit, &log);
        if (!shown && opt->loginfo && opt->always_show_header) {
                log.parent = NULL;