builtin / fmt-merge-msg.con commit Merge branch 'rj/sh-setup-mswin-pwd' (d077649)
   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        } else {
  31                return git_default_config(key, value, cb);
  32        }
  33        return 0;
  34}
  35
  36/* merge data per repository where the merged tips came from */
  37struct src_data {
  38        struct string_list branch, tag, r_branch, generic;
  39        int head_status;
  40};
  41
  42struct origin_data {
  43        unsigned char sha1[20];
  44        unsigned is_local_branch:1;
  45};
  46
  47static void init_src_data(struct src_data *data)
  48{
  49        data->branch.strdup_strings = 1;
  50        data->tag.strdup_strings = 1;
  51        data->r_branch.strdup_strings = 1;
  52        data->generic.strdup_strings = 1;
  53}
  54
  55static struct string_list srcs = STRING_LIST_INIT_DUP;
  56static struct string_list origins = STRING_LIST_INIT_DUP;
  57
  58static int handle_line(char *line)
  59{
  60        int i, len = strlen(line);
  61        struct origin_data *origin_data;
  62        char *src, *origin;
  63        struct src_data *src_data;
  64        struct string_list_item *item;
  65        int pulling_head = 0;
  66
  67        if (len < 43 || line[40] != '\t')
  68                return 1;
  69
  70        if (!prefixcmp(line + 41, "not-for-merge"))
  71                return 0;
  72
  73        if (line[41] != '\t')
  74                return 2;
  75
  76        line[40] = 0;
  77        origin_data = xcalloc(1, sizeof(struct origin_data));
  78        i = get_sha1(line, origin_data->sha1);
  79        line[40] = '\t';
  80        if (i) {
  81                free(origin_data);
  82                return 3;
  83        }
  84
  85        if (line[len - 1] == '\n')
  86                line[len - 1] = 0;
  87        line += 42;
  88
  89        /*
  90         * At this point, line points at the beginning of comment e.g.
  91         * "branch 'frotz' of git://that/repository.git".
  92         * Find the repository name and point it with src.
  93         */
  94        src = strstr(line, " of ");
  95        if (src) {
  96                *src = 0;
  97                src += 4;
  98                pulling_head = 0;
  99        } else {
 100                src = line;
 101                pulling_head = 1;
 102        }
 103
 104        item = unsorted_string_list_lookup(&srcs, src);
 105        if (!item) {
 106                item = string_list_append(&srcs, src);
 107                item->util = xcalloc(1, sizeof(struct src_data));
 108                init_src_data(item->util);
 109        }
 110        src_data = item->util;
 111
 112        if (pulling_head) {
 113                origin = src;
 114                src_data->head_status |= 1;
 115        } else if (!prefixcmp(line, "branch ")) {
 116                origin_data->is_local_branch = 1;
 117                origin = line + 7;
 118                string_list_append(&src_data->branch, origin);
 119                src_data->head_status |= 2;
 120        } else if (!prefixcmp(line, "tag ")) {
 121                origin = line;
 122                string_list_append(&src_data->tag, origin + 4);
 123                src_data->head_status |= 2;
 124        } else if (!prefixcmp(line, "remote-tracking branch ")) {
 125                origin = line + strlen("remote-tracking branch ");
 126                string_list_append(&src_data->r_branch, origin);
 127                src_data->head_status |= 2;
 128        } else {
 129                origin = src;
 130                string_list_append(&src_data->generic, line);
 131                src_data->head_status |= 2;
 132        }
 133
 134        if (!strcmp(".", src) || !strcmp(src, origin)) {
 135                int len = strlen(origin);
 136                if (origin[0] == '\'' && origin[len - 1] == '\'')
 137                        origin = xmemdupz(origin + 1, len - 2);
 138        } else {
 139                char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
 140                sprintf(new_origin, "%s of %s", origin, src);
 141                origin = new_origin;
 142        }
 143        if (strcmp(".", src))
 144                origin_data->is_local_branch = 0;
 145        string_list_append(&origins, origin)->util = origin_data;
 146        return 0;
 147}
 148
 149static void print_joined(const char *singular, const char *plural,
 150                struct string_list *list, struct strbuf *out)
 151{
 152        if (list->nr == 0)
 153                return;
 154        if (list->nr == 1) {
 155                strbuf_addf(out, "%s%s", singular, list->items[0].string);
 156        } else {
 157                int i;
 158                strbuf_addstr(out, plural);
 159                for (i = 0; i < list->nr - 1; i++)
 160                        strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
 161                                    list->items[i].string);
 162                strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
 163        }
 164}
 165
 166static void add_branch_desc(struct strbuf *out, const char *name)
 167{
 168        struct strbuf desc = STRBUF_INIT;
 169
 170        if (!read_branch_desc(&desc, name)) {
 171                const char *bp = desc.buf;
 172                while (*bp) {
 173                        const char *ep = strchrnul(bp, '\n');
 174                        if (*ep)
 175                                ep++;
 176                        strbuf_addf(out, "  : %.*s", (int)(ep - bp), bp);
 177                        bp = ep;
 178                }
 179                if (out->buf[out->len - 1] != '\n')
 180                        strbuf_addch(out, '\n');
 181        }
 182        strbuf_release(&desc);
 183}
 184
 185#define util_as_integral(elem) ((intptr_t)((elem)->util))
 186
 187static void record_person(int which, struct string_list *people,
 188                          struct commit *commit)
 189{
 190        char name_buf[MAX_GITNAME], *name, *name_end;
 191        struct string_list_item *elem;
 192        const char *field = (which == 'a') ? "\nauthor " : "\ncommitter ";
 193
 194        name = strstr(commit->buffer, field);
 195        if (!name)
 196                return;
 197        name += strlen(field);
 198        name_end = strchrnul(name, '<');
 199        if (*name_end)
 200                name_end--;
 201        while (isspace(*name_end) && name <= name_end)
 202                name_end--;
 203        if (name_end < name || name + MAX_GITNAME <= name_end)
 204                return;
 205        memcpy(name_buf, name, name_end - name + 1);
 206        name_buf[name_end - name + 1] = '\0';
 207
 208        elem = string_list_lookup(people, name_buf);
 209        if (!elem) {
 210                elem = string_list_insert(people, name_buf);
 211                elem->util = (void *)0;
 212        }
 213        elem->util = (void*)(util_as_integral(elem) + 1);
 214}
 215
 216static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
 217{
 218        const struct string_list_item *a = a_, *b = b_;
 219        return util_as_integral(b) - util_as_integral(a);
 220}
 221
 222static void add_people_count(struct strbuf *out, struct string_list *people)
 223{
 224        if (people->nr == 1)
 225                strbuf_addf(out, "%s", people->items[0].string);
 226        else if (people->nr == 2)
 227                strbuf_addf(out, "%s (%d) and %s (%d)",
 228                            people->items[0].string,
 229                            (int)util_as_integral(&people->items[0]),
 230                            people->items[1].string,
 231                            (int)util_as_integral(&people->items[1]));
 232        else if (people->nr)
 233                strbuf_addf(out, "%s (%d) and others",
 234                            people->items[0].string,
 235                            (int)util_as_integral(&people->items[0]));
 236}
 237
 238static void credit_people(struct strbuf *out,
 239                          struct string_list *them,
 240                          int kind)
 241{
 242        const char *label;
 243        const char *me;
 244
 245        if (kind == 'a') {
 246                label = "\nBy ";
 247                me = git_author_info(IDENT_NO_DATE);
 248        } else {
 249                label = "\nvia ";
 250                me = git_committer_info(IDENT_NO_DATE);
 251        }
 252
 253        if (!them->nr ||
 254            (them->nr == 1 &&
 255             me &&
 256             (me = skip_prefix(me, them->items->string)) != NULL &&
 257             skip_prefix(me, " <")))
 258                return;
 259        strbuf_addstr(out, label);
 260        add_people_count(out, them);
 261}
 262
 263static void add_people_info(struct strbuf *out,
 264                            struct string_list *authors,
 265                            struct string_list *committers)
 266{
 267        if (authors->nr)
 268                qsort(authors->items,
 269                      authors->nr, sizeof(authors->items[0]),
 270                      cmp_string_list_util_as_integral);
 271        if (committers->nr)
 272                qsort(committers->items,
 273                      committers->nr, sizeof(committers->items[0]),
 274                      cmp_string_list_util_as_integral);
 275
 276        credit_people(out, authors, 'a');
 277        credit_people(out, committers, 'c');
 278}
 279
 280static void shortlog(const char *name,
 281                     struct origin_data *origin_data,
 282                     struct commit *head,
 283                     struct rev_info *rev, int limit,
 284                     struct strbuf *out)
 285{
 286        int i, count = 0;
 287        struct commit *commit;
 288        struct object *branch;
 289        struct string_list subjects = STRING_LIST_INIT_DUP;
 290        struct string_list authors = STRING_LIST_INIT_DUP;
 291        struct string_list committers = STRING_LIST_INIT_DUP;
 292        int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
 293        struct strbuf sb = STRBUF_INIT;
 294        const unsigned char *sha1 = origin_data->sha1;
 295
 296        branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
 297        if (!branch || branch->type != OBJ_COMMIT)
 298                return;
 299
 300        setup_revisions(0, NULL, rev, NULL);
 301        add_pending_object(rev, branch, name);
 302        add_pending_object(rev, &head->object, "^HEAD");
 303        head->object.flags |= UNINTERESTING;
 304        if (prepare_revision_walk(rev))
 305                die("revision walk setup failed");
 306        while ((commit = get_revision(rev)) != NULL) {
 307                struct pretty_print_context ctx = {0};
 308
 309                if (commit->parents && commit->parents->next) {
 310                        /* do not list a merge but count committer */
 311                        record_person('c', &committers, commit);
 312                        continue;
 313                }
 314                if (!count)
 315                        /* the 'tip' committer */
 316                        record_person('c', &committers, commit);
 317                record_person('a', &authors, commit);
 318                count++;
 319                if (subjects.nr > limit)
 320                        continue;
 321
 322                format_commit_message(commit, "%s", &sb, &ctx);
 323                strbuf_ltrim(&sb);
 324
 325                if (!sb.len)
 326                        string_list_append(&subjects,
 327                                           sha1_to_hex(commit->object.sha1));
 328                else
 329                        string_list_append(&subjects, strbuf_detach(&sb, NULL));
 330        }
 331
 332        add_people_info(out, &authors, &committers);
 333        if (count > limit)
 334                strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
 335        else
 336                strbuf_addf(out, "\n* %s:\n", name);
 337
 338        if (origin_data->is_local_branch && use_branch_desc)
 339                add_branch_desc(out, name);
 340
 341        for (i = 0; i < subjects.nr; i++)
 342                if (i >= limit)
 343                        strbuf_addf(out, "  ...\n");
 344                else
 345                        strbuf_addf(out, "  %s\n", subjects.items[i].string);
 346
 347        clear_commit_marks((struct commit *)branch, flags);
 348        clear_commit_marks(head, flags);
 349        free_commit_list(rev->commits);
 350        rev->commits = NULL;
 351        rev->pending.nr = 0;
 352
 353        string_list_clear(&authors, 0);
 354        string_list_clear(&committers, 0);
 355        string_list_clear(&subjects, 0);
 356}
 357
 358static void fmt_merge_msg_title(struct strbuf *out,
 359        const char *current_branch) {
 360        int i = 0;
 361        char *sep = "";
 362
 363        strbuf_addstr(out, "Merge ");
 364        for (i = 0; i < srcs.nr; i++) {
 365                struct src_data *src_data = srcs.items[i].util;
 366                const char *subsep = "";
 367
 368                strbuf_addstr(out, sep);
 369                sep = "; ";
 370
 371                if (src_data->head_status == 1) {
 372                        strbuf_addstr(out, srcs.items[i].string);
 373                        continue;
 374                }
 375                if (src_data->head_status == 3) {
 376                        subsep = ", ";
 377                        strbuf_addstr(out, "HEAD");
 378                }
 379                if (src_data->branch.nr) {
 380                        strbuf_addstr(out, subsep);
 381                        subsep = ", ";
 382                        print_joined("branch ", "branches ", &src_data->branch,
 383                                        out);
 384                }
 385                if (src_data->r_branch.nr) {
 386                        strbuf_addstr(out, subsep);
 387                        subsep = ", ";
 388                        print_joined("remote-tracking branch ", "remote-tracking branches ",
 389                                        &src_data->r_branch, out);
 390                }
 391                if (src_data->tag.nr) {
 392                        strbuf_addstr(out, subsep);
 393                        subsep = ", ";
 394                        print_joined("tag ", "tags ", &src_data->tag, out);
 395                }
 396                if (src_data->generic.nr) {
 397                        strbuf_addstr(out, subsep);
 398                        print_joined("commit ", "commits ", &src_data->generic,
 399                                        out);
 400                }
 401                if (strcmp(".", srcs.items[i].string))
 402                        strbuf_addf(out, " of %s", srcs.items[i].string);
 403        }
 404
 405        if (!strcmp("master", current_branch))
 406                strbuf_addch(out, '\n');
 407        else
 408                strbuf_addf(out, " into %s\n", current_branch);
 409}
 410
 411static void fmt_tag_signature(struct strbuf *tagbuf,
 412                              struct strbuf *sig,
 413                              const char *buf,
 414                              unsigned long len)
 415{
 416        const char *tag_body = strstr(buf, "\n\n");
 417        if (tag_body) {
 418                tag_body += 2;
 419                strbuf_add(tagbuf, tag_body, buf + len - tag_body);
 420        }
 421        strbuf_complete_line(tagbuf);
 422        strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
 423}
 424
 425static void fmt_merge_msg_sigs(struct strbuf *out)
 426{
 427        int i, tag_number = 0, first_tag = 0;
 428        struct strbuf tagbuf = STRBUF_INIT;
 429
 430        for (i = 0; i < origins.nr; i++) {
 431                unsigned char *sha1 = origins.items[i].util;
 432                enum object_type type;
 433                unsigned long size, len;
 434                char *buf = read_sha1_file(sha1, &type, &size);
 435                struct strbuf sig = STRBUF_INIT;
 436
 437                if (!buf || type != OBJ_TAG)
 438                        goto next;
 439                len = parse_signature(buf, size);
 440
 441                if (size == len)
 442                        ; /* merely annotated */
 443                else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
 444                        if (!sig.len)
 445                                strbuf_addstr(&sig, "gpg verification failed.\n");
 446                }
 447
 448                if (!tag_number++) {
 449                        fmt_tag_signature(&tagbuf, &sig, buf, len);
 450                        first_tag = i;
 451                } else {
 452                        if (tag_number == 2) {
 453                                struct strbuf tagline = STRBUF_INIT;
 454                                strbuf_addf(&tagline, "\n# %s\n",
 455                                            origins.items[first_tag].string);
 456                                strbuf_insert(&tagbuf, 0, tagline.buf,
 457                                              tagline.len);
 458                                strbuf_release(&tagline);
 459                        }
 460                        strbuf_addf(&tagbuf, "\n# %s\n",
 461                                    origins.items[i].string);
 462                        fmt_tag_signature(&tagbuf, &sig, buf, len);
 463                }
 464                strbuf_release(&sig);
 465        next:
 466                free(buf);
 467        }
 468        if (tagbuf.len) {
 469                strbuf_addch(out, '\n');
 470                strbuf_addbuf(out, &tagbuf);
 471        }
 472        strbuf_release(&tagbuf);
 473}
 474
 475int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 476                  struct fmt_merge_msg_opts *opts)
 477{
 478        int i = 0, pos = 0;
 479        unsigned char head_sha1[20];
 480        const char *current_branch;
 481        void *current_branch_to_free;
 482
 483        /* get current branch */
 484        current_branch = current_branch_to_free =
 485                resolve_refdup("HEAD", head_sha1, 1, NULL);
 486        if (!current_branch)
 487                die("No current branch");
 488        if (!prefixcmp(current_branch, "refs/heads/"))
 489                current_branch += 11;
 490
 491        /* get a line */
 492        while (pos < in->len) {
 493                int len;
 494                char *newline, *p = in->buf + pos;
 495
 496                newline = strchr(p, '\n');
 497                len = newline ? newline - p : strlen(p);
 498                pos += len + !!newline;
 499                i++;
 500                p[len] = 0;
 501                if (handle_line(p))
 502                        die ("Error in line %d: %.*s", i, len, p);
 503        }
 504
 505        if (opts->add_title && srcs.nr)
 506                fmt_merge_msg_title(out, current_branch);
 507
 508        if (origins.nr)
 509                fmt_merge_msg_sigs(out);
 510
 511        if (opts->shortlog_len) {
 512                struct commit *head;
 513                struct rev_info rev;
 514
 515                head = lookup_commit_or_die(head_sha1, "HEAD");
 516                init_revisions(&rev, NULL);
 517                rev.commit_format = CMIT_FMT_ONELINE;
 518                rev.ignore_merges = 1;
 519                rev.limited = 1;
 520
 521                if (suffixcmp(out->buf, "\n"))
 522                        strbuf_addch(out, '\n');
 523
 524                for (i = 0; i < origins.nr; i++)
 525                        shortlog(origins.items[i].string,
 526                                 origins.items[i].util,
 527                                 head, &rev, opts->shortlog_len, out);
 528        }
 529
 530        strbuf_complete_line(out);
 531        free(current_branch_to_free);
 532        return 0;
 533}
 534
 535int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 536{
 537        const char *inpath = NULL;
 538        const char *message = NULL;
 539        int shortlog_len = -1;
 540        struct option options[] = {
 541                { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
 542                  "populate log with at most <n> entries from shortlog",
 543                  PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
 544                { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
 545                  "alias for --log (deprecated)",
 546                  PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
 547                  DEFAULT_MERGE_LOG_LEN },
 548                OPT_STRING('m', "message", &message, "text",
 549                        "use <text> as start of message"),
 550                OPT_FILENAME('F', "file", &inpath, "file to read from"),
 551                OPT_END()
 552        };
 553
 554        FILE *in = stdin;
 555        struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
 556        int ret;
 557        struct fmt_merge_msg_opts opts;
 558
 559        git_config(fmt_merge_msg_config, NULL);
 560        argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
 561                             0);
 562        if (argc > 0)
 563                usage_with_options(fmt_merge_msg_usage, options);
 564        if (shortlog_len < 0)
 565                shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
 566
 567        if (inpath && strcmp(inpath, "-")) {
 568                in = fopen(inpath, "r");
 569                if (!in)
 570                        die_errno("cannot open '%s'", inpath);
 571        }
 572
 573        if (strbuf_read(&input, fileno(in), 0) < 0)
 574                die_errno("could not read input file");
 575
 576        if (message)
 577                strbuf_addstr(&output, message);
 578
 579        memset(&opts, 0, sizeof(opts));
 580        opts.add_title = !message;
 581        opts.shortlog_len = shortlog_len;
 582
 583        ret = fmt_merge_msg(&input, &output, &opts);
 584        if (ret)
 585                return ret;
 586        write_in_full(STDOUT_FILENO, output.buf, output.len);
 587        return 0;
 588}