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