Merge branch 'ss/hashmap-typofix'
[alt-git.git] / fmt-merge-msg.c
blob24cc44bdbc9070d699447e10fc3e03bd8b71965f
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 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);
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;
40 } else {
41 return git_default_config(key, value, cb);
43 return 0;
46 /* merge data per repository where the merged tips came from */
47 struct src_data {
48 struct string_list branch, tag, r_branch, generic;
49 int head_status;
52 struct origin_data {
53 struct object_id oid;
54 unsigned is_local_branch:1;
57 static void init_src_data(struct src_data *data)
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;
65 static struct string_list srcs = STRING_LIST_INIT_DUP;
66 static struct string_list origins = STRING_LIST_INIT_DUP;
68 struct 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;
78 * I know, I know, this is inefficient, but you won't be pulling and merging
79 * hundreds of heads at a time anyway.
81 static struct merge_parent *find_merge_parent(struct merge_parents *table,
82 struct object_id *given,
83 struct object_id *commit)
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];
93 return NULL;
96 static void add_merge_parent(struct merge_parents *table,
97 struct object_id *given,
98 struct object_id *commit)
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++;
109 static int handle_line(char *line, struct merge_parents *merge_parents)
111 int i, len = strlen(line);
112 struct origin_data *origin_data;
113 char *src;
114 const char *origin, *tag_name;
115 char *to_free = NULL;
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;
122 if (len < hexsz + 3 || line[hexsz] != '\t')
123 return 1;
125 if (starts_with(line + hexsz + 1, "not-for-merge"))
126 return 0;
128 if (line[hexsz + 1] != '\t')
129 return 2;
131 i = get_oid_hex(line, &oid);
132 if (i)
133 return 3;
135 if (!find_merge_parent(merge_parents, &oid, NULL))
136 return 0; /* subsumed by other parents */
138 CALLOC_ARRAY(origin_data, 1);
139 oidcpy(&origin_data->oid, &oid);
141 if (line[len - 1] == '\n')
142 line[len - 1] = 0;
143 line += hexsz + 2;
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.
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;
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);
166 src_data = item->util;
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;
188 if (!strcmp(".", src) || !strcmp(src, origin)) {
189 int len = strlen(origin);
190 if (origin[0] == '\'' && origin[len - 1] == '\'')
191 origin = to_free = xmemdupz(origin + 1, len - 2);
192 } else
193 origin = to_free = xstrfmt("%s of %s", origin, src);
194 if (strcmp(".", src))
195 origin_data->is_local_branch = 0;
196 string_list_append(&origins, origin)->util = origin_data;
197 free(to_free);
198 return 0;
201 static void print_joined(const char *singular, const char *plural,
202 struct string_list *list, struct strbuf *out)
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);
218 static void add_branch_desc(struct strbuf *out, const char *name)
220 struct strbuf desc = STRBUF_INIT;
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;
231 strbuf_complete_line(out);
233 strbuf_release(&desc);
236 #define util_as_integral(elem) ((intptr_t)((elem)->util))
238 static void record_person_from_buf(int which, struct string_list *people,
239 const char *buffer)
241 char *name_buf, *name, *name_end;
242 struct string_list_item *elem;
243 const char *field;
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);
259 elem = string_list_lookup(people, name_buf);
260 if (!elem) {
261 elem = string_list_insert(people, name_buf);
262 elem->util = (void *)0;
264 elem->util = (void*)(util_as_integral(elem) + 1);
265 free(name_buf);
269 static void record_person(int which, struct string_list *people,
270 struct commit *commit)
272 const char *buffer = get_commit_buffer(commit, NULL);
273 record_person_from_buf(which, people, buffer);
274 unuse_commit_buffer(commit, buffer);
277 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
279 const struct string_list_item *a = a_, *b = b_;
280 return util_as_integral(b) - util_as_integral(a);
283 static void add_people_count(struct strbuf *out, struct string_list *people)
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]));
299 static void credit_people(struct strbuf *out,
300 struct string_list *them,
301 int kind)
303 const char *label;
304 const char *me;
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);
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);
324 static void add_people_info(struct strbuf *out,
325 struct string_list *authors,
326 struct string_list *committers)
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);
333 credit_people(out, authors, 'a');
334 credit_people(out, committers, 'c');
337 static 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)
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;
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;
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};
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;
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;
385 format_commit_message(commit, "%s", &sb, &ctx);
386 strbuf_ltrim(&sb);
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));
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);
403 if (origin_data->is_local_branch && use_branch_desc)
404 add_branch_desc(out, name);
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);
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;
418 string_list_clear(&authors, 0);
419 string_list_clear(&committers, 0);
420 string_list_clear(&subjects, 0);
424 * See if dest_branch matches with any glob pattern on the
425 * suppress_dest_patterns list.
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.
430 static int dest_suppressed(const char *dest_branch)
432 struct string_list_item *item;
434 for_each_string_list_item(item, &suppress_dest_patterns) {
435 if (!wildmatch(item->string, dest_branch, WM_PATHNAME))
436 return 1;
438 return 0;
441 static void fmt_merge_msg_title(struct strbuf *out,
442 const char *current_branch)
444 int i = 0;
445 char *sep = "";
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 = "";
452 strbuf_addstr(out, sep);
453 sep = "; ";
455 if (src_data->head_status == 1) {
456 strbuf_addstr(out, srcs.items[i].string);
457 continue;
459 if (src_data->head_status == 3) {
460 subsep = ", ";
461 strbuf_addstr(out, "HEAD");
463 if (src_data->branch.nr) {
464 strbuf_addstr(out, subsep);
465 subsep = ", ";
466 print_joined("branch ", "branches ", &src_data->branch,
467 out);
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);
475 if (src_data->tag.nr) {
476 strbuf_addstr(out, subsep);
477 subsep = ", ";
478 print_joined("tag ", "tags ", &src_data->tag, out);
480 if (src_data->generic.nr) {
481 strbuf_addstr(out, subsep);
482 print_joined("commit ", "commits ", &src_data->generic,
483 out);
485 if (strcmp(".", srcs.items[i].string))
486 strbuf_addf(out, " of %s", srcs.items[i].string);
489 if (!dest_suppressed(current_branch))
490 strbuf_addf(out, " into %s", current_branch);
491 strbuf_addch(out, '\n');
494 static void fmt_tag_signature(struct strbuf *tagbuf,
495 struct strbuf *sig,
496 const char *buf,
497 unsigned long len)
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);
504 strbuf_complete_line(tagbuf);
505 if (sig->len) {
506 strbuf_addch(tagbuf, '\n');
507 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
511 static void fmt_merge_msg_sigs(struct strbuf *out)
513 int i, tag_number = 0, first_tag = 0;
514 struct strbuf tagbuf = STRBUF_INIT;
516 for (i = 0; i < origins.nr; i++) {
517 struct object_id *oid = origins.items[i].util;
518 enum object_type type;
519 unsigned long size;
520 char *buf = read_object_file(oid, &type, &size);
521 char *origbuf = buf;
522 unsigned long len = size;
523 struct signature_check sigc = { NULL };
524 struct strbuf payload = STRBUF_INIT, sig = STRBUF_INIT;
526 if (!buf || type != OBJ_TAG)
527 goto next;
529 if (!parse_signature(buf, size, &payload, &sig))
530 ;/* merely annotated */
531 else {
532 buf = payload.buf;
533 len = payload.len;
534 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
535 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
536 if (check_signature(&sigc, sig.buf, sig.len) &&
537 !sigc.output)
538 strbuf_addstr(&sig, "gpg verification failed.\n");
539 else
540 strbuf_addstr(&sig, sigc.output);
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);
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);
563 strbuf_release(&payload);
564 strbuf_release(&sig);
565 signature_check_clear(&sigc);
566 next:
567 free(origbuf);
569 if (tagbuf.len) {
570 strbuf_addch(out, '\n');
571 strbuf_addbuf(out, &tagbuf);
573 strbuf_release(&tagbuf);
576 static void find_merge_parents(struct merge_parents *result,
577 struct strbuf *in, struct object_id *head)
579 struct commit_list *parents;
580 struct commit *head_commit;
581 int pos = 0, i, j;
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;
593 len = newline ? newline - p : strlen(p);
594 pos += len + !!newline;
596 if (parse_oid_hex(p, &oid, &q) ||
597 q[0] != '\t' ||
598 q[1] != '\t')
599 continue; /* skip not-for-merge */
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.
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);
612 head_commit = lookup_commit(the_repository, head);
613 if (head_commit)
614 commit_list_insert(head_commit, &parents);
615 reduce_heads_replace(&parents);
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;
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++;
631 result->nr = j;
635 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
636 struct fmt_merge_msg_opts *opts)
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;
644 if (!suppress_dest_pattern_seen) {
645 string_list_append(&suppress_dest_patterns, "main");
646 string_list_append(&suppress_dest_patterns, "master");
649 memset(&merge_parents, 0, sizeof(merge_parents));
651 /* learn the commit that we merge into and the current branch name */
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");
657 if (opts->into_name)
658 current_branch = opts->into_name;
659 else if (starts_with(current_branch, "refs/heads/"))
660 current_branch += 11;
662 find_merge_parents(&merge_parents, in, &head_oid);
664 /* get a line */
665 while (pos < in->len) {
666 int len;
667 char *newline, *p = in->buf + pos;
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);
678 if (opts->add_title && srcs.nr)
679 fmt_merge_msg_title(out, current_branch);
681 if (origins.nr)
682 fmt_merge_msg_sigs(out);
684 if (opts->shortlog_len) {
685 struct commit *head;
686 struct rev_info rev;
688 head = lookup_commit_or_die(&head_oid, "HEAD");
689 repo_init_revisions(the_repository, &rev, NULL);
690 rev.commit_format = CMIT_FMT_ONELINE;
691 diff_merges_suppress(&rev);
692 rev.limited = 1;
694 strbuf_complete_line(out);
696 for (i = 0; i < origins.nr; i++)
697 shortlog(origins.items[i].string,
698 origins.items[i].util,
699 head, &rev, opts, out);
700 release_revisions(&rev);
703 strbuf_complete_line(out);
704 free(current_branch_to_free);
705 free(merge_parents.item);
706 return 0;