432963db9006c404d4b79c7e9b5c8d539e783605
   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 (!strncmp(line + 41, "not-for-merge", 13))
  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, strdup(src),
 115                                xcalloc(1, sizeof(struct src_data)));
 116        }
 117        src_data = srcs.payload[i];
 118
 119        if (pulling_head) {
 120                origin = strdup(src);
 121                src_data->head_status |= 1;
 122        } else if (!strncmp(line, "branch ", 7)) {
 123                origin = strdup(line + 7);
 124                append_to_list(&src_data->branch, origin, NULL);
 125                src_data->head_status |= 2;
 126        } else if (!strncmp(line, "tag ", 4)) {
 127                origin = line;
 128                append_to_list(&src_data->tag, strdup(origin + 4), NULL);
 129                src_data->head_status |= 2;
 130        } else if (!strncmp(line, "remote branch ", 14)) {
 131                origin = strdup(line + 14);
 132                append_to_list(&src_data->r_branch, origin, NULL);
 133                src_data->head_status |= 2;
 134        } else {
 135                origin = strdup(src);
 136                append_to_list(&src_data->generic, strdup(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                        char *new_origin = xmalloc(len - 1);
 144                        memcpy(new_origin, origin + 1, len - 2);
 145                        new_origin[len - 2] = 0;
 146                        origin = new_origin;
 147                } else
 148                        origin = strdup(origin);
 149        } else {
 150                char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
 151                sprintf(new_origin, "%s of %s", origin, src);
 152                origin = new_origin;
 153        }
 154        append_to_list(&origins, origin, sha1);
 155        return 0;
 156}
 157
 158static void print_joined(const char *singular, const char *plural,
 159                struct list *list)
 160{
 161        if (list->nr == 0)
 162                return;
 163        if (list->nr == 1) {
 164                printf("%s%s", singular, list->list[0]);
 165        } else {
 166                int i;
 167                printf("%s", plural);
 168                for (i = 0; i < list->nr - 1; i++)
 169                        printf("%s%s", i > 0 ? ", " : "", list->list[i]);
 170                printf(" and %s", list->list[list->nr - 1]);
 171        }
 172}
 173
 174static void shortlog(const char *name, unsigned char *sha1,
 175                struct commit *head, struct rev_info *rev, int limit)
 176{
 177        int i, count = 0;
 178        struct commit *commit;
 179        struct object *branch;
 180        struct list subjects = { NULL, NULL, 0, 0 };
 181        int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;
 182
 183        branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
 184        if (!branch || branch->type != OBJ_COMMIT)
 185                return;
 186
 187        setup_revisions(0, NULL, rev, NULL);
 188        rev->ignore_merges = 1;
 189        add_pending_object(rev, branch, name);
 190        add_pending_object(rev, &head->object, "^HEAD");
 191        head->object.flags |= UNINTERESTING;
 192        prepare_revision_walk(rev);
 193        while ((commit = get_revision(rev)) != NULL) {
 194                char *oneline, *bol, *eol;
 195
 196                /* ignore merges */
 197                if (commit->parents && commit->parents->next)
 198                        continue;
 199
 200                count++;
 201                if (subjects.nr > limit)
 202                        continue;
 203
 204                bol = strstr(commit->buffer, "\n\n");
 205                if (!bol) {
 206                        append_to_list(&subjects, strdup(sha1_to_hex(
 207                                                        commit->object.sha1)),
 208                                        NULL);
 209                        continue;
 210                }
 211
 212                bol += 2;
 213                eol = strchr(bol, '\n');
 214
 215                if (eol) {
 216                        int len = eol - bol;
 217                        oneline = xmalloc(len + 1);
 218                        memcpy(oneline, bol, len);
 219                        oneline[len] = 0;
 220                } else
 221                        oneline = strdup(bol);
 222                append_to_list(&subjects, oneline, NULL);
 223        }
 224
 225        if (count > limit)
 226                printf("\n* %s: (%d commits)\n", name, count);
 227        else
 228                printf("\n* %s:\n", name);
 229
 230        for (i = 0; i < subjects.nr; i++)
 231                if (i >= limit)
 232                        printf("  ...\n");
 233                else
 234                        printf("  %s\n", subjects.list[i]);
 235
 236        clear_commit_marks((struct commit *)branch, flags);
 237        clear_commit_marks(head, flags);
 238        free_commit_list(rev->commits);
 239        rev->commits = NULL;
 240        rev->pending.nr = 0;
 241
 242        free_list(&subjects);
 243}
 244
 245int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 246{
 247        int limit = 20, i = 0;
 248        char line[1024];
 249        FILE *in = stdin;
 250        const char *sep = "";
 251        unsigned char head_sha1[20];
 252        const char *head, *current_branch;
 253
 254        git_config(fmt_merge_msg_config);
 255
 256        while (argc > 1) {
 257                if (!strcmp(argv[1], "--summary"))
 258                        merge_summary = 1;
 259                else if (!strcmp(argv[1], "--no-summary"))
 260                        merge_summary = 0;
 261                else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
 262                        if (argc < 2)
 263                                die ("Which file?");
 264                        if (!strcmp(argv[2], "-"))
 265                                in = stdin;
 266                        else {
 267                                fclose(in);
 268                                in = fopen(argv[2], "r");
 269                        }
 270                        argc--; argv++;
 271                } else
 272                        break;
 273                argc--; argv++;
 274        }
 275
 276        if (argc > 1)
 277                usage(fmt_merge_msg_usage);
 278
 279        /* get current branch */
 280        head = strdup(git_path("HEAD"));
 281        current_branch = resolve_ref(head, head_sha1, 1);
 282        current_branch += strlen(head) - 4;
 283        free((char *)head);
 284        if (!strncmp(current_branch, "refs/heads/", 11))
 285                current_branch += 11;
 286
 287        while (fgets(line, sizeof(line), in)) {
 288                i++;
 289                if (line[0] == 0)
 290                        continue;
 291                if (handle_line(line))
 292                        die ("Error in line %d: %s", i, line);
 293        }
 294
 295        printf("Merge ");
 296        for (i = 0; i < srcs.nr; i++) {
 297                struct src_data *src_data = srcs.payload[i];
 298                const char *subsep = "";
 299
 300                printf(sep);
 301                sep = "; ";
 302
 303                if (src_data->head_status == 1) {
 304                        printf(srcs.list[i]);
 305                        continue;
 306                }
 307                if (src_data->head_status == 3) {
 308                        subsep = ", ";
 309                        printf("HEAD");
 310                }
 311                if (src_data->branch.nr) {
 312                        printf(subsep);
 313                        subsep = ", ";
 314                        print_joined("branch ", "branches ", &src_data->branch);
 315                }
 316                if (src_data->r_branch.nr) {
 317                        printf(subsep);
 318                        subsep = ", ";
 319                        print_joined("remote branch ", "remote branches ",
 320                                        &src_data->r_branch);
 321                }
 322                if (src_data->tag.nr) {
 323                        printf(subsep);
 324                        subsep = ", ";
 325                        print_joined("tag ", "tags ", &src_data->tag);
 326                }
 327                if (src_data->generic.nr) {
 328                        printf(subsep);
 329                        print_joined("commit ", "commits ", &src_data->generic);
 330                }
 331                if (strcmp(".", srcs.list[i]))
 332                        printf(" of %s", srcs.list[i]);
 333        }
 334
 335        if (!strcmp("master", current_branch))
 336                putchar('\n');
 337        else
 338                printf(" into %s\n", current_branch);
 339
 340        if (merge_summary) {
 341                struct commit *head;
 342                struct rev_info rev;
 343
 344                head = lookup_commit(head_sha1);
 345                init_revisions(&rev, prefix);
 346                rev.commit_format = CMIT_FMT_ONELINE;
 347                rev.ignore_merges = 1;
 348                rev.limited = 1;
 349
 350                for (i = 0; i < origins.nr; i++)
 351                        shortlog(origins.list[i], origins.payload[i],
 352                                        head, &rev, limit);
 353        }
 354
 355        /* No cleanup yet; is standalone anyway */
 356
 357        return 0;
 358}
 359