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