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