fmt-merge-msg: fix coding style
[git/git.git] / builtin / fmt-merge-msg.c
CommitLineData
25f38f06 1#include "builtin.h"
00449f99 2#include "cache.h"
fb58c8d5 3#include "refs.h"
00449f99
JS
4#include "commit.h"
5#include "diff.h"
6#include "revision.h"
7#include "tag.h"
fcb243f7 8#include "string-list.h"
898eacd8
JH
9#include "branch.h"
10#include "fmt-merge-msg.h"
895680f0 11#include "gpg-interface.h"
00449f99 12
c8ef0383 13static const char * const fmt_merge_msg_usage[] = {
9c9b4f2f 14 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
c8ef0383
PH
15 NULL
16};
00449f99 17
898eacd8 18static int use_branch_desc;
00449f99 19
898eacd8 20int fmt_merge_msg_config(const char *key, const char *value, void *cb)
00449f99 21{
bda3b8ff
RR
22 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
23 int is_bool;
898eacd8
JH
24 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
25 if (!is_bool && merge_log_config < 0)
bda3b8ff 26 return error("%s: negative length %s", key, value);
898eacd8
JH
27 if (is_bool && merge_log_config)
28 merge_log_config = DEFAULT_MERGE_LOG_LEN;
29 } else if (!strcmp(key, "merge.branchdesc")) {
30 use_branch_desc = git_config_bool(key, value);
418a1435
JH
31 } else {
32 return git_default_config(key, value, cb);
6cd9cfef 33 }
00449f99
JS
34 return 0;
35}
36
4c0ea82d 37/* merge data per repository where the merged tips came from */
fcb243f7
SB
38struct src_data {
39 struct string_list branch, tag, r_branch, generic;
40 int head_status;
00449f99
JS
41};
42
898eacd8
JH
43struct origin_data {
44 unsigned char sha1[20];
90a321c0 45 unsigned is_local_branch:1;
898eacd8
JH
46};
47
c2e86add 48static void init_src_data(struct src_data *data)
00449f99 49{
fcb243f7
SB
50 data->branch.strdup_strings = 1;
51 data->tag.strdup_strings = 1;
52 data->r_branch.strdup_strings = 1;
53 data->generic.strdup_strings = 1;
00449f99
JS
54}
55
183113a5
TF
56static struct string_list srcs = STRING_LIST_INIT_DUP;
57static struct string_list origins = STRING_LIST_INIT_DUP;
00449f99 58
5802f81b
JH
59struct merge_parents {
60 int alloc, nr;
61 struct merge_parent {
62 unsigned char given[20];
63 unsigned char commit[20];
64 unsigned char used;
65 } *item;
66};
67
68/*
69 * I know, I know, this is inefficient, but you won't be pulling and merging
70 * hundreds of heads at a time anyway.
71 */
72static struct merge_parent *find_merge_parent(struct merge_parents *table,
73 unsigned char *given,
74 unsigned char *commit)
75{
76 int i;
77 for (i = 0; i < table->nr; i++) {
78 if (given && hashcmp(table->item[i].given, given))
79 continue;
80 if (commit && hashcmp(table->item[i].commit, commit))
81 continue;
82 return &table->item[i];
83 }
84 return NULL;
85}
86
87static void add_merge_parent(struct merge_parents *table,
88 unsigned char *given,
89 unsigned char *commit)
90{
91 if (table->nr && find_merge_parent(table, given, commit))
92 return;
93 ALLOC_GROW(table->item, table->nr + 1, table->alloc);
94 hashcpy(table->item[table->nr].given, given);
95 hashcpy(table->item[table->nr].commit, commit);
96 table->item[table->nr].used = 0;
97 table->nr++;
98}
99
100static int handle_line(char *line, struct merge_parents *merge_parents)
00449f99
JS
101{
102 int i, len = strlen(line);
898eacd8 103 struct origin_data *origin_data;
95b567c7
JK
104 char *src;
105 const char *origin;
00449f99 106 struct src_data *src_data;
fcb243f7 107 struct string_list_item *item;
e918c6ab 108 int pulling_head = 0;
5802f81b 109 unsigned char sha1[20];
00449f99
JS
110
111 if (len < 43 || line[40] != '\t')
112 return 1;
113
59556548 114 if (starts_with(line + 41, "not-for-merge"))
00449f99
JS
115 return 0;
116
117 if (line[41] != '\t')
118 return 2;
119
5802f81b
JH
120 i = get_sha1_hex(line, sha1);
121 if (i)
00449f99 122 return 3;
5802f81b
JH
123
124 if (!find_merge_parent(merge_parents, sha1, NULL))
125 return 0; /* subsumed by other parents */
126
127 origin_data = xcalloc(1, sizeof(struct origin_data));
128 hashcpy(origin_data->sha1, sha1);
00449f99
JS
129
130 if (line[len - 1] == '\n')
131 line[len - 1] = 0;
132 line += 42;
133
4c0ea82d
JH
134 /*
135 * At this point, line points at the beginning of comment e.g.
136 * "branch 'frotz' of git://that/repository.git".
137 * Find the repository name and point it with src.
138 */
00449f99
JS
139 src = strstr(line, " of ");
140 if (src) {
141 *src = 0;
142 src += 4;
e918c6ab
JH
143 pulling_head = 0;
144 } else {
145 src = line;
146 pulling_head = 1;
147 }
00449f99 148
fcb243f7
SB
149 item = unsorted_string_list_lookup(&srcs, src);
150 if (!item) {
1d2f80fa 151 item = string_list_append(&srcs, src);
fcb243f7
SB
152 item->util = xcalloc(1, sizeof(struct src_data));
153 init_src_data(item->util);
00449f99 154 }
fcb243f7 155 src_data = item->util;
00449f99 156
e918c6ab 157 if (pulling_head) {
fcb243f7 158 origin = src;
e918c6ab 159 src_data->head_status |= 1;
59556548 160 } else if (starts_with(line, "branch ")) {
898eacd8 161 origin_data->is_local_branch = 1;
fcb243f7 162 origin = line + 7;
1d2f80fa 163 string_list_append(&src_data->branch, origin);
00449f99 164 src_data->head_status |= 2;
59556548 165 } else if (starts_with(line, "tag ")) {
00449f99 166 origin = line;
1d2f80fa 167 string_list_append(&src_data->tag, origin + 4);
00449f99 168 src_data->head_status |= 2;
95b567c7 169 } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
1d2f80fa 170 string_list_append(&src_data->r_branch, origin);
00449f99 171 src_data->head_status |= 2;
00449f99 172 } else {
fcb243f7 173 origin = src;
1d2f80fa 174 string_list_append(&src_data->generic, line);
00449f99
JS
175 src_data->head_status |= 2;
176 }
177
178 if (!strcmp(".", src) || !strcmp(src, origin)) {
179 int len = strlen(origin);
fcb243f7 180 if (origin[0] == '\'' && origin[len - 1] == '\'')
182af834 181 origin = xmemdupz(origin + 1, len - 2);
28310186
JK
182 } else
183 origin = xstrfmt("%s of %s", origin, src);
898eacd8
JH
184 if (strcmp(".", src))
185 origin_data->is_local_branch = 0;
186 string_list_append(&origins, origin)->util = origin_data;
00449f99
JS
187 return 0;
188}
189
190static void print_joined(const char *singular, const char *plural,
fcb243f7 191 struct string_list *list, struct strbuf *out)
00449f99
JS
192{
193 if (list->nr == 0)
194 return;
195 if (list->nr == 1) {
fcb243f7 196 strbuf_addf(out, "%s%s", singular, list->items[0].string);
00449f99
JS
197 } else {
198 int i;
0b9a969e 199 strbuf_addstr(out, plural);
00449f99 200 for (i = 0; i < list->nr - 1; i++)
fcb243f7
SB
201 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
202 list->items[i].string);
203 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
00449f99
JS
204 }
205}
206
898eacd8
JH
207static void add_branch_desc(struct strbuf *out, const char *name)
208{
209 struct strbuf desc = STRBUF_INIT;
210
211 if (!read_branch_desc(&desc, name)) {
212 const char *bp = desc.buf;
213 while (*bp) {
214 const char *ep = strchrnul(bp, '\n');
215 if (*ep)
216 ep++;
217 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
218 bp = ep;
219 }
a0d4923d 220 strbuf_complete_line(out);
898eacd8
JH
221 }
222 strbuf_release(&desc);
223}
224
418a1435
JH
225#define util_as_integral(elem) ((intptr_t)((elem)->util))
226
1154aa42
JH
227static void record_person_from_buf(int which, struct string_list *people,
228 const char *buffer)
418a1435 229{
e21ab134 230 char *name_buf, *name, *name_end;
418a1435 231 struct string_list_item *elem;
9bcbb1c2 232 const char *field;
418a1435 233
9bcbb1c2 234 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
bc6b8fc1 235 name = strstr(buffer, field);
418a1435
JH
236 if (!name)
237 return;
238 name += strlen(field);
239 name_end = strchrnul(name, '<');
240 if (*name_end)
241 name_end--;
242 while (isspace(*name_end) && name <= name_end)
243 name_end--;
e21ab134 244 if (name_end < name)
418a1435 245 return;
e21ab134 246 name_buf = xmemdupz(name, name_end - name + 1);
418a1435
JH
247
248 elem = string_list_lookup(people, name_buf);
249 if (!elem) {
250 elem = string_list_insert(people, name_buf);
251 elem->util = (void *)0;
252 }
253 elem->util = (void*)(util_as_integral(elem) + 1);
e21ab134 254 free(name_buf);
418a1435
JH
255}
256
1154aa42
JH
257
258static void record_person(int which, struct string_list *people,
259 struct commit *commit)
260{
789e98df 261 const char *buffer = get_commit_buffer(commit, NULL);
1154aa42
JH
262 record_person_from_buf(which, people, buffer);
263 unuse_commit_buffer(commit, buffer);
264}
265
418a1435
JH
266static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
267{
268 const struct string_list_item *a = a_, *b = b_;
269 return util_as_integral(b) - util_as_integral(a);
270}
271
272static void add_people_count(struct strbuf *out, struct string_list *people)
273{
274 if (people->nr == 1)
bc57b9c0 275 strbuf_addstr(out, people->items[0].string);
418a1435
JH
276 else if (people->nr == 2)
277 strbuf_addf(out, "%s (%d) and %s (%d)",
278 people->items[0].string,
279 (int)util_as_integral(&people->items[0]),
280 people->items[1].string,
281 (int)util_as_integral(&people->items[1]));
282 else if (people->nr)
283 strbuf_addf(out, "%s (%d) and others",
284 people->items[0].string,
285 (int)util_as_integral(&people->items[0]));
286}
287
288static void credit_people(struct strbuf *out,
289 struct string_list *them,
290 int kind)
291{
292 const char *label;
293 const char *me;
294
295 if (kind == 'a') {
9927ebed 296 label = "By";
418a1435
JH
297 me = git_author_info(IDENT_NO_DATE);
298 } else {
9927ebed 299 label = "Via";
418a1435
JH
300 me = git_committer_info(IDENT_NO_DATE);
301 }
302
303 if (!them->nr ||
304 (them->nr == 1 &&
305 me &&
cf4fff57 306 skip_prefix(me, them->items->string, &me) &&
c0264180 307 starts_with(me, " <")))
418a1435 308 return;
9927ebed 309 strbuf_addf(out, "\n%c %s ", comment_line_char, label);
418a1435
JH
310 add_people_count(out, them);
311}
312
313static void add_people_info(struct strbuf *out,
314 struct string_list *authors,
315 struct string_list *committers)
316{
76dd98c1
RS
317 QSORT(authors->items, authors->nr,
318 cmp_string_list_util_as_integral);
319 QSORT(committers->items, committers->nr,
320 cmp_string_list_util_as_integral);
418a1435
JH
321
322 credit_people(out, authors, 'a');
323 credit_people(out, committers, 'c');
324}
325
898eacd8
JH
326static void shortlog(const char *name,
327 struct origin_data *origin_data,
328 struct commit *head,
9bcbb1c2
JH
329 struct rev_info *rev,
330 struct fmt_merge_msg_opts *opts,
898eacd8 331 struct strbuf *out)
00449f99
JS
332{
333 int i, count = 0;
334 struct commit *commit;
335 struct object *branch;
183113a5 336 struct string_list subjects = STRING_LIST_INIT_DUP;
418a1435
JH
337 struct string_list authors = STRING_LIST_INIT_DUP;
338 struct string_list committers = STRING_LIST_INIT_DUP;
7dc0fe3b 339 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
15cb5007 340 struct strbuf sb = STRBUF_INIT;
898eacd8 341 const unsigned char *sha1 = origin_data->sha1;
9bcbb1c2 342 int limit = opts->shortlog_len;
00449f99
JS
343
344 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
1974632c 345 if (!branch || branch->type != OBJ_COMMIT)
00449f99
JS
346 return;
347
348 setup_revisions(0, NULL, rev, NULL);
00449f99
JS
349 add_pending_object(rev, branch, name);
350 add_pending_object(rev, &head->object, "^HEAD");
351 head->object.flags |= UNINTERESTING;
3d51e1b5
MK
352 if (prepare_revision_walk(rev))
353 die("revision walk setup failed");
00449f99 354 while ((commit = get_revision(rev)) != NULL) {
15cb5007 355 struct pretty_print_context ctx = {0};
00449f99 356
418a1435
JH
357 if (commit->parents && commit->parents->next) {
358 /* do not list a merge but count committer */
9bcbb1c2
JH
359 if (opts->credit_people)
360 record_person('c', &committers, commit);
00449f99 361 continue;
418a1435 362 }
9bcbb1c2 363 if (!count && opts->credit_people)
418a1435
JH
364 /* the 'tip' committer */
365 record_person('c', &committers, commit);
9bcbb1c2
JH
366 if (opts->credit_people)
367 record_person('a', &authors, commit);
00449f99
JS
368 count++;
369 if (subjects.nr > limit)
370 continue;
371
15cb5007
SB
372 format_commit_message(commit, "%s", &sb, &ctx);
373 strbuf_ltrim(&sb);
6a28518a 374
15cb5007 375 if (!sb.len)
1d2f80fa 376 string_list_append(&subjects,
f2fd0760 377 oid_to_hex(&commit->object.oid));
15cb5007 378 else
1d2f80fa 379 string_list_append(&subjects, strbuf_detach(&sb, NULL));
00449f99
JS
380 }
381
9bcbb1c2
JH
382 if (opts->credit_people)
383 add_people_info(out, &authors, &committers);
00449f99 384 if (count > limit)
0b9a969e 385 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
00449f99 386 else
0b9a969e 387 strbuf_addf(out, "\n* %s:\n", name);
00449f99 388
898eacd8
JH
389 if (origin_data->is_local_branch && use_branch_desc)
390 add_branch_desc(out, name);
391
00449f99
JS
392 for (i = 0; i < subjects.nr; i++)
393 if (i >= limit)
a22ae753 394 strbuf_addstr(out, " ...\n");
00449f99 395 else
fcb243f7 396 strbuf_addf(out, " %s\n", subjects.items[i].string);
00449f99
JS
397
398 clear_commit_marks((struct commit *)branch, flags);
399 clear_commit_marks(head, flags);
400 free_commit_list(rev->commits);
401 rev->commits = NULL;
402 rev->pending.nr = 0;
403
418a1435
JH
404 string_list_clear(&authors, 0);
405 string_list_clear(&committers, 0);
fcb243f7 406 string_list_clear(&subjects, 0);
00449f99
JS
407}
408
cbda121c 409static void fmt_merge_msg_title(struct strbuf *out,
edd64ef4
DC
410 const char *current_branch)
411{
403994e8 412 int i = 0;
fd13b21f 413 char *sep = "";
419fe5bc 414
0b9a969e 415 strbuf_addstr(out, "Merge ");
00449f99 416 for (i = 0; i < srcs.nr; i++) {
fcb243f7 417 struct src_data *src_data = srcs.items[i].util;
00449f99
JS
418 const char *subsep = "";
419
0b9a969e 420 strbuf_addstr(out, sep);
00449f99
JS
421 sep = "; ";
422
423 if (src_data->head_status == 1) {
fcb243f7 424 strbuf_addstr(out, srcs.items[i].string);
00449f99
JS
425 continue;
426 }
427 if (src_data->head_status == 3) {
428 subsep = ", ";
0b9a969e 429 strbuf_addstr(out, "HEAD");
00449f99
JS
430 }
431 if (src_data->branch.nr) {
0b9a969e 432 strbuf_addstr(out, subsep);
00449f99 433 subsep = ", ";
0b9a969e
MV
434 print_joined("branch ", "branches ", &src_data->branch,
435 out);
00449f99
JS
436 }
437 if (src_data->r_branch.nr) {
0b9a969e 438 strbuf_addstr(out, subsep);
00449f99 439 subsep = ", ";
13931236 440 print_joined("remote-tracking branch ", "remote-tracking branches ",
0b9a969e 441 &src_data->r_branch, out);
00449f99
JS
442 }
443 if (src_data->tag.nr) {
0b9a969e 444 strbuf_addstr(out, subsep);
00449f99 445 subsep = ", ";
0b9a969e 446 print_joined("tag ", "tags ", &src_data->tag, out);
00449f99
JS
447 }
448 if (src_data->generic.nr) {
0b9a969e
MV
449 strbuf_addstr(out, subsep);
450 print_joined("commit ", "commits ", &src_data->generic,
451 out);
00449f99 452 }
fcb243f7
SB
453 if (strcmp(".", srcs.items[i].string))
454 strbuf_addf(out, " of %s", srcs.items[i].string);
00449f99
JS
455 }
456
457 if (!strcmp("master", current_branch))
0b9a969e 458 strbuf_addch(out, '\n');
00449f99 459 else
0b9a969e 460 strbuf_addf(out, " into %s\n", current_branch);
403994e8
TRC
461}
462
895680f0
JH
463static void fmt_tag_signature(struct strbuf *tagbuf,
464 struct strbuf *sig,
465 const char *buf,
466 unsigned long len)
467{
468 const char *tag_body = strstr(buf, "\n\n");
469 if (tag_body) {
470 tag_body += 2;
471 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
472 }
473 strbuf_complete_line(tagbuf);
a3347b98
LT
474 if (sig->len) {
475 strbuf_addch(tagbuf, '\n');
eff80a9f 476 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
a3347b98 477 }
895680f0
JH
478}
479
480static void fmt_merge_msg_sigs(struct strbuf *out)
481{
482 int i, tag_number = 0, first_tag = 0;
483 struct strbuf tagbuf = STRBUF_INIT;
484
485 for (i = 0; i < origins.nr; i++) {
486 unsigned char *sha1 = origins.items[i].util;
487 enum object_type type;
488 unsigned long size, len;
489 char *buf = read_sha1_file(sha1, &type, &size);
490 struct strbuf sig = STRBUF_INIT;
491
492 if (!buf || type != OBJ_TAG)
493 goto next;
494 len = parse_signature(buf, size);
495
496 if (size == len)
497 ; /* merely annotated */
9cc4ac8f 498 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {
895680f0
JH
499 if (!sig.len)
500 strbuf_addstr(&sig, "gpg verification failed.\n");
501 }
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;
89c3bbd8
RT
509 strbuf_addch(&tagline, '\n');
510 strbuf_add_commented_lines(&tagline,
511 origins.items[first_tag].string,
512 strlen(origins.items[first_tag].string));
895680f0
JH
513 strbuf_insert(&tagbuf, 0, tagline.buf,
514 tagline.len);
515 strbuf_release(&tagline);
516 }
89c3bbd8
RT
517 strbuf_addch(&tagbuf, '\n');
518 strbuf_add_commented_lines(&tagbuf,
519 origins.items[i].string,
520 strlen(origins.items[i].string));
895680f0
JH
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
5802f81b
JH
534static void find_merge_parents(struct merge_parents *result,
535 struct strbuf *in, unsigned char *head)
536{
e510ab89 537 struct commit_list *parents;
5802f81b
JH
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 unsigned char sha1[20];
547 struct commit *parent;
548 struct object *obj;
549
550 len = newline ? newline - p : strlen(p);
551 pos += len + !!newline;
552
553 if (len < 43 ||
554 get_sha1_hex(p, sha1) ||
555 p[40] != '\t' ||
556 p[41] != '\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(sha1);
564 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
565 if (!parent)
566 continue;
567 commit_list_insert(parent, &parents);
ed1c9977 568 add_merge_parent(result, obj->oid.hash, parent->object.oid.hash);
5802f81b
JH
569 }
570 head_commit = lookup_commit(head);
571 if (head_commit)
572 commit_list_insert(head_commit, &parents);
573 parents = reduce_heads(parents);
574
575 while (parents) {
e510ab89 576 struct commit *cmit = pop_commit(&parents);
5802f81b 577 for (i = 0; i < result->nr; i++)
ed1c9977 578 if (!hashcmp(result->item[i].commit, cmit->object.oid.hash))
5802f81b 579 result->item[i].used = 1;
5802f81b
JH
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
cbda121c
JH
592int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
593 struct fmt_merge_msg_opts *opts)
594{
1876166a 595 int i = 0, pos = 0;
403994e8
TRC
596 unsigned char head_sha1[20];
597 const char *current_branch;
96ec7b1e 598 void *current_branch_to_free;
5802f81b
JH
599 struct merge_parents merge_parents;
600
601 memset(&merge_parents, 0, sizeof(merge_parents));
403994e8
TRC
602
603 /* get current branch */
96ec7b1e 604 current_branch = current_branch_to_free =
7695d118 605 resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
403994e8
TRC
606 if (!current_branch)
607 die("No current branch");
59556548 608 if (starts_with(current_branch, "refs/heads/"))
403994e8
TRC
609 current_branch += 11;
610
5802f81b
JH
611 find_merge_parents(&merge_parents, in, head_sha1);
612
403994e8
TRC
613 /* get a line */
614 while (pos < in->len) {
615 int len;
616 char *newline, *p = in->buf + pos;
617
618 newline = strchr(p, '\n');
619 len = newline ? newline - p : strlen(p);
620 pos += len + !!newline;
621 i++;
622 p[len] = 0;
5802f81b 623 if (handle_line(p, &merge_parents))
403994e8
TRC
624 die ("Error in line %d: %.*s", i, len, p);
625 }
626
cbda121c
JH
627 if (opts->add_title && srcs.nr)
628 fmt_merge_msg_title(out, current_branch);
403994e8 629
895680f0
JH
630 if (origins.nr)
631 fmt_merge_msg_sigs(out);
00449f99 632
cbda121c 633 if (opts->shortlog_len) {
00449f99
JS
634 struct commit *head;
635 struct rev_info rev;
636
baf18fc2 637 head = lookup_commit_or_die(head_sha1, "HEAD");
0b9a969e 638 init_revisions(&rev, NULL);
00449f99
JS
639 rev.commit_format = CMIT_FMT_ONELINE;
640 rev.ignore_merges = 1;
641 rev.limited = 1;
642
a3347b98 643 strbuf_complete_line(out);
f0ecac2b 644
00449f99 645 for (i = 0; i < origins.nr; i++)
898eacd8
JH
646 shortlog(origins.items[i].string,
647 origins.items[i].util,
9bcbb1c2 648 head, &rev, opts, out);
00449f99 649 }
0b9a969e 650
895680f0 651 strbuf_complete_line(out);
96ec7b1e 652 free(current_branch_to_free);
5802f81b 653 free(merge_parents.item);
0b9a969e 654 return 0;
2234ec54
TRC
655}
656
0b9a969e
MV
657int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
658{
c8ef0383 659 const char *inpath = NULL;
2102440c 660 const char *message = NULL;
898eacd8 661 int shortlog_len = -1;
c8ef0383 662 struct option options[] = {
93eced6c
NTND
663 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
664 N_("populate log with at most <n> entries from shortlog"),
96e9420c 665 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
666 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
667 N_("alias for --log (deprecated)"),
96e9420c
RR
668 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
669 DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
670 OPT_STRING('m', "message", &message, N_("text"),
671 N_("use <text> as start of message")),
672 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
c8ef0383
PH
673 OPT_END()
674 };
675
0b9a969e 676 FILE *in = stdin;
f285a2d7 677 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
0b9a969e 678 int ret;
cbda121c 679 struct fmt_merge_msg_opts opts;
0b9a969e
MV
680
681 git_config(fmt_merge_msg_config, NULL);
37782920
SB
682 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
683 0);
c8ef0383
PH
684 if (argc > 0)
685 usage_with_options(fmt_merge_msg_usage, options);
898eacd8
JH
686 if (shortlog_len < 0)
687 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
c8ef0383
PH
688
689 if (inpath && strcmp(inpath, "-")) {
690 in = fopen(inpath, "r");
691 if (!in)
0721c314 692 die_errno("cannot open '%s'", inpath);
0b9a969e
MV
693 }
694
0b9a969e 695 if (strbuf_read(&input, fileno(in), 0) < 0)
d824cbba 696 die_errno("could not read input file");
1876166a
RR
697
698 if (message)
2102440c 699 strbuf_addstr(&output, message);
1876166a 700
cbda121c
JH
701 memset(&opts, 0, sizeof(opts));
702 opts.add_title = !message;
9bcbb1c2 703 opts.credit_people = 1;
cbda121c
JH
704 opts.shortlog_len = shortlog_len;
705
706 ret = fmt_merge_msg(&input, &output, &opts);
0b9a969e
MV
707 if (ret)
708 return ret;
c8ef0383 709 write_in_full(STDOUT_FILENO, output.buf, output.len);
00449f99
JS
710 return 0;
711}