8 #include "string-list.h"
10 #include "fmt-merge-msg.h"
11 #include "gpg-interface.h"
13 static const char * const fmt_merge_msg_usage
[] = {
14 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
18 static int use_branch_desc
;
20 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
22 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
24 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
25 if (!is_bool
&& merge_log_config
< 0)
26 return error("%s: negative length %s", key
, value
);
27 if (is_bool
&& merge_log_config
)
28 merge_log_config
= DEFAULT_MERGE_LOG_LEN
;
29 } else if (!strcmp(key
, "merge.branchdesc")) {
30 use_branch_desc
= git_config_bool(key
, value
);
32 return git_default_config(key
, value
, cb
);
37 /* merge data per repository where the merged tips came from */
39 struct string_list branch
, tag
, r_branch
, generic
;
45 unsigned is_local_branch
:1;
48 static void init_src_data(struct src_data
*data
)
50 data
->branch
.strdup_strings
= 1;
51 data
->tag
.strdup_strings
= 1;
52 data
->r_branch
.strdup_strings
= 1;
53 data
->generic
.strdup_strings
= 1;
56 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
57 static struct string_list origins
= STRING_LIST_INIT_DUP
;
59 struct merge_parents
{
62 struct object_id given
;
63 struct object_id commit
;
69 * I know, I know, this is inefficient, but you won't be pulling and merging
70 * hundreds of heads at a time anyway.
72 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
73 struct object_id
*given
,
74 struct object_id
*commit
)
77 for (i
= 0; i
< table
->nr
; i
++) {
78 if (given
&& oidcmp(&table
->item
[i
].given
, given
))
80 if (commit
&& oidcmp(&table
->item
[i
].commit
, commit
))
82 return &table
->item
[i
];
87 static void add_merge_parent(struct merge_parents
*table
,
88 struct object_id
*given
,
89 struct object_id
*commit
)
91 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
93 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
94 oidcpy(&table
->item
[table
->nr
].given
, given
);
95 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
96 table
->item
[table
->nr
].used
= 0;
100 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
102 int i
, len
= strlen(line
);
103 struct origin_data
*origin_data
;
106 struct src_data
*src_data
;
107 struct string_list_item
*item
;
108 int pulling_head
= 0;
109 struct object_id oid
;
111 if (len
< GIT_SHA1_HEXSZ
+ 3 || line
[GIT_SHA1_HEXSZ
] != '\t')
114 if (starts_with(line
+ GIT_SHA1_HEXSZ
+ 1, "not-for-merge"))
117 if (line
[GIT_SHA1_HEXSZ
+ 1] != '\t')
120 i
= get_oid_hex(line
, &oid
);
124 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
125 return 0; /* subsumed by other parents */
127 origin_data
= xcalloc(1, sizeof(struct origin_data
));
128 oidcpy(&origin_data
->oid
, &oid
);
130 if (line
[len
- 1] == '\n')
132 line
+= GIT_SHA1_HEXSZ
+ 2;
135 * At this point, line points at the beginning of comment e.g.
136 * "branch 'frotz' of git://that/repository.git".
137 * Find the repository name and point it with src.
139 src
= strstr(line
, " of ");
149 item
= unsorted_string_list_lookup(&srcs
, src
);
151 item
= string_list_append(&srcs
, src
);
152 item
->util
= xcalloc(1, sizeof(struct src_data
));
153 init_src_data(item
->util
);
155 src_data
= item
->util
;
159 src_data
->head_status
|= 1;
160 } else if (starts_with(line
, "branch ")) {
161 origin_data
->is_local_branch
= 1;
163 string_list_append(&src_data
->branch
, origin
);
164 src_data
->head_status
|= 2;
165 } else if (starts_with(line
, "tag ")) {
167 string_list_append(&src_data
->tag
, origin
+ 4);
168 src_data
->head_status
|= 2;
169 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
170 string_list_append(&src_data
->r_branch
, origin
);
171 src_data
->head_status
|= 2;
174 string_list_append(&src_data
->generic
, line
);
175 src_data
->head_status
|= 2;
178 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
179 int len
= strlen(origin
);
180 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
181 origin
= xmemdupz(origin
+ 1, len
- 2);
183 origin
= xstrfmt("%s of %s", origin
, src
);
184 if (strcmp(".", src
))
185 origin_data
->is_local_branch
= 0;
186 string_list_append(&origins
, origin
)->util
= origin_data
;
190 static void print_joined(const char *singular
, const char *plural
,
191 struct string_list
*list
, struct strbuf
*out
)
196 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
199 strbuf_addstr(out
, plural
);
200 for (i
= 0; i
< list
->nr
- 1; i
++)
201 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
202 list
->items
[i
].string
);
203 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
207 static void add_branch_desc(struct strbuf
*out
, const char *name
)
209 struct strbuf desc
= STRBUF_INIT
;
211 if (!read_branch_desc(&desc
, name
)) {
212 const char *bp
= desc
.buf
;
214 const char *ep
= strchrnul(bp
, '\n');
217 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
220 strbuf_complete_line(out
);
222 strbuf_release(&desc
);
225 #define util_as_integral(elem) ((intptr_t)((elem)->util))
227 static void record_person_from_buf(int which
, struct string_list
*people
,
230 char *name_buf
, *name
, *name_end
;
231 struct string_list_item
*elem
;
234 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
235 name
= strstr(buffer
, field
);
238 name
+= strlen(field
);
239 name_end
= strchrnul(name
, '<');
242 while (isspace(*name_end
) && name
<= name_end
)
246 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
248 elem
= string_list_lookup(people
, name_buf
);
250 elem
= string_list_insert(people
, name_buf
);
251 elem
->util
= (void *)0;
253 elem
->util
= (void*)(util_as_integral(elem
) + 1);
258 static void record_person(int which
, struct string_list
*people
,
259 struct commit
*commit
)
261 const char *buffer
= get_commit_buffer(commit
, NULL
);
262 record_person_from_buf(which
, people
, buffer
);
263 unuse_commit_buffer(commit
, buffer
);
266 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
268 const struct string_list_item
*a
= a_
, *b
= b_
;
269 return util_as_integral(b
) - util_as_integral(a
);
272 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
275 strbuf_addstr(out
, people
->items
[0].string
);
276 else if (people
->nr
== 2)
277 strbuf_addf(out
, "%s (%d) and %s (%d)",
278 people
->items
[0].string
,
279 (int)util_as_integral(&people
->items
[0]),
280 people
->items
[1].string
,
281 (int)util_as_integral(&people
->items
[1]));
283 strbuf_addf(out
, "%s (%d) and others",
284 people
->items
[0].string
,
285 (int)util_as_integral(&people
->items
[0]));
288 static void credit_people(struct strbuf
*out
,
289 struct string_list
*them
,
297 me
= git_author_info(IDENT_NO_DATE
);
300 me
= git_committer_info(IDENT_NO_DATE
);
306 skip_prefix(me
, them
->items
->string
, &me
) &&
307 starts_with(me
, " <")))
309 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
310 add_people_count(out
, them
);
313 static void add_people_info(struct strbuf
*out
,
314 struct string_list
*authors
,
315 struct string_list
*committers
)
317 QSORT(authors
->items
, authors
->nr
,
318 cmp_string_list_util_as_integral
);
319 QSORT(committers
->items
, committers
->nr
,
320 cmp_string_list_util_as_integral
);
322 credit_people(out
, authors
, 'a');
323 credit_people(out
, committers
, 'c');
326 static void shortlog(const char *name
,
327 struct origin_data
*origin_data
,
329 struct rev_info
*rev
,
330 struct fmt_merge_msg_opts
*opts
,
334 struct commit
*commit
;
335 struct object
*branch
;
336 struct string_list subjects
= STRING_LIST_INIT_DUP
;
337 struct string_list authors
= STRING_LIST_INIT_DUP
;
338 struct string_list committers
= STRING_LIST_INIT_DUP
;
339 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
340 struct strbuf sb
= STRBUF_INIT
;
341 const struct object_id
*oid
= &origin_data
->oid
;
342 int limit
= opts
->shortlog_len
;
344 branch
= deref_tag(parse_object(oid
->hash
), oid_to_hex(oid
), GIT_SHA1_HEXSZ
);
345 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
348 setup_revisions(0, NULL
, rev
, NULL
);
349 add_pending_object(rev
, branch
, name
);
350 add_pending_object(rev
, &head
->object
, "^HEAD");
351 head
->object
.flags
|= UNINTERESTING
;
352 if (prepare_revision_walk(rev
))
353 die("revision walk setup failed");
354 while ((commit
= get_revision(rev
)) != NULL
) {
355 struct pretty_print_context ctx
= {0};
357 if (commit
->parents
&& commit
->parents
->next
) {
358 /* do not list a merge but count committer */
359 if (opts
->credit_people
)
360 record_person('c', &committers
, commit
);
363 if (!count
&& opts
->credit_people
)
364 /* the 'tip' committer */
365 record_person('c', &committers
, commit
);
366 if (opts
->credit_people
)
367 record_person('a', &authors
, commit
);
369 if (subjects
.nr
> limit
)
372 format_commit_message(commit
, "%s", &sb
, &ctx
);
376 string_list_append(&subjects
,
377 oid_to_hex(&commit
->object
.oid
));
379 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
382 if (opts
->credit_people
)
383 add_people_info(out
, &authors
, &committers
);
385 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
387 strbuf_addf(out
, "\n* %s:\n", name
);
389 if (origin_data
->is_local_branch
&& use_branch_desc
)
390 add_branch_desc(out
, name
);
392 for (i
= 0; i
< subjects
.nr
; i
++)
394 strbuf_addstr(out
, " ...\n");
396 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
398 clear_commit_marks((struct commit
*)branch
, flags
);
399 clear_commit_marks(head
, flags
);
400 free_commit_list(rev
->commits
);
404 string_list_clear(&authors
, 0);
405 string_list_clear(&committers
, 0);
406 string_list_clear(&subjects
, 0);
409 static void fmt_merge_msg_title(struct strbuf
*out
,
410 const char *current_branch
) {
414 strbuf_addstr(out
, "Merge ");
415 for (i
= 0; i
< srcs
.nr
; i
++) {
416 struct src_data
*src_data
= srcs
.items
[i
].util
;
417 const char *subsep
= "";
419 strbuf_addstr(out
, sep
);
422 if (src_data
->head_status
== 1) {
423 strbuf_addstr(out
, srcs
.items
[i
].string
);
426 if (src_data
->head_status
== 3) {
428 strbuf_addstr(out
, "HEAD");
430 if (src_data
->branch
.nr
) {
431 strbuf_addstr(out
, subsep
);
433 print_joined("branch ", "branches ", &src_data
->branch
,
436 if (src_data
->r_branch
.nr
) {
437 strbuf_addstr(out
, subsep
);
439 print_joined("remote-tracking branch ", "remote-tracking branches ",
440 &src_data
->r_branch
, out
);
442 if (src_data
->tag
.nr
) {
443 strbuf_addstr(out
, subsep
);
445 print_joined("tag ", "tags ", &src_data
->tag
, out
);
447 if (src_data
->generic
.nr
) {
448 strbuf_addstr(out
, subsep
);
449 print_joined("commit ", "commits ", &src_data
->generic
,
452 if (strcmp(".", srcs
.items
[i
].string
))
453 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
456 if (!strcmp("master", current_branch
))
457 strbuf_addch(out
, '\n');
459 strbuf_addf(out
, " into %s\n", current_branch
);
462 static void fmt_tag_signature(struct strbuf
*tagbuf
,
467 const char *tag_body
= strstr(buf
, "\n\n");
470 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
472 strbuf_complete_line(tagbuf
);
474 strbuf_addch(tagbuf
, '\n');
475 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
479 static void fmt_merge_msg_sigs(struct strbuf
*out
)
481 int i
, tag_number
= 0, first_tag
= 0;
482 struct strbuf tagbuf
= STRBUF_INIT
;
484 for (i
= 0; i
< origins
.nr
; i
++) {
485 unsigned char *sha1
= origins
.items
[i
].util
;
486 enum object_type type
;
487 unsigned long size
, len
;
488 char *buf
= read_sha1_file(sha1
, &type
, &size
);
489 struct strbuf sig
= STRBUF_INIT
;
491 if (!buf
|| type
!= OBJ_TAG
)
493 len
= parse_signature(buf
, size
);
496 ; /* merely annotated */
497 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
, NULL
)) {
499 strbuf_addstr(&sig
, "gpg verification failed.\n");
503 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
506 if (tag_number
== 2) {
507 struct strbuf tagline
= STRBUF_INIT
;
508 strbuf_addch(&tagline
, '\n');
509 strbuf_add_commented_lines(&tagline
,
510 origins
.items
[first_tag
].string
,
511 strlen(origins
.items
[first_tag
].string
));
512 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
514 strbuf_release(&tagline
);
516 strbuf_addch(&tagbuf
, '\n');
517 strbuf_add_commented_lines(&tagbuf
,
518 origins
.items
[i
].string
,
519 strlen(origins
.items
[i
].string
));
520 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
522 strbuf_release(&sig
);
527 strbuf_addch(out
, '\n');
528 strbuf_addbuf(out
, &tagbuf
);
530 strbuf_release(&tagbuf
);
533 static void find_merge_parents(struct merge_parents
*result
,
534 struct strbuf
*in
, struct object_id
*head
)
536 struct commit_list
*parents
;
537 struct commit
*head_commit
;
541 while (pos
< in
->len
) {
543 char *p
= in
->buf
+ pos
;
544 char *newline
= strchr(p
, '\n');
545 struct object_id oid
;
546 struct commit
*parent
;
549 len
= newline
? newline
- p
: strlen(p
);
550 pos
+= len
+ !!newline
;
552 if (len
< GIT_SHA1_HEXSZ
+ 3 ||
553 get_oid_hex(p
, &oid
) ||
554 p
[GIT_SHA1_HEXSZ
] != '\t' ||
555 p
[GIT_SHA1_HEXSZ
+ 1] != '\t')
556 continue; /* skip not-for-merge */
558 * Do not use get_merge_parent() here; we do not have
559 * "name" here and we do not want to contaminate its
562 obj
= parse_object(oid
.hash
);
563 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
566 commit_list_insert(parent
, &parents
);
567 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
569 head_commit
= lookup_commit(head
->hash
);
571 commit_list_insert(head_commit
, &parents
);
572 parents
= reduce_heads(parents
);
575 struct commit
*cmit
= pop_commit(&parents
);
576 for (i
= 0; i
< result
->nr
; i
++)
577 if (!oidcmp(&result
->item
[i
].commit
, &cmit
->object
.oid
))
578 result
->item
[i
].used
= 1;
581 for (i
= j
= 0; i
< result
->nr
; i
++) {
582 if (result
->item
[i
].used
) {
584 result
->item
[j
] = result
->item
[i
];
591 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
592 struct fmt_merge_msg_opts
*opts
)
595 struct object_id head_oid
;
596 const char *current_branch
;
597 void *current_branch_to_free
;
598 struct merge_parents merge_parents
;
600 memset(&merge_parents
, 0, sizeof(merge_parents
));
602 /* get current branch */
603 current_branch
= current_branch_to_free
=
604 resolve_refdup("HEAD", RESOLVE_REF_READING
, head_oid
.hash
, NULL
);
606 die("No current branch");
607 if (starts_with(current_branch
, "refs/heads/"))
608 current_branch
+= 11;
610 find_merge_parents(&merge_parents
, in
, &head_oid
);
613 while (pos
< in
->len
) {
615 char *newline
, *p
= in
->buf
+ pos
;
617 newline
= strchr(p
, '\n');
618 len
= newline
? newline
- p
: strlen(p
);
619 pos
+= len
+ !!newline
;
622 if (handle_line(p
, &merge_parents
))
623 die ("Error in line %d: %.*s", i
, len
, p
);
626 if (opts
->add_title
&& srcs
.nr
)
627 fmt_merge_msg_title(out
, current_branch
);
630 fmt_merge_msg_sigs(out
);
632 if (opts
->shortlog_len
) {
636 head
= lookup_commit_or_die(head_oid
.hash
, "HEAD");
637 init_revisions(&rev
, NULL
);
638 rev
.commit_format
= CMIT_FMT_ONELINE
;
639 rev
.ignore_merges
= 1;
642 strbuf_complete_line(out
);
644 for (i
= 0; i
< origins
.nr
; i
++)
645 shortlog(origins
.items
[i
].string
,
646 origins
.items
[i
].util
,
647 head
, &rev
, opts
, out
);
650 strbuf_complete_line(out
);
651 free(current_branch_to_free
);
652 free(merge_parents
.item
);
656 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
658 const char *inpath
= NULL
;
659 const char *message
= NULL
;
660 int shortlog_len
= -1;
661 struct option options
[] = {
662 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
663 N_("populate log with at most <n> entries from shortlog"),
664 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
665 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, N_("n"),
666 N_("alias for --log (deprecated)"),
667 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
668 DEFAULT_MERGE_LOG_LEN
},
669 OPT_STRING('m', "message", &message
, N_("text"),
670 N_("use <text> as start of message")),
671 OPT_FILENAME('F', "file", &inpath
, N_("file to read from")),
676 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
678 struct fmt_merge_msg_opts opts
;
680 git_config(fmt_merge_msg_config
, NULL
);
681 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
684 usage_with_options(fmt_merge_msg_usage
, options
);
685 if (shortlog_len
< 0)
686 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
688 if (inpath
&& strcmp(inpath
, "-")) {
689 in
= fopen(inpath
, "r");
691 die_errno("cannot open '%s'", inpath
);
694 if (strbuf_read(&input
, fileno(in
), 0) < 0)
695 die_errno("could not read input file");
698 strbuf_addstr(&output
, message
);
700 memset(&opts
, 0, sizeof(opts
));
701 opts
.add_title
= !message
;
702 opts
.credit_people
= 1;
703 opts
.shortlog_len
= shortlog_len
;
705 ret
= fmt_merge_msg(&input
, &output
, &opts
);
708 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);