log-tree.con commit hex.c: reduce memory footprint of sha1_to_hex static buffers (0b868f0)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "tag.h"
   5#include "graph.h"
   6#include "log-tree.h"
   7#include "reflog-walk.h"
   8#include "refs.h"
   9#include "string-list.h"
  10
  11struct decoration name_decoration = { "object names" };
  12
  13static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
  14{
  15        int plen = strlen(prefix);
  16        int nlen = strlen(name);
  17        struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
  18        memcpy(res->name, prefix, plen);
  19        memcpy(res->name + plen, name, nlen + 1);
  20        res->next = add_decoration(&name_decoration, obj, res);
  21}
  22
  23static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
  24{
  25        struct object *obj = parse_object(sha1);
  26        if (!obj)
  27                return 0;
  28        if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
  29                refname = prettify_refname(refname);
  30        add_name_decoration("", refname, obj);
  31        while (obj->type == OBJ_TAG) {
  32                obj = ((struct tag *)obj)->tagged;
  33                if (!obj)
  34                        break;
  35                add_name_decoration("tag: ", refname, obj);
  36        }
  37        return 0;
  38}
  39
  40void load_ref_decorations(int flags)
  41{
  42        static int loaded;
  43        if (!loaded) {
  44                loaded = 1;
  45                for_each_ref(add_ref_decoration, &flags);
  46                head_ref(add_ref_decoration, &flags);
  47        }
  48}
  49
  50static void show_parents(struct commit *commit, int abbrev)
  51{
  52        struct commit_list *p;
  53        for (p = commit->parents; p ; p = p->next) {
  54                struct commit *parent = p->item;
  55                printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
  56        }
  57}
  58
  59void show_decorations(struct rev_info *opt, struct commit *commit)
  60{
  61        const char *prefix;
  62        struct name_decoration *decoration;
  63
  64        if (opt->show_source && commit->util)
  65                printf("\t%s", (char *) commit->util);
  66        if (!opt->show_decorations)
  67                return;
  68        decoration = lookup_decoration(&name_decoration, &commit->object);
  69        if (!decoration)
  70                return;
  71        prefix = " (";
  72        while (decoration) {
  73                printf("%s%s", prefix, decoration->name);
  74                prefix = ", ";
  75                decoration = decoration->next;
  76        }
  77        putchar(')');
  78}
  79
  80/*
  81 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
  82 * Signed-off-by: and Acked-by: lines.
  83 */
  84static int detect_any_signoff(char *letter, int size)
  85{
  86        char *cp;
  87        int seen_colon = 0;
  88        int seen_at = 0;
  89        int seen_name = 0;
  90        int seen_head = 0;
  91
  92        cp = letter + size;
  93        while (letter <= --cp && *cp == '\n')
  94                continue;
  95
  96        while (letter <= cp) {
  97                char ch = *cp--;
  98                if (ch == '\n')
  99                        break;
 100
 101                if (!seen_at) {
 102                        if (ch == '@')
 103                                seen_at = 1;
 104                        continue;
 105                }
 106                if (!seen_colon) {
 107                        if (ch == '@')
 108                                return 0;
 109                        else if (ch == ':')
 110                                seen_colon = 1;
 111                        else
 112                                seen_name = 1;
 113                        continue;
 114                }
 115                if (('A' <= ch && ch <= 'Z') ||
 116                    ('a' <= ch && ch <= 'z') ||
 117                    ch == '-') {
 118                        seen_head = 1;
 119                        continue;
 120                }
 121                /* no empty last line doesn't match */
 122                return 0;
 123        }
 124        return seen_head && seen_name;
 125}
 126
 127static void append_signoff(struct strbuf *sb, const char *signoff)
 128{
 129        static const char signed_off_by[] = "Signed-off-by: ";
 130        size_t signoff_len = strlen(signoff);
 131        int has_signoff = 0;
 132        char *cp;
 133
 134        cp = sb->buf;
 135
 136        /* First see if we already have the sign-off by the signer */
 137        while ((cp = strstr(cp, signed_off_by))) {
 138
 139                has_signoff = 1;
 140
 141                cp += strlen(signed_off_by);
 142                if (cp + signoff_len >= sb->buf + sb->len)
 143                        break;
 144                if (strncmp(cp, signoff, signoff_len))
 145                        continue;
 146                if (!isspace(cp[signoff_len]))
 147                        continue;
 148                /* we already have him */
 149                return;
 150        }
 151
 152        if (!has_signoff)
 153                has_signoff = detect_any_signoff(sb->buf, sb->len);
 154
 155        if (!has_signoff)
 156                strbuf_addch(sb, '\n');
 157
 158        strbuf_addstr(sb, signed_off_by);
 159        strbuf_add(sb, signoff, signoff_len);
 160        strbuf_addch(sb, '\n');
 161}
 162
 163static unsigned int digits_in_number(unsigned int number)
 164{
 165        unsigned int i = 10, result = 1;
 166        while (i <= number) {
 167                i *= 10;
 168                result++;
 169        }
 170        return result;
 171}
 172
 173void get_patch_filename(struct commit *commit, int nr, const char *suffix,
 174                        struct strbuf *buf)
 175{
 176        int suffix_len = strlen(suffix) + 1;
 177        int start_len = buf->len;
 178
 179        strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
 180        if (commit) {
 181                int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
 182                struct pretty_print_context ctx = {0};
 183                ctx.date_mode = DATE_NORMAL;
 184
 185                format_commit_message(commit, "%f", buf, &ctx);
 186                if (max_len < buf->len)
 187                        strbuf_setlen(buf, max_len);
 188                strbuf_addstr(buf, suffix);
 189        }
 190}
 191
 192void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 193                             const char **subject_p,
 194                             const char **extra_headers_p,
 195                             int *need_8bit_cte_p)
 196{
 197        const char *subject = NULL;
 198        const char *extra_headers = opt->extra_headers;
 199        const char *name = sha1_to_hex(commit->object.sha1);
 200
 201        *need_8bit_cte_p = 0; /* unknown */
 202        if (opt->total > 0) {
 203                static char buffer[64];
 204                snprintf(buffer, sizeof(buffer),
 205                         "Subject: [%s %0*d/%d] ",
 206                         opt->subject_prefix,
 207                         digits_in_number(opt->total),
 208                         opt->nr, opt->total);
 209                subject = buffer;
 210        } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 211                static char buffer[256];
 212                snprintf(buffer, sizeof(buffer),
 213                         "Subject: [%s] ",
 214                         opt->subject_prefix);
 215                subject = buffer;
 216        } else {
 217                subject = "Subject: ";
 218        }
 219
 220        printf("From %s Mon Sep 17 00:00:00 2001\n", name);
 221        graph_show_oneline(opt->graph);
 222        if (opt->message_id) {
 223                printf("Message-Id: <%s>\n", opt->message_id);
 224                graph_show_oneline(opt->graph);
 225        }
 226        if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
 227                int i, n;
 228                n = opt->ref_message_ids->nr;
 229                printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
 230                for (i = 0; i < n; i++)
 231                        printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
 232                               opt->ref_message_ids->items[i].string);
 233                graph_show_oneline(opt->graph);
 234        }
 235        if (opt->mime_boundary) {
 236                static char subject_buffer[1024];
 237                static char buffer[1024];
 238                struct strbuf filename =  STRBUF_INIT;
 239                *need_8bit_cte_p = -1; /* NEVER */
 240                snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 241                         "%s"
 242                         "MIME-Version: 1.0\n"
 243                         "Content-Type: multipart/mixed;"
 244                         " boundary=\"%s%s\"\n"
 245                         "\n"
 246                         "This is a multi-part message in MIME "
 247                         "format.\n"
 248                         "--%s%s\n"
 249                         "Content-Type: text/plain; "
 250                         "charset=UTF-8; format=fixed\n"
 251                         "Content-Transfer-Encoding: 8bit\n\n",
 252                         extra_headers ? extra_headers : "",
 253                         mime_boundary_leader, opt->mime_boundary,
 254                         mime_boundary_leader, opt->mime_boundary);
 255                extra_headers = subject_buffer;
 256
 257                get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
 258                                    opt->patch_suffix, &filename);
 259                snprintf(buffer, sizeof(buffer) - 1,
 260                         "\n--%s%s\n"
 261                         "Content-Type: text/x-patch;"
 262                         " name=\"%s\"\n"
 263                         "Content-Transfer-Encoding: 8bit\n"
 264                         "Content-Disposition: %s;"
 265                         " filename=\"%s\"\n\n",
 266                         mime_boundary_leader, opt->mime_boundary,
 267                         filename.buf,
 268                         opt->no_inline ? "attachment" : "inline",
 269                         filename.buf);
 270                opt->diffopt.stat_sep = buffer;
 271                strbuf_release(&filename);
 272        }
 273        *subject_p = subject;
 274        *extra_headers_p = extra_headers;
 275}
 276
 277void show_log(struct rev_info *opt)
 278{
 279        struct strbuf msgbuf = STRBUF_INIT;
 280        struct log_info *log = opt->loginfo;
 281        struct commit *commit = log->commit, *parent = log->parent;
 282        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
 283        const char *extra_headers = opt->extra_headers;
 284        struct pretty_print_context ctx = {0};
 285
 286        opt->loginfo = NULL;
 287        if (!opt->verbose_header) {
 288                graph_show_commit(opt->graph);
 289
 290                if (!opt->graph) {
 291                        if (commit->object.flags & BOUNDARY)
 292                                putchar('-');
 293                        else if (commit->object.flags & UNINTERESTING)
 294                                putchar('^');
 295                        else if (opt->left_right) {
 296                                if (commit->object.flags & SYMMETRIC_LEFT)
 297                                        putchar('<');
 298                                else
 299                                        putchar('>');
 300                        }
 301                }
 302                fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
 303                if (opt->print_parents)
 304                        show_parents(commit, abbrev_commit);
 305                show_decorations(opt, commit);
 306                if (opt->graph && !graph_is_commit_finished(opt->graph)) {
 307                        putchar('\n');
 308                        graph_show_remainder(opt->graph);
 309                }
 310                putchar(opt->diffopt.line_termination);
 311                return;
 312        }
 313
 314        /*
 315         * If use_terminator is set, we already handled any record termination
 316         * at the end of the last record.
 317         * Otherwise, add a diffopt.line_termination character before all
 318         * entries but the first.  (IOW, as a separator between entries)
 319         */
 320        if (opt->shown_one && !opt->use_terminator) {
 321                /*
 322                 * If entries are separated by a newline, the output
 323                 * should look human-readable.  If the last entry ended
 324                 * with a newline, print the graph output before this
 325                 * newline.  Otherwise it will end up as a completely blank
 326                 * line and will look like a gap in the graph.
 327                 *
 328                 * If the entry separator is not a newline, the output is
 329                 * primarily intended for programmatic consumption, and we
 330                 * never want the extra graph output before the entry
 331                 * separator.
 332                 */
 333                if (opt->diffopt.line_termination == '\n' &&
 334                    !opt->missing_newline)
 335                        graph_show_padding(opt->graph);
 336                putchar(opt->diffopt.line_termination);
 337        }
 338        opt->shown_one = 1;
 339
 340        /*
 341         * If the history graph was requested,
 342         * print the graph, up to this commit's line
 343         */
 344        graph_show_commit(opt->graph);
 345
 346        /*
 347         * Print header line of header..
 348         */
 349
 350        if (opt->commit_format == CMIT_FMT_EMAIL) {
 351                log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
 352                                        &ctx.need_8bit_cte);
 353        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 354                fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
 355                if (opt->commit_format != CMIT_FMT_ONELINE)
 356                        fputs("commit ", stdout);
 357
 358                if (!opt->graph) {
 359                        if (commit->object.flags & BOUNDARY)
 360                                putchar('-');
 361                        else if (commit->object.flags & UNINTERESTING)
 362                                putchar('^');
 363                        else if (opt->left_right) {
 364                                if (commit->object.flags & SYMMETRIC_LEFT)
 365                                        putchar('<');
 366                                else
 367                                        putchar('>');
 368                        }
 369                }
 370                fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
 371                      stdout);
 372                if (opt->print_parents)
 373                        show_parents(commit, abbrev_commit);
 374                if (parent)
 375                        printf(" (from %s)",
 376                               find_unique_abbrev(parent->object.sha1,
 377                                                  abbrev_commit));
 378                show_decorations(opt, commit);
 379                printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
 380                if (opt->commit_format == CMIT_FMT_ONELINE) {
 381                        putchar(' ');
 382                } else {
 383                        putchar('\n');
 384                        graph_show_oneline(opt->graph);
 385                }
 386                if (opt->reflog_info) {
 387                        /*
 388                         * setup_revisions() ensures that opt->reflog_info
 389                         * and opt->graph cannot both be set,
 390                         * so we don't need to worry about printing the
 391                         * graph info here.
 392                         */
 393                        show_reflog_message(opt->reflog_info,
 394                                    opt->commit_format == CMIT_FMT_ONELINE,
 395                                    opt->date_mode_explicit ?
 396                                        opt->date_mode :
 397                                        DATE_NORMAL);
 398                        if (opt->commit_format == CMIT_FMT_ONELINE)
 399                                return;
 400                }
 401        }
 402
 403        if (!commit->buffer)
 404                return;
 405
 406        /*
 407         * And then the pretty-printed message itself
 408         */
 409        if (ctx.need_8bit_cte >= 0)
 410                ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
 411        ctx.date_mode = opt->date_mode;
 412        ctx.abbrev = opt->diffopt.abbrev;
 413        ctx.after_subject = extra_headers;
 414        ctx.reflog_info = opt->reflog_info;
 415        pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
 416
 417        if (opt->add_signoff)
 418                append_signoff(&msgbuf, opt->add_signoff);
 419        if (opt->show_log_size) {
 420                printf("log size %i\n", (int)msgbuf.len);
 421                graph_show_oneline(opt->graph);
 422        }
 423
 424        /*
 425         * Set opt->missing_newline if msgbuf doesn't
 426         * end in a newline (including if it is empty)
 427         */
 428        if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
 429                opt->missing_newline = 1;
 430        else
 431                opt->missing_newline = 0;
 432
 433        if (opt->graph)
 434                graph_show_commit_msg(opt->graph, &msgbuf);
 435        else
 436                fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
 437        if (opt->use_terminator) {
 438                if (!opt->missing_newline)
 439                        graph_show_padding(opt->graph);
 440                putchar('\n');
 441        }
 442
 443        strbuf_release(&msgbuf);
 444}
 445
 446int log_tree_diff_flush(struct rev_info *opt)
 447{
 448        diffcore_std(&opt->diffopt);
 449
 450        if (diff_queue_is_empty()) {
 451                int saved_fmt = opt->diffopt.output_format;
 452                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 453                diff_flush(&opt->diffopt);
 454                opt->diffopt.output_format = saved_fmt;
 455                return 0;
 456        }
 457
 458        if (opt->loginfo && !opt->no_commit_id) {
 459                /* When showing a verbose header (i.e. log message),
 460                 * and not in --pretty=oneline format, we would want
 461                 * an extra newline between the end of log and the
 462                 * output for readability.
 463                 */
 464                show_log(opt);
 465                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
 466                    opt->verbose_header &&
 467                    opt->commit_format != CMIT_FMT_ONELINE) {
 468                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 469                        if ((pch & opt->diffopt.output_format) == pch)
 470                                printf("---");
 471                        putchar('\n');
 472                }
 473        }
 474        diff_flush(&opt->diffopt);
 475        return 1;
 476}
 477
 478static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 479{
 480        unsigned const char *sha1 = commit->object.sha1;
 481
 482        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 483        return !opt->loginfo;
 484}
 485
 486/*
 487 * Show the diff of a commit.
 488 *
 489 * Return true if we printed any log info messages
 490 */
 491static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 492{
 493        int showed_log;
 494        struct commit_list *parents;
 495        unsigned const char *sha1 = commit->object.sha1;
 496
 497        if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
 498                return 0;
 499
 500        /* Root commit? */
 501        parents = commit->parents;
 502        if (!parents) {
 503                if (opt->show_root_diff) {
 504                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
 505                        log_tree_diff_flush(opt);
 506                }
 507                return !opt->loginfo;
 508        }
 509
 510        /* More than one parent? */
 511        if (parents && parents->next) {
 512                if (opt->ignore_merges)
 513                        return 0;
 514                else if (opt->combine_merges)
 515                        return do_diff_combined(opt, commit);
 516
 517                /* If we show individual diffs, show the parent info */
 518                log->parent = parents->item;
 519        }
 520
 521        showed_log = 0;
 522        for (;;) {
 523                struct commit *parent = parents->item;
 524
 525                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 526                log_tree_diff_flush(opt);
 527
 528                showed_log |= !opt->loginfo;
 529
 530                /* Set up the log info for the next parent, if any.. */
 531                parents = parents->next;
 532                if (!parents)
 533                        break;
 534                log->parent = parents->item;
 535                opt->loginfo = log;
 536        }
 537        return showed_log;
 538}
 539
 540int log_tree_commit(struct rev_info *opt, struct commit *commit)
 541{
 542        struct log_info log;
 543        int shown;
 544
 545        log.commit = commit;
 546        log.parent = NULL;
 547        opt->loginfo = &log;
 548
 549        shown = log_tree_diff(opt, commit, &log);
 550        if (!shown && opt->loginfo && opt->always_show_header) {
 551                log.parent = NULL;
 552                show_log(opt);
 553                shown = 1;
 554        }
 555        opt->loginfo = NULL;
 556        maybe_flush_or_die(stdout, "stdout");
 557        return shown;
 558}