]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fmt-merge-msg.c
revision: add --grep-reflog to filter commits by reflog messages
[thirdparty/git.git] / builtin / fmt-merge-msg.c
CommitLineData
25f38f06 1#include "builtin.h"
00449f99
JS
2#include "cache.h"
3#include "commit.h"
4#include "diff.h"
5#include "revision.h"
6#include "tag.h"
fcb243f7 7#include "string-list.h"
898eacd8
JH
8#include "branch.h"
9#include "fmt-merge-msg.h"
895680f0 10#include "gpg-interface.h"
00449f99 11
c8ef0383 12static const char * const fmt_merge_msg_usage[] = {
93eced6c 13 N_("git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]"),
c8ef0383
PH
14 NULL
15};
00449f99 16
898eacd8 17static int use_branch_desc;
00449f99 18
898eacd8 19int fmt_merge_msg_config(const char *key, const char *value, void *cb)
00449f99 20{
bda3b8ff
RR
21 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
22 int is_bool;
898eacd8
JH
23 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
24 if (!is_bool && merge_log_config < 0)
bda3b8ff 25 return error("%s: negative length %s", key, value);
898eacd8
JH
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);
418a1435
JH
30 } else {
31 return git_default_config(key, value, cb);
6cd9cfef 32 }
00449f99
JS
33 return 0;
34}
35
4c0ea82d 36/* merge data per repository where the merged tips came from */
fcb243f7
SB
37struct src_data {
38 struct string_list branch, tag, r_branch, generic;
39 int head_status;
00449f99
JS
40};
41
898eacd8
JH
42struct origin_data {
43 unsigned char sha1[20];
90a321c0 44 unsigned is_local_branch:1;
898eacd8
JH
45};
46
c2e86add 47static void init_src_data(struct src_data *data)
00449f99 48{
fcb243f7
SB
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;
00449f99
JS
53}
54
183113a5
TF
55static struct string_list srcs = STRING_LIST_INIT_DUP;
56static struct string_list origins = STRING_LIST_INIT_DUP;
00449f99 57
5802f81b
JH
58struct 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;
65};
66
67/*
68 * I know, I know, this is inefficient, but you won't be pulling and merging
69 * hundreds of heads at a time anyway.
70 */
71static struct merge_parent *find_merge_parent(struct merge_parents *table,
72 unsigned char *given,
73 unsigned char *commit)
74{
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];
82 }
83 return NULL;
84}
85
86static void add_merge_parent(struct merge_parents *table,
87 unsigned char *given,
88 unsigned char *commit)
89{
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++;
97}
98
99static int handle_line(char *line, struct merge_parents *merge_parents)
00449f99
JS
100{
101 int i, len = strlen(line);
898eacd8 102 struct origin_data *origin_data;
00449f99
JS
103 char *src, *origin;
104 struct src_data *src_data;
fcb243f7 105 struct string_list_item *item;
e918c6ab 106 int pulling_head = 0;
5802f81b 107 unsigned char sha1[20];
00449f99
JS
108
109 if (len < 43 || line[40] != '\t')
110 return 1;
111
cc44c765 112 if (!prefixcmp(line + 41, "not-for-merge"))
00449f99
JS
113 return 0;
114
115 if (line[41] != '\t')
116 return 2;
117
5802f81b
JH
118 i = get_sha1_hex(line, sha1);
119 if (i)
00449f99 120 return 3;
5802f81b
JH
121
122 if (!find_merge_parent(merge_parents, sha1, NULL))
123 return 0; /* subsumed by other parents */
124
125 origin_data = xcalloc(1, sizeof(struct origin_data));
126 hashcpy(origin_data->sha1, sha1);
00449f99
JS
127
128 if (line[len - 1] == '\n')
129 line[len - 1] = 0;
130 line += 42;
131
4c0ea82d
JH
132 /*
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.
136 */
00449f99
JS
137 src = strstr(line, " of ");
138 if (src) {
139 *src = 0;
140 src += 4;
e918c6ab
JH
141 pulling_head = 0;
142 } else {
143 src = line;
144 pulling_head = 1;
145 }
00449f99 146
fcb243f7
SB
147 item = unsorted_string_list_lookup(&srcs, src);
148 if (!item) {
1d2f80fa 149 item = string_list_append(&srcs, src);
fcb243f7
SB
150 item->util = xcalloc(1, sizeof(struct src_data));
151 init_src_data(item->util);
00449f99 152 }
fcb243f7 153 src_data = item->util;
00449f99 154
e918c6ab 155 if (pulling_head) {
fcb243f7 156 origin = src;
e918c6ab 157 src_data->head_status |= 1;
cc44c765 158 } else if (!prefixcmp(line, "branch ")) {
898eacd8 159 origin_data->is_local_branch = 1;
fcb243f7 160 origin = line + 7;
1d2f80fa 161 string_list_append(&src_data->branch, origin);
00449f99 162 src_data->head_status |= 2;
cc44c765 163 } else if (!prefixcmp(line, "tag ")) {
00449f99 164 origin = line;
1d2f80fa 165 string_list_append(&src_data->tag, origin + 4);
00449f99 166 src_data->head_status |= 2;
13931236
MM
167 } else if (!prefixcmp(line, "remote-tracking branch ")) {
168 origin = line + strlen("remote-tracking branch ");
1d2f80fa 169 string_list_append(&src_data->r_branch, origin);
00449f99 170 src_data->head_status |= 2;
00449f99 171 } else {
fcb243f7 172 origin = src;
1d2f80fa 173 string_list_append(&src_data->generic, line);
00449f99
JS
174 src_data->head_status |= 2;
175 }
176
177 if (!strcmp(".", src) || !strcmp(src, origin)) {
178 int len = strlen(origin);
fcb243f7 179 if (origin[0] == '\'' && origin[len - 1] == '\'')
182af834 180 origin = xmemdupz(origin + 1, len - 2);
00449f99 181 } else {
2d7320d0 182 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
00449f99
JS
183 sprintf(new_origin, "%s of %s", origin, src);
184 origin = new_origin;
185 }
898eacd8
JH
186 if (strcmp(".", src))
187 origin_data->is_local_branch = 0;
188 string_list_append(&origins, origin)->util = origin_data;
00449f99
JS
189 return 0;
190}
191
192static void print_joined(const char *singular, const char *plural,
fcb243f7 193 struct string_list *list, struct strbuf *out)
00449f99
JS
194{
195 if (list->nr == 0)
196 return;
197 if (list->nr == 1) {
fcb243f7 198 strbuf_addf(out, "%s%s", singular, list->items[0].string);
00449f99
JS
199 } else {
200 int i;
0b9a969e 201 strbuf_addstr(out, plural);
00449f99 202 for (i = 0; i < list->nr - 1; i++)
fcb243f7
SB
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);
00449f99
JS
206 }
207}
208
898eacd8
JH
209static void add_branch_desc(struct strbuf *out, const char *name)
210{
211 struct strbuf desc = STRBUF_INIT;
212
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;
221 }
222 if (out->buf[out->len - 1] != '\n')
223 strbuf_addch(out, '\n');
224 }
225 strbuf_release(&desc);
226}
227
418a1435
JH
228#define util_as_integral(elem) ((intptr_t)((elem)->util))
229
230static void record_person(int which, struct string_list *people,
231 struct commit *commit)
232{
e21ab134 233 char *name_buf, *name, *name_end;
418a1435
JH
234 struct string_list_item *elem;
235 const char *field = (which == 'a') ? "\nauthor " : "\ncommitter ";
236
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--;
e21ab134 246 if (name_end < name)
418a1435 247 return;
e21ab134 248 name_buf = xmemdupz(name, name_end - name + 1);
418a1435
JH
249
250 elem = string_list_lookup(people, name_buf);
251 if (!elem) {
252 elem = string_list_insert(people, name_buf);
253 elem->util = (void *)0;
254 }
255 elem->util = (void*)(util_as_integral(elem) + 1);
e21ab134 256 free(name_buf);
418a1435
JH
257}
258
259static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
260{
261 const struct string_list_item *a = a_, *b = b_;
262 return util_as_integral(b) - util_as_integral(a);
263}
264
265static void add_people_count(struct strbuf *out, struct string_list *people)
266{
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]));
279}
280
281static void credit_people(struct strbuf *out,
282 struct string_list *them,
283 int kind)
284{
285 const char *label;
286 const char *me;
287
288 if (kind == 'a') {
9830a9ca 289 label = "\n# By ";
418a1435
JH
290 me = git_author_info(IDENT_NO_DATE);
291 } else {
9830a9ca 292 label = "\n# Via ";
418a1435
JH
293 me = git_committer_info(IDENT_NO_DATE);
294 }
295
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);
304}
305
306static void add_people_info(struct strbuf *out,
307 struct string_list *authors,
308 struct string_list *committers)
309{
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);
318
319 credit_people(out, authors, 'a');
320 credit_people(out, committers, 'c');
321}
322
898eacd8
JH
323static 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)
00449f99
JS
328{
329 int i, count = 0;
330 struct commit *commit;
331 struct object *branch;
183113a5 332 struct string_list subjects = STRING_LIST_INIT_DUP;
418a1435
JH
333 struct string_list authors = STRING_LIST_INIT_DUP;
334 struct string_list committers = STRING_LIST_INIT_DUP;
7dc0fe3b 335 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
15cb5007 336 struct strbuf sb = STRBUF_INIT;
898eacd8 337 const unsigned char *sha1 = origin_data->sha1;
00449f99
JS
338
339 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
1974632c 340 if (!branch || branch->type != OBJ_COMMIT)
00449f99
JS
341 return;
342
343 setup_revisions(0, NULL, rev, NULL);
00449f99
JS
344 add_pending_object(rev, branch, name);
345 add_pending_object(rev, &head->object, "^HEAD");
346 head->object.flags |= UNINTERESTING;
3d51e1b5
MK
347 if (prepare_revision_walk(rev))
348 die("revision walk setup failed");
00449f99 349 while ((commit = get_revision(rev)) != NULL) {
15cb5007 350 struct pretty_print_context ctx = {0};
00449f99 351
418a1435
JH
352 if (commit->parents && commit->parents->next) {
353 /* do not list a merge but count committer */
354 record_person('c', &committers, commit);
00449f99 355 continue;
418a1435
JH
356 }
357 if (!count)
358 /* the 'tip' committer */
359 record_person('c', &committers, commit);
360 record_person('a', &authors, commit);
00449f99
JS
361 count++;
362 if (subjects.nr > limit)
363 continue;
364
15cb5007
SB
365 format_commit_message(commit, "%s", &sb, &ctx);
366 strbuf_ltrim(&sb);
6a28518a 367
15cb5007 368 if (!sb.len)
1d2f80fa
JP
369 string_list_append(&subjects,
370 sha1_to_hex(commit->object.sha1));
15cb5007 371 else
1d2f80fa 372 string_list_append(&subjects, strbuf_detach(&sb, NULL));
00449f99
JS
373 }
374
418a1435 375 add_people_info(out, &authors, &committers);
00449f99 376 if (count > limit)
0b9a969e 377 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
00449f99 378 else
0b9a969e 379 strbuf_addf(out, "\n* %s:\n", name);
00449f99 380
898eacd8
JH
381 if (origin_data->is_local_branch && use_branch_desc)
382 add_branch_desc(out, name);
383
00449f99
JS
384 for (i = 0; i < subjects.nr; i++)
385 if (i >= limit)
0b9a969e 386 strbuf_addf(out, " ...\n");
00449f99 387 else
fcb243f7 388 strbuf_addf(out, " %s\n", subjects.items[i].string);
00449f99
JS
389
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;
395
418a1435
JH
396 string_list_clear(&authors, 0);
397 string_list_clear(&committers, 0);
fcb243f7 398 string_list_clear(&subjects, 0);
00449f99
JS
399}
400
cbda121c 401static void fmt_merge_msg_title(struct strbuf *out,
403994e8
TRC
402 const char *current_branch) {
403 int i = 0;
fd13b21f 404 char *sep = "";
419fe5bc 405
0b9a969e 406 strbuf_addstr(out, "Merge ");
00449f99 407 for (i = 0; i < srcs.nr; i++) {
fcb243f7 408 struct src_data *src_data = srcs.items[i].util;
00449f99
JS
409 const char *subsep = "";
410
0b9a969e 411 strbuf_addstr(out, sep);
00449f99
JS
412 sep = "; ";
413
414 if (src_data->head_status == 1) {
fcb243f7 415 strbuf_addstr(out, srcs.items[i].string);
00449f99
JS
416 continue;
417 }
418 if (src_data->head_status == 3) {
419 subsep = ", ";
0b9a969e 420 strbuf_addstr(out, "HEAD");
00449f99
JS
421 }
422 if (src_data->branch.nr) {
0b9a969e 423 strbuf_addstr(out, subsep);
00449f99 424 subsep = ", ";
0b9a969e
MV
425 print_joined("branch ", "branches ", &src_data->branch,
426 out);
00449f99
JS
427 }
428 if (src_data->r_branch.nr) {
0b9a969e 429 strbuf_addstr(out, subsep);
00449f99 430 subsep = ", ";
13931236 431 print_joined("remote-tracking branch ", "remote-tracking branches ",
0b9a969e 432 &src_data->r_branch, out);
00449f99
JS
433 }
434 if (src_data->tag.nr) {
0b9a969e 435 strbuf_addstr(out, subsep);
00449f99 436 subsep = ", ";
0b9a969e 437 print_joined("tag ", "tags ", &src_data->tag, out);
00449f99
JS
438 }
439 if (src_data->generic.nr) {
0b9a969e
MV
440 strbuf_addstr(out, subsep);
441 print_joined("commit ", "commits ", &src_data->generic,
442 out);
00449f99 443 }
fcb243f7
SB
444 if (strcmp(".", srcs.items[i].string))
445 strbuf_addf(out, " of %s", srcs.items[i].string);
00449f99
JS
446 }
447
448 if (!strcmp("master", current_branch))
0b9a969e 449 strbuf_addch(out, '\n');
00449f99 450 else
0b9a969e 451 strbuf_addf(out, " into %s\n", current_branch);
403994e8
TRC
452}
453
895680f0
JH
454static void fmt_tag_signature(struct strbuf *tagbuf,
455 struct strbuf *sig,
456 const char *buf,
457 unsigned long len)
458{
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);
463 }
464 strbuf_complete_line(tagbuf);
a3347b98
LT
465 if (sig->len) {
466 strbuf_addch(tagbuf, '\n');
467 strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
468 }
895680f0
JH
469}
470
471static void fmt_merge_msg_sigs(struct strbuf *out)
472{
473 int i, tag_number = 0, first_tag = 0;
474 struct strbuf tagbuf = STRBUF_INIT;
475
476 for (i = 0; i < origins.nr; i++) {
477 unsigned char *sha1 = origins.items[i].util;
478 enum object_type type;
479 unsigned long size, len;
480 char *buf = read_sha1_file(sha1, &type, &size);
481 struct strbuf sig = STRBUF_INIT;
482
483 if (!buf || type != OBJ_TAG)
484 goto next;
485 len = parse_signature(buf, size);
486
487 if (size == len)
488 ; /* merely annotated */
489 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
490 if (!sig.len)
491 strbuf_addstr(&sig, "gpg verification failed.\n");
492 }
493
494 if (!tag_number++) {
495 fmt_tag_signature(&tagbuf, &sig, buf, len);
496 first_tag = i;
497 } else {
498 if (tag_number == 2) {
499 struct strbuf tagline = STRBUF_INIT;
500 strbuf_addf(&tagline, "\n# %s\n",
501 origins.items[first_tag].string);
502 strbuf_insert(&tagbuf, 0, tagline.buf,
503 tagline.len);
504 strbuf_release(&tagline);
505 }
506 strbuf_addf(&tagbuf, "\n# %s\n",
507 origins.items[i].string);
508 fmt_tag_signature(&tagbuf, &sig, buf, len);
509 }
510 strbuf_release(&sig);
511 next:
512 free(buf);
513 }
514 if (tagbuf.len) {
515 strbuf_addch(out, '\n');
516 strbuf_addbuf(out, &tagbuf);
517 }
518 strbuf_release(&tagbuf);
519}
520
5802f81b
JH
521static void find_merge_parents(struct merge_parents *result,
522 struct strbuf *in, unsigned char *head)
523{
524 struct commit_list *parents, *next;
525 struct commit *head_commit;
526 int pos = 0, i, j;
527
528 parents = NULL;
529 while (pos < in->len) {
530 int len;
531 char *p = in->buf + pos;
532 char *newline = strchr(p, '\n');
533 unsigned char sha1[20];
534 struct commit *parent;
535 struct object *obj;
536
537 len = newline ? newline - p : strlen(p);
538 pos += len + !!newline;
539
540 if (len < 43 ||
541 get_sha1_hex(p, sha1) ||
542 p[40] != '\t' ||
543 p[41] != '\t')
544 continue; /* skip not-for-merge */
545 /*
546 * Do not use get_merge_parent() here; we do not have
547 * "name" here and we do not want to contaminate its
548 * util field yet.
549 */
550 obj = parse_object(sha1);
551 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
552 if (!parent)
553 continue;
554 commit_list_insert(parent, &parents);
555 add_merge_parent(result, obj->sha1, parent->object.sha1);
556 }
557 head_commit = lookup_commit(head);
558 if (head_commit)
559 commit_list_insert(head_commit, &parents);
560 parents = reduce_heads(parents);
561
562 while (parents) {
563 for (i = 0; i < result->nr; i++)
564 if (!hashcmp(result->item[i].commit,
565 parents->item->object.sha1))
566 result->item[i].used = 1;
567 next = parents->next;
568 free(parents);
569 parents = next;
570 }
571
572 for (i = j = 0; i < result->nr; i++) {
573 if (result->item[i].used) {
574 if (i != j)
575 result->item[j] = result->item[i];
576 j++;
577 }
578 }
579 result->nr = j;
580}
581
cbda121c
JH
582int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
583 struct fmt_merge_msg_opts *opts)
584{
1876166a 585 int i = 0, pos = 0;
403994e8
TRC
586 unsigned char head_sha1[20];
587 const char *current_branch;
96ec7b1e 588 void *current_branch_to_free;
5802f81b
JH
589 struct merge_parents merge_parents;
590
591 memset(&merge_parents, 0, sizeof(merge_parents));
403994e8
TRC
592
593 /* get current branch */
96ec7b1e
NTND
594 current_branch = current_branch_to_free =
595 resolve_refdup("HEAD", head_sha1, 1, NULL);
403994e8
TRC
596 if (!current_branch)
597 die("No current branch");
598 if (!prefixcmp(current_branch, "refs/heads/"))
599 current_branch += 11;
600
5802f81b
JH
601 find_merge_parents(&merge_parents, in, head_sha1);
602
403994e8
TRC
603 /* get a line */
604 while (pos < in->len) {
605 int len;
606 char *newline, *p = in->buf + pos;
607
608 newline = strchr(p, '\n');
609 len = newline ? newline - p : strlen(p);
610 pos += len + !!newline;
611 i++;
612 p[len] = 0;
5802f81b 613 if (handle_line(p, &merge_parents))
403994e8
TRC
614 die ("Error in line %d: %.*s", i, len, p);
615 }
616
cbda121c
JH
617 if (opts->add_title && srcs.nr)
618 fmt_merge_msg_title(out, current_branch);
403994e8 619
895680f0
JH
620 if (origins.nr)
621 fmt_merge_msg_sigs(out);
00449f99 622
cbda121c 623 if (opts->shortlog_len) {
00449f99
JS
624 struct commit *head;
625 struct rev_info rev;
626
baf18fc2 627 head = lookup_commit_or_die(head_sha1, "HEAD");
0b9a969e 628 init_revisions(&rev, NULL);
00449f99
JS
629 rev.commit_format = CMIT_FMT_ONELINE;
630 rev.ignore_merges = 1;
631 rev.limited = 1;
632
a3347b98 633 strbuf_complete_line(out);
f0ecac2b 634
00449f99 635 for (i = 0; i < origins.nr; i++)
898eacd8
JH
636 shortlog(origins.items[i].string,
637 origins.items[i].util,
cbda121c 638 head, &rev, opts->shortlog_len, out);
00449f99 639 }
0b9a969e 640
895680f0 641 strbuf_complete_line(out);
96ec7b1e 642 free(current_branch_to_free);
5802f81b 643 free(merge_parents.item);
0b9a969e 644 return 0;
2234ec54
TRC
645}
646
0b9a969e
MV
647int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
648{
c8ef0383 649 const char *inpath = NULL;
2102440c 650 const char *message = NULL;
898eacd8 651 int shortlog_len = -1;
c8ef0383 652 struct option options[] = {
93eced6c
NTND
653 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
654 N_("populate log with at most <n> entries from shortlog"),
96e9420c 655 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
656 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
657 N_("alias for --log (deprecated)"),
96e9420c
RR
658 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
659 DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
660 OPT_STRING('m', "message", &message, N_("text"),
661 N_("use <text> as start of message")),
662 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
c8ef0383
PH
663 OPT_END()
664 };
665
0b9a969e 666 FILE *in = stdin;
f285a2d7 667 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
0b9a969e 668 int ret;
cbda121c 669 struct fmt_merge_msg_opts opts;
0b9a969e
MV
670
671 git_config(fmt_merge_msg_config, NULL);
37782920
SB
672 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
673 0);
c8ef0383
PH
674 if (argc > 0)
675 usage_with_options(fmt_merge_msg_usage, options);
898eacd8
JH
676 if (shortlog_len < 0)
677 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
c8ef0383
PH
678
679 if (inpath && strcmp(inpath, "-")) {
680 in = fopen(inpath, "r");
681 if (!in)
0721c314 682 die_errno("cannot open '%s'", inpath);
0b9a969e
MV
683 }
684
0b9a969e 685 if (strbuf_read(&input, fileno(in), 0) < 0)
d824cbba 686 die_errno("could not read input file");
1876166a
RR
687
688 if (message)
2102440c 689 strbuf_addstr(&output, message);
1876166a 690
cbda121c
JH
691 memset(&opts, 0, sizeof(opts));
692 opts.add_title = !message;
693 opts.shortlog_len = shortlog_len;
694
695 ret = fmt_merge_msg(&input, &output, &opts);
0b9a969e
MV
696 if (ret)
697 return ret;
c8ef0383 698 write_in_full(STDOUT_FILENO, output.buf, output.len);
00449f99
JS
699 return 0;
700}