7 #include "string-list.h"
9 #include "fmt-merge-msg.h"
10 #include "gpg-interface.h"
12 static const char * const fmt_merge_msg_usage[] = {
13 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
17 static int use_branch_desc;
19 int fmt_merge_msg_config(const char *key, const char *value, void *cb)
21 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
23 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
24 if (!is_bool && merge_log_config < 0)
25 return error("%s: negative length %s", key, value);
26 if (is_bool && merge_log_config)
27 merge_log_config = DEFAULT_MERGE_LOG_LEN;
28 } else if (!strcmp(key, "merge.branchdesc")) {
29 use_branch_desc = git_config_bool(key, value);
31 return git_default_config(key, value, cb);
36 /* merge data per repository where the merged tips came from */
38 struct string_list branch, tag, r_branch, generic;
43 unsigned char sha1[20];
44 unsigned is_local_branch:1;
47 static void init_src_data(struct src_data *data)
49 data->branch.strdup_strings = 1;
50 data->tag.strdup_strings = 1;
51 data->r_branch.strdup_strings = 1;
52 data->generic.strdup_strings = 1;
55 static struct string_list srcs = STRING_LIST_INIT_DUP;
56 static struct string_list origins = STRING_LIST_INIT_DUP;
58 struct merge_parents {
61 unsigned char given[20];
62 unsigned char commit[20];
68 * I know, I know, this is inefficient, but you won't be pulling and merging
69 * hundreds of heads at a time anyway.
71 static struct merge_parent *find_merge_parent(struct merge_parents *table,
73 unsigned char *commit)
76 for (i = 0; i < table->nr; i++) {
77 if (given && hashcmp(table->item[i].given, given))
79 if (commit && hashcmp(table->item[i].commit, commit))
81 return &table->item[i];
86 static void add_merge_parent(struct merge_parents *table,
88 unsigned char *commit)
90 if (table->nr && find_merge_parent(table, given, commit))
92 ALLOC_GROW(table->item, table->nr + 1, table->alloc);
93 hashcpy(table->item[table->nr].given, given);
94 hashcpy(table->item[table->nr].commit, commit);
95 table->item[table->nr].used = 0;
99 static int handle_line(char *line, struct merge_parents *merge_parents)
101 int i, len = strlen(line);
102 struct origin_data *origin_data;
105 struct src_data *src_data;
106 struct string_list_item *item;
107 int pulling_head = 0;
108 unsigned char sha1[20];
110 if (len < 43 || line[40] != '\t')
113 if (starts_with(line + 41, "not-for-merge"))
116 if (line[41] != '\t')
119 i = get_sha1_hex(line, sha1);
123 if (!find_merge_parent(merge_parents, sha1, NULL))
124 return 0; /* subsumed by other parents */
126 origin_data = xcalloc(1, sizeof(struct origin_data));
127 hashcpy(origin_data->sha1, sha1);
129 if (line[len - 1] == '\n')
134 * At this point, line points at the beginning of comment e.g.
135 * "branch 'frotz' of git://that/repository.git".
136 * Find the repository name and point it with src.
138 src = strstr(line, " of ");
148 item = unsorted_string_list_lookup(&srcs, src);
150 item = string_list_append(&srcs, src);
151 item->util = xcalloc(1, sizeof(struct src_data));
152 init_src_data(item->util);
154 src_data = item->util;
158 src_data->head_status |= 1;
159 } else if (starts_with(line, "branch ")) {
160 origin_data->is_local_branch = 1;
162 string_list_append(&src_data->branch, origin);
163 src_data->head_status |= 2;
164 } else if (starts_with(line, "tag ")) {
166 string_list_append(&src_data->tag, origin + 4);
167 src_data->head_status |= 2;
168 } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
169 string_list_append(&src_data->r_branch, origin);
170 src_data->head_status |= 2;
173 string_list_append(&src_data->generic, line);
174 src_data->head_status |= 2;
177 if (!strcmp(".", src) || !strcmp(src, origin)) {
178 int len = strlen(origin);
179 if (origin[0] == '\'' && origin[len - 1] == '\'')
180 origin = xmemdupz(origin + 1, len - 2);
182 origin = xstrfmt("%s of %s", origin, src);
183 if (strcmp(".", src))
184 origin_data->is_local_branch = 0;
185 string_list_append(&origins, origin)->util = origin_data;
189 static void print_joined(const char *singular, const char *plural,
190 struct string_list *list, struct strbuf *out)
195 strbuf_addf(out, "%s%s", singular, list->items[0].string);
198 strbuf_addstr(out, plural);
199 for (i = 0; i < list->nr - 1; i++)
200 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
201 list->items[i].string);
202 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
206 static void add_branch_desc(struct strbuf *out, const char *name)
208 struct strbuf desc = STRBUF_INIT;
210 if (!read_branch_desc(&desc, name)) {
211 const char *bp = desc.buf;
213 const char *ep = strchrnul(bp, '\n');
216 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
219 strbuf_complete_line(out);
221 strbuf_release(&desc);
224 #define util_as_integral(elem) ((intptr_t)((elem)->util))
226 static void record_person_from_buf(int which, struct string_list *people,
229 char *name_buf, *name, *name_end;
230 struct string_list_item *elem;
233 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
234 name = strstr(buffer, field);
237 name += strlen(field);
238 name_end = strchrnul(name, '<');
241 while (isspace(*name_end) && name <= name_end)
245 name_buf = xmemdupz(name, name_end - name + 1);
247 elem = string_list_lookup(people, name_buf);
249 elem = string_list_insert(people, name_buf);
250 elem->util = (void *)0;
252 elem->util = (void*)(util_as_integral(elem) + 1);
257 static void record_person(int which, struct string_list *people,
258 struct commit *commit)
260 const char *buffer = get_commit_buffer(commit, NULL);
261 record_person_from_buf(which, people, buffer);
262 unuse_commit_buffer(commit, buffer);
265 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
267 const struct string_list_item *a = a_, *b = b_;
268 return util_as_integral(b) - util_as_integral(a);
271 static void add_people_count(struct strbuf *out, struct string_list *people)
274 strbuf_addf(out, "%s", people->items[0].string);
275 else if (people->nr == 2)
276 strbuf_addf(out, "%s (%d) and %s (%d)",
277 people->items[0].string,
278 (int)util_as_integral(&people->items[0]),
279 people->items[1].string,
280 (int)util_as_integral(&people->items[1]));
282 strbuf_addf(out, "%s (%d) and others",
283 people->items[0].string,
284 (int)util_as_integral(&people->items[0]));
287 static void credit_people(struct strbuf *out,
288 struct string_list *them,
296 me = git_author_info(IDENT_NO_DATE);
299 me = git_committer_info(IDENT_NO_DATE);
305 skip_prefix(me, them->items->string, &me) &&
306 starts_with(me, " <")))
308 strbuf_addf(out, "\n%c %s ", comment_line_char, label);
309 add_people_count(out, them);
312 static void add_people_info(struct strbuf *out,
313 struct string_list *authors,
314 struct string_list *committers)
317 qsort(authors->items,
318 authors->nr, sizeof(authors->items[0]),
319 cmp_string_list_util_as_integral);
321 qsort(committers->items,
322 committers->nr, sizeof(committers->items[0]),
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,
332 struct rev_info *rev,
333 struct fmt_merge_msg_opts *opts,
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 unsigned char *sha1 = origin_data->sha1;
345 int limit = opts->shortlog_len;
347 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
348 if (!branch || branch->type != OBJ_COMMIT)
351 setup_revisions(0, NULL, rev, NULL);
352 add_pending_object(rev, branch, name);
353 add_pending_object(rev, &head->object, "^HEAD");
354 head->object.flags |= UNINTERESTING;
355 if (prepare_revision_walk(rev))
356 die("revision walk setup failed");
357 while ((commit = get_revision(rev)) != NULL) {
358 struct pretty_print_context ctx = {0};
360 if (commit->parents && commit->parents->next) {
361 /* do not list a merge but count committer */
362 if (opts->credit_people)
363 record_person('c', &committers, commit);
366 if (!count && opts->credit_people)
367 /* the 'tip' committer */
368 record_person('c', &committers, commit);
369 if (opts->credit_people)
370 record_person('a', &authors, commit);
372 if (subjects.nr > limit)
375 format_commit_message(commit, "%s", &sb, &ctx);
379 string_list_append(&subjects,
380 sha1_to_hex(commit->object.sha1));
382 string_list_append(&subjects, strbuf_detach(&sb, NULL));
385 if (opts->credit_people)
386 add_people_info(out, &authors, &committers);
388 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
390 strbuf_addf(out, "\n* %s:\n", name);
392 if (origin_data->is_local_branch && use_branch_desc)
393 add_branch_desc(out, name);
395 for (i = 0; i < subjects.nr; i++)
397 strbuf_addf(out, " ...\n");
399 strbuf_addf(out, " %s\n", subjects.items[i].string);
401 clear_commit_marks((struct commit *)branch, flags);
402 clear_commit_marks(head, flags);
403 free_commit_list(rev->commits);
407 string_list_clear(&authors, 0);
408 string_list_clear(&committers, 0);
409 string_list_clear(&subjects, 0);
412 static void fmt_merge_msg_title(struct strbuf *out,
413 const char *current_branch) {
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 = "";
422 strbuf_addstr(out, sep);
425 if (src_data->head_status == 1) {
426 strbuf_addstr(out, srcs.items[i].string);
429 if (src_data->head_status == 3) {
431 strbuf_addstr(out, "HEAD");
433 if (src_data->branch.nr) {
434 strbuf_addstr(out, subsep);
436 print_joined("branch ", "branches ", &src_data->branch,
439 if (src_data->r_branch.nr) {
440 strbuf_addstr(out, subsep);
442 print_joined("remote-tracking branch ", "remote-tracking branches ",
443 &src_data->r_branch, out);
445 if (src_data->tag.nr) {
446 strbuf_addstr(out, subsep);
448 print_joined("tag ", "tags ", &src_data->tag, out);
450 if (src_data->generic.nr) {
451 strbuf_addstr(out, subsep);
452 print_joined("commit ", "commits ", &src_data->generic,
455 if (strcmp(".", srcs.items[i].string))
456 strbuf_addf(out, " of %s", srcs.items[i].string);
459 if (!strcmp("master", current_branch))
460 strbuf_addch(out, '\n');
462 strbuf_addf(out, " into %s\n", current_branch);
465 static void fmt_tag_signature(struct strbuf *tagbuf,
470 const char *tag_body = strstr(buf, "\n\n");
473 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
475 strbuf_complete_line(tagbuf);
477 strbuf_addch(tagbuf, '\n');
478 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
482 static void fmt_merge_msg_sigs(struct strbuf *out)
484 int i, tag_number = 0, first_tag = 0;
485 struct strbuf tagbuf = STRBUF_INIT;
487 for (i = 0; i < origins.nr; i++) {
488 unsigned char *sha1 = origins.items[i].util;
489 enum object_type type;
490 unsigned long size, len;
491 char *buf = read_sha1_file(sha1, &type, &size);
492 struct strbuf sig = STRBUF_INIT;
494 if (!buf || type != OBJ_TAG)
496 len = parse_signature(buf, size);
499 ; /* merely annotated */
500 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {
502 strbuf_addstr(&sig, "gpg verification failed.\n");
506 fmt_tag_signature(&tagbuf, &sig, buf, len);
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,
517 strbuf_release(&tagline);
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);
525 strbuf_release(&sig);
530 strbuf_addch(out, '\n');
531 strbuf_addbuf(out, &tagbuf);
533 strbuf_release(&tagbuf);
536 static void find_merge_parents(struct merge_parents *result,
537 struct strbuf *in, unsigned char *head)
539 struct commit_list *parents, *next;
540 struct commit *head_commit;
544 while (pos < in->len) {
546 char *p = in->buf + pos;
547 char *newline = strchr(p, '\n');
548 unsigned char sha1[20];
549 struct commit *parent;
552 len = newline ? newline - p : strlen(p);
553 pos += len + !!newline;
556 get_sha1_hex(p, sha1) ||
559 continue; /* skip not-for-merge */
561 * Do not use get_merge_parent() here; we do not have
562 * "name" here and we do not want to contaminate its
565 obj = parse_object(sha1);
566 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
569 commit_list_insert(parent, &parents);
570 add_merge_parent(result, obj->sha1, parent->object.sha1);
572 head_commit = lookup_commit(head);
574 commit_list_insert(head_commit, &parents);
575 parents = reduce_heads(parents);
578 for (i = 0; i < result->nr; i++)
579 if (!hashcmp(result->item[i].commit,
580 parents->item->object.sha1))
581 result->item[i].used = 1;
582 next = parents->next;
587 for (i = j = 0; i < result->nr; i++) {
588 if (result->item[i].used) {
590 result->item[j] = result->item[i];
597 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
598 struct fmt_merge_msg_opts *opts)
601 unsigned char head_sha1[20];
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_sha1, NULL);
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_sha1);
619 while (pos < in->len) {
621 char *newline, *p = in->buf + pos;
623 newline = strchr(p, '\n');
624 len = newline ? newline - p : strlen(p);
625 pos += len + !!newline;
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);
636 fmt_merge_msg_sigs(out);
638 if (opts->shortlog_len) {
642 head = lookup_commit_or_die(head_sha1, "HEAD");
643 init_revisions(&rev, NULL);
644 rev.commit_format = CMIT_FMT_ONELINE;
645 rev.ignore_merges = 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);
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")),
682 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
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,
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");
697 die_errno("cannot open '%s'", inpath);
700 if (strbuf_read(&input, fileno(in), 0) < 0)
701 die_errno("could not read input file");
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);
714 write_in_full(STDOUT_FILENO, output.buf, output.len);