7 #include "string-list.h"
9 #include "fmt-merge-msg.h"
10 #include "gpg-interface.h"
12 static const char * const fmt_merge_msg_usage
[] = {
13 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
17 static int use_branch_desc
;
19 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
21 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
23 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
24 if (!is_bool
&& merge_log_config
< 0)
25 return error("%s: negative length %s", key
, value
);
26 if (is_bool
&& merge_log_config
)
27 merge_log_config
= DEFAULT_MERGE_LOG_LEN
;
28 } else if (!strcmp(key
, "merge.branchdesc")) {
29 use_branch_desc
= git_config_bool(key
, value
);
34 /* merge data per repository where the merged tips came from */
36 struct string_list branch
, tag
, r_branch
, generic
;
41 unsigned char sha1
[20];
42 unsigned is_local_branch
:1;
45 static void init_src_data(struct src_data
*data
)
47 data
->branch
.strdup_strings
= 1;
48 data
->tag
.strdup_strings
= 1;
49 data
->r_branch
.strdup_strings
= 1;
50 data
->generic
.strdup_strings
= 1;
53 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
54 static struct string_list origins
= STRING_LIST_INIT_DUP
;
56 struct merge_parents
{
59 unsigned char given
[20];
60 unsigned char commit
[20];
66 * I know, I know, this is inefficient, but you won't be pulling and merging
67 * hundreds of heads at a time anyway.
69 static struct merge_parent
*find_merge_parent(struct merge_parents
*table
,
71 unsigned char *commit
)
74 for (i
= 0; i
< table
->nr
; i
++) {
75 if (given
&& hashcmp(table
->item
[i
].given
, given
))
77 if (commit
&& hashcmp(table
->item
[i
].commit
, commit
))
79 return &table
->item
[i
];
84 static void add_merge_parent(struct merge_parents
*table
,
86 unsigned char *commit
)
88 if (table
->nr
&& find_merge_parent(table
, given
, commit
))
90 ALLOC_GROW(table
->item
, table
->nr
+ 1, table
->alloc
);
91 hashcpy(table
->item
[table
->nr
].given
, given
);
92 hashcpy(table
->item
[table
->nr
].commit
, commit
);
93 table
->item
[table
->nr
].used
= 0;
97 static int handle_line(char *line
, struct merge_parents
*merge_parents
)
99 int i
, len
= strlen(line
);
100 struct origin_data
*origin_data
;
102 struct src_data
*src_data
;
103 struct string_list_item
*item
;
104 int pulling_head
= 0;
105 unsigned char sha1
[20];
107 if (len
< 43 || line
[40] != '\t')
110 if (!prefixcmp(line
+ 41, "not-for-merge"))
113 if (line
[41] != '\t')
116 i
= get_sha1_hex(line
, sha1
);
120 if (!find_merge_parent(merge_parents
, sha1
, NULL
))
121 return 0; /* subsumed by other parents */
123 origin_data
= xcalloc(1, sizeof(struct origin_data
));
124 hashcpy(origin_data
->sha1
, sha1
);
126 if (line
[len
- 1] == '\n')
131 * At this point, line points at the beginning of comment e.g.
132 * "branch 'frotz' of git://that/repository.git".
133 * Find the repository name and point it with src.
135 src
= strstr(line
, " of ");
145 item
= unsorted_string_list_lookup(&srcs
, src
);
147 item
= string_list_append(&srcs
, src
);
148 item
->util
= xcalloc(1, sizeof(struct src_data
));
149 init_src_data(item
->util
);
151 src_data
= item
->util
;
155 src_data
->head_status
|= 1;
156 } else if (!prefixcmp(line
, "branch ")) {
157 origin_data
->is_local_branch
= 1;
159 string_list_append(&src_data
->branch
, origin
);
160 src_data
->head_status
|= 2;
161 } else if (!prefixcmp(line
, "tag ")) {
163 string_list_append(&src_data
->tag
, origin
+ 4);
164 src_data
->head_status
|= 2;
165 } else if (!prefixcmp(line
, "remote-tracking branch ")) {
166 origin
= line
+ strlen("remote-tracking branch ");
167 string_list_append(&src_data
->r_branch
, origin
);
168 src_data
->head_status
|= 2;
171 string_list_append(&src_data
->generic
, line
);
172 src_data
->head_status
|= 2;
175 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
176 int len
= strlen(origin
);
177 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
178 origin
= xmemdupz(origin
+ 1, len
- 2);
180 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
181 sprintf(new_origin
, "%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 if (out
->buf
[out
->len
- 1] != '\n')
221 strbuf_addch(out
, '\n');
223 strbuf_release(&desc
);
226 static void shortlog(const char *name
,
227 struct origin_data
*origin_data
,
229 struct rev_info
*rev
, int limit
,
233 struct commit
*commit
;
234 struct object
*branch
;
235 struct string_list subjects
= STRING_LIST_INIT_DUP
;
236 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
237 struct strbuf sb
= STRBUF_INIT
;
238 const unsigned char *sha1
= origin_data
->sha1
;
240 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
241 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
244 setup_revisions(0, NULL
, rev
, NULL
);
245 rev
->ignore_merges
= 1;
246 add_pending_object(rev
, branch
, name
);
247 add_pending_object(rev
, &head
->object
, "^HEAD");
248 head
->object
.flags
|= UNINTERESTING
;
249 if (prepare_revision_walk(rev
))
250 die("revision walk setup failed");
251 while ((commit
= get_revision(rev
)) != NULL
) {
252 struct pretty_print_context ctx
= {0};
255 if (commit
->parents
&& commit
->parents
->next
)
259 if (subjects
.nr
> limit
)
262 format_commit_message(commit
, "%s", &sb
, &ctx
);
266 string_list_append(&subjects
,
267 sha1_to_hex(commit
->object
.sha1
));
269 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
273 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
275 strbuf_addf(out
, "\n* %s:\n", name
);
277 if (origin_data
->is_local_branch
&& use_branch_desc
)
278 add_branch_desc(out
, name
);
280 for (i
= 0; i
< subjects
.nr
; i
++)
282 strbuf_addf(out
, " ...\n");
284 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
286 clear_commit_marks((struct commit
*)branch
, flags
);
287 clear_commit_marks(head
, flags
);
288 free_commit_list(rev
->commits
);
292 string_list_clear(&subjects
, 0);
295 static void fmt_merge_msg_title(struct strbuf
*out
,
296 const char *current_branch
) {
300 strbuf_addstr(out
, "Merge ");
301 for (i
= 0; i
< srcs
.nr
; i
++) {
302 struct src_data
*src_data
= srcs
.items
[i
].util
;
303 const char *subsep
= "";
305 strbuf_addstr(out
, sep
);
308 if (src_data
->head_status
== 1) {
309 strbuf_addstr(out
, srcs
.items
[i
].string
);
312 if (src_data
->head_status
== 3) {
314 strbuf_addstr(out
, "HEAD");
316 if (src_data
->branch
.nr
) {
317 strbuf_addstr(out
, subsep
);
319 print_joined("branch ", "branches ", &src_data
->branch
,
322 if (src_data
->r_branch
.nr
) {
323 strbuf_addstr(out
, subsep
);
325 print_joined("remote-tracking branch ", "remote-tracking branches ",
326 &src_data
->r_branch
, out
);
328 if (src_data
->tag
.nr
) {
329 strbuf_addstr(out
, subsep
);
331 print_joined("tag ", "tags ", &src_data
->tag
, out
);
333 if (src_data
->generic
.nr
) {
334 strbuf_addstr(out
, subsep
);
335 print_joined("commit ", "commits ", &src_data
->generic
,
338 if (strcmp(".", srcs
.items
[i
].string
))
339 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
342 if (!strcmp("master", current_branch
))
343 strbuf_addch(out
, '\n');
345 strbuf_addf(out
, " into %s\n", current_branch
);
348 static void fmt_tag_signature(struct strbuf
*tagbuf
,
353 const char *tag_body
= strstr(buf
, "\n\n");
356 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
358 strbuf_complete_line(tagbuf
);
359 strbuf_add_lines(tagbuf
, "# ", sig
->buf
, sig
->len
);
362 static void fmt_merge_msg_sigs(struct strbuf
*out
)
364 int i
, tag_number
= 0, first_tag
= 0;
365 struct strbuf tagbuf
= STRBUF_INIT
;
367 for (i
= 0; i
< origins
.nr
; i
++) {
368 unsigned char *sha1
= origins
.items
[i
].util
;
369 enum object_type type
;
370 unsigned long size
, len
;
371 char *buf
= read_sha1_file(sha1
, &type
, &size
);
372 struct strbuf sig
= STRBUF_INIT
;
374 if (!buf
|| type
!= OBJ_TAG
)
376 len
= parse_signature(buf
, size
);
379 ; /* merely annotated */
380 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
)) {
382 strbuf_addstr(&sig
, "gpg verification failed.\n");
386 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
389 if (tag_number
== 2) {
390 struct strbuf tagline
= STRBUF_INIT
;
391 strbuf_addf(&tagline
, "\n# %s\n",
392 origins
.items
[first_tag
].string
);
393 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
395 strbuf_release(&tagline
);
397 strbuf_addf(&tagbuf
, "\n# %s\n",
398 origins
.items
[i
].string
);
399 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
401 strbuf_release(&sig
);
406 strbuf_addch(out
, '\n');
407 strbuf_addbuf(out
, &tagbuf
);
409 strbuf_release(&tagbuf
);
412 static void find_merge_parents(struct merge_parents
*result
,
413 struct strbuf
*in
, unsigned char *head
)
415 struct commit_list
*parents
, *next
;
416 struct commit
*head_commit
;
420 while (pos
< in
->len
) {
422 char *p
= in
->buf
+ pos
;
423 char *newline
= strchr(p
, '\n');
424 unsigned char sha1
[20];
425 struct commit
*parent
;
428 len
= newline
? newline
- p
: strlen(p
);
429 pos
+= len
+ !!newline
;
432 get_sha1_hex(p
, sha1
) ||
435 continue; /* skip not-for-merge */
437 * Do not use get_merge_parent() here; we do not have
438 * "name" here and we do not want to contaminate its
441 obj
= parse_object(sha1
);
442 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
445 commit_list_insert(parent
, &parents
);
446 add_merge_parent(result
, obj
->sha1
, parent
->object
.sha1
);
448 head_commit
= lookup_commit(head
);
450 commit_list_insert(head_commit
, &parents
);
451 parents
= reduce_heads(parents
);
454 for (i
= 0; i
< result
->nr
; i
++)
455 if (!hashcmp(result
->item
[i
].commit
,
456 parents
->item
->object
.sha1
))
457 result
->item
[i
].used
= 1;
458 next
= parents
->next
;
463 for (i
= j
= 0; i
< result
->nr
; i
++) {
464 if (result
->item
[i
].used
) {
466 result
->item
[j
] = result
->item
[i
];
473 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
474 struct fmt_merge_msg_opts
*opts
)
477 unsigned char head_sha1
[20];
478 const char *current_branch
;
479 void *current_branch_to_free
;
480 struct merge_parents merge_parents
;
482 memset(&merge_parents
, 0, sizeof(merge_parents
));
484 /* get current branch */
485 current_branch
= current_branch_to_free
=
486 resolve_refdup("HEAD", head_sha1
, 1, NULL
);
488 die("No current branch");
489 if (!prefixcmp(current_branch
, "refs/heads/"))
490 current_branch
+= 11;
492 find_merge_parents(&merge_parents
, in
, head_sha1
);
495 while (pos
< in
->len
) {
497 char *newline
, *p
= in
->buf
+ pos
;
499 newline
= strchr(p
, '\n');
500 len
= newline
? newline
- p
: strlen(p
);
501 pos
+= len
+ !!newline
;
504 if (handle_line(p
, &merge_parents
))
505 die ("Error in line %d: %.*s", i
, len
, p
);
508 if (opts
->add_title
&& srcs
.nr
)
509 fmt_merge_msg_title(out
, current_branch
);
512 fmt_merge_msg_sigs(out
);
514 if (opts
->shortlog_len
) {
518 head
= lookup_commit_or_die(head_sha1
, "HEAD");
519 init_revisions(&rev
, NULL
);
520 rev
.commit_format
= CMIT_FMT_ONELINE
;
521 rev
.ignore_merges
= 1;
524 if (suffixcmp(out
->buf
, "\n"))
525 strbuf_addch(out
, '\n');
527 for (i
= 0; i
< origins
.nr
; i
++)
528 shortlog(origins
.items
[i
].string
,
529 origins
.items
[i
].util
,
530 head
, &rev
, opts
->shortlog_len
, out
);
533 strbuf_complete_line(out
);
534 free(current_branch_to_free
);
535 free(merge_parents
.item
);
539 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
541 const char *inpath
= NULL
;
542 const char *message
= NULL
;
543 int shortlog_len
= -1;
544 struct option options
[] = {
545 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
546 "populate log with at most <n> entries from shortlog",
547 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
548 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
549 "alias for --log (deprecated)",
550 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
551 DEFAULT_MERGE_LOG_LEN
},
552 OPT_STRING('m', "message", &message
, "text",
553 "use <text> as start of message"),
554 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
559 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
561 struct fmt_merge_msg_opts opts
;
563 git_config(fmt_merge_msg_config
, NULL
);
564 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
567 usage_with_options(fmt_merge_msg_usage
, options
);
568 if (shortlog_len
< 0)
569 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
571 if (inpath
&& strcmp(inpath
, "-")) {
572 in
= fopen(inpath
, "r");
574 die_errno("cannot open '%s'", inpath
);
577 if (strbuf_read(&input
, fileno(in
), 0) < 0)
578 die_errno("could not read input file");
581 strbuf_addstr(&output
, message
);
583 memset(&opts
, 0, sizeof(opts
));
584 opts
.add_title
= !message
;
585 opts
.shortlog_len
= shortlog_len
;
587 ret
= fmt_merge_msg(&input
, &output
, &opts
);
590 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);