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