bash-completion: don't add quoted space for ZSH (fix regression)
[git/dscho.git] / builtin / fmt-merge-msg.c
blob8ddefb39db2e7ed13530e855f4c1d9e0b1a032ec
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);
30 } else {
31 return git_default_config(key, value, cb);
33 return 0;
36 /* merge data per repository where the merged tips came from */
37 struct src_data {
38 struct string_list branch, tag, r_branch, generic;
39 int head_status;
42 struct origin_data {
43 unsigned char sha1[20];
44 unsigned is_local_branch:1;
47 static void init_src_data(struct src_data *data)
49 data->branch.strdup_strings = 1;
50 data->tag.strdup_strings = 1;
51 data->r_branch.strdup_strings = 1;
52 data->generic.strdup_strings = 1;
55 static struct string_list srcs = STRING_LIST_INIT_DUP;
56 static struct string_list origins = STRING_LIST_INIT_DUP;
58 static int handle_line(char *line)
60 int i, len = strlen(line);
61 struct origin_data *origin_data;
62 char *src, *origin;
63 struct src_data *src_data;
64 struct string_list_item *item;
65 int pulling_head = 0;
67 if (len < 43 || line[40] != '\t')
68 return 1;
70 if (!prefixcmp(line + 41, "not-for-merge"))
71 return 0;
73 if (line[41] != '\t')
74 return 2;
76 line[40] = 0;
77 origin_data = xcalloc(1, sizeof(struct origin_data));
78 i = get_sha1(line, origin_data->sha1);
79 line[40] = '\t';
80 if (i) {
81 free(origin_data);
82 return 3;
85 if (line[len - 1] == '\n')
86 line[len - 1] = 0;
87 line += 42;
90 * At this point, line points at the beginning of comment e.g.
91 * "branch 'frotz' of git://that/repository.git".
92 * Find the repository name and point it with src.
94 src = strstr(line, " of ");
95 if (src) {
96 *src = 0;
97 src += 4;
98 pulling_head = 0;
99 } else {
100 src = line;
101 pulling_head = 1;
104 item = unsorted_string_list_lookup(&srcs, src);
105 if (!item) {
106 item = string_list_append(&srcs, src);
107 item->util = xcalloc(1, sizeof(struct src_data));
108 init_src_data(item->util);
110 src_data = item->util;
112 if (pulling_head) {
113 origin = src;
114 src_data->head_status |= 1;
115 } else if (!prefixcmp(line, "branch ")) {
116 origin_data->is_local_branch = 1;
117 origin = line + 7;
118 string_list_append(&src_data->branch, origin);
119 src_data->head_status |= 2;
120 } else if (!prefixcmp(line, "tag ")) {
121 origin = line;
122 string_list_append(&src_data->tag, origin + 4);
123 src_data->head_status |= 2;
124 } else if (!prefixcmp(line, "remote-tracking branch ")) {
125 origin = line + strlen("remote-tracking branch ");
126 string_list_append(&src_data->r_branch, origin);
127 src_data->head_status |= 2;
128 } else {
129 origin = src;
130 string_list_append(&src_data->generic, line);
131 src_data->head_status |= 2;
134 if (!strcmp(".", src) || !strcmp(src, origin)) {
135 int len = strlen(origin);
136 if (origin[0] == '\'' && origin[len - 1] == '\'')
137 origin = xmemdupz(origin + 1, len - 2);
138 } else {
139 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
140 sprintf(new_origin, "%s of %s", origin, src);
141 origin = new_origin;
143 if (strcmp(".", src))
144 origin_data->is_local_branch = 0;
145 string_list_append(&origins, origin)->util = origin_data;
146 return 0;
149 static void print_joined(const char *singular, const char *plural,
150 struct string_list *list, struct strbuf *out)
152 if (list->nr == 0)
153 return;
154 if (list->nr == 1) {
155 strbuf_addf(out, "%s%s", singular, list->items[0].string);
156 } else {
157 int i;
158 strbuf_addstr(out, plural);
159 for (i = 0; i < list->nr - 1; i++)
160 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
161 list->items[i].string);
162 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
166 static void add_branch_desc(struct strbuf *out, const char *name)
168 struct strbuf desc = STRBUF_INIT;
170 if (!read_branch_desc(&desc, name)) {
171 const char *bp = desc.buf;
172 while (*bp) {
173 const char *ep = strchrnul(bp, '\n');
174 if (*ep)
175 ep++;
176 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
177 bp = ep;
179 if (out->buf[out->len - 1] != '\n')
180 strbuf_addch(out, '\n');
182 strbuf_release(&desc);
185 #define util_as_int(elem) ((int)((elem)->util))
187 static void record_person(int which, struct string_list *people,
188 struct commit *commit)
190 char name_buf[MAX_GITNAME], *name, *name_end;
191 struct string_list_item *elem;
192 const char *field = (which == 'a') ? "\nauthor " : "\ncommitter ";
194 name = strstr(commit->buffer, field);
195 if (!name)
196 return;
197 name += strlen(field);
198 name_end = strchrnul(name, '<');
199 if (*name_end)
200 name_end--;
201 while (isspace(*name_end) && name <= name_end)
202 name_end--;
203 if (name_end < name || name + MAX_GITNAME <= name_end)
204 return;
205 memcpy(name_buf, name, name_end - name + 1);
206 name_buf[name_end - name + 1] = '\0';
208 elem = string_list_lookup(people, name_buf);
209 if (!elem) {
210 elem = string_list_insert(people, name_buf);
211 elem->util = (void *)0;
213 elem->util = (void*)((intptr_t)(util_as_int(elem) + 1));
216 static int cmp_string_list_util_as_int(const void *a_, const void *b_)
218 const struct string_list_item *a = a_, *b = b_;
219 return util_as_int(b) - util_as_int(a);
222 static void add_people_count(struct strbuf *out, struct string_list *people)
224 if (people->nr == 1)
225 strbuf_addf(out, "%s", people->items[0].string);
226 else if (people->nr == 2)
227 strbuf_addf(out, "%s (%d) and %s (%d)",
228 people->items[0].string,
229 util_as_int(&people->items[0]),
230 people->items[1].string,
231 util_as_int(&people->items[1]));
232 else if (people->nr)
233 strbuf_addf(out, "%s (%d) and others",
234 people->items[0].string,
235 util_as_int(&people->items[0]));
238 static int committer_is_me(const char *name)
240 const char *me = git_committer_info(IDENT_NO_DATE);
241 return (me &&
242 (me = skip_prefix(me, name)) != NULL &&
243 skip_prefix(me, " <"));
246 static void add_people_info(struct strbuf *out,
247 struct string_list *authors,
248 struct string_list *committers)
250 if (authors->nr)
251 qsort(authors->items,
252 authors->nr, sizeof(authors->items[0]),
253 cmp_string_list_util_as_int);
254 if (committers->nr)
255 qsort(committers->items,
256 committers->nr, sizeof(committers->items[0]),
257 cmp_string_list_util_as_int);
259 if (authors->nr) {
260 strbuf_addstr(out, "\nBy ");
261 add_people_count(out, authors);
263 if (committers->nr == 1 &&
264 committer_is_me(committers->items->string))
265 return;
266 strbuf_addstr(out, "\nvia ");
267 add_people_count(out, committers);
270 static void shortlog(const char *name,
271 struct origin_data *origin_data,
272 struct commit *head,
273 struct rev_info *rev, int limit,
274 struct strbuf *out)
276 int i, count = 0;
277 struct commit *commit;
278 struct object *branch;
279 struct string_list subjects = STRING_LIST_INIT_DUP;
280 struct string_list authors = STRING_LIST_INIT_DUP;
281 struct string_list committers = STRING_LIST_INIT_DUP;
282 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
283 struct strbuf sb = STRBUF_INIT;
284 const unsigned char *sha1 = origin_data->sha1;
286 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
287 if (!branch || branch->type != OBJ_COMMIT)
288 return;
290 setup_revisions(0, NULL, rev, NULL);
291 add_pending_object(rev, branch, name);
292 add_pending_object(rev, &head->object, "^HEAD");
293 head->object.flags |= UNINTERESTING;
294 if (prepare_revision_walk(rev))
295 die("revision walk setup failed");
296 while ((commit = get_revision(rev)) != NULL) {
297 struct pretty_print_context ctx = {0};
299 if (commit->parents && commit->parents->next) {
300 /* do not list a merge but count committer */
301 record_person('c', &committers, commit);
302 continue;
304 if (!count)
305 /* the 'tip' committer */
306 record_person('c', &committers, commit);
307 record_person('a', &authors, commit);
308 count++;
309 if (subjects.nr > limit)
310 continue;
312 format_commit_message(commit, "%s", &sb, &ctx);
313 strbuf_ltrim(&sb);
315 if (!sb.len)
316 string_list_append(&subjects,
317 sha1_to_hex(commit->object.sha1));
318 else
319 string_list_append(&subjects, strbuf_detach(&sb, NULL));
322 add_people_info(out, &authors, &committers);
323 if (count > limit)
324 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
325 else
326 strbuf_addf(out, "\n* %s:\n", name);
328 if (origin_data->is_local_branch && use_branch_desc)
329 add_branch_desc(out, name);
331 for (i = 0; i < subjects.nr; i++)
332 if (i >= limit)
333 strbuf_addf(out, " ...\n");
334 else
335 strbuf_addf(out, " %s\n", subjects.items[i].string);
337 clear_commit_marks((struct commit *)branch, flags);
338 clear_commit_marks(head, flags);
339 free_commit_list(rev->commits);
340 rev->commits = NULL;
341 rev->pending.nr = 0;
343 string_list_clear(&authors, 0);
344 string_list_clear(&committers, 0);
345 string_list_clear(&subjects, 0);
348 static void fmt_merge_msg_title(struct strbuf *out,
349 const char *current_branch) {
350 int i = 0;
351 char *sep = "";
353 strbuf_addstr(out, "Merge ");
354 for (i = 0; i < srcs.nr; i++) {
355 struct src_data *src_data = srcs.items[i].util;
356 const char *subsep = "";
358 strbuf_addstr(out, sep);
359 sep = "; ";
361 if (src_data->head_status == 1) {
362 strbuf_addstr(out, srcs.items[i].string);
363 continue;
365 if (src_data->head_status == 3) {
366 subsep = ", ";
367 strbuf_addstr(out, "HEAD");
369 if (src_data->branch.nr) {
370 strbuf_addstr(out, subsep);
371 subsep = ", ";
372 print_joined("branch ", "branches ", &src_data->branch,
373 out);
375 if (src_data->r_branch.nr) {
376 strbuf_addstr(out, subsep);
377 subsep = ", ";
378 print_joined("remote-tracking branch ", "remote-tracking branches ",
379 &src_data->r_branch, out);
381 if (src_data->tag.nr) {
382 strbuf_addstr(out, subsep);
383 subsep = ", ";
384 print_joined("tag ", "tags ", &src_data->tag, out);
386 if (src_data->generic.nr) {
387 strbuf_addstr(out, subsep);
388 print_joined("commit ", "commits ", &src_data->generic,
389 out);
391 if (strcmp(".", srcs.items[i].string))
392 strbuf_addf(out, " of %s", srcs.items[i].string);
395 if (!strcmp("master", current_branch))
396 strbuf_addch(out, '\n');
397 else
398 strbuf_addf(out, " into %s\n", current_branch);
401 static void fmt_tag_signature(struct strbuf *tagbuf,
402 struct strbuf *sig,
403 const char *buf,
404 unsigned long len)
406 const char *tag_body = strstr(buf, "\n\n");
407 if (tag_body) {
408 tag_body += 2;
409 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
411 strbuf_complete_line(tagbuf);
412 strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len);
415 static void fmt_merge_msg_sigs(struct strbuf *out)
417 int i, tag_number = 0, first_tag = 0;
418 struct strbuf tagbuf = STRBUF_INIT;
420 for (i = 0; i < origins.nr; i++) {
421 unsigned char *sha1 = origins.items[i].util;
422 enum object_type type;
423 unsigned long size, len;
424 char *buf = read_sha1_file(sha1, &type, &size);
425 struct strbuf sig = STRBUF_INIT;
427 if (!buf || type != OBJ_TAG)
428 goto next;
429 len = parse_signature(buf, size);
431 if (size == len)
432 ; /* merely annotated */
433 else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) {
434 if (!sig.len)
435 strbuf_addstr(&sig, "gpg verification failed.\n");
438 if (!tag_number++) {
439 fmt_tag_signature(&tagbuf, &sig, buf, len);
440 first_tag = i;
441 } else {
442 if (tag_number == 2) {
443 struct strbuf tagline = STRBUF_INIT;
444 strbuf_addf(&tagline, "\n# %s\n",
445 origins.items[first_tag].string);
446 strbuf_insert(&tagbuf, 0, tagline.buf,
447 tagline.len);
448 strbuf_release(&tagline);
450 strbuf_addf(&tagbuf, "\n# %s\n",
451 origins.items[i].string);
452 fmt_tag_signature(&tagbuf, &sig, buf, len);
454 strbuf_release(&sig);
455 next:
456 free(buf);
458 if (tagbuf.len) {
459 strbuf_addch(out, '\n');
460 strbuf_addbuf(out, &tagbuf);
462 strbuf_release(&tagbuf);
465 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
466 struct fmt_merge_msg_opts *opts)
468 int i = 0, pos = 0;
469 unsigned char head_sha1[20];
470 const char *current_branch;
471 void *current_branch_to_free;
473 /* get current branch */
474 current_branch = current_branch_to_free =
475 resolve_refdup("HEAD", head_sha1, 1, NULL);
476 if (!current_branch)
477 die("No current branch");
478 if (!prefixcmp(current_branch, "refs/heads/"))
479 current_branch += 11;
481 /* get a line */
482 while (pos < in->len) {
483 int len;
484 char *newline, *p = in->buf + pos;
486 newline = strchr(p, '\n');
487 len = newline ? newline - p : strlen(p);
488 pos += len + !!newline;
489 i++;
490 p[len] = 0;
491 if (handle_line(p))
492 die ("Error in line %d: %.*s", i, len, p);
495 if (opts->add_title && srcs.nr)
496 fmt_merge_msg_title(out, current_branch);
498 if (origins.nr)
499 fmt_merge_msg_sigs(out);
501 if (opts->shortlog_len) {
502 struct commit *head;
503 struct rev_info rev;
505 head = lookup_commit_or_die(head_sha1, "HEAD");
506 init_revisions(&rev, NULL);
507 rev.commit_format = CMIT_FMT_ONELINE;
508 rev.ignore_merges = 1;
509 rev.limited = 1;
511 if (suffixcmp(out->buf, "\n"))
512 strbuf_addch(out, '\n');
514 for (i = 0; i < origins.nr; i++)
515 shortlog(origins.items[i].string,
516 origins.items[i].util,
517 head, &rev, opts->shortlog_len, out);
520 strbuf_complete_line(out);
521 free(current_branch_to_free);
522 return 0;
525 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
527 const char *inpath = NULL;
528 const char *message = NULL;
529 int shortlog_len = -1;
530 struct option options[] = {
531 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
532 "populate log with at most <n> entries from shortlog",
533 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
534 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
535 "alias for --log (deprecated)",
536 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
537 DEFAULT_MERGE_LOG_LEN },
538 OPT_STRING('m', "message", &message, "text",
539 "use <text> as start of message"),
540 OPT_FILENAME('F', "file", &inpath, "file to read from"),
541 OPT_END()
544 FILE *in = stdin;
545 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
546 int ret;
547 struct fmt_merge_msg_opts opts;
549 git_config(fmt_merge_msg_config, NULL);
550 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
552 if (argc > 0)
553 usage_with_options(fmt_merge_msg_usage, options);
554 if (shortlog_len < 0)
555 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
557 if (inpath && strcmp(inpath, "-")) {
558 in = fopen(inpath, "r");
559 if (!in)
560 die_errno("cannot open '%s'", inpath);
563 if (strbuf_read(&input, fileno(in), 0) < 0)
564 die_errno("could not read input file");
566 if (message)
567 strbuf_addstr(&output, message);
569 memset(&opts, 0, sizeof(opts));
570 opts.add_title = !message;
571 opts.shortlog_len = shortlog_len;
573 ret = fmt_merge_msg(&input, &output, &opts);
574 if (ret)
575 return ret;
576 write_in_full(STDOUT_FILENO, output.buf, output.len);
577 return 0;