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