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