Merge branch 'ss/pull-rebase-preserve'
[gitweb.git] / log-tree.c
index 11676d523553db7350d015f8b81d46698e43a6f7..2c1ed0fa90170438e00a2eadbf74e15d89531613 100644 (file)
 #include "sequencer.h"
 #include "line-log.h"
 
-struct decoration name_decoration = { "object names" };
-
-enum decoration_type {
-       DECORATION_NONE = 0,
-       DECORATION_REF_LOCAL,
-       DECORATION_REF_REMOTE,
-       DECORATION_REF_TAG,
-       DECORATION_REF_STASH,
-       DECORATION_REF_HEAD,
-       DECORATION_GRAFTED,
-};
+static struct decoration name_decoration = { "object names" };
 
 static char decoration_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
@@ -66,15 +56,14 @@ static int parse_decorate_color_slot(const char *slot)
        return -1;
 }
 
-int parse_decorate_color_config(const char *var, const int ofs, const char *value)
+int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
 {
-       int slot = parse_decorate_color_slot(var + ofs);
+       int slot = parse_decorate_color_slot(slot_name);
        if (slot < 0)
                return 0;
        if (!value)
                return config_error_nonbool(var);
-       color_parse(value, var, decoration_colors[slot]);
-       return 0;
+       return color_parse(value, decoration_colors[slot]);
 }
 
 /*
@@ -84,23 +73,28 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
 #define decorate_get_color_opt(o, ix) \
        decorate_get_color((o)->use_color, ix)
 
-static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
+void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
 {
        int nlen = strlen(name);
-       struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
+       struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
        memcpy(res->name, name, nlen + 1);
        res->type = type;
        res->next = add_decoration(&name_decoration, obj, res);
 }
 
+const struct name_decoration *get_name_decoration(const struct object *obj)
+{
+       return lookup_decoration(&name_decoration, obj);
+}
+
 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 {
        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)
+               if (!check_replace_refs)
                        return 0;
                if (get_sha1_hex(refname + 13, original_sha1)) {
                        warning("invalid replace ref %s", refname);
@@ -116,11 +110,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;
@@ -179,38 +173,98 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
 }
 
 /*
- * The caller makes sure there is no funny color before
- * calling. format_decorations makes sure the same after return.
+ * Do we have HEAD in the output, and also the branch it points at?
+ * If so, find that decoration entry for that current branch.
  */
-void format_decorations(struct strbuf *sb,
+static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
+{
+       const struct name_decoration *list, *head = NULL;
+       const char *branch_name = NULL;
+       unsigned char unused[20];
+       int rru_flags;
+
+       /* First find HEAD */
+       for (list = decoration; list; list = list->next)
+               if (list->type == DECORATION_REF_HEAD) {
+                       head = list;
+                       break;
+               }
+       if (!head)
+               return NULL;
+
+       /* Now resolve and find the matching current branch */
+       branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
+       if (!(rru_flags & REF_ISSYMREF))
+               return NULL;
+       if (!skip_prefix(branch_name, "refs/heads/", &branch_name))
+               return NULL;
+
+       /* OK, do we have that ref in the list? */
+       for (list = decoration; list; list = list->next)
+               if ((list->type == DECORATION_REF_LOCAL) &&
+                   !strcmp(branch_name, list->name)) {
+                       return list;
+               }
+
+       return NULL;
+}
+
+/*
+ * The caller makes sure there is no funny color before calling.
+ * format_decorations_extended makes sure the same after return.
+ */
+void format_decorations_extended(struct strbuf *sb,
                        const struct commit *commit,
-                       int use_color)
+                       int use_color,
+                       const char *prefix,
+                       const char *separator,
+                       const char *suffix)
 {
-       const char *prefix;
-       struct name_decoration *decoration;
+       const struct name_decoration *decoration;
+       const struct name_decoration *current_and_HEAD;
        const char *color_commit =
                diff_get_color(use_color, DIFF_COMMIT);
        const char *color_reset =
                decorate_get_color(use_color, DECORATION_NONE);
 
-       decoration = lookup_decoration(&name_decoration, &commit->object);
+       decoration = get_name_decoration(&commit->object);
        if (!decoration)
                return;
-       prefix = " (";
+
+       current_and_HEAD = current_pointed_by_HEAD(decoration);
        while (decoration) {
-               strbuf_addstr(sb, color_commit);
-               strbuf_addstr(sb, prefix);
-               strbuf_addstr(sb, color_reset);
-               strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
-               if (decoration->type == DECORATION_REF_TAG)
-                       strbuf_addstr(sb, "tag: ");
-               strbuf_addstr(sb, decoration->name);
-               strbuf_addstr(sb, color_reset);
-               prefix = ", ";
+               /*
+                * When both current and HEAD are there, only
+                * show HEAD->current where HEAD would have
+                * appeared, skipping the entry for current.
+                */
+               if (decoration != current_and_HEAD) {
+                       strbuf_addstr(sb, color_commit);
+                       strbuf_addstr(sb, prefix);
+                       strbuf_addstr(sb, color_reset);
+                       strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
+                       if (decoration->type == DECORATION_REF_TAG)
+                               strbuf_addstr(sb, "tag: ");
+
+                       strbuf_addstr(sb, decoration->name);
+
+                       if (current_and_HEAD &&
+                           decoration->type == DECORATION_REF_HEAD) {
+                               strbuf_addstr(sb, color_reset);
+                               strbuf_addstr(sb, color_commit);
+                               strbuf_addstr(sb, " -> ");
+                               strbuf_addstr(sb, color_reset);
+                               strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
+                               strbuf_addstr(sb, current_and_HEAD->name);
+                       }
+                       strbuf_addstr(sb, color_reset);
+
+                       prefix = separator;
+               }
                decoration = decoration->next;
        }
        strbuf_addstr(sb, color_commit);
-       strbuf_addch(sb, ')');
+       strbuf_addstr(sb, suffix);
        strbuf_addstr(sb, color_reset);
 }
 
@@ -366,6 +420,7 @@ static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
                eol = strchrnul(bol, '\n');
                printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
                       *eol ? "\n" : "");
+               graph_show_oneline(opt->graph);
                bol = (*eol) ? (eol + 1) : eol;
        }
 }
@@ -377,7 +432,7 @@ static void show_signature(struct rev_info *opt, struct commit *commit)
        struct strbuf gpg_output = STRBUF_INIT;
        int status;
 
-       if (parse_signed_commit(commit->object.sha1, &payload, &signature) <= 0)
+       if (parse_signed_commit(commit, &payload, &signature) <= 0)
                goto out;
 
        status = verify_signed_buffer(payload.buf, payload.len,
@@ -414,10 +469,11 @@ static int is_common_merge(const struct commit *commit)
                && !commit->parents->next->next);
 }
 
-static void show_one_mergetag(struct rev_info *opt,
+static void show_one_mergetag(struct commit *commit,
                              struct commit_extra_header *extra,
-                             struct commit *commit)
+                             void *data)
 {
+       struct rev_info *opt = (struct rev_info *)data;
        unsigned char sha1[20];
        struct tag *tag;
        struct strbuf verify_message;
@@ -447,16 +503,17 @@ static void show_one_mergetag(struct rev_info *opt,
 
        payload_size = parse_signature(extra->value, extra->len);
        status = -1;
-       if (extra->len > payload_size)
-               if (verify_signed_buffer(extra->value, payload_size,
-                                        extra->value + payload_size,
-                                        extra->len - payload_size,
-                                        &verify_message, NULL)) {
-                       if (verify_message.len <= gpg_message_offset)
-                               strbuf_addstr(&verify_message, "No signature\n");
-                       else
-                               status = 0;
-               }
+       if (extra->len > payload_size) {
+               /* could have a good signature */
+               if (!verify_signed_buffer(extra->value, payload_size,
+                                         extra->value + payload_size,
+                                         extra->len - payload_size,
+                                         &verify_message, NULL))
+                       status = 0; /* good */
+               else if (verify_message.len <= gpg_message_offset)
+                       strbuf_addstr(&verify_message, "No signature\n");
+               /* otherwise we couldn't verify, which is shown as bad */
+       }
 
        show_sig_lines(opt, status, verify_message.buf);
        strbuf_release(&verify_message);
@@ -464,15 +521,7 @@ static void show_one_mergetag(struct rev_info *opt,
 
 static void show_mergetag(struct rev_info *opt, struct commit *commit)
 {
-       struct commit_extra_header *extra, *to_free;
-
-       to_free = read_commit_extra_headers(commit, NULL);
-       for (extra = to_free; extra; extra = extra->next) {
-               if (strcmp(extra->key, "mergetag"))
-                       continue; /* not a merge tag */
-               show_one_mergetag(opt, extra, commit);
-       }
-       free_commit_extra_headers(to_free);
+       for_each_mergetag(show_one_mergetag, commit, opt);
 }
 
 void show_log(struct rev_info *opt)
@@ -589,7 +638,7 @@ void show_log(struct rev_info *opt)
                show_mergetag(opt, commit);
        }
 
-       if (!commit->buffer)
+       if (!get_cached_commit_buffer(commit, NULL))
                return;
 
        if (opt->show_notes) {
@@ -655,7 +704,7 @@ void show_log(struct rev_info *opt)
                graph_show_commit_msg(opt->graph, &msgbuf);
        else
                fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
-       if (opt->use_terminator) {
+       if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
                if (!opt->missing_newline)
                        graph_show_padding(opt->graph);
                putchar(opt->diffopt.line_termination);
@@ -682,7 +731,8 @@ int log_tree_diff_flush(struct rev_info *opt)
                show_log(opt);
                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
                    opt->verbose_header &&
-                   opt->commit_format != CMIT_FMT_ONELINE) {
+                   opt->commit_format != CMIT_FMT_ONELINE &&
+                   !commit_format_is_empty(opt->commit_format)) {
                        /*
                         * When showing a verbose header (i.e. log message),
                         * and not in --pretty=oneline format, we would want
@@ -737,7 +787,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
        if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
                return 0;
 
-       parse_commit(commit);
+       parse_commit_or_die(commit);
        sha1 = commit->tree->object.sha1;
 
        /* Root commit? */
@@ -762,7 +812,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
                         * parent, showing summary diff of the others
                         * we merged _in_.
                         */
-                       parse_commit(parents->item);
+                       parse_commit_or_die(parents->item);
                        diff_tree_sha1(parents->item->tree->object.sha1,
                                       sha1, "", &opt->diffopt);
                        log_tree_diff_flush(opt);
@@ -777,7 +827,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
        for (;;) {
                struct commit *parent = parents->item;
 
-               parse_commit(parent);
+               parse_commit_or_die(parent);
                diff_tree_sha1(parent->tree->object.sha1,
                               sha1, "", &opt->diffopt);
                log_tree_diff_flush(opt);
@@ -806,12 +856,16 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
        if (opt->line_level_traverse)
                return line_log_print(opt, commit);
 
+       if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
+               printf("\n%s\n", opt->break_bar);
        shown = log_tree_diff(opt, commit, &log);
        if (!shown && opt->loginfo && opt->always_show_header) {
                log.parent = NULL;
                show_log(opt);
                shown = 1;
        }
+       if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
+               printf("\n%s\n", opt->break_bar);
        opt->loginfo = NULL;
        maybe_flush_or_die(stdout, "stdout");
        return shown;