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 "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 static int handle_line(char *line)
60 int i, len = strlen(line);
61 struct origin_data *origin_data;
63 struct src_data *src_data;
64 struct string_list_item *item;
67 if (len < 43 || line[40] != '\t')
70 if (!prefixcmp(line + 41, "not-for-merge"))
77 origin_data = xcalloc(1, sizeof(struct origin_data));
78 i = get_sha1(line, origin_data->sha1);
85 if (line[len - 1] == '\n')
90 * At this point, line points at the beginning of comment e.g.
91 * "branch 'frotz' of git://that/repository.git".
92 * Find the repository name and point it with src.
94 src = strstr(line, " of ");
104 item = unsorted_string_list_lookup(&srcs, src);
106 item = string_list_append(&srcs, src);
107 item->util = xcalloc(1, sizeof(struct src_data));
108 init_src_data(item->util);
110 src_data = item->util;
114 src_data->head_status |= 1;
115 } else if (!prefixcmp(line, "branch ")) {
116 origin_data->is_local_branch = 1;
118 string_list_append(&src_data->branch, origin);
119 src_data->head_status |= 2;
120 } else if (!prefixcmp(line, "tag ")) {
122 string_list_append(&src_data->tag, origin + 4);
123 src_data->head_status |= 2;
124 } else if (!prefixcmp(line, "remote-tracking branch ")) {
125 origin = line + strlen("remote-tracking branch ");
126 string_list_append(&src_data->r_branch, origin);
127 src_data->head_status |= 2;
130 string_list_append(&src_data->generic, line);
131 src_data->head_status |= 2;
134 if (!strcmp(".", src) || !strcmp(src, origin)) {
135 int len = strlen(origin);
136 if (origin[0] == '\'' && origin[len - 1] == '\'')
137 origin = xmemdupz(origin + 1, len - 2);
139 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
140 sprintf(new_origin, "%s of %s", origin, src);
143 if (strcmp(".", src))
144 origin_data->is_local_branch = 0;
145 string_list_append(&origins, origin)->util = origin_data;
149 static void print_joined(const char *singular, const char *plural,
150 struct string_list *list, struct strbuf *out)
155 strbuf_addf(out, "%s%s", singular, list->items[0].string);
158 strbuf_addstr(out, plural);
159 for (i = 0; i < list->nr - 1; i++)
160 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
161 list->items[i].string);
162 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
166 static void add_branch_desc(struct strbuf *out, const char *name)
168 struct strbuf desc = STRBUF_INIT;
170 if (!read_branch_desc(&desc, name)) {
171 const char *bp = desc.buf;
173 const char *ep = strchrnul(bp, '\n');
176 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
179 if (out->buf[out->len - 1] != '\n')
180 strbuf_addch(out, '\n');
182 strbuf_release(&desc);
185 #define util_as_integral(elem) ((intptr_t)((elem)->util))
187 static void record_person(int which, struct string_list *people,
188 struct commit *commit)
190 char name_buf[MAX_GITNAME], *name, *name_end;
191 struct string_list_item *elem;
192 const char *field = (which == 'a') ? "\nauthor " : "\ncommitter ";
194 name = strstr(commit->buffer, field);
197 name += strlen(field);
198 name_end = strchrnul(name, '<');
201 while (isspace(*name_end) && name <= name_end)
203 if (name_end < name || name + MAX_GITNAME <= name_end)
205 memcpy(name_buf, name, name_end - name + 1);
206 name_buf[name_end - name + 1] = '\0';
208 elem = string_list_lookup(people, name_buf);
210 elem = string_list_insert(people, name_buf);
211 elem->util = (void *)0;
213 elem->util = (void*)(util_as_integral(elem) + 1);
216 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
218 const struct string_list_item *a = a_, *b = b_;
219 return util_as_integral(b) - util_as_integral(a);
222 static void add_people_count(struct strbuf *out, struct string_list *people)
225 strbuf_addf(out, "%s", people->items[0].string);
226 else if (people->nr == 2)
227 strbuf_addf(out, "%s (%d) and %s (%d)",
228 people->items[0].string,
229 (int)util_as_integral(&people->items[0]),
230 people->items[1].string,
231 (int)util_as_integral(&people->items[1]));
233 strbuf_addf(out, "%s (%d) and others",
234 people->items[0].string,
235 (int)util_as_integral(&people->items[0]));
238 static void credit_people(struct strbuf *out,
239 struct string_list *them,
247 me = git_author_info(IDENT_NO_DATE);
250 me = git_committer_info(IDENT_NO_DATE);
256 (me = skip_prefix(me, them->items->string)) != NULL &&
257 skip_prefix(me, " <")))
259 strbuf_addstr(out, label);
260 add_people_count(out, them);
263 static void add_people_info(struct strbuf *out,
264 struct string_list *authors,
265 struct string_list *committers)
268 qsort(authors->items,
269 authors->nr, sizeof(authors->items[0]),
270 cmp_string_list_util_as_integral);
272 qsort(committers->items,
273 committers->nr, sizeof(committers->items[0]),
274 cmp_string_list_util_as_integral);
276 credit_people(out, authors, 'a');
277 credit_people(out, committers, 'c');
280 static void shortlog(const char *name,
281 struct origin_data *origin_data,
283 struct rev_info *rev, int limit,
287 struct commit *commit;
288 struct object *branch;
289 struct string_list subjects = STRING_LIST_INIT_DUP;
290 struct string_list authors = STRING_LIST_INIT_DUP;
291 struct string_list committers = STRING_LIST_INIT_DUP;
292 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
293 struct strbuf sb = STRBUF_INIT;
294 const unsigned char *sha1 = origin_data->sha1;
296 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
297 if (!branch || branch->type != OBJ_COMMIT)
300 setup_revisions(0, NULL, rev, NULL);
301 add_pending_object(rev, branch, name);
302 add_pending_object(rev, &head->object, "^HEAD");
303 head->object.flags |= UNINTERESTING;
304 if (prepare_revision_walk(rev))
305 die("revision walk setup failed");
306 while ((commit = get_revision(rev)) != NULL) {
307 struct pretty_print_context ctx = {0};
309 if (commit->parents && commit->parents->next) {
310 /* do not list a merge but count committer */
311 record_person('c', &committers, commit);
315 /* the 'tip' committer */
316 record_person('c', &committers, commit);
317 record_person('a', &authors, commit);
319 if (subjects.nr > limit)
322 format_commit_message(commit, "%s", &sb, &ctx);
326 string_list_append(&subjects,
327 sha1_to_hex(commit->object.sha1));
329 string_list_append(&subjects, strbuf_detach(&sb, NULL));
332 add_people_info(out, &authors, &committers);
334 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
336 strbuf_addf(out, "\n* %s:\n", name);
338 if (origin_data->is_local_branch && use_branch_desc)
339 add_branch_desc(out, name);
341 for (i = 0; i < subjects.nr; i++)
343 strbuf_addf(out, " ...\n");
345 strbuf_addf(out, " %s\n", subjects.items[i].string);
347 clear_commit_marks((struct commit *)branch, flags);
348 clear_commit_marks(head, flags);
349 free_commit_list(rev->commits);
353 string_list_clear(&authors, 0);
354 string_list_clear(&committers, 0);
355 string_list_clear(&subjects, 0);
358 static void fmt_merge_msg_title(struct strbuf *out,
359 const char *current_branch) {
363 strbuf_addstr(out, "Merge ");
364 for (i = 0; i < srcs.nr; i++) {
365 struct src_data *src_data = srcs.items[i].util;
366 const char *subsep = "";
368 strbuf_addstr(out, sep);
371 if (src_data->head_status == 1) {
372 strbuf_addstr(out, srcs.items[i].string);
375 if (src_data->head_status == 3) {
377 strbuf_addstr(out, "HEAD");
379 if (src_data->branch.nr) {
380 strbuf_addstr(out, subsep);
382 print_joined("branch ", "branches ", &src_data->branch,
385 if (src_data->r_branch.nr) {
386 strbuf_addstr(out, subsep);
388 print_joined("remote-tracking branch ", "remote-tracking branches ",
389 &src_data->r_branch, out);
391 if (src_data->tag.nr) {
392 strbuf_addstr(out, subsep);
394 print_joined("tag ", "tags ", &src_data->tag, out);
396 if (src_data->generic.nr) {
397 strbuf_addstr(out, subsep);
398 print_joined("commit ", "commits ", &src_data->generic,
401 if (strcmp(".", srcs.items[i].string))
402 strbuf_addf(out, " of %s", srcs.items[i].string);
405 if (!strcmp("master", current_branch))
406 strbuf_addch(out, '\n');
408 strbuf_addf(out, " into %s\n", current_branch);
411 static void fmt_tag_signature(struct strbuf *tagbuf,
416 const char *tag_body = strstr(buf, "\n\n");
419 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
421 strbuf_complete_line(tagbuf);
422 strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
425 static void fmt_merge_msg_sigs(struct strbuf *out)
427 int i, tag_number = 0, first_tag = 0;
428 struct strbuf tagbuf = STRBUF_INIT;
430 for (i = 0; i < origins.nr; i++) {
431 unsigned char *sha1 = origins.items[i].util;
432 enum object_type type;
433 unsigned long size, len;
434 char *buf = read_sha1_file(sha1, &type, &size);
435 struct strbuf sig = STRBUF_INIT;
437 if (!buf || type != OBJ_TAG)
439 len = parse_signature(buf, size);
442 ; /* merely annotated */
443 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
445 strbuf_addstr(&sig, "gpg verification failed.\n");
449 fmt_tag_signature(&tagbuf, &sig, buf, len);
452 if (tag_number == 2) {
453 struct strbuf tagline = STRBUF_INIT;
454 strbuf_addf(&tagline, "\n# %s\n",
455 origins.items[first_tag].string);
456 strbuf_insert(&tagbuf, 0, tagline.buf,
458 strbuf_release(&tagline);
460 strbuf_addf(&tagbuf, "\n# %s\n",
461 origins.items[i].string);
462 fmt_tag_signature(&tagbuf, &sig, buf, len);
464 strbuf_release(&sig);
469 strbuf_addch(out, '\n');
470 strbuf_addbuf(out, &tagbuf);
472 strbuf_release(&tagbuf);
475 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
476 struct fmt_merge_msg_opts *opts)
479 unsigned char head_sha1[20];
480 const char *current_branch;
481 void *current_branch_to_free;
483 /* get current branch */
484 current_branch = current_branch_to_free =
485 resolve_refdup("HEAD", head_sha1, 1, NULL);
487 die("No current branch");
488 if (!prefixcmp(current_branch, "refs/heads/"))
489 current_branch += 11;
492 while (pos < in->len) {
494 char *newline, *p = in->buf + pos;
496 newline = strchr(p, '\n');
497 len = newline ? newline - p : strlen(p);
498 pos += len + !!newline;
502 die ("Error in line %d: %.*s", i, len, p);
505 if (opts->add_title && srcs.nr)
506 fmt_merge_msg_title(out, current_branch);
509 fmt_merge_msg_sigs(out);
511 if (opts->shortlog_len) {
515 head = lookup_commit_or_die(head_sha1, "HEAD");
516 init_revisions(&rev, NULL);
517 rev.commit_format = CMIT_FMT_ONELINE;
518 rev.ignore_merges = 1;
521 if (suffixcmp(out->buf, "\n"))
522 strbuf_addch(out, '\n');
524 for (i = 0; i < origins.nr; i++)
525 shortlog(origins.items[i].string,
526 origins.items[i].util,
527 head, &rev, opts->shortlog_len, out);
530 strbuf_complete_line(out);
531 free(current_branch_to_free);
535 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
537 const char *inpath = NULL;
538 const char *message = NULL;
539 int shortlog_len = -1;
540 struct option options[] = {
541 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
542 "populate log with at most <n> entries from shortlog",
543 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
544 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
545 "alias for --log (deprecated)",
546 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
547 DEFAULT_MERGE_LOG_LEN },
548 OPT_STRING('m', "message", &message, "text",
549 "use <text> as start of message"),
550 OPT_FILENAME('F', "file", &inpath, "file to read from"),
555 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
557 struct fmt_merge_msg_opts opts;
559 git_config(fmt_merge_msg_config, NULL);
560 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
563 usage_with_options(fmt_merge_msg_usage, options);
564 if (shortlog_len < 0)
565 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
567 if (inpath && strcmp(inpath, "-")) {
568 in = fopen(inpath, "r");
570 die_errno("cannot open '%s'", inpath);
573 if (strbuf_read(&input, fileno(in), 0) < 0)
574 die_errno("could not read input file");
577 strbuf_addstr(&output, message);
579 memset(&opts, 0, sizeof(opts));
580 opts.add_title = !message;
581 opts.shortlog_len = shortlog_len;
583 ret = fmt_merge_msg(&input, &output, &opts);
586 write_in_full(STDOUT_FILENO, output.buf, output.len);