Git fork
at reftables-rust 1206 lines 35 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#define DISABLE_SIGN_COMPARE_WARNINGS 3 4#include "git-compat-util.h" 5#include "commit-reach.h" 6#include "config.h" 7#include "diff.h" 8#include "diffcore.h" 9#include "environment.h" 10#include "hex.h" 11#include "object-name.h" 12#include "object-file.h" 13#include "repository.h" 14#include "tmp-objdir.h" 15#include "commit.h" 16#include "tag.h" 17#include "graph.h" 18#include "log-tree.h" 19#include "merge-ort.h" 20#include "reflog-walk.h" 21#include "refs.h" 22#include "replace-object.h" 23#include "revision.h" 24#include "string-list.h" 25#include "color.h" 26#include "gpg-interface.h" 27#include "sequencer.h" 28#include "line-log.h" 29#include "help.h" 30#include "range-diff.h" 31#include "strmap.h" 32#include "tree.h" 33#include "wildmatch.h" 34#include "write-or-die.h" 35#include "pager.h" 36 37static struct decoration name_decoration = { "object names" }; 38static int decoration_loaded; 39static int decoration_flags; 40 41static char decoration_colors[][COLOR_MAXLEN] = { 42 GIT_COLOR_RESET, 43 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */ 44 GIT_COLOR_BOLD_RED, /* REF_REMOTE */ 45 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */ 46 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */ 47 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */ 48 GIT_COLOR_BOLD_BLUE, /* GRAFTED */ 49}; 50 51static const char *color_decorate_slots[] = { 52 [DECORATION_REF_LOCAL] = "branch", 53 [DECORATION_REF_REMOTE] = "remoteBranch", 54 [DECORATION_REF_TAG] = "tag", 55 [DECORATION_REF_STASH] = "stash", 56 [DECORATION_REF_HEAD] = "HEAD", 57 [DECORATION_GRAFTED] = "grafted", 58}; 59 60static const char *decorate_get_color(enum git_colorbool decorate_use_color, enum decoration_type ix) 61{ 62 if (want_color(decorate_use_color)) 63 return decoration_colors[ix]; 64 return ""; 65} 66 67define_list_config_array(color_decorate_slots); 68 69int parse_decorate_color_config(const char *var, const char *slot_name, const char *value) 70{ 71 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name); 72 if (slot < 0) 73 return 0; 74 if (!value) 75 return config_error_nonbool(var); 76 return color_parse(value, decoration_colors[slot]); 77} 78 79/* 80 * log-tree.c uses DIFF_OPT_TST for determining whether to use color 81 * for showing the commit sha1, use the same check for --decorate 82 */ 83#define decorate_get_color_opt(o, ix) \ 84 decorate_get_color((o)->use_color, ix) 85 86void add_name_decoration(enum decoration_type type, const char *name, struct object *obj) 87{ 88 struct name_decoration *res; 89 FLEX_ALLOC_STR(res, name, name); 90 res->type = type; 91 res->next = add_decoration(&name_decoration, obj, res); 92} 93 94const struct name_decoration *get_name_decoration(const struct object *obj) 95{ 96 load_ref_decorations(NULL, DECORATE_SHORT_REFS); 97 return lookup_decoration(&name_decoration, obj); 98} 99 100static int match_ref_pattern(const char *refname, 101 const struct string_list_item *item) 102{ 103 int matched = 0; 104 if (!item->util) { 105 if (!wildmatch(item->string, refname, 0)) 106 matched = 1; 107 } else { 108 const char *rest; 109 if (skip_prefix(refname, item->string, &rest) && 110 (!*rest || *rest == '/')) 111 matched = 1; 112 } 113 return matched; 114} 115 116static int ref_filter_match(const char *refname, 117 const struct decoration_filter *filter) 118{ 119 struct string_list_item *item; 120 const struct string_list *exclude_patterns = filter->exclude_ref_pattern; 121 const struct string_list *include_patterns = filter->include_ref_pattern; 122 const struct string_list *exclude_patterns_config = 123 filter->exclude_ref_config_pattern; 124 125 if (exclude_patterns && exclude_patterns->nr) { 126 for_each_string_list_item(item, exclude_patterns) { 127 if (match_ref_pattern(refname, item)) 128 return 0; 129 } 130 } 131 132 if (include_patterns && include_patterns->nr) { 133 for_each_string_list_item(item, include_patterns) { 134 if (match_ref_pattern(refname, item)) 135 return 1; 136 } 137 return 0; 138 } 139 140 if (exclude_patterns_config && exclude_patterns_config->nr) { 141 for_each_string_list_item(item, exclude_patterns_config) { 142 if (match_ref_pattern(refname, item)) 143 return 0; 144 } 145 } 146 147 return 1; 148} 149 150static int add_ref_decoration(const char *refname, const char *referent UNUSED, const struct object_id *oid, 151 int flags UNUSED, 152 void *cb_data) 153{ 154 int i; 155 struct object *obj; 156 enum object_type objtype; 157 enum decoration_type deco_type = DECORATION_NONE; 158 struct decoration_filter *filter = (struct decoration_filter *)cb_data; 159 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; 160 161 if (filter && !ref_filter_match(refname, filter)) 162 return 0; 163 164 if (starts_with(refname, git_replace_ref_base)) { 165 struct object_id original_oid; 166 if (!replace_refs_enabled(the_repository)) 167 return 0; 168 if (get_oid_hex(refname + strlen(git_replace_ref_base), 169 &original_oid)) { 170 warning("invalid replace ref %s", refname); 171 return 0; 172 } 173 obj = parse_object(the_repository, &original_oid); 174 if (obj) 175 add_name_decoration(DECORATION_GRAFTED, "replaced", obj); 176 return 0; 177 } 178 179 objtype = odb_read_object_info(the_repository->objects, oid, NULL); 180 if (objtype < 0) 181 return 0; 182 obj = lookup_object_by_type(the_repository, oid, objtype); 183 184 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) { 185 struct ref_namespace_info *info = &ref_namespace[i]; 186 187 if (!info->decoration) 188 continue; 189 if (info->exact) { 190 if (!strcmp(refname, info->ref)) { 191 deco_type = info->decoration; 192 break; 193 } 194 } else if (starts_with(refname, info->ref)) { 195 deco_type = info->decoration; 196 break; 197 } 198 } 199 200 add_name_decoration(deco_type, refname, obj); 201 while (obj->type == OBJ_TAG) { 202 if (!obj->parsed) 203 parse_object(the_repository, &obj->oid); 204 obj = ((struct tag *)obj)->tagged; 205 if (!obj) 206 break; 207 add_name_decoration(DECORATION_REF_TAG, refname, obj); 208 } 209 return 0; 210} 211 212static int add_graft_decoration(const struct commit_graft *graft, 213 void *cb_data UNUSED) 214{ 215 struct commit *commit = lookup_commit(the_repository, &graft->oid); 216 if (!commit) 217 return 0; 218 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object); 219 return 0; 220} 221 222void load_ref_decorations(struct decoration_filter *filter, int flags) 223{ 224 if (!decoration_loaded) { 225 if (filter) { 226 struct string_list_item *item; 227 for_each_string_list_item(item, filter->exclude_ref_pattern) { 228 normalize_glob_ref(item, NULL, item->string); 229 } 230 for_each_string_list_item(item, filter->include_ref_pattern) { 231 normalize_glob_ref(item, NULL, item->string); 232 } 233 for_each_string_list_item(item, filter->exclude_ref_config_pattern) { 234 normalize_glob_ref(item, NULL, item->string); 235 } 236 237 /* normalize_glob_ref duplicates the strings */ 238 filter->exclude_ref_pattern->strdup_strings = 1; 239 filter->include_ref_pattern->strdup_strings = 1; 240 filter->exclude_ref_config_pattern->strdup_strings = 1; 241 } 242 decoration_loaded = 1; 243 decoration_flags = flags; 244 refs_for_each_ref(get_main_ref_store(the_repository), 245 add_ref_decoration, filter); 246 refs_head_ref(get_main_ref_store(the_repository), 247 add_ref_decoration, filter); 248 for_each_commit_graft(add_graft_decoration, filter); 249 } 250} 251 252void load_branch_decorations(void) 253{ 254 if (!decoration_loaded) { 255 struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP; 256 struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP; 257 struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP; 258 struct decoration_filter decoration_filter = { 259 .include_ref_pattern = &decorate_refs_include, 260 .exclude_ref_pattern = &decorate_refs_exclude, 261 .exclude_ref_config_pattern = &decorate_refs_exclude_config, 262 }; 263 264 string_list_append(&decorate_refs_include, "refs/heads/"); 265 load_ref_decorations(&decoration_filter, 0); 266 267 string_list_clear(&decorate_refs_exclude, 0); 268 string_list_clear(&decorate_refs_exclude_config, 0); 269 string_list_clear(&decorate_refs_include, 0); 270 } 271} 272 273static void show_parents(struct commit *commit, int abbrev, FILE *file) 274{ 275 struct commit_list *p; 276 for (p = commit->parents; p ; p = p->next) { 277 struct commit *parent = p->item; 278 fprintf(file, " %s", 279 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev)); 280 } 281} 282 283static void show_children(struct rev_info *opt, struct commit *commit, int abbrev) 284{ 285 struct commit_list *p = lookup_decoration(&opt->children, &commit->object); 286 for ( ; p; p = p->next) { 287 fprintf(opt->diffopt.file, " %s", 288 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev)); 289 } 290} 291 292/* 293 * Do we have HEAD in the output, and also the branch it points at? 294 * If so, find that decoration entry for that current branch. 295 */ 296static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration) 297{ 298 const struct name_decoration *list, *head = NULL; 299 const char *branch_name = NULL; 300 int rru_flags; 301 302 /* First find HEAD */ 303 for (list = decoration; list; list = list->next) 304 if (list->type == DECORATION_REF_HEAD) { 305 head = list; 306 break; 307 } 308 if (!head) 309 return NULL; 310 311 /* Now resolve and find the matching current branch */ 312 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 313 "HEAD", 0, NULL, &rru_flags); 314 if (!branch_name || !(rru_flags & REF_ISSYMREF)) 315 return NULL; 316 317 if (!starts_with(branch_name, "refs/")) 318 return NULL; 319 320 /* OK, do we have that ref in the list? */ 321 for (list = decoration; list; list = list->next) 322 if ((list->type == DECORATION_REF_LOCAL) && 323 !strcmp(branch_name, list->name)) { 324 return list; 325 } 326 327 return NULL; 328} 329 330static void show_name(struct strbuf *sb, const struct name_decoration *decoration) 331{ 332 if (decoration_flags == DECORATE_SHORT_REFS) 333 strbuf_addstr(sb, prettify_refname(decoration->name)); 334 else 335 strbuf_addstr(sb, decoration->name); 336} 337 338/* 339 * The caller makes sure there is no funny color before calling. 340 * format_decorations ensures the same after return. 341 */ 342void format_decorations(struct strbuf *sb, 343 const struct commit *commit, 344 enum git_colorbool use_color, 345 const struct decoration_options *opts) 346{ 347 const struct name_decoration *decoration; 348 const struct name_decoration *current_and_HEAD; 349 const char *color_commit, *color_reset; 350 351 const char *prefix = " ("; 352 const char *suffix = ")"; 353 const char *separator = ", "; 354 const char *pointer = " -> "; 355 const char *tag = "tag: "; 356 357 decoration = get_name_decoration(&commit->object); 358 if (!decoration) 359 return; 360 361 if (opts) { 362 if (opts->prefix) 363 prefix = opts->prefix; 364 if (opts->suffix) 365 suffix = opts->suffix; 366 if (opts->separator) 367 separator = opts->separator; 368 if (opts->pointer) 369 pointer = opts->pointer; 370 if (opts->tag) 371 tag = opts->tag; 372 } 373 374 color_commit = diff_get_color(use_color, DIFF_COMMIT); 375 color_reset = decorate_get_color(use_color, DECORATION_NONE); 376 377 current_and_HEAD = current_pointed_by_HEAD(decoration); 378 while (decoration) { 379 /* 380 * When both current and HEAD are there, only 381 * show HEAD->current where HEAD would have 382 * appeared, skipping the entry for current. 383 */ 384 if (decoration != current_and_HEAD) { 385 const char *color = 386 decorate_get_color(use_color, decoration->type); 387 388 if (*prefix) { 389 strbuf_addstr(sb, color_commit); 390 strbuf_addstr(sb, prefix); 391 strbuf_addstr(sb, color_reset); 392 } 393 394 if (*tag && decoration->type == DECORATION_REF_TAG) { 395 strbuf_addstr(sb, color); 396 strbuf_addstr(sb, tag); 397 strbuf_addstr(sb, color_reset); 398 } 399 400 strbuf_addstr(sb, color); 401 show_name(sb, decoration); 402 strbuf_addstr(sb, color_reset); 403 404 if (current_and_HEAD && 405 decoration->type == DECORATION_REF_HEAD) { 406 strbuf_addstr(sb, color_commit); 407 strbuf_addstr(sb, pointer); 408 strbuf_addstr(sb, color_reset); 409 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type)); 410 show_name(sb, current_and_HEAD); 411 strbuf_addstr(sb, color_reset); 412 } 413 414 prefix = separator; 415 } 416 decoration = decoration->next; 417 } 418 if (*suffix) { 419 strbuf_addstr(sb, color_commit); 420 strbuf_addstr(sb, suffix); 421 strbuf_addstr(sb, color_reset); 422 } 423} 424 425void show_decorations(struct rev_info *opt, struct commit *commit) 426{ 427 struct strbuf sb = STRBUF_INIT; 428 429 if (opt->sources) { 430 char **slot = revision_sources_peek(opt->sources, commit); 431 432 if (slot && *slot) 433 fprintf(opt->diffopt.file, "\t%s", *slot); 434 } 435 if (!opt->show_decorations) 436 return; 437 format_decorations(&sb, commit, opt->diffopt.use_color, NULL); 438 fputs(sb.buf, opt->diffopt.file); 439 strbuf_release(&sb); 440} 441 442void fmt_output_subject(struct strbuf *filename, 443 const char *subject, 444 struct rev_info *info) 445{ 446 const char *suffix = info->patch_suffix; 447 int nr = info->nr; 448 int start_len = filename->len; 449 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1); 450 451 if (info->reroll_count) { 452 struct strbuf temp = STRBUF_INIT; 453 454 strbuf_addf(&temp, "v%s", info->reroll_count); 455 format_sanitized_subject(filename, temp.buf, temp.len); 456 strbuf_addstr(filename, "-"); 457 strbuf_release(&temp); 458 } 459 strbuf_addf(filename, "%04d-%s", nr, subject); 460 461 if (max_len < filename->len) 462 strbuf_setlen(filename, max_len); 463 strbuf_addstr(filename, suffix); 464} 465 466void fmt_output_commit(struct strbuf *filename, 467 struct commit *commit, 468 struct rev_info *info) 469{ 470 struct pretty_print_context ctx = {0}; 471 struct strbuf subject = STRBUF_INIT; 472 473 repo_format_commit_message(the_repository, commit, "%f", &subject, 474 &ctx); 475 fmt_output_subject(filename, subject.buf, info); 476 strbuf_release(&subject); 477} 478 479void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt) 480{ 481 if (opt->total > 0) { 482 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ", 483 opt->subject_prefix, 484 *opt->subject_prefix ? " " : "", 485 decimal_width(opt->total), 486 opt->nr, opt->total); 487 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) { 488 strbuf_addf(sb, "Subject: [%s] ", 489 opt->subject_prefix); 490 } else { 491 strbuf_addstr(sb, "Subject: "); 492 } 493} 494 495void log_write_email_headers(struct rev_info *opt, struct commit *commit, 496 char **extra_headers_p, 497 int *need_8bit_cte_p, 498 int maybe_multipart) 499{ 500 struct strbuf headers = STRBUF_INIT; 501 const char *name = oid_to_hex(opt->zero_commit ? 502 null_oid(the_hash_algo) : &commit->object.oid); 503 504 *need_8bit_cte_p = 0; /* unknown */ 505 506 if (opt->extra_headers && *opt->extra_headers) 507 strbuf_addstr(&headers, opt->extra_headers); 508 509 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name); 510 graph_show_oneline(opt->graph); 511 if (opt->message_id) { 512 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id); 513 graph_show_oneline(opt->graph); 514 } 515 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) { 516 int i, n; 517 n = opt->ref_message_ids->nr; 518 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string); 519 for (i = 0; i < n; i++) 520 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "), 521 opt->ref_message_ids->items[i].string); 522 graph_show_oneline(opt->graph); 523 } 524 if (opt->mime_boundary && maybe_multipart) { 525 static struct strbuf buffer = STRBUF_INIT; 526 struct strbuf filename = STRBUF_INIT; 527 *need_8bit_cte_p = -1; /* NEVER */ 528 529 strbuf_reset(&buffer); 530 531 strbuf_addf(&headers, 532 "MIME-Version: 1.0\n" 533 "Content-Type: multipart/mixed;" 534 " boundary=\"%s%s\"\n" 535 "\n" 536 "This is a multi-part message in MIME " 537 "format.\n" 538 "--%s%s\n" 539 "Content-Type: text/plain; " 540 "charset=UTF-8; format=fixed\n" 541 "Content-Transfer-Encoding: 8bit\n\n", 542 mime_boundary_leader, opt->mime_boundary, 543 mime_boundary_leader, opt->mime_boundary); 544 545 if (opt->numbered_files) 546 strbuf_addf(&filename, "%d", opt->nr); 547 else 548 fmt_output_commit(&filename, commit, opt); 549 strbuf_addf(&buffer, 550 "\n--%s%s\n" 551 "Content-Type: text/x-patch;" 552 " name=\"%s\"\n" 553 "Content-Transfer-Encoding: 8bit\n" 554 "Content-Disposition: %s;" 555 " filename=\"%s\"\n\n", 556 mime_boundary_leader, opt->mime_boundary, 557 filename.buf, 558 opt->no_inline ? "attachment" : "inline", 559 filename.buf); 560 opt->diffopt.stat_sep = buffer.buf; 561 strbuf_release(&filename); 562 } 563 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL; 564} 565 566static void show_sig_lines(struct rev_info *opt, int status, const char *bol) 567{ 568 const char *color, *reset, *eol; 569 570 color = diff_get_color_opt(&opt->diffopt, 571 status ? DIFF_WHITESPACE : DIFF_FRAGINFO); 572 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET); 573 while (*bol) { 574 eol = strchrnul(bol, '\n'); 575 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset, 576 *eol ? "\n" : ""); 577 graph_show_oneline(opt->graph); 578 bol = (*eol) ? (eol + 1) : eol; 579 } 580} 581 582static void show_signature(struct rev_info *opt, struct commit *commit) 583{ 584 struct strbuf payload = STRBUF_INIT; 585 struct strbuf signature = STRBUF_INIT; 586 struct signature_check sigc = { 0 }; 587 int status; 588 589 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0) 590 goto out; 591 592 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT; 593 sigc.payload = strbuf_detach(&payload, &sigc.payload_len); 594 status = check_signature(&sigc, signature.buf, signature.len); 595 if (status && !sigc.output) 596 show_sig_lines(opt, status, "No signature\n"); 597 else 598 show_sig_lines(opt, status, sigc.output); 599 signature_check_clear(&sigc); 600 601 out: 602 strbuf_release(&payload); 603 strbuf_release(&signature); 604} 605 606static int which_parent(const struct object_id *oid, const struct commit *commit) 607{ 608 int nth; 609 const struct commit_list *parent; 610 611 for (nth = 0, parent = commit->parents; parent; parent = parent->next) { 612 if (oideq(&parent->item->object.oid, oid)) 613 return nth; 614 nth++; 615 } 616 return -1; 617} 618 619static int is_common_merge(const struct commit *commit) 620{ 621 return (commit->parents 622 && commit->parents->next 623 && !commit->parents->next->next); 624} 625 626static int show_one_mergetag(struct commit *commit, 627 struct commit_extra_header *extra, 628 void *data) 629{ 630 struct rev_info *opt = (struct rev_info *)data; 631 struct object_id oid; 632 struct tag *tag; 633 struct strbuf verify_message; 634 struct signature_check sigc = { 0 }; 635 int status, nth; 636 struct strbuf payload = STRBUF_INIT; 637 struct strbuf signature = STRBUF_INIT; 638 639 hash_object_file(the_hash_algo, extra->value, extra->len, 640 OBJ_TAG, &oid); 641 tag = lookup_tag(the_repository, &oid); 642 if (!tag) 643 return -1; /* error message already given */ 644 645 strbuf_init(&verify_message, 256); 646 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len)) 647 strbuf_addstr(&verify_message, "malformed mergetag\n"); 648 else if (is_common_merge(commit) && 649 oideq(&tag->tagged->oid, 650 &commit->parents->next->item->object.oid)) 651 strbuf_addf(&verify_message, 652 "merged tag '%s'\n", tag->tag); 653 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0) 654 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n", 655 tag->tag, oid_to_hex(&tag->tagged->oid)); 656 else 657 strbuf_addf(&verify_message, 658 "parent #%d, tagged '%s'\n", nth + 1, tag->tag); 659 660 status = -1; 661 if (parse_signature(extra->value, extra->len, &payload, &signature)) { 662 /* could have a good signature */ 663 sigc.payload_type = SIGNATURE_PAYLOAD_TAG; 664 sigc.payload = strbuf_detach(&payload, &sigc.payload_len); 665 status = check_signature(&sigc, signature.buf, signature.len); 666 if (sigc.output) 667 strbuf_addstr(&verify_message, sigc.output); 668 else 669 strbuf_addstr(&verify_message, "No signature\n"); 670 signature_check_clear(&sigc); 671 /* otherwise we couldn't verify, which is shown as bad */ 672 } 673 674 show_sig_lines(opt, status, verify_message.buf); 675 strbuf_release(&verify_message); 676 strbuf_release(&payload); 677 strbuf_release(&signature); 678 return 0; 679} 680 681static int show_mergetag(struct rev_info *opt, struct commit *commit) 682{ 683 return for_each_mergetag(show_one_mergetag, commit, opt); 684} 685 686static void next_commentary_block(struct rev_info *opt, struct strbuf *sb) 687{ 688 const char *x = opt->shown_dashes ? "\n" : "---\n"; 689 if (sb) 690 strbuf_addstr(sb, x); 691 else 692 fputs(x, opt->diffopt.file); 693 opt->shown_dashes = 1; 694} 695 696static void show_diff_of_diff(struct rev_info *opt) 697{ 698 if (!cmit_fmt_is_mail(opt->commit_format)) 699 return; 700 701 if (opt->idiff_oid1) { 702 struct diff_queue_struct dq; 703 704 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); 705 diff_queue_init(&diff_queued_diff); 706 707 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title); 708 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2, 709 &opt->diffopt); 710 711 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); 712 } 713 714 if (opt->rdiff1) { 715 struct diff_queue_struct dq; 716 struct diff_options opts; 717 struct range_diff_options range_diff_opts = { 718 .creation_factor = opt->creation_factor, 719 .dual_color = 1, 720 .max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT, 721 .diffopt = &opts, 722 .log_arg = &opt->rdiff_log_arg 723 }; 724 725 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); 726 diff_queue_init(&diff_queued_diff); 727 728 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title); 729 /* 730 * Pass minimum required diff-options to range-diff; others 731 * can be added later if deemed desirable. 732 */ 733 repo_diff_setup(the_repository, &opts); 734 opts.file = opt->diffopt.file; 735 opts.use_color = opt->diffopt.use_color; 736 diff_setup_done(&opts); 737 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts); 738 739 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); 740 } 741} 742 743void show_log(struct rev_info *opt) 744{ 745 struct strbuf msgbuf = STRBUF_INIT; 746 struct log_info *log = opt->loginfo; 747 struct commit *commit = log->commit, *parent = log->parent; 748 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz; 749 struct pretty_print_context ctx = {0}; 750 751 opt->loginfo = NULL; 752 if (!opt->verbose_header) { 753 graph_show_commit(opt->graph); 754 755 if (!opt->graph) 756 put_revision_mark(opt, commit); 757 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit), 758 opt->diffopt.file); 759 if (opt->print_parents) 760 show_parents(commit, abbrev_commit, opt->diffopt.file); 761 if (opt->children.name) 762 show_children(opt, commit, abbrev_commit); 763 show_decorations(opt, commit); 764 if (opt->graph && !graph_is_commit_finished(opt->graph)) { 765 putc('\n', opt->diffopt.file); 766 graph_show_remainder(opt->graph); 767 } 768 putc(opt->diffopt.line_termination, opt->diffopt.file); 769 return; 770 } 771 772 /* 773 * If use_terminator is set, we already handled any record termination 774 * at the end of the last record. 775 * Otherwise, add a diffopt.line_termination character before all 776 * entries but the first. (IOW, as a separator between entries) 777 */ 778 if (opt->shown_one && !opt->use_terminator) { 779 /* 780 * If entries are separated by a newline, the output 781 * should look human-readable. If the last entry ended 782 * with a newline, print the graph output before this 783 * newline. Otherwise it will end up as a completely blank 784 * line and will look like a gap in the graph. 785 * 786 * If the entry separator is not a newline, the output is 787 * primarily intended for programmatic consumption, and we 788 * never want the extra graph output before the entry 789 * separator. 790 */ 791 if (opt->diffopt.line_termination == '\n' && 792 !opt->missing_newline) 793 graph_show_padding(opt->graph); 794 putc(opt->diffopt.line_termination, opt->diffopt.file); 795 } 796 opt->shown_one = 1; 797 798 /* 799 * If the history graph was requested, 800 * print the graph, up to this commit's line 801 */ 802 graph_show_commit(opt->graph); 803 804 /* 805 * Print header line of header.. 806 */ 807 808 if (cmit_fmt_is_mail(opt->commit_format)) { 809 log_write_email_headers(opt, commit, &ctx.after_subject, 810 &ctx.need_8bit_cte, 1); 811 ctx.rev = opt; 812 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) { 813 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file); 814 if (opt->commit_format != CMIT_FMT_ONELINE) 815 fputs("commit ", opt->diffopt.file); 816 817 if (!opt->graph) 818 put_revision_mark(opt, commit); 819 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, 820 abbrev_commit), 821 opt->diffopt.file); 822 if (opt->print_parents) 823 show_parents(commit, abbrev_commit, opt->diffopt.file); 824 if (opt->children.name) 825 show_children(opt, commit, abbrev_commit); 826 if (parent) 827 fprintf(opt->diffopt.file, " (from %s)", 828 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit)); 829 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file); 830 show_decorations(opt, commit); 831 if (opt->commit_format == CMIT_FMT_ONELINE) { 832 putc(' ', opt->diffopt.file); 833 } else { 834 putc('\n', opt->diffopt.file); 835 graph_show_oneline(opt->graph); 836 } 837 if (opt->reflog_info) { 838 /* 839 * setup_revisions() ensures that opt->reflog_info 840 * and opt->graph cannot both be set, 841 * so we don't need to worry about printing the 842 * graph info here. 843 */ 844 show_reflog_message(opt->reflog_info, 845 opt->commit_format == CMIT_FMT_ONELINE, 846 opt->date_mode, 847 opt->date_mode_explicit); 848 if (opt->commit_format == CMIT_FMT_ONELINE) 849 return; 850 } 851 } 852 853 if (opt->show_signature) { 854 show_signature(opt, commit); 855 show_mergetag(opt, commit); 856 } 857 858 if (opt->show_notes) { 859 int raw; 860 struct strbuf notebuf = STRBUF_INIT; 861 862 raw = (opt->commit_format == CMIT_FMT_USERFORMAT); 863 format_display_notes(&commit->object.oid, &notebuf, 864 get_log_output_encoding(), raw); 865 ctx.notes_message = strbuf_detach(&notebuf, NULL); 866 } 867 868 /* 869 * And then the pretty-printed message itself 870 */ 871 if (ctx.need_8bit_cte >= 0 && opt->add_signoff) 872 ctx.need_8bit_cte = 873 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT)); 874 ctx.date_mode = opt->date_mode; 875 ctx.date_mode_explicit = opt->date_mode_explicit; 876 ctx.abbrev = opt->diffopt.abbrev; 877 ctx.preserve_subject = opt->preserve_subject; 878 ctx.encode_email_headers = opt->encode_email_headers; 879 ctx.reflog_info = opt->reflog_info; 880 ctx.fmt = opt->commit_format; 881 ctx.mailmap = opt->mailmap; 882 ctx.color = opt->diffopt.use_color; 883 ctx.expand_tabs_in_log = opt->expand_tabs_in_log; 884 ctx.output_encoding = get_log_output_encoding(); 885 ctx.rev = opt; 886 if (opt->from_ident.mail_begin && opt->from_ident.name_begin) 887 ctx.from_ident = &opt->from_ident; 888 if (opt->graph) 889 ctx.graph_width = graph_width(opt->graph); 890 pretty_print_commit(&ctx, commit, &msgbuf); 891 892 if (opt->add_signoff) 893 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP); 894 895 if ((ctx.fmt != CMIT_FMT_USERFORMAT) && 896 ctx.notes_message && *ctx.notes_message) { 897 if (cmit_fmt_is_mail(ctx.fmt)) 898 next_commentary_block(opt, &msgbuf); 899 strbuf_addstr(&msgbuf, ctx.notes_message); 900 } 901 902 if (opt->show_log_size) { 903 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len); 904 graph_show_oneline(opt->graph); 905 } 906 907 /* 908 * Set opt->missing_newline if msgbuf doesn't 909 * end in a newline (including if it is empty) 910 */ 911 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n') 912 opt->missing_newline = 1; 913 else 914 opt->missing_newline = 0; 915 916 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf); 917 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) { 918 if (!opt->missing_newline) 919 graph_show_padding(opt->graph); 920 putc(opt->diffopt.line_termination, opt->diffopt.file); 921 } 922 923 strbuf_release(&msgbuf); 924 free(ctx.notes_message); 925 free(ctx.after_subject); 926} 927 928int log_tree_diff_flush(struct rev_info *opt) 929{ 930 opt->shown_dashes = 0; 931 diffcore_std(&opt->diffopt); 932 933 if (diff_queue_is_empty(&opt->diffopt)) { 934 int saved_fmt = opt->diffopt.output_format; 935 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; 936 diff_flush(&opt->diffopt); 937 opt->diffopt.output_format = saved_fmt; 938 return 0; 939 } 940 941 if (opt->loginfo && !opt->no_commit_id) { 942 show_log(opt); 943 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) && 944 opt->verbose_header && 945 opt->commit_format != CMIT_FMT_ONELINE && 946 !commit_format_is_empty(opt->commit_format)) { 947 /* 948 * When showing a verbose header (i.e. log message), 949 * and not in --pretty=oneline format, we would want 950 * an extra newline between the end of log and the 951 * diff/diffstat output for readability. 952 */ 953 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH; 954 fputs(diff_line_prefix(&opt->diffopt), opt->diffopt.file); 955 956 /* 957 * We may have shown three-dashes line early 958 * between generated commentary (notes, etc.) 959 * and the log message, in which case we only 960 * want a blank line after the commentary 961 * without (an extra) three-dashes line. 962 * Otherwise, we show the three-dashes line if 963 * we are showing the patch with diffstat, but 964 * in that case, there is no extra blank line 965 * after the three-dashes line. 966 */ 967 if (!opt->shown_dashes && 968 (pch & opt->diffopt.output_format) == pch) 969 fprintf(opt->diffopt.file, "---"); 970 putc('\n', opt->diffopt.file); 971 } 972 } 973 diff_flush(&opt->diffopt); 974 return 1; 975} 976 977static int do_diff_combined(struct rev_info *opt, struct commit *commit) 978{ 979 diff_tree_combined_merge(commit, opt); 980 return !opt->loginfo; 981} 982 983static void setup_additional_headers(struct diff_options *o, 984 struct strmap *all_headers) 985{ 986 struct hashmap_iter iter; 987 struct strmap_entry *entry; 988 989 /* 990 * Make o->additional_path_headers contain the subset of all_headers 991 * that match o->pathspec. If there aren't any that match o->pathspec, 992 * then make o->additional_path_headers be NULL. 993 */ 994 995 if (!o->pathspec.nr) { 996 o->additional_path_headers = all_headers; 997 return; 998 } 999 1000 o->additional_path_headers = xmalloc(sizeof(struct strmap)); 1001 strmap_init_with_options(o->additional_path_headers, NULL, 0); 1002 strmap_for_each_entry(all_headers, &iter, entry) { 1003 if (match_pathspec(the_repository->index, &o->pathspec, 1004 entry->key, strlen(entry->key), 1005 0 /* prefix */, NULL /* seen */, 1006 0 /* is_dir */)) 1007 strmap_put(o->additional_path_headers, 1008 entry->key, entry->value); 1009 } 1010 if (!strmap_get_size(o->additional_path_headers)) { 1011 strmap_clear(o->additional_path_headers, 0); 1012 FREE_AND_NULL(o->additional_path_headers); 1013 } 1014} 1015 1016static void cleanup_additional_headers(struct diff_options *o) 1017{ 1018 if (!o->pathspec.nr) { 1019 o->additional_path_headers = NULL; 1020 return; 1021 } 1022 if (!o->additional_path_headers) 1023 return; 1024 1025 strmap_clear(o->additional_path_headers, 0); 1026 FREE_AND_NULL(o->additional_path_headers); 1027} 1028 1029static int do_remerge_diff(struct rev_info *opt, 1030 struct commit_list *parents, 1031 struct object_id *oid) 1032{ 1033 struct merge_options o; 1034 struct commit_list *bases = NULL; 1035 struct merge_result res = {0}; 1036 struct pretty_print_context ctx = {0}; 1037 struct commit *parent1 = parents->item; 1038 struct commit *parent2 = parents->next->item; 1039 struct strbuf parent1_desc = STRBUF_INIT; 1040 struct strbuf parent2_desc = STRBUF_INIT; 1041 1042 /* 1043 * Lazily prepare a temporary object directory and rotate it 1044 * into the alternative object store list as the primary. 1045 */ 1046 if (opt->remerge_diff && !opt->remerge_objdir) { 1047 opt->remerge_objdir = tmp_objdir_create(the_repository, "remerge-diff"); 1048 if (!opt->remerge_objdir) 1049 return error(_("unable to create temporary object directory")); 1050 tmp_objdir_replace_primary_odb(opt->remerge_objdir, 1); 1051 } 1052 1053 /* Setup merge options */ 1054 init_ui_merge_options(&o, the_repository); 1055 o.show_rename_progress = 0; 1056 o.record_conflict_msgs_as_headers = 1; 1057 o.msg_header_prefix = "remerge"; 1058 1059 ctx.abbrev = DEFAULT_ABBREV; 1060 repo_format_commit_message(the_repository, parent1, "%h (%s)", 1061 &parent1_desc, &ctx); 1062 repo_format_commit_message(the_repository, parent2, "%h (%s)", 1063 &parent2_desc, &ctx); 1064 o.branch1 = parent1_desc.buf; 1065 o.branch2 = parent2_desc.buf; 1066 1067 /* Parse the relevant commits and get the merge bases */ 1068 parse_commit_or_die(parent1); 1069 parse_commit_or_die(parent2); 1070 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0) 1071 exit(128); 1072 1073 /* Re-merge the parents */ 1074 merge_incore_recursive(&o, bases, parent1, parent2, &res); 1075 1076 /* Show the diff */ 1077 setup_additional_headers(&opt->diffopt, res.path_messages); 1078 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt); 1079 log_tree_diff_flush(opt); 1080 1081 /* Cleanup */ 1082 free_commit_list(bases); 1083 cleanup_additional_headers(&opt->diffopt); 1084 strbuf_release(&parent1_desc); 1085 strbuf_release(&parent2_desc); 1086 merge_finalize(&o, &res); 1087 1088 /* Clean up the contents of the temporary object directory */ 1089 tmp_objdir_discard_objects(opt->remerge_objdir); 1090 1091 return !opt->loginfo; 1092} 1093 1094/* 1095 * Show the diff of a commit. 1096 * 1097 * Return true if we printed any log info messages 1098 */ 1099static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) 1100{ 1101 int showed_log; 1102 struct commit_list *parents; 1103 struct object_id *oid; 1104 int is_merge; 1105 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status; 1106 1107 if (!all_need_diff && !opt->merges_need_diff) 1108 return 0; 1109 1110 parse_commit_or_die(commit); 1111 oid = get_commit_tree_oid(commit); 1112 1113 parents = get_saved_parents(opt, commit); 1114 is_merge = parents && parents->next; 1115 if (!is_merge && !all_need_diff) 1116 return 0; 1117 1118 /* Root commit? */ 1119 if (!parents) { 1120 if (opt->show_root_diff) { 1121 diff_root_tree_oid(oid, "", &opt->diffopt); 1122 log_tree_diff_flush(opt); 1123 } 1124 return !opt->loginfo; 1125 } 1126 1127 if (is_merge) { 1128 int octopus = (parents->next->next != NULL); 1129 1130 if (opt->remerge_diff) { 1131 if (octopus) { 1132 show_log(opt); 1133 fprintf(opt->diffopt.file, 1134 "diff: warning: Skipping remerge-diff " 1135 "for octopus merges.\n"); 1136 return 1; 1137 } 1138 return do_remerge_diff(opt, parents, oid); 1139 } 1140 if (opt->combine_merges) 1141 return do_diff_combined(opt, commit); 1142 if (opt->separate_merges) { 1143 if (!opt->first_parent_merges) { 1144 /* Show parent info for multiple diffs */ 1145 log->parent = parents->item; 1146 } 1147 } else 1148 return 0; 1149 } 1150 1151 showed_log = 0; 1152 for (;;) { 1153 struct commit *parent = parents->item; 1154 1155 parse_commit_or_die(parent); 1156 diff_tree_oid(get_commit_tree_oid(parent), 1157 oid, "", &opt->diffopt); 1158 log_tree_diff_flush(opt); 1159 1160 showed_log |= !opt->loginfo; 1161 1162 /* Set up the log info for the next parent, if any.. */ 1163 parents = parents->next; 1164 if (!parents || opt->first_parent_merges) 1165 break; 1166 log->parent = parents->item; 1167 opt->loginfo = log; 1168 } 1169 return showed_log; 1170} 1171 1172int log_tree_commit(struct rev_info *opt, struct commit *commit) 1173{ 1174 struct log_info log; 1175 int shown; 1176 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */ 1177 int no_free = opt->diffopt.no_free; 1178 1179 log.commit = commit; 1180 log.parent = NULL; 1181 opt->loginfo = &log; 1182 opt->diffopt.no_free = 1; 1183 1184 /* NEEDSWORK: no restoring of no_free? Why? */ 1185 if (opt->line_level_traverse) 1186 return line_log_print(opt, commit); 1187 1188 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage) 1189 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); 1190 shown = log_tree_diff(opt, commit, &log); 1191 if (!shown && opt->loginfo && opt->always_show_header) { 1192 log.parent = NULL; 1193 show_log(opt); 1194 shown = 1; 1195 } 1196 if (opt->track_linear && !opt->linear && opt->reverse_output_stage) 1197 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); 1198 if (shown) 1199 show_diff_of_diff(opt); 1200 opt->loginfo = NULL; 1201 maybe_flush_or_die(opt->diffopt.file, "stdout"); 1202 opt->diffopt.no_free = no_free; 1203 1204 diff_free(&opt->diffopt); 1205 return shown; 1206}