1 #include "git-compat-util.h"
5 #include "object-store.h"
7 #include "diff-merges.h"
11 #include "string-list.h"
13 #include "fmt-merge-msg.h"
14 #include "commit-reach.h"
15 #include "gpg-interface.h"
17 static int use_branch_desc
;
18 static int suppress_dest_pattern_seen
;
19 static struct string_list suppress_dest_patterns
= STRING_LIST_INIT_DUP
;
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
);
32 } else if (!strcmp(key
, "merge.suppressdest")) {
34 return config_error_nonbool(key
);
36 string_list_clear(&suppress_dest_patterns
, 0);
38 string_list_append(&suppress_dest_patterns
, value
);
39 suppress_dest_pattern_seen
= 1;
41 return git_default_config(key
, value
, cb
);
46 /* merge data per repository where the merged tips came from */
48 struct string_list branch
, tag
, r_branch
, generic
;
54 unsigned is_local_branch
:1;
57 static void init_src_data(struct src_data
*data
)
59 data
->branch
.strdup_strings
= 1;
60 data
->tag
.strdup_strings
= 1;
61 data
->r_branch
.strdup_strings
= 1;
62 data
->generic
.strdup_strings
= 1;
65 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
66 static struct string_list origins
= STRING_LIST_INIT_DUP
;
68 struct merge_parents
{
71 struct object_id given
;
72 struct object_id commit
;
78 * I know, I know, this is inefficient, but you won't be pulling and merging
79 * hundreds of heads at a time anyway.
81 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
82 struct object_id
*given
,
83 struct object_id
*commit
)
86 for (i
= 0; i
< table
->nr
; i
++) {
87 if (given
&& !oideq(&table
->item
[i
].given
, given
))
89 if (commit
&& !oideq(&table
->item
[i
].commit
, commit
))
91 return &table
->item
[i
];
96 static void add_merge_parent(struct merge_parents
*table
,
97 struct object_id
*given
,
98 struct object_id
*commit
)
100 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
102 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
103 oidcpy(&table
->item
[table
->nr
].given
, given
);
104 oidcpy(&table
->item
[table
->nr
].commit
, commit
);
105 table
->item
[table
->nr
].used
= 0;
109 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
111 int i
, len
= strlen(line
);
112 struct origin_data
*origin_data
;
114 const char *origin
, *tag_name
;
115 char *to_free
= NULL
;
116 struct src_data
*src_data
;
117 struct string_list_item
*item
;
118 int pulling_head
= 0;
119 struct object_id oid
;
120 const unsigned hexsz
= the_hash_algo
->hexsz
;
122 if (len
< hexsz
+ 3 || line
[hexsz
] != '\t')
125 if (starts_with(line
+ hexsz
+ 1, "not-for-merge"))
128 if (line
[hexsz
+ 1] != '\t')
131 i
= get_oid_hex(line
, &oid
);
135 if (!find_merge_parent(merge_parents
, &oid
, NULL
))
136 return 0; /* subsumed by other parents */
138 CALLOC_ARRAY(origin_data
, 1);
139 oidcpy(&origin_data
->oid
, &oid
);
141 if (line
[len
- 1] == '\n')
146 * At this point, line points at the beginning of comment e.g.
147 * "branch 'frotz' of git://that/repository.git".
148 * Find the repository name and point it with src.
150 src
= strstr(line
, " of ");
160 item
= unsorted_string_list_lookup(&srcs
, src
);
162 item
= string_list_append(&srcs
, src
);
163 item
->util
= xcalloc(1, sizeof(struct src_data
));
164 init_src_data(item
->util
);
166 src_data
= item
->util
;
170 src_data
->head_status
|= 1;
171 } else if (skip_prefix(line
, "branch ", &origin
)) {
172 origin_data
->is_local_branch
= 1;
173 string_list_append(&src_data
->branch
, origin
);
174 src_data
->head_status
|= 2;
175 } else if (skip_prefix(line
, "tag ", &tag_name
)) {
177 string_list_append(&src_data
->tag
, tag_name
);
178 src_data
->head_status
|= 2;
179 } else if (skip_prefix(line
, "remote-tracking branch ", &origin
)) {
180 string_list_append(&src_data
->r_branch
, origin
);
181 src_data
->head_status
|= 2;
184 string_list_append(&src_data
->generic
, line
);
185 src_data
->head_status
|= 2;
188 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
189 int len
= strlen(origin
);
190 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
191 origin
= to_free
= xmemdupz(origin
+ 1, len
- 2);
193 origin
= to_free
= xstrfmt("%s of %s", origin
, src
);
194 if (strcmp(".", src
))
195 origin_data
->is_local_branch
= 0;
196 string_list_append(&origins
, origin
)->util
= origin_data
;
201 static void print_joined(const char *singular
, const char *plural
,
202 struct string_list
*list
, struct strbuf
*out
)
207 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
210 strbuf_addstr(out
, plural
);
211 for (i
= 0; i
< list
->nr
- 1; i
++)
212 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
213 list
->items
[i
].string
);
214 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
218 static void add_branch_desc(struct strbuf
*out
, const char *name
)
220 struct strbuf desc
= STRBUF_INIT
;
222 if (!read_branch_desc(&desc
, name
)) {
223 const char *bp
= desc
.buf
;
225 const char *ep
= strchrnul(bp
, '\n');
228 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
231 strbuf_complete_line(out
);
233 strbuf_release(&desc
);
236 #define util_as_integral(elem) ((intptr_t)((elem)->util))
238 static void record_person_from_buf(int which
, struct string_list
*people
,
241 char *name_buf
, *name
, *name_end
;
242 struct string_list_item
*elem
;
245 field
= (which
== 'a') ? "\nauthor " : "\ncommitter ";
246 name
= strstr(buffer
, field
);
249 name
+= strlen(field
);
250 name_end
= strchrnul(name
, '<');
253 while (isspace(*name_end
) && name
<= name_end
)
257 name_buf
= xmemdupz(name
, name_end
- name
+ 1);
259 elem
= string_list_lookup(people
, name_buf
);
261 elem
= string_list_insert(people
, name_buf
);
262 elem
->util
= (void *)0;
264 elem
->util
= (void*)(util_as_integral(elem
) + 1);
269 static void record_person(int which
, struct string_list
*people
,
270 struct commit
*commit
)
272 const char *buffer
= get_commit_buffer(commit
, NULL
);
273 record_person_from_buf(which
, people
, buffer
);
274 unuse_commit_buffer(commit
, buffer
);
277 static int cmp_string_list_util_as_integral(const void *a_
, const void *b_
)
279 const struct string_list_item
*a
= a_
, *b
= b_
;
280 return util_as_integral(b
) - util_as_integral(a
);
283 static void add_people_count(struct strbuf
*out
, struct string_list
*people
)
286 strbuf_addstr(out
, people
->items
[0].string
);
287 else if (people
->nr
== 2)
288 strbuf_addf(out
, "%s (%d) and %s (%d)",
289 people
->items
[0].string
,
290 (int)util_as_integral(&people
->items
[0]),
291 people
->items
[1].string
,
292 (int)util_as_integral(&people
->items
[1]));
294 strbuf_addf(out
, "%s (%d) and others",
295 people
->items
[0].string
,
296 (int)util_as_integral(&people
->items
[0]));
299 static void credit_people(struct strbuf
*out
,
300 struct string_list
*them
,
308 me
= git_author_info(IDENT_NO_DATE
);
311 me
= git_committer_info(IDENT_NO_DATE
);
317 skip_prefix(me
, them
->items
->string
, &me
) &&
318 starts_with(me
, " <")))
320 strbuf_addf(out
, "\n%c %s ", comment_line_char
, label
);
321 add_people_count(out
, them
);
324 static void add_people_info(struct strbuf
*out
,
325 struct string_list
*authors
,
326 struct string_list
*committers
)
328 QSORT(authors
->items
, authors
->nr
,
329 cmp_string_list_util_as_integral
);
330 QSORT(committers
->items
, committers
->nr
,
331 cmp_string_list_util_as_integral
);
333 credit_people(out
, authors
, 'a');
334 credit_people(out
, committers
, 'c');
337 static void shortlog(const char *name
,
338 struct origin_data
*origin_data
,
340 struct rev_info
*rev
,
341 struct fmt_merge_msg_opts
*opts
,
345 struct commit
*commit
;
346 struct object
*branch
;
347 struct string_list subjects
= STRING_LIST_INIT_DUP
;
348 struct string_list authors
= STRING_LIST_INIT_DUP
;
349 struct string_list committers
= STRING_LIST_INIT_DUP
;
350 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
351 struct strbuf sb
= STRBUF_INIT
;
352 const struct object_id
*oid
= &origin_data
->oid
;
353 int limit
= opts
->shortlog_len
;
355 branch
= deref_tag(the_repository
, parse_object(the_repository
, oid
),
357 the_hash_algo
->hexsz
);
358 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
361 setup_revisions(0, NULL
, rev
, NULL
);
362 add_pending_object(rev
, branch
, name
);
363 add_pending_object(rev
, &head
->object
, "^HEAD");
364 head
->object
.flags
|= UNINTERESTING
;
365 if (prepare_revision_walk(rev
))
366 die("revision walk setup failed");
367 while ((commit
= get_revision(rev
)) != NULL
) {
368 struct pretty_print_context ctx
= {0};
370 if (commit
->parents
&& commit
->parents
->next
) {
371 /* do not list a merge but count committer */
372 if (opts
->credit_people
)
373 record_person('c', &committers
, commit
);
376 if (!count
&& opts
->credit_people
)
377 /* the 'tip' committer */
378 record_person('c', &committers
, commit
);
379 if (opts
->credit_people
)
380 record_person('a', &authors
, commit
);
382 if (subjects
.nr
> limit
)
385 format_commit_message(commit
, "%s", &sb
, &ctx
);
389 string_list_append(&subjects
,
390 oid_to_hex(&commit
->object
.oid
));
392 string_list_append_nodup(&subjects
,
393 strbuf_detach(&sb
, NULL
));
396 if (opts
->credit_people
)
397 add_people_info(out
, &authors
, &committers
);
399 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
401 strbuf_addf(out
, "\n* %s:\n", name
);
403 if (origin_data
->is_local_branch
&& use_branch_desc
)
404 add_branch_desc(out
, name
);
406 for (i
= 0; i
< subjects
.nr
; i
++)
408 strbuf_addstr(out
, " ...\n");
410 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
412 clear_commit_marks((struct commit
*)branch
, flags
);
413 clear_commit_marks(head
, flags
);
414 free_commit_list(rev
->commits
);
418 string_list_clear(&authors
, 0);
419 string_list_clear(&committers
, 0);
420 string_list_clear(&subjects
, 0);
424 * See if dest_branch matches with any glob pattern on the
425 * suppress_dest_patterns list.
427 * We may want to also allow negative matches e.g. ":!glob" like we do
428 * for pathspec, but for now, let's keep it simple and stupid.
430 static int dest_suppressed(const char *dest_branch
)
432 struct string_list_item
*item
;
434 for_each_string_list_item(item
, &suppress_dest_patterns
) {
435 if (!wildmatch(item
->string
, dest_branch
, WM_PATHNAME
))
441 static void fmt_merge_msg_title(struct strbuf
*out
,
442 const char *current_branch
)
447 strbuf_addstr(out
, "Merge ");
448 for (i
= 0; i
< srcs
.nr
; i
++) {
449 struct src_data
*src_data
= srcs
.items
[i
].util
;
450 const char *subsep
= "";
452 strbuf_addstr(out
, sep
);
455 if (src_data
->head_status
== 1) {
456 strbuf_addstr(out
, srcs
.items
[i
].string
);
459 if (src_data
->head_status
== 3) {
461 strbuf_addstr(out
, "HEAD");
463 if (src_data
->branch
.nr
) {
464 strbuf_addstr(out
, subsep
);
466 print_joined("branch ", "branches ", &src_data
->branch
,
469 if (src_data
->r_branch
.nr
) {
470 strbuf_addstr(out
, subsep
);
472 print_joined("remote-tracking branch ", "remote-tracking branches ",
473 &src_data
->r_branch
, out
);
475 if (src_data
->tag
.nr
) {
476 strbuf_addstr(out
, subsep
);
478 print_joined("tag ", "tags ", &src_data
->tag
, out
);
480 if (src_data
->generic
.nr
) {
481 strbuf_addstr(out
, subsep
);
482 print_joined("commit ", "commits ", &src_data
->generic
,
485 if (strcmp(".", srcs
.items
[i
].string
))
486 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
489 if (!dest_suppressed(current_branch
))
490 strbuf_addf(out
, " into %s", current_branch
);
491 strbuf_addch(out
, '\n');
494 static void fmt_tag_signature(struct strbuf
*tagbuf
,
499 const char *tag_body
= strstr(buf
, "\n\n");
502 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
504 strbuf_complete_line(tagbuf
);
506 strbuf_addch(tagbuf
, '\n');
507 strbuf_add_commented_lines(tagbuf
, sig
->buf
, sig
->len
);
511 static void fmt_merge_msg_sigs(struct strbuf
*out
)
513 int i
, tag_number
= 0, first_tag
= 0;
514 struct strbuf tagbuf
= STRBUF_INIT
;
516 for (i
= 0; i
< origins
.nr
; i
++) {
517 struct object_id
*oid
= origins
.items
[i
].util
;
518 enum object_type type
;
520 char *buf
= read_object_file(oid
, &type
, &size
);
522 unsigned long len
= size
;
523 struct signature_check sigc
= { NULL
};
524 struct strbuf payload
= STRBUF_INIT
, sig
= STRBUF_INIT
;
526 if (!buf
|| type
!= OBJ_TAG
)
529 if (!parse_signature(buf
, size
, &payload
, &sig
))
530 ;/* merely annotated */
534 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
535 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
536 if (check_signature(&sigc
, sig
.buf
, sig
.len
) &&
538 strbuf_addstr(&sig
, "gpg verification failed.\n");
540 strbuf_addstr(&sig
, sigc
.output
);
544 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
547 if (tag_number
== 2) {
548 struct strbuf tagline
= STRBUF_INIT
;
549 strbuf_addch(&tagline
, '\n');
550 strbuf_add_commented_lines(&tagline
,
551 origins
.items
[first_tag
].string
,
552 strlen(origins
.items
[first_tag
].string
));
553 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
555 strbuf_release(&tagline
);
557 strbuf_addch(&tagbuf
, '\n');
558 strbuf_add_commented_lines(&tagbuf
,
559 origins
.items
[i
].string
,
560 strlen(origins
.items
[i
].string
));
561 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
563 strbuf_release(&payload
);
564 strbuf_release(&sig
);
565 signature_check_clear(&sigc
);
570 strbuf_addch(out
, '\n');
571 strbuf_addbuf(out
, &tagbuf
);
573 strbuf_release(&tagbuf
);
576 static void find_merge_parents(struct merge_parents
*result
,
577 struct strbuf
*in
, struct object_id
*head
)
579 struct commit_list
*parents
;
580 struct commit
*head_commit
;
584 while (pos
< in
->len
) {
586 char *p
= in
->buf
+ pos
;
587 char *newline
= strchr(p
, '\n');
589 struct object_id oid
;
590 struct commit
*parent
;
593 len
= newline
? newline
- p
: strlen(p
);
594 pos
+= len
+ !!newline
;
596 if (parse_oid_hex(p
, &oid
, &q
) ||
599 continue; /* skip not-for-merge */
601 * Do not use get_merge_parent() here; we do not have
602 * "name" here and we do not want to contaminate its
605 obj
= parse_object(the_repository
, &oid
);
606 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
609 commit_list_insert(parent
, &parents
);
610 add_merge_parent(result
, &obj
->oid
, &parent
->object
.oid
);
612 head_commit
= lookup_commit(the_repository
, head
);
614 commit_list_insert(head_commit
, &parents
);
615 reduce_heads_replace(&parents
);
618 struct commit
*cmit
= pop_commit(&parents
);
619 for (i
= 0; i
< result
->nr
; i
++)
620 if (oideq(&result
->item
[i
].commit
, &cmit
->object
.oid
))
621 result
->item
[i
].used
= 1;
624 for (i
= j
= 0; i
< result
->nr
; i
++) {
625 if (result
->item
[i
].used
) {
627 result
->item
[j
] = result
->item
[i
];
635 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
636 struct fmt_merge_msg_opts
*opts
)
639 struct object_id head_oid
;
640 const char *current_branch
;
641 void *current_branch_to_free
;
642 struct merge_parents merge_parents
;
644 if (!suppress_dest_pattern_seen
) {
645 string_list_append(&suppress_dest_patterns
, "main");
646 string_list_append(&suppress_dest_patterns
, "master");
649 memset(&merge_parents
, 0, sizeof(merge_parents
));
651 /* learn the commit that we merge into and the current branch name */
652 current_branch
= current_branch_to_free
=
653 resolve_refdup("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
);
655 die("No current branch");
658 current_branch
= opts
->into_name
;
659 else if (starts_with(current_branch
, "refs/heads/"))
660 current_branch
+= 11;
662 find_merge_parents(&merge_parents
, in
, &head_oid
);
665 while (pos
< in
->len
) {
667 char *newline
, *p
= in
->buf
+ pos
;
669 newline
= strchr(p
, '\n');
670 len
= newline
? newline
- p
: strlen(p
);
671 pos
+= len
+ !!newline
;
674 if (handle_line(p
, &merge_parents
))
675 die("error in line %d: %.*s", i
, len
, p
);
678 if (opts
->add_title
&& srcs
.nr
)
679 fmt_merge_msg_title(out
, current_branch
);
682 fmt_merge_msg_sigs(out
);
684 if (opts
->shortlog_len
) {
688 head
= lookup_commit_or_die(&head_oid
, "HEAD");
689 repo_init_revisions(the_repository
, &rev
, NULL
);
690 rev
.commit_format
= CMIT_FMT_ONELINE
;
691 diff_merges_suppress(&rev
);
694 strbuf_complete_line(out
);
696 for (i
= 0; i
< origins
.nr
; i
++)
697 shortlog(origins
.items
[i
].string
,
698 origins
.items
[i
].util
,
699 head
, &rev
, opts
, out
);
700 release_revisions(&rev
);
703 strbuf_complete_line(out
);
704 free(current_branch_to_free
);
705 free(merge_parents
.item
);