builtin / fmt-merge-msg.con commit status: add --column (323d053)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "commit.h"
   4#include "diff.h"
   5#include "revision.h"
   6#include "tag.h"
   7#include "string-list.h"
   8#include "branch.h"
   9#include "fmt-merge-msg.h"
  10#include "gpg-interface.h"
  11
  12static const char * const fmt_merge_msg_usage[] = {
  13        "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
  14        NULL
  15};
  16
  17static int use_branch_desc;
  18
  19int fmt_merge_msg_config(const char *key, const char *value, void *cb)
  20{
  21        if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
  22                int is_bool;
  23                merge_log_config = git_config_bool_or_int(key, value, &is_bool);
  24                if (!is_bool && merge_log_config < 0)
  25                        return error("%s: negative length %s", key, value);
  26                if (is_bool && merge_log_config)
  27                        merge_log_config = DEFAULT_MERGE_LOG_LEN;
  28        } else if (!strcmp(key, "merge.branchdesc")) {
  29                use_branch_desc = git_config_bool(key, value);
  30        }
  31        return 0;
  32}
  33
  34/* merge data per repository where the merged tips came from */
  35struct src_data {
  36        struct string_list branch, tag, r_branch, generic;
  37        int head_status;
  38};
  39
  40struct origin_data {
  41        unsigned char sha1[20];
  42        unsigned is_local_branch:1;
  43};
  44
  45static void init_src_data(struct src_data *data)
  46{
  47        data->branch.strdup_strings = 1;
  48        data->tag.strdup_strings = 1;
  49        data->r_branch.strdup_strings = 1;
  50        data->generic.strdup_strings = 1;
  51}
  52
  53static struct string_list srcs = STRING_LIST_INIT_DUP;
  54static struct string_list origins = STRING_LIST_INIT_DUP;
  55
  56static int handle_line(char *line)
  57{
  58        int i, len = strlen(line);
  59        struct origin_data *origin_data;
  60        char *src, *origin;
  61        struct src_data *src_data;
  62        struct string_list_item *item;
  63        int pulling_head = 0;
  64
  65        if (len < 43 || line[40] != '\t')
  66                return 1;
  67
  68        if (!prefixcmp(line + 41, "not-for-merge"))
  69                return 0;
  70
  71        if (line[41] != '\t')
  72                return 2;
  73
  74        line[40] = 0;
  75        origin_data = xcalloc(1, sizeof(struct origin_data));
  76        i = get_sha1(line, origin_data->sha1);
  77        line[40] = '\t';
  78        if (i) {
  79                free(origin_data);
  80                return 3;
  81        }
  82
  83        if (line[len - 1] == '\n')
  84                line[len - 1] = 0;
  85        line += 42;
  86
  87        /*
  88         * At this point, line points at the beginning of comment e.g.
  89         * "branch 'frotz' of git://that/repository.git".
  90         * Find the repository name and point it with src.
  91         */
  92        src = strstr(line, " of ");
  93        if (src) {
  94                *src = 0;
  95                src += 4;
  96                pulling_head = 0;
  97        } else {
  98                src = line;
  99                pulling_head = 1;
 100        }
 101
 102        item = unsorted_string_list_lookup(&srcs, src);
 103        if (!item) {
 104                item = string_list_append(&srcs, src);
 105                item->util = xcalloc(1, sizeof(struct src_data));
 106                init_src_data(item->util);
 107        }
 108        src_data = item->util;
 109
 110        if (pulling_head) {
 111                origin = src;
 112                src_data->head_status |= 1;
 113        } else if (!prefixcmp(line, "branch ")) {
 114                origin_data->is_local_branch = 1;
 115                origin = line + 7;
 116                string_list_append(&src_data->branch, origin);
 117                src_data->head_status |= 2;
 118        } else if (!prefixcmp(line, "tag ")) {
 119                origin = line;
 120                string_list_append(&src_data->tag, origin + 4);
 121                src_data->head_status |= 2;
 122        } else if (!prefixcmp(line, "remote-tracking branch ")) {
 123                origin = line + strlen("remote-tracking branch ");
 124                string_list_append(&src_data->r_branch, origin);
 125                src_data->head_status |= 2;
 126        } else {
 127                origin = src;
 128                string_list_append(&src_data->generic, line);
 129                src_data->head_status |= 2;
 130        }
 131
 132        if (!strcmp(".", src) || !strcmp(src, origin)) {
 133                int len = strlen(origin);
 134                if (origin[0] == '\'' && origin[len - 1] == '\'')
 135                        origin = xmemdupz(origin + 1, len - 2);
 136        } else {
 137                char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
 138                sprintf(new_origin, "%s of %s", origin, src);
 139                origin = new_origin;
 140        }
 141        if (strcmp(".", src))
 142                origin_data->is_local_branch = 0;
 143        string_list_append(&origins, origin)->util = origin_data;
 144        return 0;
 145}
 146
 147static void print_joined(const char *singular, const char *plural,
 148                struct string_list *list, struct strbuf *out)
 149{
 150        if (list->nr == 0)
 151                return;
 152        if (list->nr == 1) {
 153                strbuf_addf(out, "%s%s", singular, list->items[0].string);
 154        } else {
 155                int i;
 156                strbuf_addstr(out, plural);
 157                for (i = 0; i < list->nr - 1; i++)
 158                        strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
 159                                    list->items[i].string);
 160                strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
 161        }
 162}
 163
 164static void add_branch_desc(struct strbuf *out, const char *name)
 165{
 166        struct strbuf desc = STRBUF_INIT;
 167
 168        if (!read_branch_desc(&desc, name)) {
 169                const char *bp = desc.buf;
 170                while (*bp) {
 171                        const char *ep = strchrnul(bp, '\n');
 172                        if (*ep)
 173                                ep++;
 174                        strbuf_addf(out, "  : %.*s", (int)(ep - bp), bp);
 175                        bp = ep;
 176                }
 177                if (out->buf[out->len - 1] != '\n')
 178                        strbuf_addch(out, '\n');
 179        }
 180        strbuf_release(&desc);
 181}
 182
 183static void shortlog(const char *name,
 184                     struct origin_data *origin_data,
 185                     struct commit *head,
 186                     struct rev_info *rev, int limit,
 187                     struct strbuf *out)
 188{
 189        int i, count = 0;
 190        struct commit *commit;
 191        struct object *branch;
 192        struct string_list subjects = STRING_LIST_INIT_DUP;
 193        int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
 194        struct strbuf sb = STRBUF_INIT;
 195        const unsigned char *sha1 = origin_data->sha1;
 196
 197        branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
 198        if (!branch || branch->type != OBJ_COMMIT)
 199                return;
 200
 201        setup_revisions(0, NULL, rev, NULL);
 202        rev->ignore_merges = 1;
 203        add_pending_object(rev, branch, name);
 204        add_pending_object(rev, &head->object, "^HEAD");
 205        head->object.flags |= UNINTERESTING;
 206        if (prepare_revision_walk(rev))
 207                die("revision walk setup failed");
 208        while ((commit = get_revision(rev)) != NULL) {
 209                struct pretty_print_context ctx = {0};
 210
 211                /* ignore merges */
 212                if (commit->parents && commit->parents->next)
 213                        continue;
 214
 215                count++;
 216                if (subjects.nr > limit)
 217                        continue;
 218
 219                format_commit_message(commit, "%s", &sb, &ctx);
 220                strbuf_ltrim(&sb);
 221
 222                if (!sb.len)
 223                        string_list_append(&subjects,
 224                                           sha1_to_hex(commit->object.sha1));
 225                else
 226                        string_list_append(&subjects, strbuf_detach(&sb, NULL));
 227        }
 228
 229        if (count > limit)
 230                strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
 231        else
 232                strbuf_addf(out, "\n* %s:\n", name);
 233
 234        if (origin_data->is_local_branch && use_branch_desc)
 235                add_branch_desc(out, name);
 236
 237        for (i = 0; i < subjects.nr; i++)
 238                if (i >= limit)
 239                        strbuf_addf(out, "  ...\n");
 240                else
 241                        strbuf_addf(out, "  %s\n", subjects.items[i].string);
 242
 243        clear_commit_marks((struct commit *)branch, flags);
 244        clear_commit_marks(head, flags);
 245        free_commit_list(rev->commits);
 246        rev->commits = NULL;
 247        rev->pending.nr = 0;
 248
 249        string_list_clear(&subjects, 0);
 250}
 251
 252static void fmt_merge_msg_title(struct strbuf *out,
 253        const char *current_branch) {
 254        int i = 0;
 255        char *sep = "";
 256
 257        strbuf_addstr(out, "Merge ");
 258        for (i = 0; i < srcs.nr; i++) {
 259                struct src_data *src_data = srcs.items[i].util;
 260                const char *subsep = "";
 261
 262                strbuf_addstr(out, sep);
 263                sep = "; ";
 264
 265                if (src_data->head_status == 1) {
 266                        strbuf_addstr(out, srcs.items[i].string);
 267                        continue;
 268                }
 269                if (src_data->head_status == 3) {
 270                        subsep = ", ";
 271                        strbuf_addstr(out, "HEAD");
 272                }
 273                if (src_data->branch.nr) {
 274                        strbuf_addstr(out, subsep);
 275                        subsep = ", ";
 276                        print_joined("branch ", "branches ", &src_data->branch,
 277                                        out);
 278                }
 279                if (src_data->r_branch.nr) {
 280                        strbuf_addstr(out, subsep);
 281                        subsep = ", ";
 282                        print_joined("remote-tracking branch ", "remote-tracking branches ",
 283                                        &src_data->r_branch, out);
 284                }
 285                if (src_data->tag.nr) {
 286                        strbuf_addstr(out, subsep);
 287                        subsep = ", ";
 288                        print_joined("tag ", "tags ", &src_data->tag, out);
 289                }
 290                if (src_data->generic.nr) {
 291                        strbuf_addstr(out, subsep);
 292                        print_joined("commit ", "commits ", &src_data->generic,
 293                                        out);
 294                }
 295                if (strcmp(".", srcs.items[i].string))
 296                        strbuf_addf(out, " of %s", srcs.items[i].string);
 297        }
 298
 299        if (!strcmp("master", current_branch))
 300                strbuf_addch(out, '\n');
 301        else
 302                strbuf_addf(out, " into %s\n", current_branch);
 303}
 304
 305static void fmt_tag_signature(struct strbuf *tagbuf,
 306                              struct strbuf *sig,
 307                              const char *buf,
 308                              unsigned long len)
 309{
 310        const char *tag_body = strstr(buf, "\n\n");
 311        if (tag_body) {
 312                tag_body += 2;
 313                strbuf_add(tagbuf, tag_body, buf + len - tag_body);
 314        }
 315        strbuf_complete_line(tagbuf);
 316        strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
 317}
 318
 319static void fmt_merge_msg_sigs(struct strbuf *out)
 320{
 321        int i, tag_number = 0, first_tag = 0;
 322        struct strbuf tagbuf = STRBUF_INIT;
 323
 324        for (i = 0; i < origins.nr; i++) {
 325                unsigned char *sha1 = origins.items[i].util;
 326                enum object_type type;
 327                unsigned long size, len;
 328                char *buf = read_sha1_file(sha1, &type, &size);
 329                struct strbuf sig = STRBUF_INIT;
 330
 331                if (!buf || type != OBJ_TAG)
 332                        goto next;
 333                len = parse_signature(buf, size);
 334
 335                if (size == len)
 336                        ; /* merely annotated */
 337                else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
 338                        if (!sig.len)
 339                                strbuf_addstr(&sig, "gpg verification failed.\n");
 340                }
 341
 342                if (!tag_number++) {
 343                        fmt_tag_signature(&tagbuf, &sig, buf, len);
 344                        first_tag = i;
 345                } else {
 346                        if (tag_number == 2) {
 347                                struct strbuf tagline = STRBUF_INIT;
 348                                strbuf_addf(&tagline, "\n# %s\n",
 349                                            origins.items[first_tag].string);
 350                                strbuf_insert(&tagbuf, 0, tagline.buf,
 351                                              tagline.len);
 352                                strbuf_release(&tagline);
 353                        }
 354                        strbuf_addf(&tagbuf, "\n# %s\n",
 355                                    origins.items[i].string);
 356                        fmt_tag_signature(&tagbuf, &sig, buf, len);
 357                }
 358                strbuf_release(&sig);
 359        next:
 360                free(buf);
 361        }
 362        if (tagbuf.len) {
 363                strbuf_addch(out, '\n');
 364                strbuf_addbuf(out, &tagbuf);
 365        }
 366        strbuf_release(&tagbuf);
 367}
 368
 369int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 370                  struct fmt_merge_msg_opts *opts)
 371{
 372        int i = 0, pos = 0;
 373        unsigned char head_sha1[20];
 374        const char *current_branch;
 375        void *current_branch_to_free;
 376
 377        /* get current branch */
 378        current_branch = current_branch_to_free =
 379                resolve_refdup("HEAD", head_sha1, 1, NULL);
 380        if (!current_branch)
 381                die("No current branch");
 382        if (!prefixcmp(current_branch, "refs/heads/"))
 383                current_branch += 11;
 384
 385        /* get a line */
 386        while (pos < in->len) {
 387                int len;
 388                char *newline, *p = in->buf + pos;
 389
 390                newline = strchr(p, '\n');
 391                len = newline ? newline - p : strlen(p);
 392                pos += len + !!newline;
 393                i++;
 394                p[len] = 0;
 395                if (handle_line(p))
 396                        die ("Error in line %d: %.*s", i, len, p);
 397        }
 398
 399        if (opts->add_title && srcs.nr)
 400                fmt_merge_msg_title(out, current_branch);
 401
 402        if (origins.nr)
 403                fmt_merge_msg_sigs(out);
 404
 405        if (opts->shortlog_len) {
 406                struct commit *head;
 407                struct rev_info rev;
 408
 409                head = lookup_commit_or_die(head_sha1, "HEAD");
 410                init_revisions(&rev, NULL);
 411                rev.commit_format = CMIT_FMT_ONELINE;
 412                rev.ignore_merges = 1;
 413                rev.limited = 1;
 414
 415                if (suffixcmp(out->buf, "\n"))
 416                        strbuf_addch(out, '\n');
 417
 418                for (i = 0; i < origins.nr; i++)
 419                        shortlog(origins.items[i].string,
 420                                 origins.items[i].util,
 421                                 head, &rev, opts->shortlog_len, out);
 422        }
 423
 424        strbuf_complete_line(out);
 425        free(current_branch_to_free);
 426        return 0;
 427}
 428
 429int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 430{
 431        const char *inpath = NULL;
 432        const char *message = NULL;
 433        int shortlog_len = -1;
 434        struct option options[] = {
 435                { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
 436                  "populate log with at most <n> entries from shortlog",
 437                  PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
 438                { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
 439                  "alias for --log (deprecated)",
 440                  PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
 441                  DEFAULT_MERGE_LOG_LEN },
 442                OPT_STRING('m', "message", &message, "text",
 443                        "use <text> as start of message"),
 444                OPT_FILENAME('F', "file", &inpath, "file to read from"),
 445                OPT_END()
 446        };
 447
 448        FILE *in = stdin;
 449        struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
 450        int ret;
 451        struct fmt_merge_msg_opts opts;
 452
 453        git_config(fmt_merge_msg_config, NULL);
 454        argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
 455                             0);
 456        if (argc > 0)
 457                usage_with_options(fmt_merge_msg_usage, options);
 458        if (shortlog_len < 0)
 459                shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
 460
 461        if (inpath && strcmp(inpath, "-")) {
 462                in = fopen(inpath, "r");
 463                if (!in)
 464                        die_errno("cannot open '%s'", inpath);
 465        }
 466
 467        if (strbuf_read(&input, fileno(in), 0) < 0)
 468                die_errno("could not read input file");
 469
 470        if (message)
 471                strbuf_addstr(&output, message);
 472
 473        memset(&opts, 0, sizeof(opts));
 474        opts.add_title = !message;
 475        opts.shortlog_len = shortlog_len;
 476
 477        ret = fmt_merge_msg(&input, &output, &opts);
 478        if (ret)
 479                return ret;
 480        write_in_full(STDOUT_FILENO, output.buf, output.len);
 481        return 0;
 482}