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