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