log-tree.con commit Merge branch 'master' into next (5f62524)
   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
  15static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
  16{
  17        int signoff_len = strlen(signoff);
  18        static const char signed_off_by[] = "Signed-off-by: ";
  19        char *cp = buf;
  20
  21        /* Do we have enough space to add it? */
  22        if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 2)
  23                return at;
  24
  25        /* First see if we already have the sign-off by the signer */
  26        while (1) {
  27                cp = strstr(cp, signed_off_by);
  28                if (!cp)
  29                        break;
  30                cp += strlen(signed_off_by);
  31                if ((cp + signoff_len < buf + at) &&
  32                    !strncmp(cp, signoff, signoff_len) &&
  33                    isspace(cp[signoff_len]))
  34                        return at; /* we already have him */
  35        }
  36
  37        strcpy(buf + at, signed_off_by);
  38        at += strlen(signed_off_by);
  39        strcpy(buf + at, signoff);
  40        at += signoff_len;
  41        buf[at++] = '\n';
  42        buf[at] = 0;
  43        return at;
  44}
  45
  46void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
  47{
  48        static char this_header[16384];
  49        struct commit *commit = log->commit, *parent = log->parent;
  50        int abbrev = opt->diffopt.abbrev;
  51        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
  52        const char *extra;
  53        int len;
  54        char *subject = NULL, *after_subject = NULL;
  55
  56        opt->loginfo = NULL;
  57        if (!opt->verbose_header) {
  58                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
  59                if (opt->parents)
  60                        show_parents(commit, abbrev_commit);
  61                putchar('\n');
  62                return;
  63        }
  64
  65        /*
  66         * The "oneline" format has several special cases:
  67         *  - The pretty-printed commit lacks a newline at the end
  68         *    of the buffer, but we do want to make sure that we
  69         *    have a newline there. If the separator isn't already
  70         *    a newline, add an extra one.
  71         *  - unlike other log messages, the one-line format does
  72         *    not have an empty line between entries.
  73         */
  74        extra = "";
  75        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
  76                extra = "\n";
  77        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
  78                putchar('\n');
  79        opt->shown_one = 1;
  80
  81        /*
  82         * Print header line of header..
  83         */
  84
  85        if (opt->commit_format == CMIT_FMT_EMAIL) {
  86                char *sha1 = sha1_to_hex(commit->object.sha1);
  87                if (opt->total > 0) {
  88                        static char buffer[64];
  89                        snprintf(buffer, sizeof(buffer),
  90                                        "Subject: [PATCH %d/%d] ",
  91                                        opt->nr, opt->total);
  92                        subject = buffer;
  93                } else if (opt->total == 0)
  94                        subject = "Subject: [PATCH] ";
  95                else
  96                        subject = "Subject: ";
  97
  98                printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
  99                if (opt->mime_boundary) {
 100                        static char subject_buffer[1024];
 101                        static char buffer[1024];
 102                        snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 103                                 "MIME-Version: 1.0\n"
 104                                 "Content-Type: multipart/mixed;\n"
 105                                 " boundary=\"%s%s\"\n"
 106                                 "\n"
 107                                 "This is a multi-part message in MIME "
 108                                 "format.\n"
 109                                 "--%s%s\n"
 110                                 "Content-Type: text/plain; "
 111                                 "charset=UTF-8; format=fixed\n"
 112                                 "Content-Transfer-Encoding: 8bit\n\n",
 113                                 mime_boundary_leader, opt->mime_boundary,
 114                                 mime_boundary_leader, opt->mime_boundary);
 115                        after_subject = subject_buffer;
 116
 117                        snprintf(buffer, sizeof(buffer) - 1,
 118                                 "--%s%s\n"
 119                                 "Content-Type: text/x-patch;\n"
 120                                 " name=\"%s.diff\"\n"
 121                                 "Content-Transfer-Encoding: 8bit\n"
 122                                 "Content-Disposition: inline;\n"
 123                                 " filename=\"%s.diff\"\n\n",
 124                                 mime_boundary_leader, opt->mime_boundary,
 125                                 sha1, sha1);
 126                        opt->diffopt.stat_sep = buffer;
 127                }
 128        } else {
 129                printf("%s%s",
 130                       opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
 131                       diff_unique_abbrev(commit->object.sha1, abbrev_commit));
 132                if (opt->parents)
 133                        show_parents(commit, abbrev_commit);
 134                if (parent)
 135                        printf(" (from %s)",
 136                               diff_unique_abbrev(parent->object.sha1,
 137                                                  abbrev_commit));
 138                putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
 139        }
 140
 141        /*
 142         * And then the pretty-printed message itself
 143         */
 144        len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject, after_subject);
 145
 146        if (opt->add_signoff)
 147                len = append_signoff(this_header, sizeof(this_header), len,
 148                                     opt->add_signoff);
 149        printf("%s%s%s", this_header, extra, sep);
 150}
 151
 152int log_tree_diff_flush(struct rev_info *opt)
 153{
 154        diffcore_std(&opt->diffopt);
 155
 156        if (diff_queue_is_empty()) {
 157                int saved_fmt = opt->diffopt.output_format;
 158                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 159                diff_flush(&opt->diffopt);
 160                opt->diffopt.output_format = saved_fmt;
 161                return 0;
 162        }
 163
 164        if (opt->loginfo && !opt->no_commit_id)
 165                show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
 166        diff_flush(&opt->diffopt);
 167        return 1;
 168}
 169
 170static int diff_root_tree(struct rev_info *opt,
 171                          const unsigned char *new, const char *base)
 172{
 173        int retval;
 174        void *tree;
 175        struct tree_desc empty, real;
 176
 177        tree = read_object_with_reference(new, tree_type, &real.size, NULL);
 178        if (!tree)
 179                die("unable to read root tree (%s)", sha1_to_hex(new));
 180        real.buf = tree;
 181
 182        empty.buf = "";
 183        empty.size = 0;
 184        retval = diff_tree(&empty, &real, base, &opt->diffopt);
 185        free(tree);
 186        log_tree_diff_flush(opt);
 187        return retval;
 188}
 189
 190static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 191{
 192        unsigned const char *sha1 = commit->object.sha1;
 193
 194        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 195        return !opt->loginfo;
 196}
 197
 198/*
 199 * Show the diff of a commit.
 200 *
 201 * Return true if we printed any log info messages
 202 */
 203static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 204{
 205        int showed_log;
 206        struct commit_list *parents;
 207        unsigned const char *sha1 = commit->object.sha1;
 208
 209        if (!opt->diff)
 210                return 0;
 211
 212        /* Root commit? */
 213        parents = commit->parents;
 214        if (!parents) {
 215                if (opt->show_root_diff)
 216                        diff_root_tree(opt, sha1, "");
 217                return !opt->loginfo;
 218        }
 219
 220        /* More than one parent? */
 221        if (parents && parents->next) {
 222                if (opt->ignore_merges)
 223                        return 0;
 224                else if (opt->combine_merges)
 225                        return do_diff_combined(opt, commit);
 226
 227                /* If we show individual diffs, show the parent info */
 228                log->parent = parents->item;
 229        }
 230
 231        showed_log = 0;
 232        for (;;) {
 233                struct commit *parent = parents->item;
 234
 235                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 236                log_tree_diff_flush(opt);
 237
 238                showed_log |= !opt->loginfo;
 239
 240                /* Set up the log info for the next parent, if any.. */
 241                parents = parents->next;
 242                if (!parents)
 243                        break;
 244                log->parent = parents->item;
 245                opt->loginfo = log;
 246        }
 247        return showed_log;
 248}
 249
 250int log_tree_commit(struct rev_info *opt, struct commit *commit)
 251{
 252        struct log_info log;
 253        int shown;
 254
 255        log.commit = commit;
 256        log.parent = NULL;
 257        opt->loginfo = &log;
 258
 259        shown = log_tree_diff(opt, commit, &log);
 260        if (!shown && opt->loginfo && opt->always_show_header) {
 261                log.parent = NULL;
 262                show_log(opt, opt->loginfo, "");
 263                shown = 1;
 264        }
 265        opt->loginfo = NULL;
 266        return shown;
 267}