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