log-tree.con commit Export some email and pretty-printing functions (b02bd65)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "log-tree.h"
   5#include "reflog-walk.h"
   6
   7struct decoration name_decoration = { "object names" };
   8
   9static void show_parents(struct commit *commit, int abbrev)
  10{
  11        struct commit_list *p;
  12        for (p = commit->parents; p ; p = p->next) {
  13                struct commit *parent = p->item;
  14                printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
  15        }
  16}
  17
  18void show_decorations(struct commit *commit)
  19{
  20        const char *prefix;
  21        struct name_decoration *decoration;
  22
  23        decoration = lookup_decoration(&name_decoration, &commit->object);
  24        if (!decoration)
  25                return;
  26        prefix = " (";
  27        while (decoration) {
  28                printf("%s%s", prefix, decoration->name);
  29                prefix = ", ";
  30                decoration = decoration->next;
  31        }
  32        putchar(')');
  33}
  34
  35/*
  36 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
  37 * Signed-off-by: and Acked-by: lines.
  38 */
  39static int detect_any_signoff(char *letter, int size)
  40{
  41        char ch, *cp;
  42        int seen_colon = 0;
  43        int seen_at = 0;
  44        int seen_name = 0;
  45        int seen_head = 0;
  46
  47        cp = letter + size;
  48        while (letter <= --cp && (ch = *cp) == '\n')
  49                continue;
  50
  51        while (letter <= cp) {
  52                ch = *cp--;
  53                if (ch == '\n')
  54                        break;
  55
  56                if (!seen_at) {
  57                        if (ch == '@')
  58                                seen_at = 1;
  59                        continue;
  60                }
  61                if (!seen_colon) {
  62                        if (ch == '@')
  63                                return 0;
  64                        else if (ch == ':')
  65                                seen_colon = 1;
  66                        else
  67                                seen_name = 1;
  68                        continue;
  69                }
  70                if (('A' <= ch && ch <= 'Z') ||
  71                    ('a' <= ch && ch <= 'z') ||
  72                    ch == '-') {
  73                        seen_head = 1;
  74                        continue;
  75                }
  76                /* no empty last line doesn't match */
  77                return 0;
  78        }
  79        return seen_head && seen_name;
  80}
  81
  82static void append_signoff(struct strbuf *sb, const char *signoff)
  83{
  84        static const char signed_off_by[] = "Signed-off-by: ";
  85        size_t signoff_len = strlen(signoff);
  86        int has_signoff = 0;
  87        char *cp;
  88
  89        cp = sb->buf;
  90
  91        /* First see if we already have the sign-off by the signer */
  92        while ((cp = strstr(cp, signed_off_by))) {
  93
  94                has_signoff = 1;
  95
  96                cp += strlen(signed_off_by);
  97                if (cp + signoff_len >= sb->buf + sb->len)
  98                        break;
  99                if (strncmp(cp, signoff, signoff_len))
 100                        continue;
 101                if (!isspace(cp[signoff_len]))
 102                        continue;
 103                /* we already have him */
 104                return;
 105        }
 106
 107        if (!has_signoff)
 108                has_signoff = detect_any_signoff(sb->buf, sb->len);
 109
 110        if (!has_signoff)
 111                strbuf_addch(sb, '\n');
 112
 113        strbuf_addstr(sb, signed_off_by);
 114        strbuf_add(sb, signoff, signoff_len);
 115        strbuf_addch(sb, '\n');
 116}
 117
 118static unsigned int digits_in_number(unsigned int number)
 119{
 120        unsigned int i = 10, result = 1;
 121        while (i <= number) {
 122                i *= 10;
 123                result++;
 124        }
 125        return result;
 126}
 127
 128static int has_non_ascii(const char *s)
 129{
 130        int ch;
 131        if (!s)
 132                return 0;
 133        while ((ch = *s++) != '\0') {
 134                if (non_ascii(ch))
 135                        return 1;
 136        }
 137        return 0;
 138}
 139
 140void log_write_email_headers(struct rev_info *opt, const char *name,
 141                             const char **subject_p, const char **extra_headers_p)
 142{
 143        const char *subject = NULL;
 144        const char *extra_headers = opt->extra_headers;
 145        if (opt->total > 0) {
 146                static char buffer[64];
 147                snprintf(buffer, sizeof(buffer),
 148                         "Subject: [%s %0*d/%d] ",
 149                         opt->subject_prefix,
 150                         digits_in_number(opt->total),
 151                         opt->nr, opt->total);
 152                subject = buffer;
 153        } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 154                static char buffer[256];
 155                snprintf(buffer, sizeof(buffer),
 156                         "Subject: [%s] ",
 157                         opt->subject_prefix);
 158                subject = buffer;
 159        } else {
 160                subject = "Subject: ";
 161        }
 162
 163        printf("From %s Mon Sep 17 00:00:00 2001\n", name);
 164        if (opt->message_id)
 165                printf("Message-Id: <%s>\n", opt->message_id);
 166        if (opt->ref_message_id)
 167                printf("In-Reply-To: <%s>\nReferences: <%s>\n",
 168                       opt->ref_message_id, opt->ref_message_id);
 169        if (opt->mime_boundary) {
 170                static char subject_buffer[1024];
 171                static char buffer[1024];
 172                snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 173                         "%s"
 174                         "MIME-Version: 1.0\n"
 175                         "Content-Type: multipart/mixed;"
 176                         " boundary=\"%s%s\"\n"
 177                         "\n"
 178                         "This is a multi-part message in MIME "
 179                         "format.\n"
 180                         "--%s%s\n"
 181                         "Content-Type: text/plain; "
 182                         "charset=UTF-8; format=fixed\n"
 183                         "Content-Transfer-Encoding: 8bit\n\n",
 184                         extra_headers ? extra_headers : "",
 185                         mime_boundary_leader, opt->mime_boundary,
 186                         mime_boundary_leader, opt->mime_boundary);
 187                extra_headers = subject_buffer;
 188
 189                snprintf(buffer, sizeof(buffer) - 1,
 190                         "--%s%s\n"
 191                         "Content-Type: text/x-patch;"
 192                         " name=\"%s.diff\"\n"
 193                         "Content-Transfer-Encoding: 8bit\n"
 194                         "Content-Disposition: %s;"
 195                         " filename=\"%s.diff\"\n\n",
 196                         mime_boundary_leader, opt->mime_boundary,
 197                         name,
 198                         opt->no_inline ? "attachment" : "inline",
 199                         name);
 200                opt->diffopt.stat_sep = buffer;
 201        }
 202        *subject_p = subject;
 203        *extra_headers_p = extra_headers;
 204}
 205
 206void show_log(struct rev_info *opt, const char *sep)
 207{
 208        struct strbuf msgbuf;
 209        struct log_info *log = opt->loginfo;
 210        struct commit *commit = log->commit, *parent = log->parent;
 211        int abbrev = opt->diffopt.abbrev;
 212        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
 213        const char *extra;
 214        const char *subject = NULL, *extra_headers = opt->extra_headers;
 215
 216        opt->loginfo = NULL;
 217        if (!opt->verbose_header) {
 218                if (opt->left_right) {
 219                        if (commit->object.flags & BOUNDARY)
 220                                putchar('-');
 221                        else if (commit->object.flags & SYMMETRIC_LEFT)
 222                                putchar('<');
 223                        else
 224                                putchar('>');
 225                }
 226                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
 227                if (opt->parents)
 228                        show_parents(commit, abbrev_commit);
 229                show_decorations(commit);
 230                putchar(opt->diffopt.line_termination);
 231                return;
 232        }
 233
 234        /*
 235         * The "oneline" format has several special cases:
 236         *  - The pretty-printed commit lacks a newline at the end
 237         *    of the buffer, but we do want to make sure that we
 238         *    have a newline there. If the separator isn't already
 239         *    a newline, add an extra one.
 240         *  - unlike other log messages, the one-line format does
 241         *    not have an empty line between entries.
 242         */
 243        extra = "";
 244        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
 245                extra = "\n";
 246        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
 247                putchar(opt->diffopt.line_termination);
 248        opt->shown_one = 1;
 249
 250        /*
 251         * Print header line of header..
 252         */
 253
 254        if (opt->commit_format == CMIT_FMT_EMAIL) {
 255                log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
 256                                        &subject, &extra_headers);
 257        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 258                fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
 259                if (opt->commit_format != CMIT_FMT_ONELINE)
 260                        fputs("commit ", stdout);
 261                if (commit->object.flags & BOUNDARY)
 262                        putchar('-');
 263                else if (opt->left_right) {
 264                        if (commit->object.flags & SYMMETRIC_LEFT)
 265                                putchar('<');
 266                        else
 267                                putchar('>');
 268                }
 269                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
 270                      stdout);
 271                if (opt->parents)
 272                        show_parents(commit, abbrev_commit);
 273                if (parent)
 274                        printf(" (from %s)",
 275                               diff_unique_abbrev(parent->object.sha1,
 276                                                  abbrev_commit));
 277                show_decorations(commit);
 278                printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
 279                putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
 280                if (opt->reflog_info) {
 281                        show_reflog_message(opt->reflog_info,
 282                                    opt->commit_format == CMIT_FMT_ONELINE,
 283                                    opt->date_mode);
 284                        if (opt->commit_format == CMIT_FMT_ONELINE) {
 285                                printf("%s", sep);
 286                                return;
 287                        }
 288                }
 289        }
 290
 291        /*
 292         * And then the pretty-printed message itself
 293         */
 294        strbuf_init(&msgbuf, 0);
 295        pretty_print_commit(opt->commit_format, commit, &msgbuf,
 296                            abbrev, subject, extra_headers, opt->date_mode,
 297                            has_non_ascii(opt->add_signoff));
 298
 299        if (opt->add_signoff)
 300                append_signoff(&msgbuf, opt->add_signoff);
 301        if (opt->show_log_size)
 302                printf("log size %i\n", (int)msgbuf.len);
 303
 304        if (msgbuf.len)
 305                printf("%s%s%s", msgbuf.buf, extra, sep);
 306        strbuf_release(&msgbuf);
 307}
 308
 309int log_tree_diff_flush(struct rev_info *opt)
 310{
 311        diffcore_std(&opt->diffopt);
 312
 313        if (diff_queue_is_empty()) {
 314                int saved_fmt = opt->diffopt.output_format;
 315                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 316                diff_flush(&opt->diffopt);
 317                opt->diffopt.output_format = saved_fmt;
 318                return 0;
 319        }
 320
 321        if (opt->loginfo && !opt->no_commit_id) {
 322                /* When showing a verbose header (i.e. log message),
 323                 * and not in --pretty=oneline format, we would want
 324                 * an extra newline between the end of log and the
 325                 * output for readability.
 326                 */
 327                show_log(opt, opt->diffopt.msg_sep);
 328                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
 329                    opt->verbose_header &&
 330                    opt->commit_format != CMIT_FMT_ONELINE) {
 331                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 332                        if ((pch & opt->diffopt.output_format) == pch)
 333                                printf("---");
 334                        putchar('\n');
 335                }
 336        }
 337        diff_flush(&opt->diffopt);
 338        return 1;
 339}
 340
 341static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 342{
 343        unsigned const char *sha1 = commit->object.sha1;
 344
 345        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 346        return !opt->loginfo;
 347}
 348
 349/*
 350 * Show the diff of a commit.
 351 *
 352 * Return true if we printed any log info messages
 353 */
 354static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 355{
 356        int showed_log;
 357        struct commit_list *parents;
 358        unsigned const char *sha1 = commit->object.sha1;
 359
 360        if (!opt->diff)
 361                return 0;
 362
 363        /* Root commit? */
 364        parents = commit->parents;
 365        if (!parents) {
 366                if (opt->show_root_diff) {
 367                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
 368                        log_tree_diff_flush(opt);
 369                }
 370                return !opt->loginfo;
 371        }
 372
 373        /* More than one parent? */
 374        if (parents && parents->next) {
 375                if (opt->ignore_merges)
 376                        return 0;
 377                else if (opt->combine_merges)
 378                        return do_diff_combined(opt, commit);
 379
 380                /* If we show individual diffs, show the parent info */
 381                log->parent = parents->item;
 382        }
 383
 384        showed_log = 0;
 385        for (;;) {
 386                struct commit *parent = parents->item;
 387
 388                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 389                log_tree_diff_flush(opt);
 390
 391                showed_log |= !opt->loginfo;
 392
 393                /* Set up the log info for the next parent, if any.. */
 394                parents = parents->next;
 395                if (!parents)
 396                        break;
 397                log->parent = parents->item;
 398                opt->loginfo = log;
 399        }
 400        return showed_log;
 401}
 402
 403int log_tree_commit(struct rev_info *opt, struct commit *commit)
 404{
 405        struct log_info log;
 406        int shown;
 407
 408        log.commit = commit;
 409        log.parent = NULL;
 410        opt->loginfo = &log;
 411
 412        shown = log_tree_diff(opt, commit, &log);
 413        if (!shown && opt->loginfo && opt->always_show_header) {
 414                log.parent = NULL;
 415                show_log(opt, "");
 416                shown = 1;
 417        }
 418        opt->loginfo = NULL;
 419        maybe_flush_or_die(stdout, "stdout");
 420        return shown;
 421}