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