log-tree.con commit git-format-patch: Make the second and subsequent mails replies to the first (d1566f7)
   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, const char *sep)
  47{
  48        static char this_header[16384];
  49        struct log_info *log = opt->loginfo;
  50        struct commit *commit = log->commit, *parent = log->parent;
  51        int abbrev = opt->diffopt.abbrev;
  52        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
  53        const char *extra;
  54        int len;
  55        const char *subject = NULL, *extra_headers = opt->extra_headers;
  56
  57        opt->loginfo = NULL;
  58        if (!opt->verbose_header) {
  59                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
  60                if (opt->parents)
  61                        show_parents(commit, abbrev_commit);
  62                putchar('\n');
  63                return;
  64        }
  65
  66        /*
  67         * The "oneline" format has several special cases:
  68         *  - The pretty-printed commit lacks a newline at the end
  69         *    of the buffer, but we do want to make sure that we
  70         *    have a newline there. If the separator isn't already
  71         *    a newline, add an extra one.
  72         *  - unlike other log messages, the one-line format does
  73         *    not have an empty line between entries.
  74         */
  75        extra = "";
  76        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
  77                extra = "\n";
  78        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
  79                putchar('\n');
  80        opt->shown_one = 1;
  81
  82        /*
  83         * Print header line of header..
  84         */
  85
  86        if (opt->commit_format == CMIT_FMT_EMAIL) {
  87                char *sha1 = sha1_to_hex(commit->object.sha1);
  88                if (opt->total > 0) {
  89                        static char buffer[64];
  90                        snprintf(buffer, sizeof(buffer),
  91                                        "Subject: [PATCH %d/%d] ",
  92                                        opt->nr, opt->total);
  93                        subject = buffer;
  94                } else if (opt->total == 0)
  95                        subject = "Subject: [PATCH] ";
  96                else
  97                        subject = "Subject: ";
  98
  99                printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
 100                if (opt->message_id)
 101                        printf("Message-Id: <%s>\n", opt->message_id);
 102                if (opt->ref_message_id)
 103                        printf("In-Reply-To: <%s>\nReferences: <%s>\n",
 104                               opt->ref_message_id, opt->ref_message_id);
 105                if (opt->mime_boundary) {
 106                        static char subject_buffer[1024];
 107                        static char buffer[1024];
 108                        snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 109                                 "%s"
 110                                 "MIME-Version: 1.0\n"
 111                                 "Content-Type: multipart/mixed;\n"
 112                                 " boundary=\"%s%s\"\n"
 113                                 "\n"
 114                                 "This is a multi-part message in MIME "
 115                                 "format.\n"
 116                                 "--%s%s\n"
 117                                 "Content-Type: text/plain; "
 118                                 "charset=UTF-8; format=fixed\n"
 119                                 "Content-Transfer-Encoding: 8bit\n\n",
 120                                 extra_headers ? extra_headers : "",
 121                                 mime_boundary_leader, opt->mime_boundary,
 122                                 mime_boundary_leader, opt->mime_boundary);
 123                        extra_headers = subject_buffer;
 124
 125                        snprintf(buffer, sizeof(buffer) - 1,
 126                                 "--%s%s\n"
 127                                 "Content-Type: text/x-patch;\n"
 128                                 " name=\"%s.diff\"\n"
 129                                 "Content-Transfer-Encoding: 8bit\n"
 130                                 "Content-Disposition: inline;\n"
 131                                 " filename=\"%s.diff\"\n\n",
 132                                 mime_boundary_leader, opt->mime_boundary,
 133                                 sha1, sha1);
 134                        opt->diffopt.stat_sep = buffer;
 135                }
 136        } else {
 137                printf("%s%s",
 138                       opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
 139                       diff_unique_abbrev(commit->object.sha1, abbrev_commit));
 140                if (opt->parents)
 141                        show_parents(commit, abbrev_commit);
 142                if (parent)
 143                        printf(" (from %s)",
 144                               diff_unique_abbrev(parent->object.sha1,
 145                                                  abbrev_commit));
 146                putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
 147        }
 148
 149        /*
 150         * And then the pretty-printed message itself
 151         */
 152        len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject, extra_headers);
 153
 154        if (opt->add_signoff)
 155                len = append_signoff(this_header, sizeof(this_header), len,
 156                                     opt->add_signoff);
 157        printf("%s%s%s", this_header, extra, sep);
 158}
 159
 160int log_tree_diff_flush(struct rev_info *opt)
 161{
 162        diffcore_std(&opt->diffopt);
 163
 164        if (diff_queue_is_empty()) {
 165                int saved_fmt = opt->diffopt.output_format;
 166                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 167                diff_flush(&opt->diffopt);
 168                opt->diffopt.output_format = saved_fmt;
 169                return 0;
 170        }
 171
 172        if (opt->loginfo && !opt->no_commit_id) {
 173                /* When showing a verbose header (i.e. log message),
 174                 * and not in --pretty=oneline format, we would want
 175                 * an extra newline between the end of log and the
 176                 * output for readability.
 177                 */
 178                show_log(opt, opt->diffopt.msg_sep);
 179                if (opt->verbose_header &&
 180                    opt->commit_format != CMIT_FMT_ONELINE) {
 181                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 182                        if ((pch & opt->diffopt.output_format) == pch)
 183                                printf("---%c", opt->diffopt.line_termination);
 184                        else
 185                                putchar(opt->diffopt.line_termination);
 186                }
 187        }
 188        diff_flush(&opt->diffopt);
 189        return 1;
 190}
 191
 192static int diff_root_tree(struct rev_info *opt,
 193                          const unsigned char *new, const char *base)
 194{
 195        int retval;
 196        void *tree;
 197        struct tree_desc empty, real;
 198
 199        tree = read_object_with_reference(new, tree_type, &real.size, NULL);
 200        if (!tree)
 201                die("unable to read root tree (%s)", sha1_to_hex(new));
 202        real.buf = tree;
 203
 204        empty.buf = "";
 205        empty.size = 0;
 206        retval = diff_tree(&empty, &real, base, &opt->diffopt);
 207        free(tree);
 208        log_tree_diff_flush(opt);
 209        return retval;
 210}
 211
 212static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 213{
 214        unsigned const char *sha1 = commit->object.sha1;
 215
 216        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 217        return !opt->loginfo;
 218}
 219
 220/*
 221 * Show the diff of a commit.
 222 *
 223 * Return true if we printed any log info messages
 224 */
 225static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 226{
 227        int showed_log;
 228        struct commit_list *parents;
 229        unsigned const char *sha1 = commit->object.sha1;
 230
 231        if (!opt->diff)
 232                return 0;
 233
 234        /* Root commit? */
 235        parents = commit->parents;
 236        if (!parents) {
 237                if (opt->show_root_diff)
 238                        diff_root_tree(opt, sha1, "");
 239                return !opt->loginfo;
 240        }
 241
 242        /* More than one parent? */
 243        if (parents && parents->next) {
 244                if (opt->ignore_merges)
 245                        return 0;
 246                else if (opt->combine_merges)
 247                        return do_diff_combined(opt, commit);
 248
 249                /* If we show individual diffs, show the parent info */
 250                log->parent = parents->item;
 251        }
 252
 253        showed_log = 0;
 254        for (;;) {
 255                struct commit *parent = parents->item;
 256
 257                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 258                log_tree_diff_flush(opt);
 259
 260                showed_log |= !opt->loginfo;
 261
 262                /* Set up the log info for the next parent, if any.. */
 263                parents = parents->next;
 264                if (!parents)
 265                        break;
 266                log->parent = parents->item;
 267                opt->loginfo = log;
 268        }
 269        return showed_log;
 270}
 271
 272int log_tree_commit(struct rev_info *opt, struct commit *commit)
 273{
 274        struct log_info log;
 275        int shown;
 276
 277        log.commit = commit;
 278        log.parent = NULL;
 279        opt->loginfo = &log;
 280
 281        shown = log_tree_diff(opt, commit, &log);
 282        if (!shown && opt->loginfo && opt->always_show_header) {
 283                log.parent = NULL;
 284                show_log(opt, "");
 285                shown = 1;
 286        }
 287        opt->loginfo = NULL;
 288        return shown;
 289}