log-tree.con commit Merge branch 'jc/tartree' into jc/builtin-n-tar-tree (1af0d11)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "log-tree.h"
   5
   6static void show_parents(struct commit *commit, int abbrev)
   7{
   8        struct commit_list *p;
   9        for (p = commit->parents; p ; p = p->next) {
  10                struct commit *parent = p->item;
  11                printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
  12        }
  13}
  14
  15void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
  16{
  17        static char this_header[16384];
  18        struct commit *commit = log->commit, *parent = log->parent;
  19        int abbrev = opt->diffopt.abbrev;
  20        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
  21        const char *extra;
  22        int len;
  23
  24        opt->loginfo = NULL;
  25        if (!opt->verbose_header) {
  26                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
  27                if (opt->parents)
  28                        show_parents(commit, abbrev_commit);
  29                putchar('\n');
  30                return;
  31        }
  32
  33        /*
  34         * The "oneline" format has several special cases:
  35         *  - The pretty-printed commit lacks a newline at the end
  36         *    of the buffer, but we do want to make sure that we
  37         *    have a newline there. If the separator isn't already
  38         *    a newline, add an extra one.
  39         *  - unlike other log messages, the one-line format does
  40         *    not have an empty line between entries.
  41         */
  42        extra = "";
  43        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
  44                extra = "\n";
  45        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
  46                putchar('\n');
  47        opt->shown_one = 1;
  48
  49        /*
  50         * Print header line of header..
  51         */
  52        printf("%s%s",
  53                opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
  54                diff_unique_abbrev(commit->object.sha1, abbrev_commit));
  55        if (opt->parents)
  56                show_parents(commit, abbrev_commit);
  57        if (parent)
  58                printf(" (from %s)", diff_unique_abbrev(parent->object.sha1, abbrev_commit));
  59        putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
  60
  61        /*
  62         * And then the pretty-printed message itself
  63         */
  64        len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev);
  65        printf("%s%s%s", this_header, extra, sep);
  66}
  67
  68int log_tree_diff_flush(struct rev_info *opt)
  69{
  70        diffcore_std(&opt->diffopt);
  71
  72        if (diff_queue_is_empty()) {
  73                int saved_fmt = opt->diffopt.output_format;
  74                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
  75                diff_flush(&opt->diffopt);
  76                opt->diffopt.output_format = saved_fmt;
  77                return 0;
  78        }
  79
  80        if (opt->loginfo && !opt->no_commit_id)
  81                show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
  82        diff_flush(&opt->diffopt);
  83        return 1;
  84}
  85
  86static int diff_root_tree(struct rev_info *opt,
  87                          const unsigned char *new, const char *base)
  88{
  89        int retval;
  90        void *tree;
  91        struct tree_desc empty, real;
  92
  93        tree = read_object_with_reference(new, tree_type, &real.size, NULL);
  94        if (!tree)
  95                die("unable to read root tree (%s)", sha1_to_hex(new));
  96        real.buf = tree;
  97
  98        empty.buf = "";
  99        empty.size = 0;
 100        retval = diff_tree(&empty, &real, base, &opt->diffopt);
 101        free(tree);
 102        log_tree_diff_flush(opt);
 103        return retval;
 104}
 105
 106static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 107{
 108        unsigned const char *sha1 = commit->object.sha1;
 109
 110        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 111        return !opt->loginfo;
 112}
 113
 114/*
 115 * Show the diff of a commit.
 116 *
 117 * Return true if we printed any log info messages
 118 */
 119static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 120{
 121        int showed_log;
 122        struct commit_list *parents;
 123        unsigned const char *sha1 = commit->object.sha1;
 124
 125        if (!opt->diff)
 126                return 0;
 127
 128        /* Root commit? */
 129        parents = commit->parents;
 130        if (!parents) {
 131                if (opt->show_root_diff)
 132                        diff_root_tree(opt, sha1, "");
 133                return !opt->loginfo;
 134        }
 135
 136        /* More than one parent? */
 137        if (parents && parents->next) {
 138                if (opt->ignore_merges)
 139                        return 0;
 140                else if (opt->combine_merges)
 141                        return do_diff_combined(opt, commit);
 142
 143                /* If we show individual diffs, show the parent info */
 144                log->parent = parents->item;
 145        }
 146
 147        showed_log = 0;
 148        for (;;) {
 149                struct commit *parent = parents->item;
 150
 151                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 152                log_tree_diff_flush(opt);
 153
 154                showed_log |= !opt->loginfo;
 155
 156                /* Set up the log info for the next parent, if any.. */
 157                parents = parents->next;
 158                if (!parents)
 159                        break;
 160                log->parent = parents->item;
 161                opt->loginfo = log;
 162        }
 163        return showed_log;
 164}
 165
 166int log_tree_commit(struct rev_info *opt, struct commit *commit)
 167{
 168        struct log_info log;
 169
 170        log.commit = commit;
 171        log.parent = NULL;
 172        opt->loginfo = &log;
 173
 174        if (!log_tree_diff(opt, commit, &log) && opt->loginfo && opt->always_show_header) {
 175                log.parent = NULL;
 176                show_log(opt, opt->loginfo, "");
 177        }
 178        opt->loginfo = NULL;
 179        return 0;
 180}