fetch-pack: sort the list of incoming refs
[git/jnareb-git.git] / builtin / fmt-merge-msg.c
bloba517f1794a1c1bcc0939ad8b81d482356c20d2ba
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.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 "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>]",
14 NULL
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")) {
22 int is_bool;
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);
30 } else {
31 return git_default_config(key, value, cb);
33 return 0;
36 /* merge data per repository where the merged tips came from */
37 struct src_data {
38 struct string_list branch, tag, r_branch, generic;
39 int head_status;
42 struct origin_data {
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 {
59 int alloc, nr;
60 struct merge_parent {
61 unsigned char given[20];
62 unsigned char commit[20];
63 unsigned char used;
64 } *item;
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,
72 unsigned char *given,
73 unsigned char *commit)
75 int i;
76 for (i = 0; i < table->nr; i++) {
77 if (given && hashcmp(table->item[i].given, given))
78 continue;
79 if (commit && hashcmp(table->item[i].commit, commit))
80 continue;
81 return &table->item[i];
83 return NULL;
86 static void add_merge_parent(struct merge_parents *table,
87 unsigned char *given,
88 unsigned char *commit)
90 if (table->nr && find_merge_parent(table, given, commit))
91 return;
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;
96 table->nr++;
99 static int handle_line(char *line, struct merge_parents *merge_parents)
101 int i, len = strlen(line);
102 struct origin_data *origin_data;
103 char *src, *origin;
104 struct src_data *src_data;
105 struct string_list_item *item;
106 int pulling_head = 0;
107 unsigned char sha1[20];
109 if (len < 43 || line[40] != '\t')
110 return 1;
112 if (!prefixcmp(line + 41, "not-for-merge"))
113 return 0;
115 if (line[41] != '\t')
116 return 2;
118 i = get_sha1_hex(line, sha1);
119 if (i)
120 return 3;
122 if (!find_merge_parent(merge_parents, sha1, NULL))
123 return 0; /* subsumed by other parents */
125 origin_data = xcalloc(1, sizeof(struct origin_data));
126 hashcpy(origin_data->sha1, sha1);
128 if (line[len - 1] == '\n')
129 line[len - 1] = 0;
130 line += 42;
133 * At this point, line points at the beginning of comment e.g.
134 * "branch 'frotz' of git://that/repository.git".
135 * Find the repository name and point it with src.
137 src = strstr(line, " of ");
138 if (src) {
139 *src = 0;
140 src += 4;
141 pulling_head = 0;
142 } else {
143 src = line;
144 pulling_head = 1;
147 item = unsorted_string_list_lookup(&srcs, src);
148 if (!item) {
149 item = string_list_append(&srcs, src);
150 item->util = xcalloc(1, sizeof(struct src_data));
151 init_src_data(item->util);
153 src_data = item->util;
155 if (pulling_head) {
156 origin = src;
157 src_data->head_status |= 1;
158 } else if (!prefixcmp(line, "branch ")) {
159 origin_data->is_local_branch = 1;
160 origin = line + 7;
161 string_list_append(&src_data->branch, origin);
162 src_data->head_status |= 2;
163 } else if (!prefixcmp(line, "tag ")) {
164 origin = line;
165 string_list_append(&src_data->tag, origin + 4);
166 src_data->head_status |= 2;
167 } else if (!prefixcmp(line, "remote-tracking branch ")) {
168 origin = line + strlen("remote-tracking branch ");
169 string_list_append(&src_data->r_branch, origin);
170 src_data->head_status |= 2;
171 } else {
172 origin = src;
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);
181 } else {
182 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
183 sprintf(new_origin, "%s of %s", origin, src);
184 origin = new_origin;
186 if (strcmp(".", src))
187 origin_data->is_local_branch = 0;
188 string_list_append(&origins, origin)->util = origin_data;
189 return 0;
192 static void print_joined(const char *singular, const char *plural,
193 struct string_list *list, struct strbuf *out)
195 if (list->nr == 0)
196 return;
197 if (list->nr == 1) {
198 strbuf_addf(out, "%s%s", singular, list->items[0].string);
199 } else {
200 int i;
201 strbuf_addstr(out, plural);
202 for (i = 0; i < list->nr - 1; i++)
203 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
204 list->items[i].string);
205 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
209 static void add_branch_desc(struct strbuf *out, const char *name)
211 struct strbuf desc = STRBUF_INIT;
213 if (!read_branch_desc(&desc, name)) {
214 const char *bp = desc.buf;
215 while (*bp) {
216 const char *ep = strchrnul(bp, '\n');
217 if (*ep)
218 ep++;
219 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
220 bp = ep;
222 if (out->buf[out->len - 1] != '\n')
223 strbuf_addch(out, '\n');
225 strbuf_release(&desc);
228 #define util_as_integral(elem) ((intptr_t)((elem)->util))
230 static void record_person(int which, struct string_list *people,
231 struct commit *commit)
233 char name_buf[MAX_GITNAME], *name, *name_end;
234 struct string_list_item *elem;
235 const char *field = (which == 'a') ? "\nauthor " : "\ncommitter ";
237 name = strstr(commit->buffer, field);
238 if (!name)
239 return;
240 name += strlen(field);
241 name_end = strchrnul(name, '<');
242 if (*name_end)
243 name_end--;
244 while (isspace(*name_end) && name <= name_end)
245 name_end--;
246 if (name_end < name || name + MAX_GITNAME <= name_end)
247 return;
248 memcpy(name_buf, name, name_end - name + 1);
249 name_buf[name_end - name + 1] = '\0';
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);
259 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
261 const struct string_list_item *a = a_, *b = b_;
262 return util_as_integral(b) - util_as_integral(a);
265 static void add_people_count(struct strbuf *out, struct string_list *people)
267 if (people->nr == 1)
268 strbuf_addf(out, "%s", people->items[0].string);
269 else if (people->nr == 2)
270 strbuf_addf(out, "%s (%d) and %s (%d)",
271 people->items[0].string,
272 (int)util_as_integral(&people->items[0]),
273 people->items[1].string,
274 (int)util_as_integral(&people->items[1]));
275 else if (people->nr)
276 strbuf_addf(out, "%s (%d) and others",
277 people->items[0].string,
278 (int)util_as_integral(&people->items[0]));
281 static void credit_people(struct strbuf *out,
282 struct string_list *them,
283 int kind)
285 const char *label;
286 const char *me;
288 if (kind == 'a') {
289 label = "\nBy ";
290 me = git_author_info(IDENT_NO_DATE);
291 } else {
292 label = "\nvia ";
293 me = git_committer_info(IDENT_NO_DATE);
296 if (!them->nr ||
297 (them->nr == 1 &&
298 me &&
299 (me = skip_prefix(me, them->items->string)) != NULL &&
300 skip_prefix(me, " <")))
301 return;
302 strbuf_addstr(out, label);
303 add_people_count(out, them);
306 static void add_people_info(struct strbuf *out,
307 struct string_list *authors,
308 struct string_list *committers)
310 if (authors->nr)
311 qsort(authors->items,
312 authors->nr, sizeof(authors->items[0]),
313 cmp_string_list_util_as_integral);
314 if (committers->nr)
315 qsort(committers->items,
316 committers->nr, sizeof(committers->items[0]),
317 cmp_string_list_util_as_integral);
319 credit_people(out, authors, 'a');
320 credit_people(out, committers, 'c');
323 static void shortlog(const char *name,
324 struct origin_data *origin_data,
325 struct commit *head,
326 struct rev_info *rev, int limit,
327 struct strbuf *out)
329 int i, count = 0;
330 struct commit *commit;
331 struct object *branch;
332 struct string_list subjects = STRING_LIST_INIT_DUP;
333 struct string_list authors = STRING_LIST_INIT_DUP;
334 struct string_list committers = STRING_LIST_INIT_DUP;
335 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
336 struct strbuf sb = STRBUF_INIT;
337 const unsigned char *sha1 = origin_data->sha1;
339 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
340 if (!branch || branch->type != OBJ_COMMIT)
341 return;
343 setup_revisions(0, NULL, rev, NULL);
344 add_pending_object(rev, branch, name);
345 add_pending_object(rev, &head->object, "^HEAD");
346 head->object.flags |= UNINTERESTING;
347 if (prepare_revision_walk(rev))
348 die("revision walk setup failed");
349 while ((commit = get_revision(rev)) != NULL) {
350 struct pretty_print_context ctx = {0};
352 if (commit->parents && commit->parents->next) {
353 /* do not list a merge but count committer */
354 record_person('c', &committers, commit);
355 continue;
357 if (!count)
358 /* the 'tip' committer */
359 record_person('c', &committers, commit);
360 record_person('a', &authors, commit);
361 count++;
362 if (subjects.nr > limit)
363 continue;
365 format_commit_message(commit, "%s", &sb, &ctx);
366 strbuf_ltrim(&sb);
368 if (!sb.len)
369 string_list_append(&subjects,
370 sha1_to_hex(commit->object.sha1));
371 else
372 string_list_append(&subjects, strbuf_detach(&sb, NULL));
375 add_people_info(out, &authors, &committers);
376 if (count > limit)
377 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
378 else
379 strbuf_addf(out, "\n* %s:\n", name);
381 if (origin_data->is_local_branch && use_branch_desc)
382 add_branch_desc(out, name);
384 for (i = 0; i < subjects.nr; i++)
385 if (i >= limit)
386 strbuf_addf(out, " ...\n");
387 else
388 strbuf_addf(out, " %s\n", subjects.items[i].string);
390 clear_commit_marks((struct commit *)branch, flags);
391 clear_commit_marks(head, flags);
392 free_commit_list(rev->commits);
393 rev->commits = NULL;
394 rev->pending.nr = 0;
396 string_list_clear(&authors, 0);
397 string_list_clear(&committers, 0);
398 string_list_clear(&subjects, 0);
401 static void fmt_merge_msg_title(struct strbuf *out,
402 const char *current_branch) {
403 int i = 0;
404 char *sep = "";
406 strbuf_addstr(out, "Merge ");
407 for (i = 0; i < srcs.nr; i++) {
408 struct src_data *src_data = srcs.items[i].util;
409 const char *subsep = "";
411 strbuf_addstr(out, sep);
412 sep = "; ";
414 if (src_data->head_status == 1) {
415 strbuf_addstr(out, srcs.items[i].string);
416 continue;
418 if (src_data->head_status == 3) {
419 subsep = ", ";
420 strbuf_addstr(out, "HEAD");
422 if (src_data->branch.nr) {
423 strbuf_addstr(out, subsep);
424 subsep = ", ";
425 print_joined("branch ", "branches ", &src_data->branch,
426 out);
428 if (src_data->r_branch.nr) {
429 strbuf_addstr(out, subsep);
430 subsep = ", ";
431 print_joined("remote-tracking branch ", "remote-tracking branches ",
432 &src_data->r_branch, out);
434 if (src_data->tag.nr) {
435 strbuf_addstr(out, subsep);
436 subsep = ", ";
437 print_joined("tag ", "tags ", &src_data->tag, out);
439 if (src_data->generic.nr) {
440 strbuf_addstr(out, subsep);
441 print_joined("commit ", "commits ", &src_data->generic,
442 out);
444 if (strcmp(".", srcs.items[i].string))
445 strbuf_addf(out, " of %s", srcs.items[i].string);
448 if (!strcmp("master", current_branch))
449 strbuf_addch(out, '\n');
450 else
451 strbuf_addf(out, " into %s\n", current_branch);
454 static void fmt_tag_signature(struct strbuf *tagbuf,
455 struct strbuf *sig,
456 const char *buf,
457 unsigned long len)
459 const char *tag_body = strstr(buf, "\n\n");
460 if (tag_body) {
461 tag_body += 2;
462 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
464 strbuf_complete_line(tagbuf);
465 strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
468 static void fmt_merge_msg_sigs(struct strbuf *out)
470 int i, tag_number = 0, first_tag = 0;
471 struct strbuf tagbuf = STRBUF_INIT;
473 for (i = 0; i < origins.nr; i++) {
474 unsigned char *sha1 = origins.items[i].util;
475 enum object_type type;
476 unsigned long size, len;
477 char *buf = read_sha1_file(sha1, &type, &size);
478 struct strbuf sig = STRBUF_INIT;
480 if (!buf || type != OBJ_TAG)
481 goto next;
482 len = parse_signature(buf, size);
484 if (size == len)
485 ; /* merely annotated */
486 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
487 if (!sig.len)
488 strbuf_addstr(&sig, "gpg verification failed.\n");
491 if (!tag_number++) {
492 fmt_tag_signature(&tagbuf, &sig, buf, len);
493 first_tag = i;
494 } else {
495 if (tag_number == 2) {
496 struct strbuf tagline = STRBUF_INIT;
497 strbuf_addf(&tagline, "\n# %s\n",
498 origins.items[first_tag].string);
499 strbuf_insert(&tagbuf, 0, tagline.buf,
500 tagline.len);
501 strbuf_release(&tagline);
503 strbuf_addf(&tagbuf, "\n# %s\n",
504 origins.items[i].string);
505 fmt_tag_signature(&tagbuf, &sig, buf, len);
507 strbuf_release(&sig);
508 next:
509 free(buf);
511 if (tagbuf.len) {
512 strbuf_addch(out, '\n');
513 strbuf_addbuf(out, &tagbuf);
515 strbuf_release(&tagbuf);
518 static void find_merge_parents(struct merge_parents *result,
519 struct strbuf *in, unsigned char *head)
521 struct commit_list *parents, *next;
522 struct commit *head_commit;
523 int pos = 0, i, j;
525 parents = NULL;
526 while (pos < in->len) {
527 int len;
528 char *p = in->buf + pos;
529 char *newline = strchr(p, '\n');
530 unsigned char sha1[20];
531 struct commit *parent;
532 struct object *obj;
534 len = newline ? newline - p : strlen(p);
535 pos += len + !!newline;
537 if (len < 43 ||
538 get_sha1_hex(p, sha1) ||
539 p[40] != '\t' ||
540 p[41] != '\t')
541 continue; /* skip not-for-merge */
543 * Do not use get_merge_parent() here; we do not have
544 * "name" here and we do not want to contaminate its
545 * util field yet.
547 obj = parse_object(sha1);
548 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
549 if (!parent)
550 continue;
551 commit_list_insert(parent, &parents);
552 add_merge_parent(result, obj->sha1, parent->object.sha1);
554 head_commit = lookup_commit(head);
555 if (head_commit)
556 commit_list_insert(head_commit, &parents);
557 parents = reduce_heads(parents);
559 while (parents) {
560 for (i = 0; i < result->nr; i++)
561 if (!hashcmp(result->item[i].commit,
562 parents->item->object.sha1))
563 result->item[i].used = 1;
564 next = parents->next;
565 free(parents);
566 parents = next;
569 for (i = j = 0; i < result->nr; i++) {
570 if (result->item[i].used) {
571 if (i != j)
572 result->item[j] = result->item[i];
573 j++;
576 result->nr = j;
579 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
580 struct fmt_merge_msg_opts *opts)
582 int i = 0, pos = 0;
583 unsigned char head_sha1[20];
584 const char *current_branch;
585 void *current_branch_to_free;
586 struct merge_parents merge_parents;
588 memset(&merge_parents, 0, sizeof(merge_parents));
590 /* get current branch */
591 current_branch = current_branch_to_free =
592 resolve_refdup("HEAD", head_sha1, 1, NULL);
593 if (!current_branch)
594 die("No current branch");
595 if (!prefixcmp(current_branch, "refs/heads/"))
596 current_branch += 11;
598 find_merge_parents(&merge_parents, in, head_sha1);
600 /* get a line */
601 while (pos < in->len) {
602 int len;
603 char *newline, *p = in->buf + pos;
605 newline = strchr(p, '\n');
606 len = newline ? newline - p : strlen(p);
607 pos += len + !!newline;
608 i++;
609 p[len] = 0;
610 if (handle_line(p, &merge_parents))
611 die ("Error in line %d: %.*s", i, len, p);
614 if (opts->add_title && srcs.nr)
615 fmt_merge_msg_title(out, current_branch);
617 if (origins.nr)
618 fmt_merge_msg_sigs(out);
620 if (opts->shortlog_len) {
621 struct commit *head;
622 struct rev_info rev;
624 head = lookup_commit_or_die(head_sha1, "HEAD");
625 init_revisions(&rev, NULL);
626 rev.commit_format = CMIT_FMT_ONELINE;
627 rev.ignore_merges = 1;
628 rev.limited = 1;
630 if (suffixcmp(out->buf, "\n"))
631 strbuf_addch(out, '\n');
633 for (i = 0; i < origins.nr; i++)
634 shortlog(origins.items[i].string,
635 origins.items[i].util,
636 head, &rev, opts->shortlog_len, out);
639 strbuf_complete_line(out);
640 free(current_branch_to_free);
641 free(merge_parents.item);
642 return 0;
645 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
647 const char *inpath = NULL;
648 const char *message = NULL;
649 int shortlog_len = -1;
650 struct option options[] = {
651 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
652 "populate log with at most <n> entries from shortlog",
653 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
654 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
655 "alias for --log (deprecated)",
656 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
657 DEFAULT_MERGE_LOG_LEN },
658 OPT_STRING('m', "message", &message, "text",
659 "use <text> as start of message"),
660 OPT_FILENAME('F', "file", &inpath, "file to read from"),
661 OPT_END()
664 FILE *in = stdin;
665 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
666 int ret;
667 struct fmt_merge_msg_opts opts;
669 git_config(fmt_merge_msg_config, NULL);
670 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
672 if (argc > 0)
673 usage_with_options(fmt_merge_msg_usage, options);
674 if (shortlog_len < 0)
675 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
677 if (inpath && strcmp(inpath, "-")) {
678 in = fopen(inpath, "r");
679 if (!in)
680 die_errno("cannot open '%s'", inpath);
683 if (strbuf_read(&input, fileno(in), 0) < 0)
684 die_errno("could not read input file");
686 if (message)
687 strbuf_addstr(&output, message);
689 memset(&opts, 0, sizeof(opts));
690 opts.add_title = !message;
691 opts.shortlog_len = shortlog_len;
693 ret = fmt_merge_msg(&input, &output, &opts);
694 if (ret)
695 return ret;
696 write_in_full(STDOUT_FILENO, output.buf, output.len);
697 return 0;