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