]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fmt-merge-msg.c
commit-reach: move walk methods from commit.c
[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"
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
a74093da
SB
347 branch = deref_tag(the_repository, parse_object(the_repository, oid),
348 oid_to_hex(oid),
109cd76d 349 GIT_SHA1_HEXSZ);
1974632c 350 if (!branch || branch->type != OBJ_COMMIT)
00449f99
JS
351 return;
352
353 setup_revisions(0, NULL, rev, NULL);
00449f99
JS
354 add_pending_object(rev, branch, name);
355 add_pending_object(rev, &head->object, "^HEAD");
356 head->object.flags |= UNINTERESTING;
3d51e1b5
MK
357 if (prepare_revision_walk(rev))
358 die("revision walk setup failed");
00449f99 359 while ((commit = get_revision(rev)) != NULL) {
15cb5007 360 struct pretty_print_context ctx = {0};
00449f99 361
418a1435
JH
362 if (commit->parents && commit->parents->next) {
363 /* do not list a merge but count committer */
9bcbb1c2
JH
364 if (opts->credit_people)
365 record_person('c', &committers, commit);
00449f99 366 continue;
418a1435 367 }
9bcbb1c2 368 if (!count && opts->credit_people)
418a1435
JH
369 /* the 'tip' committer */
370 record_person('c', &committers, commit);
9bcbb1c2
JH
371 if (opts->credit_people)
372 record_person('a', &authors, commit);
00449f99
JS
373 count++;
374 if (subjects.nr > limit)
375 continue;
376
15cb5007
SB
377 format_commit_message(commit, "%s", &sb, &ctx);
378 strbuf_ltrim(&sb);
6a28518a 379
15cb5007 380 if (!sb.len)
1d2f80fa 381 string_list_append(&subjects,
f2fd0760 382 oid_to_hex(&commit->object.oid));
15cb5007 383 else
addcf6cf
RS
384 string_list_append_nodup(&subjects,
385 strbuf_detach(&sb, NULL));
00449f99
JS
386 }
387
9bcbb1c2
JH
388 if (opts->credit_people)
389 add_people_info(out, &authors, &committers);
00449f99 390 if (count > limit)
0b9a969e 391 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
00449f99 392 else
0b9a969e 393 strbuf_addf(out, "\n* %s:\n", name);
00449f99 394
898eacd8
JH
395 if (origin_data->is_local_branch && use_branch_desc)
396 add_branch_desc(out, name);
397
00449f99
JS
398 for (i = 0; i < subjects.nr; i++)
399 if (i >= limit)
a22ae753 400 strbuf_addstr(out, " ...\n");
00449f99 401 else
fcb243f7 402 strbuf_addf(out, " %s\n", subjects.items[i].string);
00449f99
JS
403
404 clear_commit_marks((struct commit *)branch, flags);
405 clear_commit_marks(head, flags);
406 free_commit_list(rev->commits);
407 rev->commits = NULL;
408 rev->pending.nr = 0;
409
418a1435
JH
410 string_list_clear(&authors, 0);
411 string_list_clear(&committers, 0);
fcb243f7 412 string_list_clear(&subjects, 0);
00449f99
JS
413}
414
cbda121c 415static void fmt_merge_msg_title(struct strbuf *out,
edd64ef4
DC
416 const char *current_branch)
417{
403994e8 418 int i = 0;
fd13b21f 419 char *sep = "";
419fe5bc 420
0b9a969e 421 strbuf_addstr(out, "Merge ");
00449f99 422 for (i = 0; i < srcs.nr; i++) {
fcb243f7 423 struct src_data *src_data = srcs.items[i].util;
00449f99
JS
424 const char *subsep = "";
425
0b9a969e 426 strbuf_addstr(out, sep);
00449f99
JS
427 sep = "; ";
428
429 if (src_data->head_status == 1) {
fcb243f7 430 strbuf_addstr(out, srcs.items[i].string);
00449f99
JS
431 continue;
432 }
433 if (src_data->head_status == 3) {
434 subsep = ", ";
0b9a969e 435 strbuf_addstr(out, "HEAD");
00449f99
JS
436 }
437 if (src_data->branch.nr) {
0b9a969e 438 strbuf_addstr(out, subsep);
00449f99 439 subsep = ", ";
0b9a969e
MV
440 print_joined("branch ", "branches ", &src_data->branch,
441 out);
00449f99
JS
442 }
443 if (src_data->r_branch.nr) {
0b9a969e 444 strbuf_addstr(out, subsep);
00449f99 445 subsep = ", ";
13931236 446 print_joined("remote-tracking branch ", "remote-tracking branches ",
0b9a969e 447 &src_data->r_branch, out);
00449f99
JS
448 }
449 if (src_data->tag.nr) {
0b9a969e 450 strbuf_addstr(out, subsep);
00449f99 451 subsep = ", ";
0b9a969e 452 print_joined("tag ", "tags ", &src_data->tag, out);
00449f99
JS
453 }
454 if (src_data->generic.nr) {
0b9a969e
MV
455 strbuf_addstr(out, subsep);
456 print_joined("commit ", "commits ", &src_data->generic,
457 out);
00449f99 458 }
fcb243f7
SB
459 if (strcmp(".", srcs.items[i].string))
460 strbuf_addf(out, " of %s", srcs.items[i].string);
00449f99
JS
461 }
462
463 if (!strcmp("master", current_branch))
0b9a969e 464 strbuf_addch(out, '\n');
00449f99 465 else
0b9a969e 466 strbuf_addf(out, " into %s\n", current_branch);
403994e8
TRC
467}
468
895680f0
JH
469static void fmt_tag_signature(struct strbuf *tagbuf,
470 struct strbuf *sig,
471 const char *buf,
472 unsigned long len)
473{
474 const char *tag_body = strstr(buf, "\n\n");
475 if (tag_body) {
476 tag_body += 2;
477 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
478 }
479 strbuf_complete_line(tagbuf);
a3347b98
LT
480 if (sig->len) {
481 strbuf_addch(tagbuf, '\n');
eff80a9f 482 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
a3347b98 483 }
895680f0
JH
484}
485
486static void fmt_merge_msg_sigs(struct strbuf *out)
487{
488 int i, tag_number = 0, first_tag = 0;
489 struct strbuf tagbuf = STRBUF_INIT;
490
491 for (i = 0; i < origins.nr; i++) {
f8ddeff5 492 struct object_id *oid = origins.items[i].util;
895680f0
JH
493 enum object_type type;
494 unsigned long size, len;
b4f5aca4 495 char *buf = read_object_file(oid, &type, &size);
895680f0
JH
496 struct strbuf sig = STRBUF_INIT;
497
498 if (!buf || type != OBJ_TAG)
499 goto next;
500 len = parse_signature(buf, size);
501
502 if (size == len)
503 ; /* merely annotated */
9cc4ac8f 504 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {
895680f0
JH
505 if (!sig.len)
506 strbuf_addstr(&sig, "gpg verification failed.\n");
507 }
508
509 if (!tag_number++) {
510 fmt_tag_signature(&tagbuf, &sig, buf, len);
511 first_tag = i;
512 } else {
513 if (tag_number == 2) {
514 struct strbuf tagline = STRBUF_INIT;
89c3bbd8
RT
515 strbuf_addch(&tagline, '\n');
516 strbuf_add_commented_lines(&tagline,
517 origins.items[first_tag].string,
518 strlen(origins.items[first_tag].string));
895680f0
JH
519 strbuf_insert(&tagbuf, 0, tagline.buf,
520 tagline.len);
521 strbuf_release(&tagline);
522 }
89c3bbd8
RT
523 strbuf_addch(&tagbuf, '\n');
524 strbuf_add_commented_lines(&tagbuf,
525 origins.items[i].string,
526 strlen(origins.items[i].string));
895680f0
JH
527 fmt_tag_signature(&tagbuf, &sig, buf, len);
528 }
529 strbuf_release(&sig);
530 next:
531 free(buf);
532 }
533 if (tagbuf.len) {
534 strbuf_addch(out, '\n');
535 strbuf_addbuf(out, &tagbuf);
536 }
537 strbuf_release(&tagbuf);
538}
539
5802f81b 540static void find_merge_parents(struct merge_parents *result,
175ccdcf 541 struct strbuf *in, struct object_id *head)
5802f81b 542{
e510ab89 543 struct commit_list *parents;
5802f81b
JH
544 struct commit *head_commit;
545 int pos = 0, i, j;
546
547 parents = NULL;
548 while (pos < in->len) {
549 int len;
550 char *p = in->buf + pos;
551 char *newline = strchr(p, '\n');
175ccdcf 552 struct object_id oid;
5802f81b
JH
553 struct commit *parent;
554 struct object *obj;
555
556 len = newline ? newline - p : strlen(p);
557 pos += len + !!newline;
558
175ccdcf 559 if (len < GIT_SHA1_HEXSZ + 3 ||
560 get_oid_hex(p, &oid) ||
561 p[GIT_SHA1_HEXSZ] != '\t' ||
562 p[GIT_SHA1_HEXSZ + 1] != '\t')
5802f81b
JH
563 continue; /* skip not-for-merge */
564 /*
565 * Do not use get_merge_parent() here; we do not have
566 * "name" here and we do not want to contaminate its
567 * util field yet.
568 */
109cd76d 569 obj = parse_object(the_repository, &oid);
5802f81b
JH
570 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
571 if (!parent)
572 continue;
573 commit_list_insert(parent, &parents);
175ccdcf 574 add_merge_parent(result, &obj->oid, &parent->object.oid);
5802f81b 575 }
c1f5eb49 576 head_commit = lookup_commit(the_repository, head);
5802f81b
JH
577 if (head_commit)
578 commit_list_insert(head_commit, &parents);
4da72644 579 reduce_heads_replace(&parents);
5802f81b
JH
580
581 while (parents) {
e510ab89 582 struct commit *cmit = pop_commit(&parents);
5802f81b 583 for (i = 0; i < result->nr; i++)
175ccdcf 584 if (!oidcmp(&result->item[i].commit, &cmit->object.oid))
5802f81b 585 result->item[i].used = 1;
5802f81b
JH
586 }
587
588 for (i = j = 0; i < result->nr; i++) {
589 if (result->item[i].used) {
590 if (i != j)
591 result->item[j] = result->item[i];
592 j++;
593 }
594 }
595 result->nr = j;
596}
597
cbda121c
JH
598int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
599 struct fmt_merge_msg_opts *opts)
600{
1876166a 601 int i = 0, pos = 0;
175ccdcf 602 struct object_id head_oid;
403994e8 603 const char *current_branch;
96ec7b1e 604 void *current_branch_to_free;
5802f81b
JH
605 struct merge_parents merge_parents;
606
607 memset(&merge_parents, 0, sizeof(merge_parents));
403994e8
TRC
608
609 /* get current branch */
96ec7b1e 610 current_branch = current_branch_to_free =
0f2dc722 611 resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL);
403994e8
TRC
612 if (!current_branch)
613 die("No current branch");
59556548 614 if (starts_with(current_branch, "refs/heads/"))
403994e8
TRC
615 current_branch += 11;
616
175ccdcf 617 find_merge_parents(&merge_parents, in, &head_oid);
5802f81b 618
403994e8
TRC
619 /* get a line */
620 while (pos < in->len) {
621 int len;
622 char *newline, *p = in->buf + pos;
623
624 newline = strchr(p, '\n');
625 len = newline ? newline - p : strlen(p);
626 pos += len + !!newline;
627 i++;
628 p[len] = 0;
5802f81b 629 if (handle_line(p, &merge_parents))
403994e8
TRC
630 die ("Error in line %d: %.*s", i, len, p);
631 }
632
cbda121c
JH
633 if (opts->add_title && srcs.nr)
634 fmt_merge_msg_title(out, current_branch);
403994e8 635
895680f0
JH
636 if (origins.nr)
637 fmt_merge_msg_sigs(out);
00449f99 638
cbda121c 639 if (opts->shortlog_len) {
00449f99
JS
640 struct commit *head;
641 struct rev_info rev;
642
bc83266a 643 head = lookup_commit_or_die(&head_oid, "HEAD");
0b9a969e 644 init_revisions(&rev, NULL);
00449f99
JS
645 rev.commit_format = CMIT_FMT_ONELINE;
646 rev.ignore_merges = 1;
647 rev.limited = 1;
648
a3347b98 649 strbuf_complete_line(out);
f0ecac2b 650
00449f99 651 for (i = 0; i < origins.nr; i++)
898eacd8
JH
652 shortlog(origins.items[i].string,
653 origins.items[i].util,
9bcbb1c2 654 head, &rev, opts, out);
00449f99 655 }
0b9a969e 656
895680f0 657 strbuf_complete_line(out);
96ec7b1e 658 free(current_branch_to_free);
5802f81b 659 free(merge_parents.item);
0b9a969e 660 return 0;
2234ec54
TRC
661}
662
0b9a969e
MV
663int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
664{
c8ef0383 665 const char *inpath = NULL;
2102440c 666 const char *message = NULL;
898eacd8 667 int shortlog_len = -1;
c8ef0383 668 struct option options[] = {
93eced6c
NTND
669 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
670 N_("populate log with at most <n> entries from shortlog"),
96e9420c 671 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
672 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
673 N_("alias for --log (deprecated)"),
96e9420c
RR
674 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
675 DEFAULT_MERGE_LOG_LEN },
93eced6c
NTND
676 OPT_STRING('m', "message", &message, N_("text"),
677 N_("use <text> as start of message")),
678 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
c8ef0383
PH
679 OPT_END()
680 };
681
0b9a969e 682 FILE *in = stdin;
f285a2d7 683 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
0b9a969e 684 int ret;
cbda121c 685 struct fmt_merge_msg_opts opts;
0b9a969e
MV
686
687 git_config(fmt_merge_msg_config, NULL);
37782920
SB
688 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
689 0);
c8ef0383
PH
690 if (argc > 0)
691 usage_with_options(fmt_merge_msg_usage, options);
898eacd8
JH
692 if (shortlog_len < 0)
693 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
c8ef0383
PH
694
695 if (inpath && strcmp(inpath, "-")) {
696 in = fopen(inpath, "r");
697 if (!in)
0721c314 698 die_errno("cannot open '%s'", inpath);
0b9a969e
MV
699 }
700
0b9a969e 701 if (strbuf_read(&input, fileno(in), 0) < 0)
d824cbba 702 die_errno("could not read input file");
1876166a
RR
703
704 if (message)
2102440c 705 strbuf_addstr(&output, message);
1876166a 706
cbda121c
JH
707 memset(&opts, 0, sizeof(opts));
708 opts.add_title = !message;
9bcbb1c2 709 opts.credit_people = 1;
cbda121c
JH
710 opts.shortlog_len = shortlog_len;
711
712 ret = fmt_merge_msg(&input, &output, &opts);
0b9a969e
MV
713 if (ret)
714 return ret;
c8ef0383 715 write_in_full(STDOUT_FILENO, output.buf, output.len);
00449f99
JS
716 return 0;
717}