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