Merge branch 'jc/doc-submitting-patches-choice-of-base'
[git/debian.git] / fmt-merge-msg.c
blobe5c0aff2bf684f2726a2628a80cedddfc1048e91
1 #include "config.h"
2 #include "refs.h"
3 #include "object-store.h"
4 #include "diff.h"
5 #include "diff-merges.h"
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 #include "gpg-interface.h"
14 static int use_branch_desc;
15 static int suppress_dest_pattern_seen;
16 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
18 int fmt_merge_msg_config(const char *key, const char *value, void *cb)
20 int status = 0;
22 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
23 int is_bool;
24 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
25 if (!is_bool && merge_log_config < 0)
26 return error("%s: negative length %s", key, value);
27 if (is_bool && merge_log_config)
28 merge_log_config = DEFAULT_MERGE_LOG_LEN;
29 } else if (!strcmp(key, "merge.branchdesc")) {
30 use_branch_desc = git_config_bool(key, value);
31 } else if (!strcmp(key, "merge.suppressdest")) {
32 if (!value)
33 return config_error_nonbool(key);
34 if (!*value)
35 string_list_clear(&suppress_dest_patterns, 0);
36 else
37 string_list_append(&suppress_dest_patterns, value);
38 suppress_dest_pattern_seen = 1;
39 } else {
40 status = git_gpg_config(key, value, NULL);
41 if (status)
42 return status;
43 return git_default_config(key, value, cb);
45 return 0;
48 /* merge data per repository where the merged tips came from */
49 struct src_data {
50 struct string_list branch, tag, r_branch, generic;
51 int head_status;
54 struct origin_data {
55 struct object_id oid;
56 unsigned is_local_branch:1;
59 static void init_src_data(struct src_data *data)
61 data->branch.strdup_strings = 1;
62 data->tag.strdup_strings = 1;
63 data->r_branch.strdup_strings = 1;
64 data->generic.strdup_strings = 1;
67 static struct string_list srcs = STRING_LIST_INIT_DUP;
68 static struct string_list origins = STRING_LIST_INIT_DUP;
70 struct merge_parents {
71 int alloc, nr;
72 struct merge_parent {
73 struct object_id given;
74 struct object_id commit;
75 unsigned char used;
76 } *item;
80 * I know, I know, this is inefficient, but you won't be pulling and merging
81 * hundreds of heads at a time anyway.
83 static struct merge_parent *find_merge_parent(struct merge_parents *table,
84 struct object_id *given,
85 struct object_id *commit)
87 int i;
88 for (i = 0; i < table->nr; i++) {
89 if (given && !oideq(&table->item[i].given, given))
90 continue;
91 if (commit && !oideq(&table->item[i].commit, commit))
92 continue;
93 return &table->item[i];
95 return NULL;
98 static void add_merge_parent(struct merge_parents *table,
99 struct object_id *given,
100 struct object_id *commit)
102 if (table->nr && find_merge_parent(table, given, commit))
103 return;
104 ALLOC_GROW(table->item, table->nr + 1, table->alloc);
105 oidcpy(&table->item[table->nr].given, given);
106 oidcpy(&table->item[table->nr].commit, commit);
107 table->item[table->nr].used = 0;
108 table->nr++;
111 static int handle_line(char *line, struct merge_parents *merge_parents)
113 int i, len = strlen(line);
114 struct origin_data *origin_data;
115 char *src;
116 const char *origin, *tag_name;
117 char *to_free = NULL;
118 struct src_data *src_data;
119 struct string_list_item *item;
120 int pulling_head = 0;
121 struct object_id oid;
122 const unsigned hexsz = the_hash_algo->hexsz;
124 if (len < hexsz + 3 || line[hexsz] != '\t')
125 return 1;
127 if (starts_with(line + hexsz + 1, "not-for-merge"))
128 return 0;
130 if (line[hexsz + 1] != '\t')
131 return 2;
133 i = get_oid_hex(line, &oid);
134 if (i)
135 return 3;
137 if (!find_merge_parent(merge_parents, &oid, NULL))
138 return 0; /* subsumed by other parents */
140 CALLOC_ARRAY(origin_data, 1);
141 oidcpy(&origin_data->oid, &oid);
143 if (line[len - 1] == '\n')
144 line[len - 1] = 0;
145 line += hexsz + 2;
148 * At this point, line points at the beginning of comment e.g.
149 * "branch 'frotz' of git://that/repository.git".
150 * Find the repository name and point it with src.
152 src = strstr(line, " of ");
153 if (src) {
154 *src = 0;
155 src += 4;
156 pulling_head = 0;
157 } else {
158 src = line;
159 pulling_head = 1;
162 item = unsorted_string_list_lookup(&srcs, src);
163 if (!item) {
164 item = string_list_append(&srcs, src);
165 item->util = xcalloc(1, sizeof(struct src_data));
166 init_src_data(item->util);
168 src_data = item->util;
170 if (pulling_head) {
171 origin = src;
172 src_data->head_status |= 1;
173 } else if (skip_prefix(line, "branch ", &origin)) {
174 origin_data->is_local_branch = 1;
175 string_list_append(&src_data->branch, origin);
176 src_data->head_status |= 2;
177 } else if (skip_prefix(line, "tag ", &tag_name)) {
178 origin = line;
179 string_list_append(&src_data->tag, tag_name);
180 src_data->head_status |= 2;
181 } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
182 string_list_append(&src_data->r_branch, origin);
183 src_data->head_status |= 2;
184 } else {
185 origin = src;
186 string_list_append(&src_data->generic, line);
187 src_data->head_status |= 2;
190 if (!strcmp(".", src) || !strcmp(src, origin)) {
191 int len = strlen(origin);
192 if (origin[0] == '\'' && origin[len - 1] == '\'')
193 origin = to_free = xmemdupz(origin + 1, len - 2);
194 } else
195 origin = to_free = xstrfmt("%s of %s", origin, src);
196 if (strcmp(".", src))
197 origin_data->is_local_branch = 0;
198 string_list_append(&origins, origin)->util = origin_data;
199 free(to_free);
200 return 0;
203 static void print_joined(const char *singular, const char *plural,
204 struct string_list *list, struct strbuf *out)
206 if (list->nr == 0)
207 return;
208 if (list->nr == 1) {
209 strbuf_addf(out, "%s%s", singular, list->items[0].string);
210 } else {
211 int i;
212 strbuf_addstr(out, plural);
213 for (i = 0; i < list->nr - 1; i++)
214 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
215 list->items[i].string);
216 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
220 static void add_branch_desc(struct strbuf *out, const char *name)
222 struct strbuf desc = STRBUF_INIT;
224 if (!read_branch_desc(&desc, name)) {
225 const char *bp = desc.buf;
226 while (*bp) {
227 const char *ep = strchrnul(bp, '\n');
228 if (*ep)
229 ep++;
230 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
231 bp = ep;
233 strbuf_complete_line(out);
235 strbuf_release(&desc);
238 #define util_as_integral(elem) ((intptr_t)((elem)->util))
240 static void record_person_from_buf(int which, struct string_list *people,
241 const char *buffer)
243 char *name_buf, *name, *name_end;
244 struct string_list_item *elem;
245 const char *field;
247 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
248 name = strstr(buffer, field);
249 if (!name)
250 return;
251 name += strlen(field);
252 name_end = strchrnul(name, '<');
253 if (*name_end)
254 name_end--;
255 while (isspace(*name_end) && name <= name_end)
256 name_end--;
257 if (name_end < name)
258 return;
259 name_buf = xmemdupz(name, name_end - name + 1);
261 elem = string_list_lookup(people, name_buf);
262 if (!elem) {
263 elem = string_list_insert(people, name_buf);
264 elem->util = (void *)0;
266 elem->util = (void*)(util_as_integral(elem) + 1);
267 free(name_buf);
271 static void record_person(int which, struct string_list *people,
272 struct commit *commit)
274 const char *buffer = get_commit_buffer(commit, NULL);
275 record_person_from_buf(which, people, buffer);
276 unuse_commit_buffer(commit, buffer);
279 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
281 const struct string_list_item *a = a_, *b = b_;
282 return util_as_integral(b) - util_as_integral(a);
285 static void add_people_count(struct strbuf *out, struct string_list *people)
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]));
301 static void credit_people(struct strbuf *out,
302 struct string_list *them,
303 int kind)
305 const char *label;
306 const char *me;
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);
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);
326 static void add_people_info(struct strbuf *out,
327 struct string_list *authors,
328 struct string_list *committers)
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);
335 credit_people(out, authors, 'a');
336 credit_people(out, committers, 'c');
339 static 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)
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;
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;
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};
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;
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;
387 format_commit_message(commit, "%s", &sb, &ctx);
388 strbuf_ltrim(&sb);
390 if (!sb.len)
391 string_list_append(&subjects,
392 oid_to_hex(&commit->object.oid));
393 else
394 string_list_append_nodup(&subjects,
395 strbuf_detach(&sb, NULL));
398 if (opts->credit_people)
399 add_people_info(out, &authors, &committers);
400 if (count > limit)
401 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
402 else
403 strbuf_addf(out, "\n* %s:\n", name);
405 if (origin_data->is_local_branch && use_branch_desc)
406 add_branch_desc(out, name);
408 for (i = 0; i < subjects.nr; i++)
409 if (i >= limit)
410 strbuf_addstr(out, " ...\n");
411 else
412 strbuf_addf(out, " %s\n", subjects.items[i].string);
414 clear_commit_marks((struct commit *)branch, flags);
415 clear_commit_marks(head, flags);
416 free_commit_list(rev->commits);
417 rev->commits = NULL;
418 rev->pending.nr = 0;
420 string_list_clear(&authors, 0);
421 string_list_clear(&committers, 0);
422 string_list_clear(&subjects, 0);
426 * See if dest_branch matches with any glob pattern on the
427 * suppress_dest_patterns list.
429 * We may want to also allow negative matches e.g. ":!glob" like we do
430 * for pathspec, but for now, let's keep it simple and stupid.
432 static int dest_suppressed(const char *dest_branch)
434 struct string_list_item *item;
436 for_each_string_list_item(item, &suppress_dest_patterns) {
437 if (!wildmatch(item->string, dest_branch, WM_PATHNAME))
438 return 1;
440 return 0;
443 static void fmt_merge_msg_title(struct strbuf *out,
444 const char *current_branch)
446 int i = 0;
447 char *sep = "";
449 strbuf_addstr(out, "Merge ");
450 for (i = 0; i < srcs.nr; i++) {
451 struct src_data *src_data = srcs.items[i].util;
452 const char *subsep = "";
454 strbuf_addstr(out, sep);
455 sep = "; ";
457 if (src_data->head_status == 1) {
458 strbuf_addstr(out, srcs.items[i].string);
459 continue;
461 if (src_data->head_status == 3) {
462 subsep = ", ";
463 strbuf_addstr(out, "HEAD");
465 if (src_data->branch.nr) {
466 strbuf_addstr(out, subsep);
467 subsep = ", ";
468 print_joined("branch ", "branches ", &src_data->branch,
469 out);
471 if (src_data->r_branch.nr) {
472 strbuf_addstr(out, subsep);
473 subsep = ", ";
474 print_joined("remote-tracking branch ", "remote-tracking branches ",
475 &src_data->r_branch, out);
477 if (src_data->tag.nr) {
478 strbuf_addstr(out, subsep);
479 subsep = ", ";
480 print_joined("tag ", "tags ", &src_data->tag, out);
482 if (src_data->generic.nr) {
483 strbuf_addstr(out, subsep);
484 print_joined("commit ", "commits ", &src_data->generic,
485 out);
487 if (strcmp(".", srcs.items[i].string))
488 strbuf_addf(out, " of %s", srcs.items[i].string);
491 if (!dest_suppressed(current_branch))
492 strbuf_addf(out, " into %s", current_branch);
493 strbuf_addch(out, '\n');
496 static void fmt_tag_signature(struct strbuf *tagbuf,
497 struct strbuf *sig,
498 const char *buf,
499 unsigned long len)
501 const char *tag_body = strstr(buf, "\n\n");
502 if (tag_body) {
503 tag_body += 2;
504 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
506 strbuf_complete_line(tagbuf);
507 if (sig->len) {
508 strbuf_addch(tagbuf, '\n');
509 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
513 static void fmt_merge_msg_sigs(struct strbuf *out)
515 int i, tag_number = 0, first_tag = 0;
516 struct strbuf tagbuf = STRBUF_INIT;
518 for (i = 0; i < origins.nr; i++) {
519 struct object_id *oid = origins.items[i].util;
520 enum object_type type;
521 unsigned long size;
522 char *buf = read_object_file(oid, &type, &size);
523 char *origbuf = buf;
524 unsigned long len = size;
525 struct signature_check sigc = { NULL };
526 struct strbuf payload = STRBUF_INIT, sig = STRBUF_INIT;
528 if (!buf || type != OBJ_TAG)
529 goto next;
531 if (!parse_signature(buf, size, &payload, &sig))
532 ;/* merely annotated */
533 else {
534 buf = payload.buf;
535 len = payload.len;
536 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
537 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
538 if (check_signature(&sigc, sig.buf, sig.len) &&
539 !sigc.output)
540 strbuf_addstr(&sig, "gpg verification failed.\n");
541 else
542 strbuf_addstr(&sig, sigc.output);
544 signature_check_clear(&sigc);
546 if (!tag_number++) {
547 fmt_tag_signature(&tagbuf, &sig, buf, len);
548 first_tag = i;
549 } else {
550 if (tag_number == 2) {
551 struct strbuf tagline = STRBUF_INIT;
552 strbuf_addch(&tagline, '\n');
553 strbuf_add_commented_lines(&tagline,
554 origins.items[first_tag].string,
555 strlen(origins.items[first_tag].string));
556 strbuf_insert(&tagbuf, 0, tagline.buf,
557 tagline.len);
558 strbuf_release(&tagline);
560 strbuf_addch(&tagbuf, '\n');
561 strbuf_add_commented_lines(&tagbuf,
562 origins.items[i].string,
563 strlen(origins.items[i].string));
564 fmt_tag_signature(&tagbuf, &sig, buf, len);
566 strbuf_release(&payload);
567 strbuf_release(&sig);
568 next:
569 free(origbuf);
571 if (tagbuf.len) {
572 strbuf_addch(out, '\n');
573 strbuf_addbuf(out, &tagbuf);
575 strbuf_release(&tagbuf);
578 static void find_merge_parents(struct merge_parents *result,
579 struct strbuf *in, struct object_id *head)
581 struct commit_list *parents;
582 struct commit *head_commit;
583 int pos = 0, i, j;
585 parents = NULL;
586 while (pos < in->len) {
587 int len;
588 char *p = in->buf + pos;
589 char *newline = strchr(p, '\n');
590 const char *q;
591 struct object_id oid;
592 struct commit *parent;
593 struct object *obj;
595 len = newline ? newline - p : strlen(p);
596 pos += len + !!newline;
598 if (parse_oid_hex(p, &oid, &q) ||
599 q[0] != '\t' ||
600 q[1] != '\t')
601 continue; /* skip not-for-merge */
603 * Do not use get_merge_parent() here; we do not have
604 * "name" here and we do not want to contaminate its
605 * util field yet.
607 obj = parse_object(the_repository, &oid);
608 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
609 if (!parent)
610 continue;
611 commit_list_insert(parent, &parents);
612 add_merge_parent(result, &obj->oid, &parent->object.oid);
614 head_commit = lookup_commit(the_repository, head);
615 if (head_commit)
616 commit_list_insert(head_commit, &parents);
617 reduce_heads_replace(&parents);
619 while (parents) {
620 struct commit *cmit = pop_commit(&parents);
621 for (i = 0; i < result->nr; i++)
622 if (oideq(&result->item[i].commit, &cmit->object.oid))
623 result->item[i].used = 1;
626 for (i = j = 0; i < result->nr; i++) {
627 if (result->item[i].used) {
628 if (i != j)
629 result->item[j] = result->item[i];
630 j++;
633 result->nr = j;
637 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
638 struct fmt_merge_msg_opts *opts)
640 int i = 0, pos = 0;
641 struct object_id head_oid;
642 const char *current_branch;
643 void *current_branch_to_free;
644 struct merge_parents merge_parents;
646 if (!suppress_dest_pattern_seen) {
647 string_list_append(&suppress_dest_patterns, "main");
648 string_list_append(&suppress_dest_patterns, "master");
651 memset(&merge_parents, 0, sizeof(merge_parents));
653 /* learn the commit that we merge into and the current branch name */
654 current_branch = current_branch_to_free =
655 resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL);
656 if (!current_branch)
657 die("No current branch");
659 if (opts->into_name)
660 current_branch = opts->into_name;
661 else if (starts_with(current_branch, "refs/heads/"))
662 current_branch += 11;
664 find_merge_parents(&merge_parents, in, &head_oid);
666 /* get a line */
667 while (pos < in->len) {
668 int len;
669 char *newline, *p = in->buf + pos;
671 newline = strchr(p, '\n');
672 len = newline ? newline - p : strlen(p);
673 pos += len + !!newline;
674 i++;
675 p[len] = 0;
676 if (handle_line(p, &merge_parents))
677 die("error in line %d: %.*s", i, len, p);
680 if (opts->add_title && srcs.nr)
681 fmt_merge_msg_title(out, current_branch);
683 if (origins.nr)
684 fmt_merge_msg_sigs(out);
686 if (opts->shortlog_len) {
687 struct commit *head;
688 struct rev_info rev;
690 head = lookup_commit_or_die(&head_oid, "HEAD");
691 repo_init_revisions(the_repository, &rev, NULL);
692 rev.commit_format = CMIT_FMT_ONELINE;
693 diff_merges_suppress(&rev);
694 rev.limited = 1;
696 strbuf_complete_line(out);
698 for (i = 0; i < origins.nr; i++)
699 shortlog(origins.items[i].string,
700 origins.items[i].util,
701 head, &rev, opts, out);
704 strbuf_complete_line(out);
705 free(current_branch_to_free);
706 free(merge_parents.item);
707 return 0;