log-tree.con commit Update tests not to assume that generated packfiles are writable. (a587896)
   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
  18static void 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 int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
  83{
  84        static const char signed_off_by[] = "Signed-off-by: ";
  85        int signoff_len = strlen(signoff);
  86        int has_signoff = 0;
  87        char *cp = buf;
  88
  89        /* Do we have enough space to add it? */
  90        if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
  91                return at;
  92
  93        /* First see if we already have the sign-off by the signer */
  94        while ((cp = strstr(cp, signed_off_by))) {
  95
  96                has_signoff = 1;
  97
  98                cp += strlen(signed_off_by);
  99                if (cp + signoff_len >= buf + at)
 100                        break;
 101                if (strncmp(cp, signoff, signoff_len))
 102                        continue;
 103                if (!isspace(cp[signoff_len]))
 104                        continue;
 105                /* we already have him */
 106                return at;
 107        }
 108
 109        if (!has_signoff)
 110                has_signoff = detect_any_signoff(buf, at);
 111
 112        if (!has_signoff)
 113                buf[at++] = '\n';
 114
 115        strcpy(buf + at, signed_off_by);
 116        at += strlen(signed_off_by);
 117        strcpy(buf + at, signoff);
 118        at += signoff_len;
 119        buf[at++] = '\n';
 120        buf[at] = 0;
 121        return at;
 122}
 123
 124static unsigned int digits_in_number(unsigned int number)
 125{
 126        unsigned int i = 10, result = 1;
 127        while (i <= number) {
 128                i *= 10;
 129                result++;
 130        }
 131        return result;
 132}
 133
 134void show_log(struct rev_info *opt, const char *sep)
 135{
 136        static char this_header[16384];
 137        struct log_info *log = opt->loginfo;
 138        struct commit *commit = log->commit, *parent = log->parent;
 139        int abbrev = opt->diffopt.abbrev;
 140        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
 141        const char *extra;
 142        int len;
 143        const char *subject = NULL, *extra_headers = opt->extra_headers;
 144
 145        opt->loginfo = NULL;
 146        if (!opt->verbose_header) {
 147                if (opt->left_right) {
 148                        if (commit->object.flags & BOUNDARY)
 149                                putchar('-');
 150                        else if (commit->object.flags & SYMMETRIC_LEFT)
 151                                putchar('<');
 152                        else
 153                                putchar('>');
 154                }
 155                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
 156                if (opt->parents)
 157                        show_parents(commit, abbrev_commit);
 158                show_decorations(commit);
 159                putchar(opt->diffopt.line_termination);
 160                return;
 161        }
 162
 163        /*
 164         * The "oneline" format has several special cases:
 165         *  - The pretty-printed commit lacks a newline at the end
 166         *    of the buffer, but we do want to make sure that we
 167         *    have a newline there. If the separator isn't already
 168         *    a newline, add an extra one.
 169         *  - unlike other log messages, the one-line format does
 170         *    not have an empty line between entries.
 171         */
 172        extra = "";
 173        if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
 174                extra = "\n";
 175        if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
 176                putchar(opt->diffopt.line_termination);
 177        opt->shown_one = 1;
 178
 179        /*
 180         * Print header line of header..
 181         */
 182
 183        if (opt->commit_format == CMIT_FMT_EMAIL) {
 184                char *sha1 = sha1_to_hex(commit->object.sha1);
 185                if (opt->total > 0) {
 186                        static char buffer[64];
 187                        snprintf(buffer, sizeof(buffer),
 188                                        "Subject: [%s %0*d/%d] ",
 189                                        opt->subject_prefix,
 190                                        digits_in_number(opt->total),
 191                                        opt->nr, opt->total);
 192                        subject = buffer;
 193                } else if (opt->total == 0) {
 194                        static char buffer[256];
 195                        snprintf(buffer, sizeof(buffer),
 196                                        "Subject: [%s] ",
 197                                        opt->subject_prefix);
 198                        subject = buffer;
 199                } else {
 200                        subject = "Subject: ";
 201                }
 202
 203                printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
 204                if (opt->message_id)
 205                        printf("Message-Id: <%s>\n", opt->message_id);
 206                if (opt->ref_message_id)
 207                        printf("In-Reply-To: <%s>\nReferences: <%s>\n",
 208                               opt->ref_message_id, opt->ref_message_id);
 209                if (opt->mime_boundary) {
 210                        static char subject_buffer[1024];
 211                        static char buffer[1024];
 212                        snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 213                                 "%s"
 214                                 "MIME-Version: 1.0\n"
 215                                 "Content-Type: multipart/mixed;"
 216                                 " boundary=\"%s%s\"\n"
 217                                 "\n"
 218                                 "This is a multi-part message in MIME "
 219                                 "format.\n"
 220                                 "--%s%s\n"
 221                                 "Content-Type: text/plain; "
 222                                 "charset=UTF-8; format=fixed\n"
 223                                 "Content-Transfer-Encoding: 8bit\n\n",
 224                                 extra_headers ? extra_headers : "",
 225                                 mime_boundary_leader, opt->mime_boundary,
 226                                 mime_boundary_leader, opt->mime_boundary);
 227                        extra_headers = subject_buffer;
 228
 229                        snprintf(buffer, sizeof(buffer) - 1,
 230                                 "--%s%s\n"
 231                                 "Content-Type: text/x-patch;"
 232                                 " name=\"%s.diff\"\n"
 233                                 "Content-Transfer-Encoding: 8bit\n"
 234                                 "Content-Disposition: %s;"
 235                                 " filename=\"%s.diff\"\n\n",
 236                                 mime_boundary_leader, opt->mime_boundary,
 237                                 sha1,
 238                                 opt->no_inline ? "attachment" : "inline",
 239                                 sha1);
 240                        opt->diffopt.stat_sep = buffer;
 241                }
 242        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 243                fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
 244                      stdout);
 245                if (opt->commit_format != CMIT_FMT_ONELINE)
 246                        fputs("commit ", stdout);
 247                if (opt->left_right) {
 248                        if (commit->object.flags & BOUNDARY)
 249                                putchar('-');
 250                        else if (commit->object.flags & SYMMETRIC_LEFT)
 251                                putchar('<');
 252                        else
 253                                putchar('>');
 254                }
 255                fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
 256                      stdout);
 257                if (opt->parents)
 258                        show_parents(commit, abbrev_commit);
 259                if (parent)
 260                        printf(" (from %s)",
 261                               diff_unique_abbrev(parent->object.sha1,
 262                                                  abbrev_commit));
 263                show_decorations(commit);
 264                printf("%s",
 265                       diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
 266                putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
 267                if (opt->reflog_info) {
 268                        show_reflog_message(opt->reflog_info,
 269                                    opt->commit_format == CMIT_FMT_ONELINE,
 270                                    opt->relative_date);
 271                        if (opt->commit_format == CMIT_FMT_ONELINE) {
 272                                printf("%s", sep);
 273                                return;
 274                        }
 275                }
 276        }
 277
 278        /*
 279         * And then the pretty-printed message itself
 280         */
 281        len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
 282                                  sizeof(this_header), abbrev, subject,
 283                                  extra_headers, opt->relative_date);
 284
 285        if (opt->add_signoff)
 286                len = append_signoff(this_header, sizeof(this_header), len,
 287                                     opt->add_signoff);
 288        printf("%s%s%s", this_header, extra, sep);
 289}
 290
 291int log_tree_diff_flush(struct rev_info *opt)
 292{
 293        diffcore_std(&opt->diffopt);
 294
 295        if (diff_queue_is_empty()) {
 296                int saved_fmt = opt->diffopt.output_format;
 297                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 298                diff_flush(&opt->diffopt);
 299                opt->diffopt.output_format = saved_fmt;
 300                return 0;
 301        }
 302
 303        if (opt->loginfo && !opt->no_commit_id) {
 304                /* When showing a verbose header (i.e. log message),
 305                 * and not in --pretty=oneline format, we would want
 306                 * an extra newline between the end of log and the
 307                 * output for readability.
 308                 */
 309                show_log(opt, opt->diffopt.msg_sep);
 310                if (opt->verbose_header &&
 311                    opt->commit_format != CMIT_FMT_ONELINE) {
 312                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 313                        if ((pch & opt->diffopt.output_format) == pch)
 314                                printf("---");
 315                        putchar('\n');
 316                }
 317        }
 318        diff_flush(&opt->diffopt);
 319        return 1;
 320}
 321
 322static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 323{
 324        unsigned const char *sha1 = commit->object.sha1;
 325
 326        diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
 327        return !opt->loginfo;
 328}
 329
 330/*
 331 * Show the diff of a commit.
 332 *
 333 * Return true if we printed any log info messages
 334 */
 335static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 336{
 337        int showed_log;
 338        struct commit_list *parents;
 339        unsigned const char *sha1 = commit->object.sha1;
 340
 341        if (!opt->diff)
 342                return 0;
 343
 344        /* Root commit? */
 345        parents = commit->parents;
 346        if (!parents) {
 347                if (opt->show_root_diff) {
 348                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
 349                        log_tree_diff_flush(opt);
 350                }
 351                return !opt->loginfo;
 352        }
 353
 354        /* More than one parent? */
 355        if (parents && parents->next) {
 356                if (opt->ignore_merges)
 357                        return 0;
 358                else if (opt->combine_merges)
 359                        return do_diff_combined(opt, commit);
 360
 361                /* If we show individual diffs, show the parent info */
 362                log->parent = parents->item;
 363        }
 364
 365        showed_log = 0;
 366        for (;;) {
 367                struct commit *parent = parents->item;
 368
 369                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 370                log_tree_diff_flush(opt);
 371
 372                showed_log |= !opt->loginfo;
 373
 374                /* Set up the log info for the next parent, if any.. */
 375                parents = parents->next;
 376                if (!parents)
 377                        break;
 378                log->parent = parents->item;
 379                opt->loginfo = log;
 380        }
 381        return showed_log;
 382}
 383
 384int log_tree_commit(struct rev_info *opt, struct commit *commit)
 385{
 386        struct log_info log;
 387        int shown;
 388
 389        log.commit = commit;
 390        log.parent = NULL;
 391        opt->loginfo = &log;
 392
 393        shown = log_tree_diff(opt, commit, &log);
 394        if (!shown && opt->loginfo && opt->always_show_header) {
 395                log.parent = NULL;
 396                show_log(opt, "");
 397                shown = 1;
 398        }
 399        opt->loginfo = NULL;
 400        return shown;
 401}