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