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"
16 static const char * const fmt_merge_msg_usage
[] = {
17 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
21 static int use_branch_desc
;
23 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
25 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
27 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
28 if (!is_bool
&& merge_log_config
< 0)
29 return error("%s: negative length %s", key
, value
);
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
);
35 return git_default_config(key
, value
, cb
);
40 /* merge data per repository where the merged tips came from */
42 struct string_list branch
, tag
, r_branch
, generic
;
48 unsigned is_local_branch
:1;
51 static void init_src_data(struct src_data
*data
)
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;
59 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
60 static struct string_list origins
= STRING_LIST_INIT_DUP
;
62 struct merge_parents
{
65 struct object_id given
;
66 struct object_id commit
;
72 * I know, I know, this is inefficient, but you won't be pulling and merging
73 * hundreds of heads at a time anyway.
75 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
76 struct object_id
*given
,
77 struct object_id
*commit
)
80 for (i
= 0; i
< table
->nr
; i
++) {
81 if (given
&& oidcmp(&table
->item
[i
].given
, given
))
83 if (commit
&& oidcmp(&table
->item
[i
].commit
, commit
))
85 return &table
->item
[i
];
90 static void add_merge_parent(struct merge_parents
*table
,
91 struct object_id
*given
,
92 struct object_id
*commit
)
94 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
96 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
97 oidcpy(&table
->item
[table
->nr
].given
, given
);
98 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
99 table
->item
[table
->nr
].used
= 0;
103 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
105 int i
, len
= strlen(line
);
106 struct origin_data
*origin_data
;
109 struct src_data
*src_data
;
110 struct string_list_item
*item
;
111 int pulling_head
= 0;
112 struct object_id oid
;
113 const unsigned hexsz
= the_hash_algo
->hexsz
;
115 if (len
< hexsz
+ 3 || line
[hexsz
] != '\t')
118 if (starts_with(line
+ hexsz
+ 1, "not-for-merge"))
121 if (line
[hexsz
+ 1] != '\t')
124 i
= get_oid_hex(line
, &oid
);
128 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
129 return 0; /* subsumed by other parents */
131 origin_data
= xcalloc(1, sizeof(struct origin_data
));
132 oidcpy(&origin_data
->oid
, &oid
);
134 if (line
[len
- 1] == '\n')
139 * At this point, line points at the beginning of comment e.g.
140 * "branch 'frotz' of git://that/repository.git".
141 * Find the repository name and point it with src.
143 src
= strstr(line
, " of ");
153 item
= unsorted_string_list_lookup(&srcs
, src
);
155 item
= string_list_append(&srcs
, src
);
156 item
->util
= xcalloc(1, sizeof(struct src_data
));
157 init_src_data(item
->util
);
159 src_data
= item
->util
;
163 src_data
->head_status
|= 1;
164 } else if (starts_with(line
, "branch ")) {
165 origin_data
->is_local_branch
= 1;
167 string_list_append(&src_data
->branch
, origin
);
168 src_data
->head_status
|= 2;
169 } else if (starts_with(line
, "tag ")) {
171 string_list_append(&src_data
->tag
, origin
+ 4);
172 src_data
->head_status
|= 2;
173 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
174 string_list_append(&src_data
->r_branch
, origin
);
175 src_data
->head_status
|= 2;
178 string_list_append(&src_data
->generic
, line
);
179 src_data
->head_status
|= 2;
182 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
183 int len
= strlen(origin
);
184 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
185 origin
= xmemdupz(origin
+ 1, len
- 2);
187 origin
= xstrfmt("%s of %s", origin
, src
);
188 if (strcmp(".", src
))
189 origin_data
->is_local_branch
= 0;
190 string_list_append(&origins
, origin
)->util
= origin_data
;
194 static void print_joined(const char *singular
, const char *plural
,
195 struct string_list
*list
, struct strbuf
*out
)
200 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
203 strbuf_addstr(out
, plural
);
204 for (i
= 0; i
< list
->nr
- 1; i
++)
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
);
211 static void add_branch_desc(struct strbuf
*out
, const char *name
)
213 struct strbuf desc
= STRBUF_INIT
;
215 if (!read_branch_desc(&desc
, name
)) {
216 const char *bp
= desc
.buf
;
218 const char *ep
= strchrnul(bp
, '\n');
221 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
224 strbuf_complete_line(out
);
226 strbuf_release(&desc
);
229 #define util_as_integral(elem) ((intptr_t)((elem)->util))
231 static void record_person_from_buf(int which
, struct string_list
*people
,
234 char *name_buf
, *name
, *name_end
;
235 struct string_list_item
*elem
;
238 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
239 name
= strstr(buffer
, field
);
242 name
+= strlen(field
);
243 name_end
= strchrnul(name
, '<');
246 while (isspace(*name_end
) && name
<= name_end
)
250 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
252 elem
= string_list_lookup(people
, name_buf
);
254 elem
= string_list_insert(people
, name_buf
);
255 elem
->util
= (void *)0;
257 elem
->util
= (void*)(util_as_integral(elem
) + 1);
262 static void record_person(int which
, struct string_list
*people
,
263 struct commit
*commit
)
265 const char *buffer
= get_commit_buffer(commit
, NULL
);
266 record_person_from_buf(which
, people
, buffer
);
267 unuse_commit_buffer(commit
, buffer
);
270 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
272 const struct string_list_item
*a
= a_
, *b
= b_
;
273 return util_as_integral(b
) - util_as_integral(a
);
276 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
279 strbuf_addstr(out
, people
->items
[0].string
);
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]));
287 strbuf_addf(out
, "%s (%d) and others",
288 people
->items
[0].string
,
289 (int)util_as_integral(&people
->items
[0]));
292 static void credit_people(struct strbuf
*out
,
293 struct string_list
*them
,
301 me
= git_author_info(IDENT_NO_DATE
);
304 me
= git_committer_info(IDENT_NO_DATE
);
310 skip_prefix(me
, them
->items
->string
, &me
) &&
311 starts_with(me
, " <")))
313 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
314 add_people_count(out
, them
);
317 static void add_people_info(struct strbuf
*out
,
318 struct string_list
*authors
,
319 struct string_list
*committers
)
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
);
326 credit_people(out
, authors
, 'a');
327 credit_people(out
, committers
, 'c');
330 static void shortlog(const char *name
,
331 struct origin_data
*origin_data
,
333 struct rev_info
*rev
,
334 struct fmt_merge_msg_opts
*opts
,
338 struct commit
*commit
;
339 struct object
*branch
;
340 struct string_list subjects
= STRING_LIST_INIT_DUP
;
341 struct string_list authors
= STRING_LIST_INIT_DUP
;
342 struct string_list committers
= STRING_LIST_INIT_DUP
;
343 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
344 struct strbuf sb
= STRBUF_INIT
;
345 const struct object_id
*oid
= &origin_data
->oid
;
346 int limit
= opts
->shortlog_len
;
348 branch
= deref_tag(the_repository
, parse_object(the_repository
, oid
),
350 the_hash_algo
->hexsz
);
351 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
354 setup_revisions(0, NULL
, rev
, NULL
);
355 add_pending_object(rev
, branch
, name
);
356 add_pending_object(rev
, &head
->object
, "^HEAD");
357 head
->object
.flags
|= UNINTERESTING
;
358 if (prepare_revision_walk(rev
))
359 die("revision walk setup failed");
360 while ((commit
= get_revision(rev
)) != NULL
) {
361 struct pretty_print_context ctx
= {0};
363 if (commit
->parents
&& commit
->parents
->next
) {
364 /* do not list a merge but count committer */
365 if (opts
->credit_people
)
366 record_person('c', &committers
, commit
);
369 if (!count
&& opts
->credit_people
)
370 /* the 'tip' committer */
371 record_person('c', &committers
, commit
);
372 if (opts
->credit_people
)
373 record_person('a', &authors
, commit
);
375 if (subjects
.nr
> limit
)
378 format_commit_message(commit
, "%s", &sb
, &ctx
);
382 string_list_append(&subjects
,
383 oid_to_hex(&commit
->object
.oid
));
385 string_list_append_nodup(&subjects
,
386 strbuf_detach(&sb
, NULL
));
389 if (opts
->credit_people
)
390 add_people_info(out
, &authors
, &committers
);
392 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
394 strbuf_addf(out
, "\n* %s:\n", name
);
396 if (origin_data
->is_local_branch
&& use_branch_desc
)
397 add_branch_desc(out
, name
);
399 for (i
= 0; i
< subjects
.nr
; i
++)
401 strbuf_addstr(out
, " ...\n");
403 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
405 clear_commit_marks((struct commit
*)branch
, flags
);
406 clear_commit_marks(head
, flags
);
407 free_commit_list(rev
->commits
);
411 string_list_clear(&authors
, 0);
412 string_list_clear(&committers
, 0);
413 string_list_clear(&subjects
, 0);
416 static void fmt_merge_msg_title(struct strbuf
*out
,
417 const char *current_branch
)
422 strbuf_addstr(out
, "Merge ");
423 for (i
= 0; i
< srcs
.nr
; i
++) {
424 struct src_data
*src_data
= srcs
.items
[i
].util
;
425 const char *subsep
= "";
427 strbuf_addstr(out
, sep
);
430 if (src_data
->head_status
== 1) {
431 strbuf_addstr(out
, srcs
.items
[i
].string
);
434 if (src_data
->head_status
== 3) {
436 strbuf_addstr(out
, "HEAD");
438 if (src_data
->branch
.nr
) {
439 strbuf_addstr(out
, subsep
);
441 print_joined("branch ", "branches ", &src_data
->branch
,
444 if (src_data
->r_branch
.nr
) {
445 strbuf_addstr(out
, subsep
);
447 print_joined("remote-tracking branch ", "remote-tracking branches ",
448 &src_data
->r_branch
, out
);
450 if (src_data
->tag
.nr
) {
451 strbuf_addstr(out
, subsep
);
453 print_joined("tag ", "tags ", &src_data
->tag
, out
);
455 if (src_data
->generic
.nr
) {
456 strbuf_addstr(out
, subsep
);
457 print_joined("commit ", "commits ", &src_data
->generic
,
460 if (strcmp(".", srcs
.items
[i
].string
))
461 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
464 if (!strcmp("master", current_branch
))
465 strbuf_addch(out
, '\n');
467 strbuf_addf(out
, " into %s\n", current_branch
);
470 static void fmt_tag_signature(struct strbuf
*tagbuf
,
475 const char *tag_body
= strstr(buf
, "\n\n");
478 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
480 strbuf_complete_line(tagbuf
);
482 strbuf_addch(tagbuf
, '\n');
483 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
487 static void fmt_merge_msg_sigs(struct strbuf
*out
)
489 int i
, tag_number
= 0, first_tag
= 0;
490 struct strbuf tagbuf
= STRBUF_INIT
;
492 for (i
= 0; i
< origins
.nr
; i
++) {
493 struct object_id
*oid
= origins
.items
[i
].util
;
494 enum object_type type
;
495 unsigned long size
, len
;
496 char *buf
= read_object_file(oid
, &type
, &size
);
497 struct strbuf sig
= STRBUF_INIT
;
499 if (!buf
|| type
!= OBJ_TAG
)
501 len
= parse_signature(buf
, size
);
504 ; /* merely annotated */
505 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
, NULL
)) {
507 strbuf_addstr(&sig
, "gpg verification failed.\n");
511 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
514 if (tag_number
== 2) {
515 struct strbuf tagline
= STRBUF_INIT
;
516 strbuf_addch(&tagline
, '\n');
517 strbuf_add_commented_lines(&tagline
,
518 origins
.items
[first_tag
].string
,
519 strlen(origins
.items
[first_tag
].string
));
520 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
522 strbuf_release(&tagline
);
524 strbuf_addch(&tagbuf
, '\n');
525 strbuf_add_commented_lines(&tagbuf
,
526 origins
.items
[i
].string
,
527 strlen(origins
.items
[i
].string
));
528 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
530 strbuf_release(&sig
);
535 strbuf_addch(out
, '\n');
536 strbuf_addbuf(out
, &tagbuf
);
538 strbuf_release(&tagbuf
);
541 static void find_merge_parents(struct merge_parents
*result
,
542 struct strbuf
*in
, struct object_id
*head
)
544 struct commit_list
*parents
;
545 struct commit
*head_commit
;
549 while (pos
< in
->len
) {
551 char *p
= in
->buf
+ pos
;
552 char *newline
= strchr(p
, '\n');
554 struct object_id oid
;
555 struct commit
*parent
;
558 len
= newline
? newline
- p
: strlen(p
);
559 pos
+= len
+ !!newline
;
561 if (parse_oid_hex(p
, &oid
, &q
) ||
564 continue; /* skip not-for-merge */
566 * Do not use get_merge_parent() here; we do not have
567 * "name" here and we do not want to contaminate its
570 obj
= parse_object(the_repository
, &oid
);
571 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
574 commit_list_insert(parent
, &parents
);
575 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
577 head_commit
= lookup_commit(the_repository
, head
);
579 commit_list_insert(head_commit
, &parents
);
580 reduce_heads_replace(&parents
);
583 struct commit
*cmit
= pop_commit(&parents
);
584 for (i
= 0; i
< result
->nr
; i
++)
585 if (!oidcmp(&result
->item
[i
].commit
, &cmit
->object
.oid
))
586 result
->item
[i
].used
= 1;
589 for (i
= j
= 0; i
< result
->nr
; i
++) {
590 if (result
->item
[i
].used
) {
592 result
->item
[j
] = result
->item
[i
];
599 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
600 struct fmt_merge_msg_opts
*opts
)
603 struct object_id head_oid
;
604 const char *current_branch
;
605 void *current_branch_to_free
;
606 struct merge_parents merge_parents
;
608 memset(&merge_parents
, 0, sizeof(merge_parents
));
610 /* get current branch */
611 current_branch
= current_branch_to_free
=
612 resolve_refdup("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
);
614 die("No current branch");
615 if (starts_with(current_branch
, "refs/heads/"))
616 current_branch
+= 11;
618 find_merge_parents(&merge_parents
, in
, &head_oid
);
621 while (pos
< in
->len
) {
623 char *newline
, *p
= in
->buf
+ pos
;
625 newline
= strchr(p
, '\n');
626 len
= newline
? newline
- p
: strlen(p
);
627 pos
+= len
+ !!newline
;
630 if (handle_line(p
, &merge_parents
))
631 die("error in line %d: %.*s", i
, len
, p
);
634 if (opts
->add_title
&& srcs
.nr
)
635 fmt_merge_msg_title(out
, current_branch
);
638 fmt_merge_msg_sigs(out
);
640 if (opts
->shortlog_len
) {
644 head
= lookup_commit_or_die(&head_oid
, "HEAD");
645 init_revisions(&rev
, NULL
);
646 rev
.commit_format
= CMIT_FMT_ONELINE
;
647 rev
.ignore_merges
= 1;
650 strbuf_complete_line(out
);
652 for (i
= 0; i
< origins
.nr
; i
++)
653 shortlog(origins
.items
[i
].string
,
654 origins
.items
[i
].util
,
655 head
, &rev
, opts
, out
);
658 strbuf_complete_line(out
);
659 free(current_branch_to_free
);
660 free(merge_parents
.item
);
664 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
666 const char *inpath
= NULL
;
667 const char *message
= NULL
;
668 int shortlog_len
= -1;
669 struct option options
[] = {
670 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
671 N_("populate log with at most <n> entries from shortlog"),
672 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
673 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, N_("n"),
674 N_("alias for --log (deprecated)"),
675 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
676 DEFAULT_MERGE_LOG_LEN
},
677 OPT_STRING('m', "message", &message
, N_("text"),
678 N_("use <text> as start of message")),
679 OPT_FILENAME('F', "file", &inpath
, N_("file to read from")),
684 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
686 struct fmt_merge_msg_opts opts
;
688 git_config(fmt_merge_msg_config
, NULL
);
689 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
692 usage_with_options(fmt_merge_msg_usage
, options
);
693 if (shortlog_len
< 0)
694 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
696 if (inpath
&& strcmp(inpath
, "-")) {
697 in
= fopen(inpath
, "r");
699 die_errno("cannot open '%s'", inpath
);
702 if (strbuf_read(&input
, fileno(in
), 0) < 0)
703 die_errno("could not read input file");
706 strbuf_addstr(&output
, message
);
708 memset(&opts
, 0, sizeof(opts
));
709 opts
.add_title
= !message
;
710 opts
.credit_people
= 1;
711 opts
.shortlog_len
= shortlog_len
;
713 ret
= fmt_merge_msg(&input
, &output
, &opts
);
716 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);