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