5 #include "object-store.h"
10 #include "string-list.h"
12 #include "fmt-merge-msg.h"
13 #include "gpg-interface.h"
14 #include "repository.h"
15 #include "commit-reach.h"
17 static const char * const fmt_merge_msg_usage
[] = {
18 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
22 static int use_branch_desc
;
24 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
26 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
28 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
29 if (!is_bool
&& merge_log_config
< 0)
30 return error("%s: negative length %s", key
, value
);
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
);
36 return git_default_config(key
, value
, cb
);
41 /* merge data per repository where the merged tips came from */
43 struct string_list branch
, tag
, r_branch
, generic
;
49 unsigned is_local_branch
:1;
52 static void init_src_data(struct src_data
*data
)
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;
60 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
61 static struct string_list origins
= STRING_LIST_INIT_DUP
;
63 struct merge_parents
{
66 struct object_id given
;
67 struct object_id commit
;
73 * I know, I know, this is inefficient, but you won't be pulling and merging
74 * hundreds of heads at a time anyway.
76 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
77 struct object_id
*given
,
78 struct object_id
*commit
)
81 for (i
= 0; i
< table
->nr
; i
++) {
82 if (given
&& !oideq(&table
->item
[i
].given
, given
))
84 if (commit
&& !oideq(&table
->item
[i
].commit
, commit
))
86 return &table
->item
[i
];
91 static void add_merge_parent(struct merge_parents
*table
,
92 struct object_id
*given
,
93 struct object_id
*commit
)
95 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
97 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
98 oidcpy(&table
->item
[table
->nr
].given
, given
);
99 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
100 table
->item
[table
->nr
].used
= 0;
104 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
106 int i
, len
= strlen(line
);
107 struct origin_data
*origin_data
;
110 struct src_data
*src_data
;
111 struct string_list_item
*item
;
112 int pulling_head
= 0;
113 struct object_id oid
;
114 const unsigned hexsz
= the_hash_algo
->hexsz
;
116 if (len
< hexsz
+ 3 || line
[hexsz
] != '\t')
119 if (starts_with(line
+ hexsz
+ 1, "not-for-merge"))
122 if (line
[hexsz
+ 1] != '\t')
125 i
= get_oid_hex(line
, &oid
);
129 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
130 return 0; /* subsumed by other parents */
132 origin_data
= xcalloc(1, sizeof(struct origin_data
));
133 oidcpy(&origin_data
->oid
, &oid
);
135 if (line
[len
- 1] == '\n')
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.
144 src
= strstr(line
, " of ");
154 item
= unsorted_string_list_lookup(&srcs
, src
);
156 item
= string_list_append(&srcs
, src
);
157 item
->util
= xcalloc(1, sizeof(struct src_data
));
158 init_src_data(item
->util
);
160 src_data
= item
->util
;
164 src_data
->head_status
|= 1;
165 } else if (starts_with(line
, "branch ")) {
166 origin_data
->is_local_branch
= 1;
168 string_list_append(&src_data
->branch
, origin
);
169 src_data
->head_status
|= 2;
170 } else if (starts_with(line
, "tag ")) {
172 string_list_append(&src_data
->tag
, origin
+ 4);
173 src_data
->head_status
|= 2;
174 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
175 string_list_append(&src_data
->r_branch
, origin
);
176 src_data
->head_status
|= 2;
179 string_list_append(&src_data
->generic
, line
);
180 src_data
->head_status
|= 2;
183 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
184 int len
= strlen(origin
);
185 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
186 origin
= xmemdupz(origin
+ 1, len
- 2);
188 origin
= xstrfmt("%s of %s", origin
, src
);
189 if (strcmp(".", src
))
190 origin_data
->is_local_branch
= 0;
191 string_list_append(&origins
, origin
)->util
= origin_data
;
195 static void print_joined(const char *singular
, const char *plural
,
196 struct string_list
*list
, struct strbuf
*out
)
201 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
204 strbuf_addstr(out
, plural
);
205 for (i
= 0; i
< list
->nr
- 1; i
++)
206 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
207 list
->items
[i
].string
);
208 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
212 static void add_branch_desc(struct strbuf
*out
, const char *name
)
214 struct strbuf desc
= STRBUF_INIT
;
216 if (!read_branch_desc(&desc
, name
)) {
217 const char *bp
= desc
.buf
;
219 const char *ep
= strchrnul(bp
, '\n');
222 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
225 strbuf_complete_line(out
);
227 strbuf_release(&desc
);
230 #define util_as_integral(elem) ((intptr_t)((elem)->util))
232 static void record_person_from_buf(int which
, struct string_list
*people
,
235 char *name_buf
, *name
, *name_end
;
236 struct string_list_item
*elem
;
239 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
240 name
= strstr(buffer
, field
);
243 name
+= strlen(field
);
244 name_end
= strchrnul(name
, '<');
247 while (isspace(*name_end
) && name
<= name_end
)
251 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
253 elem
= string_list_lookup(people
, name_buf
);
255 elem
= string_list_insert(people
, name_buf
);
256 elem
->util
= (void *)0;
258 elem
->util
= (void*)(util_as_integral(elem
) + 1);
263 static void record_person(int which
, struct string_list
*people
,
264 struct commit
*commit
)
266 const char *buffer
= get_commit_buffer(commit
, NULL
);
267 record_person_from_buf(which
, people
, buffer
);
268 unuse_commit_buffer(commit
, buffer
);
271 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
273 const struct string_list_item
*a
= a_
, *b
= b_
;
274 return util_as_integral(b
) - util_as_integral(a
);
277 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
280 strbuf_addstr(out
, people
->items
[0].string
);
281 else if (people
->nr
== 2)
282 strbuf_addf(out
, "%s (%d) and %s (%d)",
283 people
->items
[0].string
,
284 (int)util_as_integral(&people
->items
[0]),
285 people
->items
[1].string
,
286 (int)util_as_integral(&people
->items
[1]));
288 strbuf_addf(out
, "%s (%d) and others",
289 people
->items
[0].string
,
290 (int)util_as_integral(&people
->items
[0]));
293 static void credit_people(struct strbuf
*out
,
294 struct string_list
*them
,
302 me
= git_author_info(IDENT_NO_DATE
);
305 me
= git_committer_info(IDENT_NO_DATE
);
311 skip_prefix(me
, them
->items
->string
, &me
) &&
312 starts_with(me
, " <")))
314 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
315 add_people_count(out
, them
);
318 static void add_people_info(struct strbuf
*out
,
319 struct string_list
*authors
,
320 struct string_list
*committers
)
322 QSORT(authors
->items
, authors
->nr
,
323 cmp_string_list_util_as_integral
);
324 QSORT(committers
->items
, committers
->nr
,
325 cmp_string_list_util_as_integral
);
327 credit_people(out
, authors
, 'a');
328 credit_people(out
, committers
, 'c');
331 static void shortlog(const char *name
,
332 struct origin_data
*origin_data
,
334 struct rev_info
*rev
,
335 struct fmt_merge_msg_opts
*opts
,
339 struct commit
*commit
;
340 struct object
*branch
;
341 struct string_list subjects
= STRING_LIST_INIT_DUP
;
342 struct string_list authors
= STRING_LIST_INIT_DUP
;
343 struct string_list committers
= STRING_LIST_INIT_DUP
;
344 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
345 struct strbuf sb
= STRBUF_INIT
;
346 const struct object_id
*oid
= &origin_data
->oid
;
347 int limit
= opts
->shortlog_len
;
349 branch
= deref_tag(the_repository
, parse_object(the_repository
, oid
),
351 the_hash_algo
->hexsz
);
352 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
355 setup_revisions(0, NULL
, rev
, NULL
);
356 add_pending_object(rev
, branch
, name
);
357 add_pending_object(rev
, &head
->object
, "^HEAD");
358 head
->object
.flags
|= UNINTERESTING
;
359 if (prepare_revision_walk(rev
))
360 die("revision walk setup failed");
361 while ((commit
= get_revision(rev
)) != NULL
) {
362 struct pretty_print_context ctx
= {0};
364 if (commit
->parents
&& commit
->parents
->next
) {
365 /* do not list a merge but count committer */
366 if (opts
->credit_people
)
367 record_person('c', &committers
, commit
);
370 if (!count
&& opts
->credit_people
)
371 /* the 'tip' committer */
372 record_person('c', &committers
, commit
);
373 if (opts
->credit_people
)
374 record_person('a', &authors
, commit
);
376 if (subjects
.nr
> limit
)
379 format_commit_message(commit
, "%s", &sb
, &ctx
);
383 string_list_append(&subjects
,
384 oid_to_hex(&commit
->object
.oid
));
386 string_list_append_nodup(&subjects
,
387 strbuf_detach(&sb
, NULL
));
390 if (opts
->credit_people
)
391 add_people_info(out
, &authors
, &committers
);
393 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
395 strbuf_addf(out
, "\n* %s:\n", name
);
397 if (origin_data
->is_local_branch
&& use_branch_desc
)
398 add_branch_desc(out
, name
);
400 for (i
= 0; i
< subjects
.nr
; i
++)
402 strbuf_addstr(out
, " ...\n");
404 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
406 clear_commit_marks((struct commit
*)branch
, flags
);
407 clear_commit_marks(head
, flags
);
408 free_commit_list(rev
->commits
);
412 string_list_clear(&authors
, 0);
413 string_list_clear(&committers
, 0);
414 string_list_clear(&subjects
, 0);
417 static void fmt_merge_msg_title(struct strbuf
*out
,
418 const char *current_branch
)
423 strbuf_addstr(out
, "Merge ");
424 for (i
= 0; i
< srcs
.nr
; i
++) {
425 struct src_data
*src_data
= srcs
.items
[i
].util
;
426 const char *subsep
= "";
428 strbuf_addstr(out
, sep
);
431 if (src_data
->head_status
== 1) {
432 strbuf_addstr(out
, srcs
.items
[i
].string
);
435 if (src_data
->head_status
== 3) {
437 strbuf_addstr(out
, "HEAD");
439 if (src_data
->branch
.nr
) {
440 strbuf_addstr(out
, subsep
);
442 print_joined("branch ", "branches ", &src_data
->branch
,
445 if (src_data
->r_branch
.nr
) {
446 strbuf_addstr(out
, subsep
);
448 print_joined("remote-tracking branch ", "remote-tracking branches ",
449 &src_data
->r_branch
, out
);
451 if (src_data
->tag
.nr
) {
452 strbuf_addstr(out
, subsep
);
454 print_joined("tag ", "tags ", &src_data
->tag
, out
);
456 if (src_data
->generic
.nr
) {
457 strbuf_addstr(out
, subsep
);
458 print_joined("commit ", "commits ", &src_data
->generic
,
461 if (strcmp(".", srcs
.items
[i
].string
))
462 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
465 if (!strcmp("master", current_branch
))
466 strbuf_addch(out
, '\n');
468 strbuf_addf(out
, " into %s\n", current_branch
);
471 static void fmt_tag_signature(struct strbuf
*tagbuf
,
476 const char *tag_body
= strstr(buf
, "\n\n");
479 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
481 strbuf_complete_line(tagbuf
);
483 strbuf_addch(tagbuf
, '\n');
484 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
488 static void fmt_merge_msg_sigs(struct strbuf
*out
)
490 int i
, tag_number
= 0, first_tag
= 0;
491 struct strbuf tagbuf
= STRBUF_INIT
;
493 for (i
= 0; i
< origins
.nr
; i
++) {
494 struct object_id
*oid
= origins
.items
[i
].util
;
495 enum object_type type
;
496 unsigned long size
, len
;
497 char *buf
= read_object_file(oid
, &type
, &size
);
498 struct strbuf sig
= STRBUF_INIT
;
500 if (!buf
|| type
!= OBJ_TAG
)
502 len
= parse_signature(buf
, size
);
505 ; /* merely annotated */
506 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
, NULL
)) {
508 strbuf_addstr(&sig
, "gpg verification failed.\n");
512 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
515 if (tag_number
== 2) {
516 struct strbuf tagline
= STRBUF_INIT
;
517 strbuf_addch(&tagline
, '\n');
518 strbuf_add_commented_lines(&tagline
,
519 origins
.items
[first_tag
].string
,
520 strlen(origins
.items
[first_tag
].string
));
521 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
523 strbuf_release(&tagline
);
525 strbuf_addch(&tagbuf
, '\n');
526 strbuf_add_commented_lines(&tagbuf
,
527 origins
.items
[i
].string
,
528 strlen(origins
.items
[i
].string
));
529 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
531 strbuf_release(&sig
);
536 strbuf_addch(out
, '\n');
537 strbuf_addbuf(out
, &tagbuf
);
539 strbuf_release(&tagbuf
);
542 static void find_merge_parents(struct merge_parents
*result
,
543 struct strbuf
*in
, struct object_id
*head
)
545 struct commit_list
*parents
;
546 struct commit
*head_commit
;
550 while (pos
< in
->len
) {
552 char *p
= in
->buf
+ pos
;
553 char *newline
= strchr(p
, '\n');
555 struct object_id oid
;
556 struct commit
*parent
;
559 len
= newline
? newline
- p
: strlen(p
);
560 pos
+= len
+ !!newline
;
562 if (parse_oid_hex(p
, &oid
, &q
) ||
565 continue; /* skip not-for-merge */
567 * Do not use get_merge_parent() here; we do not have
568 * "name" here and we do not want to contaminate its
571 obj
= parse_object(the_repository
, &oid
);
572 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
575 commit_list_insert(parent
, &parents
);
576 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
578 head_commit
= lookup_commit(the_repository
, head
);
580 commit_list_insert(head_commit
, &parents
);
581 reduce_heads_replace(&parents
);
584 struct commit
*cmit
= pop_commit(&parents
);
585 for (i
= 0; i
< result
->nr
; i
++)
586 if (oideq(&result
->item
[i
].commit
, &cmit
->object
.oid
))
587 result
->item
[i
].used
= 1;
590 for (i
= j
= 0; i
< result
->nr
; i
++) {
591 if (result
->item
[i
].used
) {
593 result
->item
[j
] = result
->item
[i
];
600 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
601 struct fmt_merge_msg_opts
*opts
)
604 struct object_id head_oid
;
605 const char *current_branch
;
606 void *current_branch_to_free
;
607 struct merge_parents merge_parents
;
609 memset(&merge_parents
, 0, sizeof(merge_parents
));
611 /* get current branch */
612 current_branch
= current_branch_to_free
=
613 resolve_refdup("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
);
615 die("No current branch");
616 if (starts_with(current_branch
, "refs/heads/"))
617 current_branch
+= 11;
619 find_merge_parents(&merge_parents
, in
, &head_oid
);
622 while (pos
< in
->len
) {
624 char *newline
, *p
= in
->buf
+ pos
;
626 newline
= strchr(p
, '\n');
627 len
= newline
? newline
- p
: strlen(p
);
628 pos
+= len
+ !!newline
;
631 if (handle_line(p
, &merge_parents
))
632 die("error in line %d: %.*s", i
, len
, p
);
635 if (opts
->add_title
&& srcs
.nr
)
636 fmt_merge_msg_title(out
, current_branch
);
639 fmt_merge_msg_sigs(out
);
641 if (opts
->shortlog_len
) {
645 head
= lookup_commit_or_die(&head_oid
, "HEAD");
646 init_revisions(&rev
, NULL
);
647 rev
.commit_format
= CMIT_FMT_ONELINE
;
648 rev
.ignore_merges
= 1;
651 strbuf_complete_line(out
);
653 for (i
= 0; i
< origins
.nr
; i
++)
654 shortlog(origins
.items
[i
].string
,
655 origins
.items
[i
].util
,
656 head
, &rev
, opts
, out
);
659 strbuf_complete_line(out
);
660 free(current_branch_to_free
);
661 free(merge_parents
.item
);
665 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
667 const char *inpath
= NULL
;
668 const char *message
= NULL
;
669 int shortlog_len
= -1;
670 struct option options
[] = {
671 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
672 N_("populate log with at most <n> entries from shortlog"),
673 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
674 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, N_("n"),
675 N_("alias for --log (deprecated)"),
676 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
677 DEFAULT_MERGE_LOG_LEN
},
678 OPT_STRING('m', "message", &message
, N_("text"),
679 N_("use <text> as start of message")),
680 OPT_FILENAME('F', "file", &inpath
, N_("file to read from")),
685 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
687 struct fmt_merge_msg_opts opts
;
689 git_config(fmt_merge_msg_config
, NULL
);
690 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
693 usage_with_options(fmt_merge_msg_usage
, options
);
694 if (shortlog_len
< 0)
695 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
697 if (inpath
&& strcmp(inpath
, "-")) {
698 in
= fopen(inpath
, "r");
700 die_errno("cannot open '%s'", inpath
);
703 if (strbuf_read(&input
, fileno(in
), 0) < 0)
704 die_errno("could not read input file");
707 strbuf_addstr(&output
, message
);
709 memset(&opts
, 0, sizeof(opts
));
710 opts
.add_title
= !message
;
711 opts
.credit_people
= 1;
712 opts
.shortlog_len
= shortlog_len
;
714 ret
= fmt_merge_msg(&input
, &output
, &opts
);
717 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);