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