9 #include "string-list.h"
11 #include "fmt-merge-msg.h"
12 #include "gpg-interface.h"
14 static const char * const fmt_merge_msg_usage
[] = {
15 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
19 static int use_branch_desc
;
21 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
23 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
25 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
26 if (!is_bool
&& merge_log_config
< 0)
27 return error("%s: negative length %s", key
, value
);
28 if (is_bool
&& merge_log_config
)
29 merge_log_config
= DEFAULT_MERGE_LOG_LEN
;
30 } else if (!strcmp(key
, "merge.branchdesc")) {
31 use_branch_desc
= git_config_bool(key
, value
);
33 return git_default_config(key
, value
, cb
);
38 /* merge data per repository where the merged tips came from */
40 struct string_list branch
, tag
, r_branch
, generic
;
46 unsigned is_local_branch
:1;
49 static void init_src_data(struct src_data
*data
)
51 data
->branch
.strdup_strings
= 1;
52 data
->tag
.strdup_strings
= 1;
53 data
->r_branch
.strdup_strings
= 1;
54 data
->generic
.strdup_strings
= 1;
57 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
58 static struct string_list origins
= STRING_LIST_INIT_DUP
;
60 struct merge_parents
{
63 struct object_id given
;
64 struct object_id commit
;
70 * I know, I know, this is inefficient, but you won't be pulling and merging
71 * hundreds of heads at a time anyway.
73 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
74 struct object_id
*given
,
75 struct object_id
*commit
)
78 for (i
= 0; i
< table
->nr
; i
++) {
79 if (given
&& oidcmp(&table
->item
[i
].given
, given
))
81 if (commit
&& oidcmp(&table
->item
[i
].commit
, commit
))
83 return &table
->item
[i
];
88 static void add_merge_parent(struct merge_parents
*table
,
89 struct object_id
*given
,
90 struct object_id
*commit
)
92 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
94 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
95 oidcpy(&table
->item
[table
->nr
].given
, given
);
96 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
97 table
->item
[table
->nr
].used
= 0;
101 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
103 int i
, len
= strlen(line
);
104 struct origin_data
*origin_data
;
107 struct src_data
*src_data
;
108 struct string_list_item
*item
;
109 int pulling_head
= 0;
110 struct object_id oid
;
112 if (len
< GIT_SHA1_HEXSZ
+ 3 || line
[GIT_SHA1_HEXSZ
] != '\t')
115 if (starts_with(line
+ GIT_SHA1_HEXSZ
+ 1, "not-for-merge"))
118 if (line
[GIT_SHA1_HEXSZ
+ 1] != '\t')
121 i
= get_oid_hex(line
, &oid
);
125 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
126 return 0; /* subsumed by other parents */
128 origin_data
= xcalloc(1, sizeof(struct origin_data
));
129 oidcpy(&origin_data
->oid
, &oid
);
131 if (line
[len
- 1] == '\n')
133 line
+= GIT_SHA1_HEXSZ
+ 2;
136 * At this point, line points at the beginning of comment e.g.
137 * "branch 'frotz' of git://that/repository.git".
138 * Find the repository name and point it with src.
140 src
= strstr(line
, " of ");
150 item
= unsorted_string_list_lookup(&srcs
, src
);
152 item
= string_list_append(&srcs
, src
);
153 item
->util
= xcalloc(1, sizeof(struct src_data
));
154 init_src_data(item
->util
);
156 src_data
= item
->util
;
160 src_data
->head_status
|= 1;
161 } else if (starts_with(line
, "branch ")) {
162 origin_data
->is_local_branch
= 1;
164 string_list_append(&src_data
->branch
, origin
);
165 src_data
->head_status
|= 2;
166 } else if (starts_with(line
, "tag ")) {
168 string_list_append(&src_data
->tag
, origin
+ 4);
169 src_data
->head_status
|= 2;
170 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
171 string_list_append(&src_data
->r_branch
, origin
);
172 src_data
->head_status
|= 2;
175 string_list_append(&src_data
->generic
, line
);
176 src_data
->head_status
|= 2;
179 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
180 int len
= strlen(origin
);
181 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
182 origin
= xmemdupz(origin
+ 1, len
- 2);
184 origin
= xstrfmt("%s of %s", origin
, src
);
185 if (strcmp(".", src
))
186 origin_data
->is_local_branch
= 0;
187 string_list_append(&origins
, origin
)->util
= origin_data
;
191 static void print_joined(const char *singular
, const char *plural
,
192 struct string_list
*list
, struct strbuf
*out
)
197 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
200 strbuf_addstr(out
, plural
);
201 for (i
= 0; i
< list
->nr
- 1; i
++)
202 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
203 list
->items
[i
].string
);
204 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
208 static void add_branch_desc(struct strbuf
*out
, const char *name
)
210 struct strbuf desc
= STRBUF_INIT
;
212 if (!read_branch_desc(&desc
, name
)) {
213 const char *bp
= desc
.buf
;
215 const char *ep
= strchrnul(bp
, '\n');
218 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
221 strbuf_complete_line(out
);
223 strbuf_release(&desc
);
226 #define util_as_integral(elem) ((intptr_t)((elem)->util))
228 static void record_person_from_buf(int which
, struct string_list
*people
,
231 char *name_buf
, *name
, *name_end
;
232 struct string_list_item
*elem
;
235 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
236 name
= strstr(buffer
, field
);
239 name
+= strlen(field
);
240 name_end
= strchrnul(name
, '<');
243 while (isspace(*name_end
) && name
<= name_end
)
247 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
249 elem
= string_list_lookup(people
, name_buf
);
251 elem
= string_list_insert(people
, name_buf
);
252 elem
->util
= (void *)0;
254 elem
->util
= (void*)(util_as_integral(elem
) + 1);
259 static void record_person(int which
, struct string_list
*people
,
260 struct commit
*commit
)
262 const char *buffer
= get_commit_buffer(commit
, NULL
);
263 record_person_from_buf(which
, people
, buffer
);
264 unuse_commit_buffer(commit
, buffer
);
267 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
269 const struct string_list_item
*a
= a_
, *b
= b_
;
270 return util_as_integral(b
) - util_as_integral(a
);
273 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
276 strbuf_addstr(out
, people
->items
[0].string
);
277 else if (people
->nr
== 2)
278 strbuf_addf(out
, "%s (%d) and %s (%d)",
279 people
->items
[0].string
,
280 (int)util_as_integral(&people
->items
[0]),
281 people
->items
[1].string
,
282 (int)util_as_integral(&people
->items
[1]));
284 strbuf_addf(out
, "%s (%d) and others",
285 people
->items
[0].string
,
286 (int)util_as_integral(&people
->items
[0]));
289 static void credit_people(struct strbuf
*out
,
290 struct string_list
*them
,
298 me
= git_author_info(IDENT_NO_DATE
);
301 me
= git_committer_info(IDENT_NO_DATE
);
307 skip_prefix(me
, them
->items
->string
, &me
) &&
308 starts_with(me
, " <")))
310 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
311 add_people_count(out
, them
);
314 static void add_people_info(struct strbuf
*out
,
315 struct string_list
*authors
,
316 struct string_list
*committers
)
318 QSORT(authors
->items
, authors
->nr
,
319 cmp_string_list_util_as_integral
);
320 QSORT(committers
->items
, committers
->nr
,
321 cmp_string_list_util_as_integral
);
323 credit_people(out
, authors
, 'a');
324 credit_people(out
, committers
, 'c');
327 static void shortlog(const char *name
,
328 struct origin_data
*origin_data
,
330 struct rev_info
*rev
,
331 struct fmt_merge_msg_opts
*opts
,
335 struct commit
*commit
;
336 struct object
*branch
;
337 struct string_list subjects
= STRING_LIST_INIT_DUP
;
338 struct string_list authors
= STRING_LIST_INIT_DUP
;
339 struct string_list committers
= STRING_LIST_INIT_DUP
;
340 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
341 struct strbuf sb
= STRBUF_INIT
;
342 const struct object_id
*oid
= &origin_data
->oid
;
343 int limit
= opts
->shortlog_len
;
345 branch
= deref_tag(parse_object(oid
), oid_to_hex(oid
), GIT_SHA1_HEXSZ
);
346 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
349 setup_revisions(0, NULL
, rev
, NULL
);
350 add_pending_object(rev
, branch
, name
);
351 add_pending_object(rev
, &head
->object
, "^HEAD");
352 head
->object
.flags
|= UNINTERESTING
;
353 if (prepare_revision_walk(rev
))
354 die("revision walk setup failed");
355 while ((commit
= get_revision(rev
)) != NULL
) {
356 struct pretty_print_context ctx
= {0};
358 if (commit
->parents
&& commit
->parents
->next
) {
359 /* do not list a merge but count committer */
360 if (opts
->credit_people
)
361 record_person('c', &committers
, commit
);
364 if (!count
&& opts
->credit_people
)
365 /* the 'tip' committer */
366 record_person('c', &committers
, commit
);
367 if (opts
->credit_people
)
368 record_person('a', &authors
, commit
);
370 if (subjects
.nr
> limit
)
373 format_commit_message(commit
, "%s", &sb
, &ctx
);
377 string_list_append(&subjects
,
378 oid_to_hex(&commit
->object
.oid
));
380 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
383 if (opts
->credit_people
)
384 add_people_info(out
, &authors
, &committers
);
386 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
388 strbuf_addf(out
, "\n* %s:\n", name
);
390 if (origin_data
->is_local_branch
&& use_branch_desc
)
391 add_branch_desc(out
, name
);
393 for (i
= 0; i
< subjects
.nr
; i
++)
395 strbuf_addstr(out
, " ...\n");
397 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
399 clear_commit_marks((struct commit
*)branch
, flags
);
400 clear_commit_marks(head
, flags
);
401 free_commit_list(rev
->commits
);
405 string_list_clear(&authors
, 0);
406 string_list_clear(&committers
, 0);
407 string_list_clear(&subjects
, 0);
410 static void fmt_merge_msg_title(struct strbuf
*out
,
411 const char *current_branch
)
416 strbuf_addstr(out
, "Merge ");
417 for (i
= 0; i
< srcs
.nr
; i
++) {
418 struct src_data
*src_data
= srcs
.items
[i
].util
;
419 const char *subsep
= "";
421 strbuf_addstr(out
, sep
);
424 if (src_data
->head_status
== 1) {
425 strbuf_addstr(out
, srcs
.items
[i
].string
);
428 if (src_data
->head_status
== 3) {
430 strbuf_addstr(out
, "HEAD");
432 if (src_data
->branch
.nr
) {
433 strbuf_addstr(out
, subsep
);
435 print_joined("branch ", "branches ", &src_data
->branch
,
438 if (src_data
->r_branch
.nr
) {
439 strbuf_addstr(out
, subsep
);
441 print_joined("remote-tracking branch ", "remote-tracking branches ",
442 &src_data
->r_branch
, out
);
444 if (src_data
->tag
.nr
) {
445 strbuf_addstr(out
, subsep
);
447 print_joined("tag ", "tags ", &src_data
->tag
, out
);
449 if (src_data
->generic
.nr
) {
450 strbuf_addstr(out
, subsep
);
451 print_joined("commit ", "commits ", &src_data
->generic
,
454 if (strcmp(".", srcs
.items
[i
].string
))
455 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
458 if (!strcmp("master", current_branch
))
459 strbuf_addch(out
, '\n');
461 strbuf_addf(out
, " into %s\n", current_branch
);
464 static void fmt_tag_signature(struct strbuf
*tagbuf
,
469 const char *tag_body
= strstr(buf
, "\n\n");
472 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
474 strbuf_complete_line(tagbuf
);
476 strbuf_addch(tagbuf
, '\n');
477 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
481 static void fmt_merge_msg_sigs(struct strbuf
*out
)
483 int i
, tag_number
= 0, first_tag
= 0;
484 struct strbuf tagbuf
= STRBUF_INIT
;
486 for (i
= 0; i
< origins
.nr
; i
++) {
487 unsigned char *sha1
= origins
.items
[i
].util
;
488 enum object_type type
;
489 unsigned long size
, len
;
490 char *buf
= read_sha1_file(sha1
, &type
, &size
);
491 struct strbuf sig
= STRBUF_INIT
;
493 if (!buf
|| type
!= OBJ_TAG
)
495 len
= parse_signature(buf
, size
);
498 ; /* merely annotated */
499 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
, NULL
)) {
501 strbuf_addstr(&sig
, "gpg verification failed.\n");
505 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
508 if (tag_number
== 2) {
509 struct strbuf tagline
= STRBUF_INIT
;
510 strbuf_addch(&tagline
, '\n');
511 strbuf_add_commented_lines(&tagline
,
512 origins
.items
[first_tag
].string
,
513 strlen(origins
.items
[first_tag
].string
));
514 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
516 strbuf_release(&tagline
);
518 strbuf_addch(&tagbuf
, '\n');
519 strbuf_add_commented_lines(&tagbuf
,
520 origins
.items
[i
].string
,
521 strlen(origins
.items
[i
].string
));
522 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
524 strbuf_release(&sig
);
529 strbuf_addch(out
, '\n');
530 strbuf_addbuf(out
, &tagbuf
);
532 strbuf_release(&tagbuf
);
535 static void find_merge_parents(struct merge_parents
*result
,
536 struct strbuf
*in
, struct object_id
*head
)
538 struct commit_list
*parents
;
539 struct commit
*head_commit
;
543 while (pos
< in
->len
) {
545 char *p
= in
->buf
+ pos
;
546 char *newline
= strchr(p
, '\n');
547 struct object_id oid
;
548 struct commit
*parent
;
551 len
= newline
? newline
- p
: strlen(p
);
552 pos
+= len
+ !!newline
;
554 if (len
< GIT_SHA1_HEXSZ
+ 3 ||
555 get_oid_hex(p
, &oid
) ||
556 p
[GIT_SHA1_HEXSZ
] != '\t' ||
557 p
[GIT_SHA1_HEXSZ
+ 1] != '\t')
558 continue; /* skip not-for-merge */
560 * Do not use get_merge_parent() here; we do not have
561 * "name" here and we do not want to contaminate its
564 obj
= parse_object(&oid
);
565 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
568 commit_list_insert(parent
, &parents
);
569 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
571 head_commit
= lookup_commit(head
);
573 commit_list_insert(head_commit
, &parents
);
574 parents
= reduce_heads(parents
);
577 struct commit
*cmit
= pop_commit(&parents
);
578 for (i
= 0; i
< result
->nr
; i
++)
579 if (!oidcmp(&result
->item
[i
].commit
, &cmit
->object
.oid
))
580 result
->item
[i
].used
= 1;
583 for (i
= j
= 0; i
< result
->nr
; i
++) {
584 if (result
->item
[i
].used
) {
586 result
->item
[j
] = result
->item
[i
];
593 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
594 struct fmt_merge_msg_opts
*opts
)
597 struct object_id head_oid
;
598 const char *current_branch
;
599 void *current_branch_to_free
;
600 struct merge_parents merge_parents
;
602 memset(&merge_parents
, 0, sizeof(merge_parents
));
604 /* get current branch */
605 current_branch
= current_branch_to_free
=
606 resolve_refdup("HEAD", RESOLVE_REF_READING
, head_oid
.hash
, NULL
);
608 die("No current branch");
609 if (starts_with(current_branch
, "refs/heads/"))
610 current_branch
+= 11;
612 find_merge_parents(&merge_parents
, in
, &head_oid
);
615 while (pos
< in
->len
) {
617 char *newline
, *p
= in
->buf
+ pos
;
619 newline
= strchr(p
, '\n');
620 len
= newline
? newline
- p
: strlen(p
);
621 pos
+= len
+ !!newline
;
624 if (handle_line(p
, &merge_parents
))
625 die ("Error in line %d: %.*s", i
, len
, p
);
628 if (opts
->add_title
&& srcs
.nr
)
629 fmt_merge_msg_title(out
, current_branch
);
632 fmt_merge_msg_sigs(out
);
634 if (opts
->shortlog_len
) {
638 head
= lookup_commit_or_die(&head_oid
, "HEAD");
639 init_revisions(&rev
, NULL
);
640 rev
.commit_format
= CMIT_FMT_ONELINE
;
641 rev
.ignore_merges
= 1;
644 strbuf_complete_line(out
);
646 for (i
= 0; i
< origins
.nr
; i
++)
647 shortlog(origins
.items
[i
].string
,
648 origins
.items
[i
].util
,
649 head
, &rev
, opts
, out
);
652 strbuf_complete_line(out
);
653 free(current_branch_to_free
);
654 free(merge_parents
.item
);
658 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
660 const char *inpath
= NULL
;
661 const char *message
= NULL
;
662 int shortlog_len
= -1;
663 struct option options
[] = {
664 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
665 N_("populate log with at most <n> entries from shortlog"),
666 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
667 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, N_("n"),
668 N_("alias for --log (deprecated)"),
669 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
670 DEFAULT_MERGE_LOG_LEN
},
671 OPT_STRING('m', "message", &message
, N_("text"),
672 N_("use <text> as start of message")),
673 OPT_FILENAME('F', "file", &inpath
, N_("file to read from")),
678 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
680 struct fmt_merge_msg_opts opts
;
682 git_config(fmt_merge_msg_config
, NULL
);
683 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
686 usage_with_options(fmt_merge_msg_usage
, options
);
687 if (shortlog_len
< 0)
688 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
690 if (inpath
&& strcmp(inpath
, "-")) {
691 in
= fopen(inpath
, "r");
693 die_errno("cannot open '%s'", inpath
);
696 if (strbuf_read(&input
, fileno(in
), 0) < 0)
697 die_errno("could not read input file");
700 strbuf_addstr(&output
, message
);
702 memset(&opts
, 0, sizeof(opts
));
703 opts
.add_title
= !message
;
704 opts
.credit_people
= 1;
705 opts
.shortlog_len
= shortlog_len
;
707 ret
= fmt_merge_msg(&input
, &output
, &opts
);
710 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);