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