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
);
360 strbuf_addch(tagbuf
, '\n');
361 strbuf_add_lines(tagbuf
, "# ", sig
->buf
, sig
->len
);
365 static void fmt_merge_msg_sigs(struct strbuf
*out
)
367 int i
, tag_number
= 0, first_tag
= 0;
368 struct strbuf tagbuf
= STRBUF_INIT
;
370 for (i
= 0; i
< origins
.nr
; i
++) {
371 unsigned char *sha1
= origins
.items
[i
].util
;
372 enum object_type type
;
373 unsigned long size
, len
;
374 char *buf
= read_sha1_file(sha1
, &type
, &size
);
375 struct strbuf sig
= STRBUF_INIT
;
377 if (!buf
|| type
!= OBJ_TAG
)
379 len
= parse_signature(buf
, size
);
382 ; /* merely annotated */
383 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
)) {
385 strbuf_addstr(&sig
, "gpg verification failed.\n");
389 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
392 if (tag_number
== 2) {
393 struct strbuf tagline
= STRBUF_INIT
;
394 strbuf_addf(&tagline
, "\n# %s\n",
395 origins
.items
[first_tag
].string
);
396 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
398 strbuf_release(&tagline
);
400 strbuf_addf(&tagbuf
, "\n# %s\n",
401 origins
.items
[i
].string
);
402 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
404 strbuf_release(&sig
);
409 strbuf_addch(out
, '\n');
410 strbuf_addbuf(out
, &tagbuf
);
412 strbuf_release(&tagbuf
);
415 static void find_merge_parents(struct merge_parents
*result
,
416 struct strbuf
*in
, unsigned char *head
)
418 struct commit_list
*parents
, *next
;
419 struct commit
*head_commit
;
423 while (pos
< in
->len
) {
425 char *p
= in
->buf
+ pos
;
426 char *newline
= strchr(p
, '\n');
427 unsigned char sha1
[20];
428 struct commit
*parent
;
431 len
= newline
? newline
- p
: strlen(p
);
432 pos
+= len
+ !!newline
;
435 get_sha1_hex(p
, sha1
) ||
438 continue; /* skip not-for-merge */
440 * Do not use get_merge_parent() here; we do not have
441 * "name" here and we do not want to contaminate its
444 obj
= parse_object(sha1
);
445 parent
= (struct commit
*)peel_to_type(NULL
, 0, obj
, OBJ_COMMIT
);
448 commit_list_insert(parent
, &parents
);
449 add_merge_parent(result
, obj
->sha1
, parent
->object
.sha1
);
451 head_commit
= lookup_commit(head
);
453 commit_list_insert(head_commit
, &parents
);
454 parents
= reduce_heads(parents
);
457 for (i
= 0; i
< result
->nr
; i
++)
458 if (!hashcmp(result
->item
[i
].commit
,
459 parents
->item
->object
.sha1
))
460 result
->item
[i
].used
= 1;
461 next
= parents
->next
;
466 for (i
= j
= 0; i
< result
->nr
; i
++) {
467 if (result
->item
[i
].used
) {
469 result
->item
[j
] = result
->item
[i
];
476 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
477 struct fmt_merge_msg_opts
*opts
)
480 unsigned char head_sha1
[20];
481 const char *current_branch
;
482 void *current_branch_to_free
;
483 struct merge_parents merge_parents
;
485 memset(&merge_parents
, 0, sizeof(merge_parents
));
487 /* get current branch */
488 current_branch
= current_branch_to_free
=
489 resolve_refdup("HEAD", head_sha1
, 1, NULL
);
491 die("No current branch");
492 if (!prefixcmp(current_branch
, "refs/heads/"))
493 current_branch
+= 11;
495 find_merge_parents(&merge_parents
, in
, head_sha1
);
498 while (pos
< in
->len
) {
500 char *newline
, *p
= in
->buf
+ pos
;
502 newline
= strchr(p
, '\n');
503 len
= newline
? newline
- p
: strlen(p
);
504 pos
+= len
+ !!newline
;
507 if (handle_line(p
, &merge_parents
))
508 die ("Error in line %d: %.*s", i
, len
, p
);
511 if (opts
->add_title
&& srcs
.nr
)
512 fmt_merge_msg_title(out
, current_branch
);
515 fmt_merge_msg_sigs(out
);
517 if (opts
->shortlog_len
) {
521 head
= lookup_commit_or_die(head_sha1
, "HEAD");
522 init_revisions(&rev
, NULL
);
523 rev
.commit_format
= CMIT_FMT_ONELINE
;
524 rev
.ignore_merges
= 1;
527 strbuf_complete_line(out
);
529 for (i
= 0; i
< origins
.nr
; i
++)
530 shortlog(origins
.items
[i
].string
,
531 origins
.items
[i
].util
,
532 head
, &rev
, opts
->shortlog_len
, out
);
535 strbuf_complete_line(out
);
536 free(current_branch_to_free
);
537 free(merge_parents
.item
);
541 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
543 const char *inpath
= NULL
;
544 const char *message
= NULL
;
545 int shortlog_len
= -1;
546 struct option options
[] = {
547 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
548 "populate log with at most <n> entries from shortlog",
549 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
550 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
551 "alias for --log (deprecated)",
552 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
553 DEFAULT_MERGE_LOG_LEN
},
554 OPT_STRING('m', "message", &message
, "text",
555 "use <text> as start of message"),
556 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
561 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
563 struct fmt_merge_msg_opts opts
;
565 git_config(fmt_merge_msg_config
, NULL
);
566 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
569 usage_with_options(fmt_merge_msg_usage
, options
);
570 if (shortlog_len
< 0)
571 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
573 if (inpath
&& strcmp(inpath
, "-")) {
574 in
= fopen(inpath
, "r");
576 die_errno("cannot open '%s'", inpath
);
579 if (strbuf_read(&input
, fileno(in
), 0) < 0)
580 die_errno("could not read input file");
583 strbuf_addstr(&output
, message
);
585 memset(&opts
, 0, sizeof(opts
));
586 opts
.add_title
= !message
;
587 opts
.shortlog_len
= shortlog_len
;
589 ret
= fmt_merge_msg(&input
, &output
, &opts
);
592 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);