log-tree.con commit am: POSIX portability fix (29df238)
   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 show_log(struct rev_info *opt, const char *sep)
 141{
 142        struct strbuf msgbuf;
 143        struct log_info *log = opt->loginfo;
 144        struct commit *commit = log->commit, *parent = log->parent;
 145        int abbrev = opt->diffopt.abbrev;
 146        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
 147        const char *extra;
 148        const char *subject = NULL, *extra_headers = opt->extra_headers;
 149        int need_8bit_cte = 0;
 150
 151        opt->loginfo = NULL;
 152        if (!opt->verbose_header) {
 153                if (opt->left_right) {
 154                        if (commit->object.flags & BOUNDARY)
 155                                putchar('-');
 156                        else if (commit->object.flags & SYMMETRIC_LEFT)
 157                                putchar('<');
 158                        else
 159                                putchar('>');
 160                }
 161                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
 162                if (opt->parents)
 163                        show_parents(commit, abbrev_commit);
 164                show_decorations(commit);
 165                putchar(opt->diffopt.line_termination);
 166                return;
 167        }
 168
 169        /*
 170         * The "oneline" format has several special cases:
 171         *  - The pretty-printed commit lacks a newline at the end
 172         *    of the buffer, but we do want to make sure that we
 173         *    have a newline there. If the separator isn't already
 174         *    a newline, add an extra one.
 175         *  - unlike other log messages, the one-line format does
 176         *    not have an empty line between entries.
 177         */
 178        extra = "";
 179        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
 180                extra = "\n";
 181        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
 182                putchar(opt->diffopt.line_termination);
 183        opt->shown_one = 1;
 184
 185        /*
 186         * Print header line of header..
 187         */
 188
 189        if (opt->commit_format == CMIT_FMT_EMAIL) {
 190                char *sha1 = sha1_to_hex(commit->object.sha1);
 191                if (opt->total > 0) {
 192                        static char buffer[64];
 193                        snprintf(buffer, sizeof(buffer),
 194                                        "Subject: [%s %0*d/%d] ",
 195                                        opt->subject_prefix,
 196                                        digits_in_number(opt->total),
 197                                        opt->nr, opt->total);
 198                        subject = buffer;
 199                } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 200                        static char buffer[256];
 201                        snprintf(buffer, sizeof(buffer),
 202                                        "Subject: [%s] ",
 203                                        opt->subject_prefix);
 204                        subject = buffer;
 205                } else {
 206                        subject = "Subject: ";
 207                }
 208
 209                printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
 210                if (opt->message_id)
 211                        printf("Message-Id: <%s>\n", opt->message_id);
 212                if (opt->ref_message_id)
 213                        printf("In-Reply-To: <%s>\nReferences: <%s>\n",
 214                               opt->ref_message_id, opt->ref_message_id);
 215                if (opt->mime_boundary) {
 216                        static char subject_buffer[1024];
 217                        static char buffer[1024];
 218
 219                        need_8bit_cte = -1; /* never */
 220                        snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 221                                 "%s"
 222                                 "MIME-Version: 1.0\n"
 223                                 "Content-Type: multipart/mixed;"
 224                                 " boundary=\"%s%s\"\n"
 225                                 "\n"
 226                                 "This is a multi-part message in MIME "
 227                                 "format.\n"
 228                                 "--%s%s\n"
 229                                 "Content-Type: text/plain; "
 230                                 "charset=UTF-8; format=fixed\n"
 231                                 "Content-Transfer-Encoding: 8bit\n\n",
 232                                 extra_headers ? extra_headers : "",
 233                                 mime_boundary_leader, opt->mime_boundary,
 234                                 mime_boundary_leader, opt->mime_boundary);
 235                        extra_headers = subject_buffer;
 236
 237                        snprintf(buffer, sizeof(buffer) - 1,
 238                                 "--%s%s\n"
 239                                 "Content-Type: text/x-patch;"
 240                                 " name=\"%s.diff\"\n"
 241                                 "Content-Transfer-Encoding: 8bit\n"
 242                                 "Content-Disposition: %s;"
 243                                 " filename=\"%s.diff\"\n\n",
 244                                 mime_boundary_leader, opt->mime_boundary,
 245                                 sha1,
 246                                 opt->no_inline ? "attachment" : "inline",
 247                                 sha1);
 248                        opt->diffopt.stat_sep = buffer;
 249                }
 250        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 251                fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
 252                if (opt->commit_format != CMIT_FMT_ONELINE)
 253                        fputs("commit ", stdout);
 254                if (commit->object.flags & BOUNDARY)
 255                        putchar('-');
 256                else if (opt->left_right) {
 257                        if (commit->object.flags & SYMMETRIC_LEFT)
 258                                putchar('<');
 259                        else
 260                                putchar('>');
 261                }
 262                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
 263                      stdout);
 264                if (opt->parents)
 265                        show_parents(commit, abbrev_commit);
 266                if (parent)
 267                        printf(" (from %s)",
 268                               diff_unique_abbrev(parent->object.sha1,
 269                                                  abbrev_commit));
 270                show_decorations(commit);
 271                printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
 272                putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
 273                if (opt->reflog_info) {
 274                        show_reflog_message(opt->reflog_info,
 275                                    opt->commit_format == CMIT_FMT_ONELINE,
 276                                    opt->date_mode);
 277                        if (opt->commit_format == CMIT_FMT_ONELINE) {
 278                                printf("%s", sep);
 279                                return;
 280                        }
 281                }
 282        }
 283
 284        /*
 285         * And then the pretty-printed message itself
 286         */
 287        strbuf_init(&msgbuf, 0);
 288        if (need_8bit_cte >= 0)
 289                need_8bit_cte = has_non_ascii(opt->add_signoff);
 290        pretty_print_commit(opt->commit_format, commit, &msgbuf,
 291                            abbrev, subject, extra_headers, opt->date_mode,
 292                            need_8bit_cte);
 293
 294        if (opt->add_signoff)
 295                append_signoff(&msgbuf, opt->add_signoff);
 296        if (opt->show_log_size)
 297                printf("log size %i\n", (int)msgbuf.len);
 298
 299        if (msgbuf.len)
 300                printf("%s%s%s", msgbuf.buf, extra, sep);
 301        strbuf_release(&msgbuf);
 302}
 303
 304int log_tree_diff_flush(struct rev_info *opt)
 305{
 306        diffcore_std(&opt->diffopt);
 307
 308        if (diff_queue_is_empty()) {
 309                int saved_fmt = opt->diffopt.output_format;
 310                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 311                diff_flush(&opt->diffopt);
 312                opt->diffopt.output_format = saved_fmt;
 313                return 0;
 314        }
 315
 316        if (opt->loginfo && !opt->no_commit_id) {
 317                /* When showing a verbose header (i.e. log message),
 318                 * and not in --pretty=oneline format, we would want
 319                 * an extra newline between the end of log and the
 320                 * output for readability.
 321                 */
 322                show_log(opt, opt->diffopt.msg_sep);
 323                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
 324                    opt->verbose_header &&
 325                    opt->commit_format != CMIT_FMT_ONELINE) {
 326                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 327                        if ((pch & opt->diffopt.output_format) == pch)
 328                                printf("---");
 329                        putchar('\n');
 330                }
 331        }
 332        diff_flush(&opt->diffopt);
 333        return 1;
 334}
 335
 336static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 337{
 338        unsigned const char *sha1 = commit->object.sha1;
 339
 340        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 341        return !opt->loginfo;
 342}
 343
 344/*
 345 * Show the diff of a commit.
 346 *
 347 * Return true if we printed any log info messages
 348 */
 349static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 350{
 351        int showed_log;
 352        struct commit_list *parents;
 353        unsigned const char *sha1 = commit->object.sha1;
 354
 355        if (!opt->diff)
 356                return 0;
 357
 358        /* Root commit? */
 359        parents = commit->parents;
 360        if (!parents) {
 361                if (opt->show_root_diff) {
 362                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
 363                        log_tree_diff_flush(opt);
 364                }
 365                return !opt->loginfo;
 366        }
 367
 368        /* More than one parent? */
 369        if (parents && parents->next) {
 370                if (opt->ignore_merges)
 371                        return 0;
 372                else if (opt->combine_merges)
 373                        return do_diff_combined(opt, commit);
 374
 375                /* If we show individual diffs, show the parent info */
 376                log->parent = parents->item;
 377        }
 378
 379        showed_log = 0;
 380        for (;;) {
 381                struct commit *parent = parents->item;
 382
 383                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 384                log_tree_diff_flush(opt);
 385
 386                showed_log |= !opt->loginfo;
 387
 388                /* Set up the log info for the next parent, if any.. */
 389                parents = parents->next;
 390                if (!parents)
 391                        break;
 392                log->parent = parents->item;
 393                opt->loginfo = log;
 394        }
 395        return showed_log;
 396}
 397
 398int log_tree_commit(struct rev_info *opt, struct commit *commit)
 399{
 400        struct log_info log;
 401        int shown;
 402
 403        log.commit = commit;
 404        log.parent = NULL;
 405        opt->loginfo = &log;
 406
 407        shown = log_tree_diff(opt, commit, &log);
 408        if (!shown && opt->loginfo && opt->always_show_header) {
 409                log.parent = NULL;
 410                show_log(opt, "");
 411                shown = 1;
 412        }
 413        opt->loginfo = NULL;
 414        maybe_flush_or_die(stdout, "stdout");
 415        return shown;
 416}