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