a2bccd6ea4ac14441a6657bc1882cd180ec52b80
   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
   9static const char * const fmt_merge_msg_usage[] = {
  10        "git fmt-merge-msg [--log|--no-log] [--file <file>]",
  11        NULL
  12};
  13
  14static int merge_summary;
  15
  16static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
  17{
  18        static int found_merge_log = 0;
  19        if (!strcmp("merge.log", key)) {
  20                found_merge_log = 1;
  21                merge_summary = git_config_bool(key, value);
  22        }
  23        if (!found_merge_log && !strcmp("merge.summary", key))
  24                merge_summary = git_config_bool(key, value);
  25        return 0;
  26}
  27
  28struct src_data {
  29        struct string_list branch, tag, r_branch, generic;
  30        int head_status;
  31};
  32
  33void init_src_data(struct src_data *data)
  34{
  35        data->branch.strdup_strings = 1;
  36        data->tag.strdup_strings = 1;
  37        data->r_branch.strdup_strings = 1;
  38        data->generic.strdup_strings = 1;
  39}
  40
  41static struct string_list srcs = { NULL, 0, 0, 1 };
  42static struct string_list origins = { NULL, 0, 0, 1 };
  43
  44static int handle_line(char *line)
  45{
  46        int i, len = strlen(line);
  47        unsigned char *sha1;
  48        char *src, *origin;
  49        struct src_data *src_data;
  50        struct string_list_item *item;
  51        int pulling_head = 0;
  52
  53        if (len < 43 || line[40] != '\t')
  54                return 1;
  55
  56        if (!prefixcmp(line + 41, "not-for-merge"))
  57                return 0;
  58
  59        if (line[41] != '\t')
  60                return 2;
  61
  62        line[40] = 0;
  63        sha1 = xmalloc(20);
  64        i = get_sha1(line, sha1);
  65        line[40] = '\t';
  66        if (i)
  67                return 3;
  68
  69        if (line[len - 1] == '\n')
  70                line[len - 1] = 0;
  71        line += 42;
  72
  73        src = strstr(line, " of ");
  74        if (src) {
  75                *src = 0;
  76                src += 4;
  77                pulling_head = 0;
  78        } else {
  79                src = line;
  80                pulling_head = 1;
  81        }
  82
  83        item = unsorted_string_list_lookup(&srcs, src);
  84        if (!item) {
  85                item = string_list_append(src, &srcs);
  86                item->util = xcalloc(1, sizeof(struct src_data));
  87                init_src_data(item->util);
  88        }
  89        src_data = item->util;
  90
  91        if (pulling_head) {
  92                origin = src;
  93                src_data->head_status |= 1;
  94        } else if (!prefixcmp(line, "branch ")) {
  95                origin = line + 7;
  96                string_list_append(origin, &src_data->branch);
  97                src_data->head_status |= 2;
  98        } else if (!prefixcmp(line, "tag ")) {
  99                origin = line;
 100                string_list_append(origin + 4, &src_data->tag);
 101                src_data->head_status |= 2;
 102        } else if (!prefixcmp(line, "remote branch ")) {
 103                origin = line + 14;
 104                string_list_append(origin, &src_data->r_branch);
 105                src_data->head_status |= 2;
 106        } else {
 107                origin = src;
 108                string_list_append(line, &src_data->generic);
 109                src_data->head_status |= 2;
 110        }
 111
 112        if (!strcmp(".", src) || !strcmp(src, origin)) {
 113                int len = strlen(origin);
 114                if (origin[0] == '\'' && origin[len - 1] == '\'')
 115                        origin = xmemdupz(origin + 1, len - 2);
 116        } else {
 117                char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
 118                sprintf(new_origin, "%s of %s", origin, src);
 119                origin = new_origin;
 120        }
 121        string_list_append(origin, &origins)->util = sha1;
 122        return 0;
 123}
 124
 125static void print_joined(const char *singular, const char *plural,
 126                struct string_list *list, struct strbuf *out)
 127{
 128        if (list->nr == 0)
 129                return;
 130        if (list->nr == 1) {
 131                strbuf_addf(out, "%s%s", singular, list->items[0].string);
 132        } else {
 133                int i;
 134                strbuf_addstr(out, plural);
 135                for (i = 0; i < list->nr - 1; i++)
 136                        strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
 137                                    list->items[i].string);
 138                strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
 139        }
 140}
 141
 142static void shortlog(const char *name, unsigned char *sha1,
 143                struct commit *head, struct rev_info *rev, int limit,
 144                struct strbuf *out)
 145{
 146        int i, count = 0;
 147        struct commit *commit;
 148        struct object *branch;
 149        struct string_list subjects = { NULL, 0, 0, 1 };
 150        int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
 151        struct strbuf sb = STRBUF_INIT;
 152
 153        branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
 154        if (!branch || branch->type != OBJ_COMMIT)
 155                return;
 156
 157        setup_revisions(0, NULL, rev, NULL);
 158        rev->ignore_merges = 1;
 159        add_pending_object(rev, branch, name);
 160        add_pending_object(rev, &head->object, "^HEAD");
 161        head->object.flags |= UNINTERESTING;
 162        if (prepare_revision_walk(rev))
 163                die("revision walk setup failed");
 164        while ((commit = get_revision(rev)) != NULL) {
 165                struct pretty_print_context ctx = {0};
 166
 167                /* ignore merges */
 168                if (commit->parents && commit->parents->next)
 169                        continue;
 170
 171                count++;
 172                if (subjects.nr > limit)
 173                        continue;
 174
 175                format_commit_message(commit, "%s", &sb, &ctx);
 176                strbuf_ltrim(&sb);
 177
 178                if (!sb.len)
 179                        string_list_append(sha1_to_hex(commit->object.sha1),
 180                                           &subjects);
 181                else
 182                        string_list_append(strbuf_detach(&sb, NULL), &subjects);
 183        }
 184
 185        if (count > limit)
 186                strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
 187        else
 188                strbuf_addf(out, "\n* %s:\n", name);
 189
 190        for (i = 0; i < subjects.nr; i++)
 191                if (i >= limit)
 192                        strbuf_addf(out, "  ...\n");
 193                else
 194                        strbuf_addf(out, "  %s\n", subjects.items[i].string);
 195
 196        clear_commit_marks((struct commit *)branch, flags);
 197        clear_commit_marks(head, flags);
 198        free_commit_list(rev->commits);
 199        rev->commits = NULL;
 200        rev->pending.nr = 0;
 201
 202        string_list_clear(&subjects, 0);
 203}
 204
 205static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
 206        struct strbuf *out) {
 207        int limit = 20, i = 0, pos = 0;
 208        char *sep = "";
 209        unsigned char head_sha1[20];
 210        const char *current_branch;
 211
 212        /* get current branch */
 213        current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
 214        if (!current_branch)
 215                die("No current branch");
 216        if (!prefixcmp(current_branch, "refs/heads/"))
 217                current_branch += 11;
 218
 219        /* get a line */
 220        while (pos < in->len) {
 221                int len;
 222                char *newline, *p = in->buf + pos;
 223
 224                newline = strchr(p, '\n');
 225                len = newline ? newline - p : strlen(p);
 226                pos += len + !!newline;
 227                i++;
 228                p[len] = 0;
 229                if (handle_line(p))
 230                        die ("Error in line %d: %.*s", i, len, p);
 231        }
 232
 233        if (!srcs.nr)
 234                return 0;
 235
 236        strbuf_addstr(out, "Merge ");
 237        for (i = 0; i < srcs.nr; i++) {
 238                struct src_data *src_data = srcs.items[i].util;
 239                const char *subsep = "";
 240
 241                strbuf_addstr(out, sep);
 242                sep = "; ";
 243
 244                if (src_data->head_status == 1) {
 245                        strbuf_addstr(out, srcs.items[i].string);
 246                        continue;
 247                }
 248                if (src_data->head_status == 3) {
 249                        subsep = ", ";
 250                        strbuf_addstr(out, "HEAD");
 251                }
 252                if (src_data->branch.nr) {
 253                        strbuf_addstr(out, subsep);
 254                        subsep = ", ";
 255                        print_joined("branch ", "branches ", &src_data->branch,
 256                                        out);
 257                }
 258                if (src_data->r_branch.nr) {
 259                        strbuf_addstr(out, subsep);
 260                        subsep = ", ";
 261                        print_joined("remote branch ", "remote branches ",
 262                                        &src_data->r_branch, out);
 263                }
 264                if (src_data->tag.nr) {
 265                        strbuf_addstr(out, subsep);
 266                        subsep = ", ";
 267                        print_joined("tag ", "tags ", &src_data->tag, out);
 268                }
 269                if (src_data->generic.nr) {
 270                        strbuf_addstr(out, subsep);
 271                        print_joined("commit ", "commits ", &src_data->generic,
 272                                        out);
 273                }
 274                if (strcmp(".", srcs.items[i].string))
 275                        strbuf_addf(out, " of %s", srcs.items[i].string);
 276        }
 277
 278        if (!strcmp("master", current_branch))
 279                strbuf_addch(out, '\n');
 280        else
 281                strbuf_addf(out, " into %s\n", current_branch);
 282
 283        if (merge_summary) {
 284                struct commit *head;
 285                struct rev_info rev;
 286
 287                head = lookup_commit(head_sha1);
 288                init_revisions(&rev, NULL);
 289                rev.commit_format = CMIT_FMT_ONELINE;
 290                rev.ignore_merges = 1;
 291                rev.limited = 1;
 292
 293                for (i = 0; i < origins.nr; i++)
 294                        shortlog(origins.items[i].string, origins.items[i].util,
 295                                        head, &rev, limit, out);
 296        }
 297        return 0;
 298}
 299
 300int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
 301        return do_fmt_merge_msg(merge_summary, in, out);
 302}
 303
 304int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 305{
 306        const char *inpath = NULL;
 307        struct option options[] = {
 308                OPT_BOOLEAN(0, "log",     &merge_summary, "populate log with the shortlog"),
 309                { OPTION_BOOLEAN, 0, "summary", &merge_summary, NULL,
 310                  "alias for --log (deprecated)",
 311                  PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
 312                OPT_FILENAME('F', "file", &inpath, "file to read from"),
 313                OPT_END()
 314        };
 315
 316        FILE *in = stdin;
 317        struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
 318        int ret;
 319
 320        git_config(fmt_merge_msg_config, NULL);
 321        argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
 322                             0);
 323        if (argc > 0)
 324                usage_with_options(fmt_merge_msg_usage, options);
 325
 326        if (inpath && strcmp(inpath, "-")) {
 327                in = fopen(inpath, "r");
 328                if (!in)
 329                        die_errno("cannot open '%s'", inpath);
 330        }
 331
 332        if (strbuf_read(&input, fileno(in), 0) < 0)
 333                die_errno("could not read input file");
 334        ret = fmt_merge_msg(merge_summary, &input, &output);
 335        if (ret)
 336                return ret;
 337        write_in_full(STDOUT_FILENO, output.buf, output.len);
 338        return 0;
 339}