1 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
9 #include "diff-merges.h"
13 #include "string-list.h"
15 #include "fmt-merge-msg.h"
16 #include "commit-reach.h"
17 #include "gpg-interface.h"
18 #include "wildmatch.h"
20 static int use_branch_desc
;
21 static int suppress_dest_pattern_seen
;
22 static struct string_list suppress_dest_patterns
= STRING_LIST_INIT_DUP
;
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
);
35 } else if (!strcmp(key
, "merge.suppressdest")) {
37 return config_error_nonbool(key
);
39 string_list_clear(&suppress_dest_patterns
, 0);
41 string_list_append(&suppress_dest_patterns
, value
);
42 suppress_dest_pattern_seen
= 1;
44 return git_default_config(key
, value
, cb
);
49 /* merge data per repository where the merged tips came from */
51 struct string_list branch
, tag
, r_branch
, generic
;
57 unsigned is_local_branch
:1;
60 static void init_src_data(struct src_data
*data
)
62 data
->branch
.strdup_strings
= 1;
63 data
->tag
.strdup_strings
= 1;
64 data
->r_branch
.strdup_strings
= 1;
65 data
->generic
.strdup_strings
= 1;
68 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
69 static struct string_list origins
= STRING_LIST_INIT_DUP
;
71 struct merge_parents
{
74 struct object_id given
;
75 struct object_id commit
;
81 * I know, I know, this is inefficient, but you won't be pulling and merging
82 * hundreds of heads at a time anyway.
84 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
85 struct object_id
*given
,
86 struct object_id
*commit
)
89 for (i
= 0; i
< table
->nr
; i
++) {
90 if (given
&& !oideq(&table
->item
[i
].given
, given
))
92 if (commit
&& !oideq(&table
->item
[i
].commit
, commit
))
94 return &table
->item
[i
];
99 static void add_merge_parent(struct merge_parents
*table
,
100 struct object_id
*given
,
101 struct object_id
*commit
)
103 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
105 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
106 oidcpy(&table
->item
[table
->nr
].given
, given
);
107 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
108 table
->item
[table
->nr
].used
= 0;
112 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
114 int i
, len
= strlen(line
);
115 struct origin_data
*origin_data
;
117 const char *origin
, *tag_name
;
118 char *to_free
= NULL
;
119 struct src_data
*src_data
;
120 struct string_list_item
*item
;
121 int pulling_head
= 0;
122 struct object_id oid
;
123 const unsigned hexsz
= the_hash_algo
->hexsz
;
125 if (len
< hexsz
+ 3 || line
[hexsz
] != '\t')
128 if (starts_with(line
+ hexsz
+ 1, "not-for-merge"))
131 if (line
[hexsz
+ 1] != '\t')
134 i
= get_oid_hex(line
, &oid
);
138 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
139 return 0; /* subsumed by other parents */
141 CALLOC_ARRAY(origin_data
, 1);
142 oidcpy(&origin_data
->oid
, &oid
);
144 if (line
[len
- 1] == '\n')
149 * At this point, line points at the beginning of comment e.g.
150 * "branch 'frotz' of git://that/repository.git".
151 * Find the repository name and point it with src.
153 src
= strstr(line
, " of ");
163 item
= unsorted_string_list_lookup(&srcs
, src
);
165 item
= string_list_append(&srcs
, src
);
166 item
->util
= xcalloc(1, sizeof(struct src_data
));
167 init_src_data(item
->util
);
169 src_data
= item
->util
;
173 src_data
->head_status
|= 1;
174 } else if (skip_prefix(line
, "branch ", &origin
)) {
175 origin_data
->is_local_branch
= 1;
176 string_list_append(&src_data
->branch
, origin
);
177 src_data
->head_status
|= 2;
178 } else if (skip_prefix(line
, "tag ", &tag_name
)) {
180 string_list_append(&src_data
->tag
, tag_name
);
181 src_data
->head_status
|= 2;
182 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
183 string_list_append(&src_data
->r_branch
, origin
);
184 src_data
->head_status
|= 2;
187 string_list_append(&src_data
->generic
, line
);
188 src_data
->head_status
|= 2;
191 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
192 int len
= strlen(origin
);
193 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
194 origin
= to_free
= xmemdupz(origin
+ 1, len
- 2);
196 origin
= to_free
= xstrfmt("%s of %s", origin
, src
);
197 if (strcmp(".", src
))
198 origin_data
->is_local_branch
= 0;
199 string_list_append(&origins
, origin
)->util
= origin_data
;
204 static void print_joined(const char *singular
, const char *plural
,
205 struct string_list
*list
, struct strbuf
*out
)
210 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
213 strbuf_addstr(out
, plural
);
214 for (i
= 0; i
< list
->nr
- 1; i
++)
215 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
216 list
->items
[i
].string
);
217 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
221 static void add_branch_desc(struct strbuf
*out
, const char *name
)
223 struct strbuf desc
= STRBUF_INIT
;
225 if (!read_branch_desc(&desc
, name
)) {
226 const char *bp
= desc
.buf
;
228 const char *ep
= strchrnul(bp
, '\n');
231 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
234 strbuf_complete_line(out
);
236 strbuf_release(&desc
);
239 #define util_as_integral(elem) ((intptr_t)((elem)->util))
241 static void record_person_from_buf(int which
, struct string_list
*people
,
244 char *name_buf
, *name
, *name_end
;
245 struct string_list_item
*elem
;
248 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
249 name
= strstr(buffer
, field
);
252 name
+= strlen(field
);
253 name_end
= strchrnul(name
, '<');
256 while (isspace(*name_end
) && name
<= name_end
)
260 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
262 elem
= string_list_lookup(people
, name_buf
);
264 elem
= string_list_insert(people
, name_buf
);
265 elem
->util
= (void *)0;
267 elem
->util
= (void*)(util_as_integral(elem
) + 1);
272 static void record_person(int which
, struct string_list
*people
,
273 struct commit
*commit
)
275 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
277 record_person_from_buf(which
, people
, buffer
);
278 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
281 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
283 const struct string_list_item
*a
= a_
, *b
= b_
;
284 return util_as_integral(b
) - util_as_integral(a
);
287 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
290 strbuf_addstr(out
, people
->items
[0].string
);
291 else if (people
->nr
== 2)
292 strbuf_addf(out
, "%s (%d) and %s (%d)",
293 people
->items
[0].string
,
294 (int)util_as_integral(&people
->items
[0]),
295 people
->items
[1].string
,
296 (int)util_as_integral(&people
->items
[1]));
298 strbuf_addf(out
, "%s (%d) and others",
299 people
->items
[0].string
,
300 (int)util_as_integral(&people
->items
[0]));
303 static void credit_people(struct strbuf
*out
,
304 struct string_list
*them
,
312 me
= git_author_info(IDENT_NO_DATE
);
315 me
= git_committer_info(IDENT_NO_DATE
);
321 skip_prefix(me
, them
->items
->string
, &me
) &&
322 starts_with(me
, " <")))
324 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
325 add_people_count(out
, them
);
328 static void add_people_info(struct strbuf
*out
,
329 struct string_list
*authors
,
330 struct string_list
*committers
)
332 QSORT(authors
->items
, authors
->nr
,
333 cmp_string_list_util_as_integral
);
334 QSORT(committers
->items
, committers
->nr
,
335 cmp_string_list_util_as_integral
);
337 credit_people(out
, authors
, 'a');
338 credit_people(out
, committers
, 'c');
341 static void shortlog(const char *name
,
342 struct origin_data
*origin_data
,
344 struct rev_info
*rev
,
345 struct fmt_merge_msg_opts
*opts
,
349 struct commit
*commit
;
350 struct object
*branch
;
351 struct string_list subjects
= STRING_LIST_INIT_DUP
;
352 struct string_list authors
= STRING_LIST_INIT_DUP
;
353 struct string_list committers
= STRING_LIST_INIT_DUP
;
354 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
355 struct strbuf sb
= STRBUF_INIT
;
356 const struct object_id
*oid
= &origin_data
->oid
;
357 int limit
= opts
->shortlog_len
;
359 branch
= deref_tag(the_repository
, parse_object(the_repository
, oid
),
361 the_hash_algo
->hexsz
);
362 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
365 setup_revisions(0, NULL
, rev
, NULL
);
366 add_pending_object(rev
, branch
, name
);
367 add_pending_object(rev
, &head
->object
, "^HEAD");
368 head
->object
.flags
|= UNINTERESTING
;
369 if (prepare_revision_walk(rev
))
370 die("revision walk setup failed");
371 while ((commit
= get_revision(rev
)) != NULL
) {
372 struct pretty_print_context ctx
= {0};
374 if (commit
->parents
&& commit
->parents
->next
) {
375 /* do not list a merge but count committer */
376 if (opts
->credit_people
)
377 record_person('c', &committers
, commit
);
380 if (!count
&& opts
->credit_people
)
381 /* the 'tip' committer */
382 record_person('c', &committers
, commit
);
383 if (opts
->credit_people
)
384 record_person('a', &authors
, commit
);
386 if (subjects
.nr
> limit
)
389 repo_format_commit_message(the_repository
, commit
, "%s", &sb
,
394 string_list_append(&subjects
,
395 oid_to_hex(&commit
->object
.oid
));
397 string_list_append_nodup(&subjects
,
398 strbuf_detach(&sb
, NULL
));
401 if (opts
->credit_people
)
402 add_people_info(out
, &authors
, &committers
);
404 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
406 strbuf_addf(out
, "\n* %s:\n", name
);
408 if (origin_data
->is_local_branch
&& use_branch_desc
)
409 add_branch_desc(out
, name
);
411 for (i
= 0; i
< subjects
.nr
; i
++)
413 strbuf_addstr(out
, " ...\n");
415 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
417 clear_commit_marks((struct commit
*)branch
, flags
);
418 clear_commit_marks(head
, flags
);
419 free_commit_list(rev
->commits
);
423 string_list_clear(&authors
, 0);
424 string_list_clear(&committers
, 0);
425 string_list_clear(&subjects
, 0);
429 * See if dest_branch matches with any glob pattern on the
430 * suppress_dest_patterns list.
432 * We may want to also allow negative matches e.g. ":!glob" like we do
433 * for pathspec, but for now, let's keep it simple and stupid.
435 static int dest_suppressed(const char *dest_branch
)
437 struct string_list_item
*item
;
439 for_each_string_list_item(item
, &suppress_dest_patterns
) {
440 if (!wildmatch(item
->string
, dest_branch
, WM_PATHNAME
))
446 static void fmt_merge_msg_title(struct strbuf
*out
,
447 const char *current_branch
)
452 strbuf_addstr(out
, "Merge ");
453 for (i
= 0; i
< srcs
.nr
; i
++) {
454 struct src_data
*src_data
= srcs
.items
[i
].util
;
455 const char *subsep
= "";
457 strbuf_addstr(out
, sep
);
460 if (src_data
->head_status
== 1) {
461 strbuf_addstr(out
, srcs
.items
[i
].string
);
464 if (src_data
->head_status
== 3) {
466 strbuf_addstr(out
, "HEAD");
468 if (src_data
->branch
.nr
) {
469 strbuf_addstr(out
, subsep
);
471 print_joined("branch ", "branches ", &src_data
->branch
,
474 if (src_data
->r_branch
.nr
) {
475 strbuf_addstr(out
, subsep
);
477 print_joined("remote-tracking branch ", "remote-tracking branches ",
478 &src_data
->r_branch
, out
);
480 if (src_data
->tag
.nr
) {
481 strbuf_addstr(out
, subsep
);
483 print_joined("tag ", "tags ", &src_data
->tag
, out
);
485 if (src_data
->generic
.nr
) {
486 strbuf_addstr(out
, subsep
);
487 print_joined("commit ", "commits ", &src_data
->generic
,
490 if (strcmp(".", srcs
.items
[i
].string
))
491 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
494 if (!dest_suppressed(current_branch
))
495 strbuf_addf(out
, " into %s", current_branch
);
496 strbuf_addch(out
, '\n');
499 static void fmt_tag_signature(struct strbuf
*tagbuf
,
504 const char *tag_body
= strstr(buf
, "\n\n");
507 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
509 strbuf_complete_line(tagbuf
);
511 strbuf_addch(tagbuf
, '\n');
512 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
516 static void fmt_merge_msg_sigs(struct strbuf
*out
)
518 int i
, tag_number
= 0, first_tag
= 0;
519 struct strbuf tagbuf
= STRBUF_INIT
;
521 for (i
= 0; i
< origins
.nr
; i
++) {
522 struct object_id
*oid
= origins
.items
[i
].util
;
523 enum object_type type
;
525 char *buf
= repo_read_object_file(the_repository
, oid
, &type
,
528 unsigned long len
= size
;
529 struct signature_check sigc
= { NULL
};
530 struct strbuf payload
= STRBUF_INIT
, sig
= STRBUF_INIT
;
532 if (!buf
|| type
!= OBJ_TAG
)
535 if (!parse_signature(buf
, size
, &payload
, &sig
))
536 ;/* merely annotated */
540 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
541 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
542 if (check_signature(&sigc
, sig
.buf
, sig
.len
) &&
544 strbuf_addstr(&sig
, "gpg verification failed.\n");
546 strbuf_addstr(&sig
, sigc
.output
);
550 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
553 if (tag_number
== 2) {
554 struct strbuf tagline
= STRBUF_INIT
;
555 strbuf_addch(&tagline
, '\n');
556 strbuf_add_commented_lines(&tagline
,
557 origins
.items
[first_tag
].string
,
558 strlen(origins
.items
[first_tag
].string
));
559 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
561 strbuf_release(&tagline
);
563 strbuf_addch(&tagbuf
, '\n');
564 strbuf_add_commented_lines(&tagbuf
,
565 origins
.items
[i
].string
,
566 strlen(origins
.items
[i
].string
));
567 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
569 strbuf_release(&payload
);
570 strbuf_release(&sig
);
571 signature_check_clear(&sigc
);
576 strbuf_addch(out
, '\n');
577 strbuf_addbuf(out
, &tagbuf
);
579 strbuf_release(&tagbuf
);
582 static void find_merge_parents(struct merge_parents
*result
,
583 struct strbuf
*in
, struct object_id
*head
)
585 struct commit_list
*parents
;
586 struct commit
*head_commit
;
590 while (pos
< in
->len
) {
592 char *p
= in
->buf
+ pos
;
593 char *newline
= strchr(p
, '\n');
595 struct object_id oid
;
596 struct commit
*parent
;
599 len
= newline
? newline
- p
: strlen(p
);
600 pos
+= len
+ !!newline
;
602 if (parse_oid_hex(p
, &oid
, &q
) ||
605 continue; /* skip not-for-merge */
607 * Do not use get_merge_parent() here; we do not have
608 * "name" here and we do not want to contaminate its
611 obj
= parse_object(the_repository
, &oid
);
612 parent
= (struct commit
*)repo_peel_to_type(the_repository
,
617 commit_list_insert(parent
, &parents
);
618 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
620 head_commit
= lookup_commit(the_repository
, head
);
622 commit_list_insert(head_commit
, &parents
);
623 reduce_heads_replace(&parents
);
626 struct commit
*cmit
= pop_commit(&parents
);
627 for (i
= 0; i
< result
->nr
; i
++)
628 if (oideq(&result
->item
[i
].commit
, &cmit
->object
.oid
))
629 result
->item
[i
].used
= 1;
632 for (i
= j
= 0; i
< result
->nr
; i
++) {
633 if (result
->item
[i
].used
) {
635 result
->item
[j
] = result
->item
[i
];
643 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
644 struct fmt_merge_msg_opts
*opts
)
647 struct object_id head_oid
;
648 const char *current_branch
;
649 void *current_branch_to_free
;
650 struct merge_parents merge_parents
;
652 if (!suppress_dest_pattern_seen
) {
653 string_list_append(&suppress_dest_patterns
, "main");
654 string_list_append(&suppress_dest_patterns
, "master");
657 memset(&merge_parents
, 0, sizeof(merge_parents
));
659 /* learn the commit that we merge into and the current branch name */
660 current_branch
= current_branch_to_free
=
661 resolve_refdup("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
);
663 die("No current branch");
666 current_branch
= opts
->into_name
;
667 else if (starts_with(current_branch
, "refs/heads/"))
668 current_branch
+= 11;
670 find_merge_parents(&merge_parents
, in
, &head_oid
);
673 while (pos
< in
->len
) {
675 char *newline
, *p
= in
->buf
+ pos
;
677 newline
= strchr(p
, '\n');
678 len
= newline
? newline
- p
: strlen(p
);
679 pos
+= len
+ !!newline
;
682 if (handle_line(p
, &merge_parents
))
683 die("error in line %d: %.*s", i
, len
, p
);
686 if (opts
->add_title
&& srcs
.nr
)
687 fmt_merge_msg_title(out
, current_branch
);
690 fmt_merge_msg_sigs(out
);
692 if (opts
->shortlog_len
) {
696 head
= lookup_commit_or_die(&head_oid
, "HEAD");
697 repo_init_revisions(the_repository
, &rev
, NULL
);
698 rev
.commit_format
= CMIT_FMT_ONELINE
;
699 diff_merges_suppress(&rev
);
702 strbuf_complete_line(out
);
704 for (i
= 0; i
< origins
.nr
; i
++)
705 shortlog(origins
.items
[i
].string
,
706 origins
.items
[i
].util
,
707 head
, &rev
, opts
, out
);
708 release_revisions(&rev
);
711 strbuf_complete_line(out
);
712 free(current_branch_to_free
);
713 free(merge_parents
.item
);