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