Merge branch 'bc/object-id'
[git/git.git] / builtin / fmt-merge-msg.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "refs.h"
5 #include "object-store.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "tag.h"
10 #include "string-list.h"
11 #include "branch.h"
12 #include "fmt-merge-msg.h"
13 #include "gpg-interface.h"
14
15 static const char * const fmt_merge_msg_usage[] = {
16 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
17 NULL
18 };
19
20 static int use_branch_desc;
21
22 int fmt_merge_msg_config(const char *key, const char *value, void *cb)
23 {
24 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
25 int is_bool;
26 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
27 if (!is_bool && merge_log_config < 0)
28 return error("%s: negative length %s", key, value);
29 if (is_bool && merge_log_config)
30 merge_log_config = DEFAULT_MERGE_LOG_LEN;
31 } else if (!strcmp(key, "merge.branchdesc")) {
32 use_branch_desc = git_config_bool(key, value);
33 } else {
34 return git_default_config(key, value, cb);
35 }
36 return 0;
37 }
38
39 /* merge data per repository where the merged tips came from */
40 struct src_data {
41 struct string_list branch, tag, r_branch, generic;
42 int head_status;
43 };
44
45 struct origin_data {
46 struct object_id oid;
47 unsigned is_local_branch:1;
48 };
49
50 static void init_src_data(struct src_data *data)
51 {
52 data->branch.strdup_strings = 1;
53 data->tag.strdup_strings = 1;
54 data->r_branch.strdup_strings = 1;
55 data->generic.strdup_strings = 1;
56 }
57
58 static struct string_list srcs = STRING_LIST_INIT_DUP;
59 static struct string_list origins = STRING_LIST_INIT_DUP;
60
61 struct merge_parents {
62 int alloc, nr;
63 struct merge_parent {
64 struct object_id given;
65 struct object_id commit;
66 unsigned char used;
67 } *item;
68 };
69
70 /*
71 * I know, I know, this is inefficient, but you won't be pulling and merging
72 * hundreds of heads at a time anyway.
73 */
74 static struct merge_parent *find_merge_parent(struct merge_parents *table,
75 struct object_id *given,
76 struct object_id *commit)
77 {
78 int i;
79 for (i = 0; i < table->nr; i++) {
80 if (given && oidcmp(&table->item[i].given, given))
81 continue;
82 if (commit && oidcmp(&table->item[i].commit, commit))
83 continue;
84 return &table->item[i];
85 }
86 return NULL;
87 }
88
89 static void add_merge_parent(struct merge_parents *table,
90 struct object_id *given,
91 struct object_id *commit)
92 {
93 if (table->nr && find_merge_parent(table, given, commit))
94 return;
95 ALLOC_GROW(table->item, table->nr + 1, table->alloc);
96 oidcpy(&table->item[table->nr].given, given);
97 oidcpy(&table->item[table->nr].commit, commit);
98 table->item[table->nr].used = 0;
99 table->nr++;
100 }
101
102 static int handle_line(char *line, struct merge_parents *merge_parents)
103 {
104 int i, len = strlen(line);
105 struct origin_data *origin_data;
106 char *src;
107 const char *origin;
108 struct src_data *src_data;
109 struct string_list_item *item;
110 int pulling_head = 0;
111 struct object_id oid;
112 const unsigned hexsz = the_hash_algo->hexsz;
113
114 if (len < hexsz + 3 || line[hexsz] != '\t')
115 return 1;
116
117 if (starts_with(line + hexsz + 1, "not-for-merge"))
118 return 0;
119
120 if (line[hexsz + 1] != '\t')
121 return 2;
122
123 i = get_oid_hex(line, &oid);
124 if (i)
125 return 3;
126
127 if (!find_merge_parent(merge_parents, &oid, NULL))
128 return 0; /* subsumed by other parents */
129
130 origin_data = xcalloc(1, sizeof(struct origin_data));
131 oidcpy(&origin_data->oid, &oid);
132
133 if (line[len - 1] == '\n')
134 line[len - 1] = 0;
135 line += hexsz + 2;
136
137 /*
138 * At this point, line points at the beginning of comment e.g.
139 * "branch 'frotz' of git://that/repository.git".
140 * Find the repository name and point it with src.
141 */
142 src = strstr(line, " of ");
143 if (src) {
144 *src = 0;
145 src += 4;
146 pulling_head = 0;
147 } else {
148 src = line;
149 pulling_head = 1;
150 }
151
152 item = unsorted_string_list_lookup(&srcs, src);
153 if (!item) {
154 item = string_list_append(&srcs, src);
155 item->util = xcalloc(1, sizeof(struct src_data));
156 init_src_data(item->util);
157 }
158 src_data = item->util;
159
160 if (pulling_head) {
161 origin = src;
162 src_data->head_status |= 1;
163 } else if (starts_with(line, "branch ")) {
164 origin_data->is_local_branch = 1;
165 origin = line + 7;
166 string_list_append(&src_data->branch, origin);
167 src_data->head_status |= 2;
168 } else if (starts_with(line, "tag ")) {
169 origin = line;
170 string_list_append(&src_data->tag, origin + 4);
171 src_data->head_status |= 2;
172 } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
173 string_list_append(&src_data->r_branch, origin);
174 src_data->head_status |= 2;
175 } else {
176 origin = src;
177 string_list_append(&src_data->generic, line);
178 src_data->head_status |= 2;
179 }
180
181 if (!strcmp(".", src) || !strcmp(src, origin)) {
182 int len = strlen(origin);
183 if (origin[0] == '\'' && origin[len - 1] == '\'')
184 origin = xmemdupz(origin + 1, len - 2);
185 } else
186 origin = xstrfmt("%s of %s", origin, src);
187 if (strcmp(".", src))
188 origin_data->is_local_branch = 0;
189 string_list_append(&origins, origin)->util = origin_data;
190 return 0;
191 }
192
193 static void print_joined(const char *singular, const char *plural,
194 struct string_list *list, struct strbuf *out)
195 {
196 if (list->nr == 0)
197 return;
198 if (list->nr == 1) {
199 strbuf_addf(out, "%s%s", singular, list->items[0].string);
200 } else {
201 int i;
202 strbuf_addstr(out, plural);
203 for (i = 0; i < list->nr - 1; i++)
204 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
205 list->items[i].string);
206 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
207 }
208 }
209
210 static void add_branch_desc(struct strbuf *out, const char *name)
211 {
212 struct strbuf desc = STRBUF_INIT;
213
214 if (!read_branch_desc(&desc, name)) {
215 const char *bp = desc.buf;
216 while (*bp) {
217 const char *ep = strchrnul(bp, '\n');
218 if (*ep)
219 ep++;
220 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
221 bp = ep;
222 }
223 strbuf_complete_line(out);
224 }
225 strbuf_release(&desc);
226 }
227
228 #define util_as_integral(elem) ((intptr_t)((elem)->util))
229
230 static void record_person_from_buf(int which, struct string_list *people,
231 const char *buffer)
232 {
233 char *name_buf, *name, *name_end;
234 struct string_list_item *elem;
235 const char *field;
236
237 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
238 name = strstr(buffer, field);
239 if (!name)
240 return;
241 name += strlen(field);
242 name_end = strchrnul(name, '<');
243 if (*name_end)
244 name_end--;
245 while (isspace(*name_end) && name <= name_end)
246 name_end--;
247 if (name_end < name)
248 return;
249 name_buf = xmemdupz(name, name_end - name + 1);
250
251 elem = string_list_lookup(people, name_buf);
252 if (!elem) {
253 elem = string_list_insert(people, name_buf);
254 elem->util = (void *)0;
255 }
256 elem->util = (void*)(util_as_integral(elem) + 1);
257 free(name_buf);
258 }
259
260
261 static void record_person(int which, struct string_list *people,
262 struct commit *commit)
263 {
264 const char *buffer = get_commit_buffer(commit, NULL);
265 record_person_from_buf(which, people, buffer);
266 unuse_commit_buffer(commit, buffer);
267 }
268
269 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
270 {
271 const struct string_list_item *a = a_, *b = b_;
272 return util_as_integral(b) - util_as_integral(a);
273 }
274
275 static void add_people_count(struct strbuf *out, struct string_list *people)
276 {
277 if (people->nr == 1)
278 strbuf_addstr(out, people->items[0].string);
279 else if (people->nr == 2)
280 strbuf_addf(out, "%s (%d) and %s (%d)",
281 people->items[0].string,
282 (int)util_as_integral(&people->items[0]),
283 people->items[1].string,
284 (int)util_as_integral(&people->items[1]));
285 else if (people->nr)
286 strbuf_addf(out, "%s (%d) and others",
287 people->items[0].string,
288 (int)util_as_integral(&people->items[0]));
289 }
290
291 static void credit_people(struct strbuf *out,
292 struct string_list *them,
293 int kind)
294 {
295 const char *label;
296 const char *me;
297
298 if (kind == 'a') {
299 label = "By";
300 me = git_author_info(IDENT_NO_DATE);
301 } else {
302 label = "Via";
303 me = git_committer_info(IDENT_NO_DATE);
304 }
305
306 if (!them->nr ||
307 (them->nr == 1 &&
308 me &&
309 skip_prefix(me, them->items->string, &me) &&
310 starts_with(me, " <")))
311 return;
312 strbuf_addf(out, "\n%c %s ", comment_line_char, label);
313 add_people_count(out, them);
314 }
315
316 static void add_people_info(struct strbuf *out,
317 struct string_list *authors,
318 struct string_list *committers)
319 {
320 QSORT(authors->items, authors->nr,
321 cmp_string_list_util_as_integral);
322 QSORT(committers->items, committers->nr,
323 cmp_string_list_util_as_integral);
324
325 credit_people(out, authors, 'a');
326 credit_people(out, committers, 'c');
327 }
328
329 static void shortlog(const char *name,
330 struct origin_data *origin_data,
331 struct commit *head,
332 struct rev_info *rev,
333 struct fmt_merge_msg_opts *opts,
334 struct strbuf *out)
335 {
336 int i, count = 0;
337 struct commit *commit;
338 struct object *branch;
339 struct string_list subjects = STRING_LIST_INIT_DUP;
340 struct string_list authors = STRING_LIST_INIT_DUP;
341 struct string_list committers = STRING_LIST_INIT_DUP;
342 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
343 struct strbuf sb = STRBUF_INIT;
344 const struct object_id *oid = &origin_data->oid;
345 int limit = opts->shortlog_len;
346
347 branch = deref_tag(parse_object(oid), oid_to_hex(oid), the_hash_algo->hexsz);
348 if (!branch || branch->type != OBJ_COMMIT)
349 return;
350
351 setup_revisions(0, NULL, rev, NULL);
352 add_pending_object(rev, branch, name);
353 add_pending_object(rev, &head->object, "^HEAD");
354 head->object.flags |= UNINTERESTING;
355 if (prepare_revision_walk(rev))
356 die("revision walk setup failed");
357 while ((commit = get_revision(rev)) != NULL) {
358 struct pretty_print_context ctx = {0};
359
360 if (commit->parents && commit->parents->next) {
361 /* do not list a merge but count committer */
362 if (opts->credit_people)
363 record_person('c', &committers, commit);
364 continue;
365 }
366 if (!count && opts->credit_people)
367 /* the 'tip' committer */
368 record_person('c', &committers, commit);
369 if (opts->credit_people)
370 record_person('a', &authors, commit);
371 count++;
372 if (subjects.nr > limit)
373 continue;
374
375 format_commit_message(commit, "%s", &sb, &ctx);
376 strbuf_ltrim(&sb);
377
378 if (!sb.len)
379 string_list_append(&subjects,
380 oid_to_hex(&commit->object.oid));
381 else
382 string_list_append_nodup(&subjects,
383 strbuf_detach(&sb, NULL));
384 }
385
386 if (opts->credit_people)
387 add_people_info(out, &authors, &committers);
388 if (count > limit)
389 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
390 else
391 strbuf_addf(out, "\n* %s:\n", name);
392
393 if (origin_data->is_local_branch && use_branch_desc)
394 add_branch_desc(out, name);
395
396 for (i = 0; i < subjects.nr; i++)
397 if (i >= limit)
398 strbuf_addstr(out, " ...\n");
399 else
400 strbuf_addf(out, " %s\n", subjects.items[i].string);
401
402 clear_commit_marks((struct commit *)branch, flags);
403 clear_commit_marks(head, flags);
404 free_commit_list(rev->commits);
405 rev->commits = NULL;
406 rev->pending.nr = 0;
407
408 string_list_clear(&authors, 0);
409 string_list_clear(&committers, 0);
410 string_list_clear(&subjects, 0);
411 }
412
413 static void fmt_merge_msg_title(struct strbuf *out,
414 const char *current_branch)
415 {
416 int i = 0;
417 char *sep = "";
418
419 strbuf_addstr(out, "Merge ");
420 for (i = 0; i < srcs.nr; i++) {
421 struct src_data *src_data = srcs.items[i].util;
422 const char *subsep = "";
423
424 strbuf_addstr(out, sep);
425 sep = "; ";
426
427 if (src_data->head_status == 1) {
428 strbuf_addstr(out, srcs.items[i].string);
429 continue;
430 }
431 if (src_data->head_status == 3) {
432 subsep = ", ";
433 strbuf_addstr(out, "HEAD");
434 }
435 if (src_data->branch.nr) {
436 strbuf_addstr(out, subsep);
437 subsep = ", ";
438 print_joined("branch ", "branches ", &src_data->branch,
439 out);
440 }
441 if (src_data->r_branch.nr) {
442 strbuf_addstr(out, subsep);
443 subsep = ", ";
444 print_joined("remote-tracking branch ", "remote-tracking branches ",
445 &src_data->r_branch, out);
446 }
447 if (src_data->tag.nr) {
448 strbuf_addstr(out, subsep);
449 subsep = ", ";
450 print_joined("tag ", "tags ", &src_data->tag, out);
451 }
452 if (src_data->generic.nr) {
453 strbuf_addstr(out, subsep);
454 print_joined("commit ", "commits ", &src_data->generic,
455 out);
456 }
457 if (strcmp(".", srcs.items[i].string))
458 strbuf_addf(out, " of %s", srcs.items[i].string);
459 }
460
461 if (!strcmp("master", current_branch))
462 strbuf_addch(out, '\n');
463 else
464 strbuf_addf(out, " into %s\n", current_branch);
465 }
466
467 static void fmt_tag_signature(struct strbuf *tagbuf,
468 struct strbuf *sig,
469 const char *buf,
470 unsigned long len)
471 {
472 const char *tag_body = strstr(buf, "\n\n");
473 if (tag_body) {
474 tag_body += 2;
475 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
476 }
477 strbuf_complete_line(tagbuf);
478 if (sig->len) {
479 strbuf_addch(tagbuf, '\n');
480 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
481 }
482 }
483
484 static void fmt_merge_msg_sigs(struct strbuf *out)
485 {
486 int i, tag_number = 0, first_tag = 0;
487 struct strbuf tagbuf = STRBUF_INIT;
488
489 for (i = 0; i < origins.nr; i++) {
490 struct object_id *oid = origins.items[i].util;
491 enum object_type type;
492 unsigned long size, len;
493 char *buf = read_object_file(oid, &type, &size);
494 struct strbuf sig = STRBUF_INIT;
495
496 if (!buf || type != OBJ_TAG)
497 goto next;
498 len = parse_signature(buf, size);
499
500 if (size == len)
501 ; /* merely annotated */
502 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {
503 if (!sig.len)
504 strbuf_addstr(&sig, "gpg verification failed.\n");
505 }
506
507 if (!tag_number++) {
508 fmt_tag_signature(&tagbuf, &sig, buf, len);
509 first_tag = i;
510 } else {
511 if (tag_number == 2) {
512 struct strbuf tagline = STRBUF_INIT;
513 strbuf_addch(&tagline, '\n');
514 strbuf_add_commented_lines(&tagline,
515 origins.items[first_tag].string,
516 strlen(origins.items[first_tag].string));
517 strbuf_insert(&tagbuf, 0, tagline.buf,
518 tagline.len);
519 strbuf_release(&tagline);
520 }
521 strbuf_addch(&tagbuf, '\n');
522 strbuf_add_commented_lines(&tagbuf,
523 origins.items[i].string,
524 strlen(origins.items[i].string));
525 fmt_tag_signature(&tagbuf, &sig, buf, len);
526 }
527 strbuf_release(&sig);
528 next:
529 free(buf);
530 }
531 if (tagbuf.len) {
532 strbuf_addch(out, '\n');
533 strbuf_addbuf(out, &tagbuf);
534 }
535 strbuf_release(&tagbuf);
536 }
537
538 static void find_merge_parents(struct merge_parents *result,
539 struct strbuf *in, struct object_id *head)
540 {
541 struct commit_list *parents;
542 struct commit *head_commit;
543 int pos = 0, i, j;
544
545 parents = NULL;
546 while (pos < in->len) {
547 int len;
548 char *p = in->buf + pos;
549 char *newline = strchr(p, '\n');
550 const char *q;
551 struct object_id oid;
552 struct commit *parent;
553 struct object *obj;
554
555 len = newline ? newline - p : strlen(p);
556 pos += len + !!newline;
557
558 if (parse_oid_hex(p, &oid, &q) ||
559 q[0] != '\t' ||
560 q[1] != '\t')
561 continue; /* skip not-for-merge */
562 /*
563 * Do not use get_merge_parent() here; we do not have
564 * "name" here and we do not want to contaminate its
565 * util field yet.
566 */
567 obj = parse_object(&oid);
568 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
569 if (!parent)
570 continue;
571 commit_list_insert(parent, &parents);
572 add_merge_parent(result, &obj->oid, &parent->object.oid);
573 }
574 head_commit = lookup_commit(head);
575 if (head_commit)
576 commit_list_insert(head_commit, &parents);
577 reduce_heads_replace(&parents);
578
579 while (parents) {
580 struct commit *cmit = pop_commit(&parents);
581 for (i = 0; i < result->nr; i++)
582 if (!oidcmp(&result->item[i].commit, &cmit->object.oid))
583 result->item[i].used = 1;
584 }
585
586 for (i = j = 0; i < result->nr; i++) {
587 if (result->item[i].used) {
588 if (i != j)
589 result->item[j] = result->item[i];
590 j++;
591 }
592 }
593 result->nr = j;
594 }
595
596 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
597 struct fmt_merge_msg_opts *opts)
598 {
599 int i = 0, pos = 0;
600 struct object_id head_oid;
601 const char *current_branch;
602 void *current_branch_to_free;
603 struct merge_parents merge_parents;
604
605 memset(&merge_parents, 0, sizeof(merge_parents));
606
607 /* get current branch */
608 current_branch = current_branch_to_free =
609 resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL);
610 if (!current_branch)
611 die("No current branch");
612 if (starts_with(current_branch, "refs/heads/"))
613 current_branch += 11;
614
615 find_merge_parents(&merge_parents, in, &head_oid);
616
617 /* get a line */
618 while (pos < in->len) {
619 int len;
620 char *newline, *p = in->buf + pos;
621
622 newline = strchr(p, '\n');
623 len = newline ? newline - p : strlen(p);
624 pos += len + !!newline;
625 i++;
626 p[len] = 0;
627 if (handle_line(p, &merge_parents))
628 die ("Error in line %d: %.*s", i, len, p);
629 }
630
631 if (opts->add_title && srcs.nr)
632 fmt_merge_msg_title(out, current_branch);
633
634 if (origins.nr)
635 fmt_merge_msg_sigs(out);
636
637 if (opts->shortlog_len) {
638 struct commit *head;
639 struct rev_info rev;
640
641 head = lookup_commit_or_die(&head_oid, "HEAD");
642 init_revisions(&rev, NULL);
643 rev.commit_format = CMIT_FMT_ONELINE;
644 rev.ignore_merges = 1;
645 rev.limited = 1;
646
647 strbuf_complete_line(out);
648
649 for (i = 0; i < origins.nr; i++)
650 shortlog(origins.items[i].string,
651 origins.items[i].util,
652 head, &rev, opts, out);
653 }
654
655 strbuf_complete_line(out);
656 free(current_branch_to_free);
657 free(merge_parents.item);
658 return 0;
659 }
660
661 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
662 {
663 const char *inpath = NULL;
664 const char *message = NULL;
665 int shortlog_len = -1;
666 struct option options[] = {
667 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
668 N_("populate log with at most <n> entries from shortlog"),
669 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
670 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
671 N_("alias for --log (deprecated)"),
672 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
673 DEFAULT_MERGE_LOG_LEN },
674 OPT_STRING('m', "message", &message, N_("text"),
675 N_("use <text> as start of message")),
676 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
677 OPT_END()
678 };
679
680 FILE *in = stdin;
681 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
682 int ret;
683 struct fmt_merge_msg_opts opts;
684
685 git_config(fmt_merge_msg_config, NULL);
686 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
687 0);
688 if (argc > 0)
689 usage_with_options(fmt_merge_msg_usage, options);
690 if (shortlog_len < 0)
691 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
692
693 if (inpath && strcmp(inpath, "-")) {
694 in = fopen(inpath, "r");
695 if (!in)
696 die_errno("cannot open '%s'", inpath);
697 }
698
699 if (strbuf_read(&input, fileno(in), 0) < 0)
700 die_errno("could not read input file");
701
702 if (message)
703 strbuf_addstr(&output, message);
704
705 memset(&opts, 0, sizeof(opts));
706 opts.add_title = !message;
707 opts.credit_people = 1;
708 opts.shortlog_len = shortlog_len;
709
710 ret = fmt_merge_msg(&input, &output, &opts);
711 if (ret)
712 return ret;
713 write_in_full(STDOUT_FILENO, output.buf, output.len);
714 return 0;
715 }