fmt-merge-message: add empty line between tag and signature verification
[git/dscho.git] / builtin / fmt-merge-msg.c
blobfb1ebcbe68c2183468a782e068a59143f66366d5
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "tag.h"
7 #include "string-list.h"
8 #include "branch.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>]",
14 NULL
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")) {
22 int is_bool;
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);
31 return 0;
34 /* merge data per repository where the merged tips came from */
35 struct src_data {
36 struct string_list branch, tag, r_branch, generic;
37 int head_status;
40 struct origin_data {
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 {
57 int alloc, nr;
58 struct merge_parent {
59 unsigned char given[20];
60 unsigned char commit[20];
61 unsigned char used;
62 } *item;
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,
70 unsigned char *given,
71 unsigned char *commit)
73 int i;
74 for (i = 0; i < table->nr; i++) {
75 if (given && hashcmp(table->item[i].given, given))
76 continue;
77 if (commit && hashcmp(table->item[i].commit, commit))
78 continue;
79 return &table->item[i];
81 return NULL;
84 static void add_merge_parent(struct merge_parents *table,
85 unsigned char *given,
86 unsigned char *commit)
88 if (table->nr && find_merge_parent(table, given, commit))
89 return;
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;
94 table->nr++;
97 static int handle_line(char *line, struct merge_parents *merge_parents)
99 int i, len = strlen(line);
100 struct origin_data *origin_data;
101 char *src, *origin;
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')
108 return 1;
110 if (!prefixcmp(line + 41, "not-for-merge"))
111 return 0;
113 if (line[41] != '\t')
114 return 2;
116 i = get_sha1_hex(line, sha1);
117 if (i)
118 return 3;
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')
127 line[len - 1] = 0;
128 line += 42;
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 ");
136 if (src) {
137 *src = 0;
138 src += 4;
139 pulling_head = 0;
140 } else {
141 src = line;
142 pulling_head = 1;
145 item = unsorted_string_list_lookup(&srcs, src);
146 if (!item) {
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;
153 if (pulling_head) {
154 origin = src;
155 src_data->head_status |= 1;
156 } else if (!prefixcmp(line, "branch ")) {
157 origin_data->is_local_branch = 1;
158 origin = line + 7;
159 string_list_append(&src_data->branch, origin);
160 src_data->head_status |= 2;
161 } else if (!prefixcmp(line, "tag ")) {
162 origin = line;
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;
169 } else {
170 origin = src;
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);
179 } else {
180 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
181 sprintf(new_origin, "%s of %s", origin, src);
182 origin = new_origin;
184 if (strcmp(".", src))
185 origin_data->is_local_branch = 0;
186 string_list_append(&origins, origin)->util = origin_data;
187 return 0;
190 static void print_joined(const char *singular, const char *plural,
191 struct string_list *list, struct strbuf *out)
193 if (list->nr == 0)
194 return;
195 if (list->nr == 1) {
196 strbuf_addf(out, "%s%s", singular, list->items[0].string);
197 } else {
198 int i;
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;
213 while (*bp) {
214 const char *ep = strchrnul(bp, '\n');
215 if (*ep)
216 ep++;
217 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
218 bp = ep;
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,
228 struct commit *head,
229 struct rev_info *rev, int limit,
230 struct strbuf *out)
232 int i, count = 0;
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)
242 return;
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};
254 /* ignore merges */
255 if (commit->parents && commit->parents->next)
256 continue;
258 count++;
259 if (subjects.nr > limit)
260 continue;
262 format_commit_message(commit, "%s", &sb, &ctx);
263 strbuf_ltrim(&sb);
265 if (!sb.len)
266 string_list_append(&subjects,
267 sha1_to_hex(commit->object.sha1));
268 else
269 string_list_append(&subjects, strbuf_detach(&sb, NULL));
272 if (count > limit)
273 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
274 else
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++)
281 if (i >= limit)
282 strbuf_addf(out, " ...\n");
283 else
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);
289 rev->commits = NULL;
290 rev->pending.nr = 0;
292 string_list_clear(&subjects, 0);
295 static void fmt_merge_msg_title(struct strbuf *out,
296 const char *current_branch) {
297 int i = 0;
298 char *sep = "";
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);
306 sep = "; ";
308 if (src_data->head_status == 1) {
309 strbuf_addstr(out, srcs.items[i].string);
310 continue;
312 if (src_data->head_status == 3) {
313 subsep = ", ";
314 strbuf_addstr(out, "HEAD");
316 if (src_data->branch.nr) {
317 strbuf_addstr(out, subsep);
318 subsep = ", ";
319 print_joined("branch ", "branches ", &src_data->branch,
320 out);
322 if (src_data->r_branch.nr) {
323 strbuf_addstr(out, subsep);
324 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);
330 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,
336 out);
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');
344 else
345 strbuf_addf(out, " into %s\n", current_branch);
348 static void fmt_tag_signature(struct strbuf *tagbuf,
349 struct strbuf *sig,
350 const char *buf,
351 unsigned long len)
353 const char *tag_body = strstr(buf, "\n\n");
354 if (tag_body) {
355 tag_body += 2;
356 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
358 strbuf_complete_line(tagbuf);
359 if (sig->len) {
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)
378 goto next;
379 len = parse_signature(buf, size);
381 if (size == len)
382 ; /* merely annotated */
383 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
384 if (!sig.len)
385 strbuf_addstr(&sig, "gpg verification failed.\n");
388 if (!tag_number++) {
389 fmt_tag_signature(&tagbuf, &sig, buf, len);
390 first_tag = i;
391 } else {
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,
397 tagline.len);
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);
405 next:
406 free(buf);
408 if (tagbuf.len) {
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;
420 int pos = 0, i, j;
422 parents = NULL;
423 while (pos < in->len) {
424 int len;
425 char *p = in->buf + pos;
426 char *newline = strchr(p, '\n');
427 unsigned char sha1[20];
428 struct commit *parent;
429 struct object *obj;
431 len = newline ? newline - p : strlen(p);
432 pos += len + !!newline;
434 if (len < 43 ||
435 get_sha1_hex(p, sha1) ||
436 p[40] != '\t' ||
437 p[41] != '\t')
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
442 * util field yet.
444 obj = parse_object(sha1);
445 parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
446 if (!parent)
447 continue;
448 commit_list_insert(parent, &parents);
449 add_merge_parent(result, obj->sha1, parent->object.sha1);
451 head_commit = lookup_commit(head);
452 if (head_commit)
453 commit_list_insert(head_commit, &parents);
454 parents = reduce_heads(parents);
456 while (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;
462 free(parents);
463 parents = next;
466 for (i = j = 0; i < result->nr; i++) {
467 if (result->item[i].used) {
468 if (i != j)
469 result->item[j] = result->item[i];
470 j++;
473 result->nr = j;
476 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
477 struct fmt_merge_msg_opts *opts)
479 int i = 0, pos = 0;
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);
490 if (!current_branch)
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);
497 /* get a line */
498 while (pos < in->len) {
499 int len;
500 char *newline, *p = in->buf + pos;
502 newline = strchr(p, '\n');
503 len = newline ? newline - p : strlen(p);
504 pos += len + !!newline;
505 i++;
506 p[len] = 0;
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);
514 if (origins.nr)
515 fmt_merge_msg_sigs(out);
517 if (opts->shortlog_len) {
518 struct commit *head;
519 struct rev_info rev;
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;
525 rev.limited = 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);
538 return 0;
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"),
557 OPT_END()
560 FILE *in = stdin;
561 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
562 int ret;
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,
568 if (argc > 0)
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");
575 if (!in)
576 die_errno("cannot open '%s'", inpath);
579 if (strbuf_read(&input, fileno(in), 0) < 0)
580 die_errno("could not read input file");
582 if (message)
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);
590 if (ret)
591 return ret;
592 write_in_full(STDOUT_FILENO, output.buf, output.len);
593 return 0;