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