fsck: mark unused parameters in various fsck callbacks
[git.git] / fmt-merge-msg.c
blob4239594ad84c8a0d613fab0ab173c6069612c8ef
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "refs.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
8 #include "diff.h"
9 #include "diff-merges.h"
10 #include "hex.h"
11 #include "revision.h"
12 #include "tag.h"
13 #include "string-list.h"
14 #include "branch.h"
15 #include "fmt-merge-msg.h"
16 #include "commit-reach.h"
17 #include "gpg-interface.h"
18 #include "wildmatch.h"
20 static int use_branch_desc;
21 static int suppress_dest_pattern_seen;
22 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
24 int fmt_merge_msg_config(const char *key, const char *value,
25 const struct config_context *ctx, void *cb)
27 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
28 int is_bool;
29 merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool);
30 if (!is_bool && merge_log_config < 0)
31 return error("%s: negative length %s", key, value);
32 if (is_bool && merge_log_config)
33 merge_log_config = DEFAULT_MERGE_LOG_LEN;
34 } else if (!strcmp(key, "merge.branchdesc")) {
35 use_branch_desc = git_config_bool(key, value);
36 } else if (!strcmp(key, "merge.suppressdest")) {
37 if (!value)
38 return config_error_nonbool(key);
39 if (!*value)
40 string_list_clear(&suppress_dest_patterns, 0);
41 else
42 string_list_append(&suppress_dest_patterns, value);
43 suppress_dest_pattern_seen = 1;
44 } else {
45 return git_default_config(key, value, ctx, cb);
47 return 0;
50 /* merge data per repository where the merged tips came from */
51 struct src_data {
52 struct string_list branch, tag, r_branch, generic;
53 int head_status;
56 struct origin_data {
57 struct object_id oid;
58 unsigned is_local_branch:1;
61 static void init_src_data(struct src_data *data)
63 data->branch.strdup_strings = 1;
64 data->tag.strdup_strings = 1;
65 data->r_branch.strdup_strings = 1;
66 data->generic.strdup_strings = 1;
69 static struct string_list srcs = STRING_LIST_INIT_DUP;
70 static struct string_list origins = STRING_LIST_INIT_DUP;
72 struct merge_parents {
73 int alloc, nr;
74 struct merge_parent {
75 struct object_id given;
76 struct object_id commit;
77 unsigned char used;
78 } *item;
82 * I know, I know, this is inefficient, but you won't be pulling and merging
83 * hundreds of heads at a time anyway.
85 static struct merge_parent *find_merge_parent(struct merge_parents *table,
86 struct object_id *given,
87 struct object_id *commit)
89 int i;
90 for (i = 0; i < table->nr; i++) {
91 if (given && !oideq(&table->item[i].given, given))
92 continue;
93 if (commit && !oideq(&table->item[i].commit, commit))
94 continue;
95 return &table->item[i];
97 return NULL;
100 static void add_merge_parent(struct merge_parents *table,
101 struct object_id *given,
102 struct object_id *commit)
104 if (table->nr && find_merge_parent(table, given, commit))
105 return;
106 ALLOC_GROW(table->item, table->nr + 1, table->alloc);
107 oidcpy(&table->item[table->nr].given, given);
108 oidcpy(&table->item[table->nr].commit, commit);
109 table->item[table->nr].used = 0;
110 table->nr++;
113 static int handle_line(char *line, struct merge_parents *merge_parents)
115 int i, len = strlen(line);
116 struct origin_data *origin_data;
117 char *src;
118 const char *origin, *tag_name;
119 char *to_free = NULL;
120 struct src_data *src_data;
121 struct string_list_item *item;
122 int pulling_head = 0;
123 struct object_id oid;
124 const unsigned hexsz = the_hash_algo->hexsz;
126 if (len < hexsz + 3 || line[hexsz] != '\t')
127 return 1;
129 if (starts_with(line + hexsz + 1, "not-for-merge"))
130 return 0;
132 if (line[hexsz + 1] != '\t')
133 return 2;
135 i = get_oid_hex(line, &oid);
136 if (i)
137 return 3;
139 if (!find_merge_parent(merge_parents, &oid, NULL))
140 return 0; /* subsumed by other parents */
142 CALLOC_ARRAY(origin_data, 1);
143 oidcpy(&origin_data->oid, &oid);
145 if (line[len - 1] == '\n')
146 line[len - 1] = 0;
147 line += hexsz + 2;
150 * At this point, line points at the beginning of comment e.g.
151 * "branch 'frotz' of git://that/repository.git".
152 * Find the repository name and point it with src.
154 src = strstr(line, " of ");
155 if (src) {
156 *src = 0;
157 src += 4;
158 pulling_head = 0;
159 } else {
160 src = line;
161 pulling_head = 1;
164 item = unsorted_string_list_lookup(&srcs, src);
165 if (!item) {
166 item = string_list_append(&srcs, src);
167 item->util = xcalloc(1, sizeof(struct src_data));
168 init_src_data(item->util);
170 src_data = item->util;
172 if (pulling_head) {
173 origin = src;
174 src_data->head_status |= 1;
175 } else if (skip_prefix(line, "branch ", &origin)) {
176 origin_data->is_local_branch = 1;
177 string_list_append(&src_data->branch, origin);
178 src_data->head_status |= 2;
179 } else if (skip_prefix(line, "tag ", &tag_name)) {
180 origin = line;
181 string_list_append(&src_data->tag, tag_name);
182 src_data->head_status |= 2;
183 } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
184 string_list_append(&src_data->r_branch, origin);
185 src_data->head_status |= 2;
186 } else {
187 origin = src;
188 string_list_append(&src_data->generic, line);
189 src_data->head_status |= 2;
192 if (!strcmp(".", src) || !strcmp(src, origin)) {
193 int len = strlen(origin);
194 if (origin[0] == '\'' && origin[len - 1] == '\'')
195 origin = to_free = xmemdupz(origin + 1, len - 2);
196 } else
197 origin = to_free = xstrfmt("%s of %s", origin, src);
198 if (strcmp(".", src))
199 origin_data->is_local_branch = 0;
200 string_list_append(&origins, origin)->util = origin_data;
201 free(to_free);
202 return 0;
205 static void print_joined(const char *singular, const char *plural,
206 struct string_list *list, struct strbuf *out)
208 if (list->nr == 0)
209 return;
210 if (list->nr == 1) {
211 strbuf_addf(out, "%s%s", singular, list->items[0].string);
212 } else {
213 int i;
214 strbuf_addstr(out, plural);
215 for (i = 0; i < list->nr - 1; i++)
216 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
217 list->items[i].string);
218 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
222 static void add_branch_desc(struct strbuf *out, const char *name)
224 struct strbuf desc = STRBUF_INIT;
226 if (!read_branch_desc(&desc, name)) {
227 const char *bp = desc.buf;
228 while (*bp) {
229 const char *ep = strchrnul(bp, '\n');
230 if (*ep)
231 ep++;
232 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
233 bp = ep;
235 strbuf_complete_line(out);
237 strbuf_release(&desc);
240 #define util_as_integral(elem) ((intptr_t)((elem)->util))
242 static void record_person_from_buf(int which, struct string_list *people,
243 const char *buffer)
245 char *name_buf, *name, *name_end;
246 struct string_list_item *elem;
247 const char *field;
249 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
250 name = strstr(buffer, field);
251 if (!name)
252 return;
253 name += strlen(field);
254 name_end = strchrnul(name, '<');
255 if (*name_end)
256 name_end--;
257 while (isspace(*name_end) && name <= name_end)
258 name_end--;
259 if (name_end < name)
260 return;
261 name_buf = xmemdupz(name, name_end - name + 1);
263 elem = string_list_lookup(people, name_buf);
264 if (!elem) {
265 elem = string_list_insert(people, name_buf);
266 elem->util = (void *)0;
268 elem->util = (void*)(util_as_integral(elem) + 1);
269 free(name_buf);
273 static void record_person(int which, struct string_list *people,
274 struct commit *commit)
276 const char *buffer = repo_get_commit_buffer(the_repository, commit,
277 NULL);
278 record_person_from_buf(which, people, buffer);
279 repo_unuse_commit_buffer(the_repository, commit, buffer);
282 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
284 const struct string_list_item *a = a_, *b = b_;
285 return util_as_integral(b) - util_as_integral(a);
288 static void add_people_count(struct strbuf *out, struct string_list *people)
290 if (people->nr == 1)
291 strbuf_addstr(out, people->items[0].string);
292 else if (people->nr == 2)
293 strbuf_addf(out, "%s (%d) and %s (%d)",
294 people->items[0].string,
295 (int)util_as_integral(&people->items[0]),
296 people->items[1].string,
297 (int)util_as_integral(&people->items[1]));
298 else if (people->nr)
299 strbuf_addf(out, "%s (%d) and others",
300 people->items[0].string,
301 (int)util_as_integral(&people->items[0]));
304 static void credit_people(struct strbuf *out,
305 struct string_list *them,
306 int kind)
308 const char *label;
309 const char *me;
311 if (kind == 'a') {
312 label = "By";
313 me = git_author_info(IDENT_NO_DATE);
314 } else {
315 label = "Via";
316 me = git_committer_info(IDENT_NO_DATE);
319 if (!them->nr ||
320 (them->nr == 1 &&
321 me &&
322 skip_prefix(me, them->items->string, &me) &&
323 starts_with(me, " <")))
324 return;
325 strbuf_addf(out, "\n%c %s ", comment_line_char, label);
326 add_people_count(out, them);
329 static void add_people_info(struct strbuf *out,
330 struct string_list *authors,
331 struct string_list *committers)
333 QSORT(authors->items, authors->nr,
334 cmp_string_list_util_as_integral);
335 QSORT(committers->items, committers->nr,
336 cmp_string_list_util_as_integral);
338 credit_people(out, authors, 'a');
339 credit_people(out, committers, 'c');
342 static void shortlog(const char *name,
343 struct origin_data *origin_data,
344 struct commit *head,
345 struct rev_info *rev,
346 struct fmt_merge_msg_opts *opts,
347 struct strbuf *out)
349 int i, count = 0;
350 struct commit *commit;
351 struct object *branch;
352 struct string_list subjects = STRING_LIST_INIT_DUP;
353 struct string_list authors = STRING_LIST_INIT_DUP;
354 struct string_list committers = STRING_LIST_INIT_DUP;
355 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
356 struct strbuf sb = STRBUF_INIT;
357 const struct object_id *oid = &origin_data->oid;
358 int limit = opts->shortlog_len;
360 branch = deref_tag(the_repository, parse_object(the_repository, oid),
361 oid_to_hex(oid),
362 the_hash_algo->hexsz);
363 if (!branch || branch->type != OBJ_COMMIT)
364 return;
366 setup_revisions(0, NULL, rev, NULL);
367 add_pending_object(rev, branch, name);
368 add_pending_object(rev, &head->object, "^HEAD");
369 head->object.flags |= UNINTERESTING;
370 if (prepare_revision_walk(rev))
371 die("revision walk setup failed");
372 while ((commit = get_revision(rev)) != NULL) {
373 struct pretty_print_context ctx = {0};
375 if (commit->parents && commit->parents->next) {
376 /* do not list a merge but count committer */
377 if (opts->credit_people)
378 record_person('c', &committers, commit);
379 continue;
381 if (!count && opts->credit_people)
382 /* the 'tip' committer */
383 record_person('c', &committers, commit);
384 if (opts->credit_people)
385 record_person('a', &authors, commit);
386 count++;
387 if (subjects.nr > limit)
388 continue;
390 repo_format_commit_message(the_repository, commit, "%s", &sb,
391 &ctx);
392 strbuf_ltrim(&sb);
394 if (!sb.len)
395 string_list_append(&subjects,
396 oid_to_hex(&commit->object.oid));
397 else
398 string_list_append_nodup(&subjects,
399 strbuf_detach(&sb, NULL));
402 if (opts->credit_people)
403 add_people_info(out, &authors, &committers);
404 if (count > limit)
405 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
406 else
407 strbuf_addf(out, "\n* %s:\n", name);
409 if (origin_data->is_local_branch && use_branch_desc)
410 add_branch_desc(out, name);
412 for (i = 0; i < subjects.nr; i++)
413 if (i >= limit)
414 strbuf_addstr(out, " ...\n");
415 else
416 strbuf_addf(out, " %s\n", subjects.items[i].string);
418 clear_commit_marks((struct commit *)branch, flags);
419 clear_commit_marks(head, flags);
420 free_commit_list(rev->commits);
421 rev->commits = NULL;
422 rev->pending.nr = 0;
424 string_list_clear(&authors, 0);
425 string_list_clear(&committers, 0);
426 string_list_clear(&subjects, 0);
430 * See if dest_branch matches with any glob pattern on the
431 * suppress_dest_patterns list.
433 * We may want to also allow negative matches e.g. ":!glob" like we do
434 * for pathspec, but for now, let's keep it simple and stupid.
436 static int dest_suppressed(const char *dest_branch)
438 struct string_list_item *item;
440 for_each_string_list_item(item, &suppress_dest_patterns) {
441 if (!wildmatch(item->string, dest_branch, WM_PATHNAME))
442 return 1;
444 return 0;
447 static void fmt_merge_msg_title(struct strbuf *out,
448 const char *current_branch)
450 int i = 0;
451 char *sep = "";
453 strbuf_addstr(out, "Merge ");
454 for (i = 0; i < srcs.nr; i++) {
455 struct src_data *src_data = srcs.items[i].util;
456 const char *subsep = "";
458 strbuf_addstr(out, sep);
459 sep = "; ";
461 if (src_data->head_status == 1) {
462 strbuf_addstr(out, srcs.items[i].string);
463 continue;
465 if (src_data->head_status == 3) {
466 subsep = ", ";
467 strbuf_addstr(out, "HEAD");
469 if (src_data->branch.nr) {
470 strbuf_addstr(out, subsep);
471 subsep = ", ";
472 print_joined("branch ", "branches ", &src_data->branch,
473 out);
475 if (src_data->r_branch.nr) {
476 strbuf_addstr(out, subsep);
477 subsep = ", ";
478 print_joined("remote-tracking branch ", "remote-tracking branches ",
479 &src_data->r_branch, out);
481 if (src_data->tag.nr) {
482 strbuf_addstr(out, subsep);
483 subsep = ", ";
484 print_joined("tag ", "tags ", &src_data->tag, out);
486 if (src_data->generic.nr) {
487 strbuf_addstr(out, subsep);
488 print_joined("commit ", "commits ", &src_data->generic,
489 out);
491 if (strcmp(".", srcs.items[i].string))
492 strbuf_addf(out, " of %s", srcs.items[i].string);
495 if (!dest_suppressed(current_branch))
496 strbuf_addf(out, " into %s", current_branch);
497 strbuf_addch(out, '\n');
500 static void fmt_tag_signature(struct strbuf *tagbuf,
501 struct strbuf *sig,
502 const char *buf,
503 unsigned long len)
505 const char *tag_body = strstr(buf, "\n\n");
506 if (tag_body) {
507 tag_body += 2;
508 strbuf_add(tagbuf, tag_body, buf + len - tag_body);
510 strbuf_complete_line(tagbuf);
511 if (sig->len) {
512 strbuf_addch(tagbuf, '\n');
513 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
514 comment_line_char);
518 static void fmt_merge_msg_sigs(struct strbuf *out)
520 int i, tag_number = 0, first_tag = 0;
521 struct strbuf tagbuf = STRBUF_INIT;
523 for (i = 0; i < origins.nr; i++) {
524 struct object_id *oid = origins.items[i].util;
525 enum object_type type;
526 unsigned long size;
527 char *buf = repo_read_object_file(the_repository, oid, &type,
528 &size);
529 char *origbuf = buf;
530 unsigned long len = size;
531 struct signature_check sigc = { NULL };
532 struct strbuf payload = STRBUF_INIT, sig = STRBUF_INIT;
534 if (!buf || type != OBJ_TAG)
535 goto next;
537 if (!parse_signature(buf, size, &payload, &sig))
538 ;/* merely annotated */
539 else {
540 buf = payload.buf;
541 len = payload.len;
542 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
543 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
544 if (check_signature(&sigc, sig.buf, sig.len) &&
545 !sigc.output)
546 strbuf_addstr(&sig, "gpg verification failed.\n");
547 else
548 strbuf_addstr(&sig, sigc.output);
551 if (!tag_number++) {
552 fmt_tag_signature(&tagbuf, &sig, buf, len);
553 first_tag = i;
554 } else {
555 if (tag_number == 2) {
556 struct strbuf tagline = STRBUF_INIT;
557 strbuf_addch(&tagline, '\n');
558 strbuf_add_commented_lines(&tagline,
559 origins.items[first_tag].string,
560 strlen(origins.items[first_tag].string),
561 comment_line_char);
562 strbuf_insert(&tagbuf, 0, tagline.buf,
563 tagline.len);
564 strbuf_release(&tagline);
566 strbuf_addch(&tagbuf, '\n');
567 strbuf_add_commented_lines(&tagbuf,
568 origins.items[i].string,
569 strlen(origins.items[i].string),
570 comment_line_char);
571 fmt_tag_signature(&tagbuf, &sig, buf, len);
573 strbuf_release(&payload);
574 strbuf_release(&sig);
575 signature_check_clear(&sigc);
576 next:
577 free(origbuf);
579 if (tagbuf.len) {
580 strbuf_addch(out, '\n');
581 strbuf_addbuf(out, &tagbuf);
583 strbuf_release(&tagbuf);
586 static void find_merge_parents(struct merge_parents *result,
587 struct strbuf *in, struct object_id *head)
589 struct commit_list *parents;
590 struct commit *head_commit;
591 int pos = 0, i, j;
593 parents = NULL;
594 while (pos < in->len) {
595 int len;
596 char *p = in->buf + pos;
597 char *newline = strchr(p, '\n');
598 const char *q;
599 struct object_id oid;
600 struct commit *parent;
601 struct object *obj;
603 len = newline ? newline - p : strlen(p);
604 pos += len + !!newline;
606 if (parse_oid_hex(p, &oid, &q) ||
607 q[0] != '\t' ||
608 q[1] != '\t')
609 continue; /* skip not-for-merge */
611 * Do not use get_merge_parent() here; we do not have
612 * "name" here and we do not want to contaminate its
613 * util field yet.
615 obj = parse_object(the_repository, &oid);
616 parent = (struct commit *)repo_peel_to_type(the_repository,
617 NULL, 0, obj,
618 OBJ_COMMIT);
619 if (!parent)
620 continue;
621 commit_list_insert(parent, &parents);
622 add_merge_parent(result, &obj->oid, &parent->object.oid);
624 head_commit = lookup_commit(the_repository, head);
625 if (head_commit)
626 commit_list_insert(head_commit, &parents);
627 reduce_heads_replace(&parents);
629 while (parents) {
630 struct commit *cmit = pop_commit(&parents);
631 for (i = 0; i < result->nr; i++)
632 if (oideq(&result->item[i].commit, &cmit->object.oid))
633 result->item[i].used = 1;
636 for (i = j = 0; i < result->nr; i++) {
637 if (result->item[i].used) {
638 if (i != j)
639 result->item[j] = result->item[i];
640 j++;
643 result->nr = j;
647 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
648 struct fmt_merge_msg_opts *opts)
650 int i = 0, pos = 0;
651 struct object_id head_oid;
652 const char *current_branch;
653 void *current_branch_to_free;
654 struct merge_parents merge_parents;
656 if (!suppress_dest_pattern_seen) {
657 string_list_append(&suppress_dest_patterns, "main");
658 string_list_append(&suppress_dest_patterns, "master");
661 memset(&merge_parents, 0, sizeof(merge_parents));
663 /* learn the commit that we merge into and the current branch name */
664 current_branch = current_branch_to_free =
665 resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL);
666 if (!current_branch)
667 die("No current branch");
669 if (opts->into_name)
670 current_branch = opts->into_name;
671 else if (starts_with(current_branch, "refs/heads/"))
672 current_branch += 11;
674 find_merge_parents(&merge_parents, in, &head_oid);
676 /* get a line */
677 while (pos < in->len) {
678 int len;
679 char *newline, *p = in->buf + pos;
681 newline = strchr(p, '\n');
682 len = newline ? newline - p : strlen(p);
683 pos += len + !!newline;
684 i++;
685 p[len] = 0;
686 if (handle_line(p, &merge_parents))
687 die("error in line %d: %.*s", i, len, p);
690 if (opts->add_title && srcs.nr)
691 fmt_merge_msg_title(out, current_branch);
693 if (origins.nr)
694 fmt_merge_msg_sigs(out);
696 if (opts->shortlog_len) {
697 struct commit *head;
698 struct rev_info rev;
700 head = lookup_commit_or_die(&head_oid, "HEAD");
701 repo_init_revisions(the_repository, &rev, NULL);
702 rev.commit_format = CMIT_FMT_ONELINE;
703 diff_merges_suppress(&rev);
704 rev.limited = 1;
706 strbuf_complete_line(out);
708 for (i = 0; i < origins.nr; i++)
709 shortlog(origins.items[i].string,
710 origins.items[i].util,
711 head, &rev, opts, out);
712 release_revisions(&rev);
715 strbuf_complete_line(out);
716 free(current_branch_to_free);
717 free(merge_parents.item);
718 return 0;