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