Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/log.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / log.c` 2858 lines ยท 80430 bytes Raw Blame History 1 /* 2 * Builtin "git log" and related commands (show, whatchanged) 3 * 4 * (C) Copyright 2006 Linus Torvalds 5 * 2006 Junio Hamano 6 */ 7 8 #define USE_THE_REPOSITORY_VARIABLE 9 10 #include "builtin.h" 11 #include "abspath.h" 12 #include "config.h" 13 #include "environment.h" 14 #include "gettext.h" 15 #include "hex.h" 16 #include "refs.h" 17 #include "object-name.h" 18 #include "odb.h" 19 #include "odb/streaming.h" 20 #include "pager.h" 21 #include "color.h" 22 #include "commit.h" 23 #include "diff.h" 24 #include "diffcore.h" 25 #include "diff-merges.h" 26 #include "revision.h" 27 #include "log-tree.h" 28 #include "oid-array.h" 29 #include "oidset.h" 30 #include "tag.h" 31 #include "reflog-walk.h" 32 #include "patch-ids.h" 33 #include "path.h" 34 #include "shortlog.h" 35 #include "remote.h" 36 #include "string-list.h" 37 #include "parse-options.h" 38 #include "line-log.h" 39 #include "branch.h" 40 #include "version.h" 41 #include "mailmap.h" 42 #include "progress.h" 43 #include "commit-slab.h" 44 #include "advice.h" 45 #include "utf8.h" 46 47 #include "commit-reach.h" 48 #include "promisor-remote.h" 49 #include "range-diff.h" 50 #include "tmp-objdir.h" 51 #include "tree.h" 52 #include "userdiff.h" 53 #include "write-or-die.h" 54 55 #define MAIL_DEFAULT_WRAP 72 56 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100 57 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64 58 59 static unsigned int force_in_body_from; 60 static int stdout_mboxrd; 61 static int format_no_prefix; 62 63 static const char * const builtin_log_usage[] = { 64 N_("git log [] [] [[--] ...]"), 65 N_("git show [] ..."), 66 NULL 67 }; 68 69 struct line_opt_callback_data { 70 struct rev_info *rev; 71 const char *prefix; 72 struct string_list args; 73 }; 74 75 static int session_is_interactive(void) 76 { 77 return isatty(1) || pager_in_use(); 78 } 79 80 static int auto_decoration_style(void) 81 { 82 return session_is_interactive() ? DECORATE_SHORT_REFS : 0; 83 } 84 85 static int parse_decoration_style(const char *value) 86 { 87 switch (git_parse_maybe_bool(value)) { 88 case 1: 89 return DECORATE_SHORT_REFS; 90 case 0: 91 return 0; 92 default: 93 break; 94 } 95 if (!strcmp(value, "full")) 96 return DECORATE_FULL_REFS; 97 else if (!strcmp(value, "short")) 98 return DECORATE_SHORT_REFS; 99 else if (!strcmp(value, "auto")) 100 return auto_decoration_style(); 101 /* 102 * Please update _git_log() in git-completion.bash when you 103 * add new decoration styles. 104 */ 105 return -1; 106 } 107 108 struct log_config { 109 int default_abbrev_commit; 110 int default_show_root; 111 int default_follow; 112 int default_show_signature; 113 int default_encode_email_headers; 114 int decoration_style; 115 int decoration_given; 116 int use_mailmap_config; 117 char *fmt_patch_subject_prefix; 118 int fmt_patch_name_max; 119 char *fmt_pretty; 120 char *default_date_mode; 121 122 #ifndef WITH_BREAKING_CHANGES 123 /* 124 * Note: git_log_config() does not touch this member and that 125 * is very deliberate. This member is only to be used to 126 * resurrect whatchanged that is deprecated. 127 */ 128 int i_still_use_this; 129 #endif 130 }; 131 132 static void log_config_init(struct log_config *cfg) 133 { 134 memset(cfg, 0, sizeof(*cfg)); 135 cfg->default_show_root = 1; 136 cfg->default_encode_email_headers = 1; 137 cfg->use_mailmap_config = 1; 138 cfg->fmt_patch_subject_prefix = xstrdup("PATCH"); 139 cfg->fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT; 140 cfg->decoration_style = auto_decoration_style(); 141 } 142 143 static void log_config_release(struct log_config *cfg) 144 { 145 free(cfg->default_date_mode); 146 free(cfg->fmt_pretty); 147 free(cfg->fmt_patch_subject_prefix); 148 } 149 150 static int use_default_decoration_filter = 1; 151 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP; 152 static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP; 153 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP; 154 155 static int clear_decorations_callback(const struct option *opt UNUSED, 156 const char *arg, int unset) 157 { 158 BUG_ON_OPT_NEG(unset); 159 BUG_ON_OPT_ARG(arg); 160 string_list_clear(&decorate_refs_include, 0); 161 string_list_clear(&decorate_refs_exclude, 0); 162 use_default_decoration_filter = 0; 163 return 0; 164 } 165 166 static int decorate_callback(const struct option *opt, const char *arg, 167 int unset) 168 { 169 struct log_config *cfg = opt->value; 170 171 if (unset) 172 cfg->decoration_style = 0; 173 else if (arg) 174 cfg->decoration_style = parse_decoration_style(arg); 175 else 176 cfg->decoration_style = DECORATE_SHORT_REFS; 177 178 if (cfg->decoration_style < 0) 179 die(_("invalid --decorate option: %s"), arg); 180 181 cfg->decoration_given = 1; 182 183 return 0; 184 } 185 186 static int log_line_range_callback(const struct option *option, const char *arg, int unset) 187 { 188 struct line_opt_callback_data *data = option->value; 189 190 BUG_ON_OPT_NEG(unset); 191 192 if (!arg) 193 return -1; 194 195 data->rev->line_level_traverse = 1; 196 string_list_append(&data->args, arg); 197 198 return 0; 199 } 200 201 static void cmd_log_init_defaults(struct rev_info *rev, 202 struct log_config *cfg) 203 { 204 if (cfg->fmt_pretty) 205 get_commit_format(cfg->fmt_pretty, rev); 206 if (cfg->default_follow) 207 rev->diffopt.flags.default_follow_renames = 1; 208 rev->verbose_header = 1; 209 init_diffstat_widths(&rev->diffopt); 210 rev->diffopt.flags.recursive = 1; 211 rev->diffopt.flags.allow_textconv = 1; 212 rev->abbrev_commit = cfg->default_abbrev_commit; 213 rev->show_root_diff = cfg->default_show_root; 214 rev->subject_prefix = cfg->fmt_patch_subject_prefix; 215 rev->patch_name_max = cfg->fmt_patch_name_max; 216 rev->show_signature = cfg->default_show_signature; 217 rev->encode_email_headers = cfg->default_encode_email_headers; 218 219 if (cfg->default_date_mode) 220 parse_date_format(cfg->default_date_mode, &rev->date_mode); 221 } 222 223 static void set_default_decoration_filter(struct decoration_filter *decoration_filter) 224 { 225 char *value = NULL; 226 struct string_list *include = decoration_filter->include_ref_pattern; 227 const struct string_list *config_exclude; 228 229 if (!repo_config_get_string_multi(the_repository, "log.excludeDecoration", 230 &config_exclude)) { 231 struct string_list_item *item; 232 for_each_string_list_item(item, config_exclude) 233 string_list_append(decoration_filter->exclude_ref_config_pattern, 234 item->string); 235 } 236 237 /* 238 * By default, decorate_all is disabled. Enable it if 239 * log.initialDecorationSet=all. Don't ever disable it by config, 240 * since the command-line takes precedent. 241 */ 242 if (use_default_decoration_filter && 243 !repo_config_get_string(the_repository, "log.initialdecorationset", &value) && 244 !strcmp("all", value)) 245 use_default_decoration_filter = 0; 246 free(value); 247 248 if (!use_default_decoration_filter || 249 decoration_filter->exclude_ref_pattern->nr || 250 decoration_filter->include_ref_pattern->nr || 251 decoration_filter->exclude_ref_config_pattern->nr) 252 return; 253 254 /* 255 * No command-line or config options were given, so 256 * populate with sensible defaults. 257 */ 258 for (size_t i = 0; i < ARRAY_SIZE(ref_namespace); i++) { 259 if (!ref_namespace[i].decoration) 260 continue; 261 262 string_list_append(include, ref_namespace[i].ref); 263 } 264 } 265 266 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, 267 struct rev_info *rev, struct setup_revision_opt *opt, 268 struct log_config *cfg) 269 { 270 struct userformat_want w; 271 int quiet = 0, source = 0, mailmap; 272 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP}; 273 struct decoration_filter decoration_filter = { 274 .exclude_ref_pattern = &decorate_refs_exclude, 275 .include_ref_pattern = &decorate_refs_include, 276 .exclude_ref_config_pattern = &decorate_refs_exclude_config, 277 }; 278 static struct revision_sources revision_sources; 279 280 const struct option builtin_log_options[] = { 281 OPT__QUIET(&quiet, N_("suppress diff output")), 282 OPT_BOOL(0, "source", &source, N_("show source")), 283 OPT_BOOL(0, "use-mailmap", &mailmap, N_("use mail map file")), 284 #ifndef WITH_BREAKING_CHANGES 285 OPT_HIDDEN_BOOL(0, "i-still-use-this", &cfg->i_still_use_this, 286 ""), 287 #endif 288 OPT_ALIAS(0, "mailmap", "use-mailmap"), 289 OPT_CALLBACK_F(0, "clear-decorations", NULL, NULL, 290 N_("clear all previously-defined decoration filters"), 291 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 292 clear_decorations_callback), 293 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include, 294 N_("pattern"), N_("only decorate refs that match ")), 295 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude, 296 N_("pattern"), N_("do not decorate refs that match ")), 297 OPT_CALLBACK_F(0, "decorate", cfg, NULL, N_("decorate options"), 298 PARSE_OPT_OPTARG, decorate_callback), 299 OPT_CALLBACK('L', NULL, &line_cb, "range:file", 300 N_("trace the evolution of line range , or function : in "), 301 log_line_range_callback), 302 OPT_END() 303 }; 304 305 line_cb.rev = rev; 306 line_cb.prefix = prefix; 307 308 mailmap = cfg->use_mailmap_config; 309 argc = parse_options(argc, argv, prefix, 310 builtin_log_options, builtin_log_usage, 311 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | 312 PARSE_OPT_KEEP_DASHDASH); 313 314 if (quiet) 315 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT; 316 argc = setup_revisions(argc, argv, rev, opt); 317 318 /* Any arguments at this point are not recognized */ 319 if (argc > 1) 320 die(_("unrecognized argument: %s"), argv[1]); 321 322 if (rev->line_level_traverse && rev->prune_data.nr) 323 die(_("-L: cannot be used with pathspec")); 324 325 memset(&w, 0, sizeof(w)); 326 userformat_find_requirements(NULL, &w); 327 328 if (!rev->show_notes_given && (!rev->pretty_given || w.notes)) 329 rev->show_notes = 1; 330 if (rev->show_notes) 331 load_display_notes(&rev->notes_opt); 332 333 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) || 334 rev->diffopt.filter || rev->diffopt.flags.follow_renames) 335 rev->always_show_header = 0; 336 337 if (source || w.source) { 338 init_revision_sources(&revision_sources); 339 rev->sources = &revision_sources; 340 } 341 342 if (mailmap) { 343 rev->mailmap = xmalloc(sizeof(struct string_list)); 344 string_list_init_nodup(rev->mailmap); 345 read_mailmap(the_repository, rev->mailmap); 346 } 347 348 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) { 349 /* 350 * "log --pretty=raw" is special; ignore UI oriented 351 * configuration variables such as decoration. 352 */ 353 if (!cfg->decoration_given) 354 cfg->decoration_style = 0; 355 if (!rev->abbrev_commit_given) 356 rev->abbrev_commit = 0; 357 } 358 359 if (rev->commit_format == CMIT_FMT_USERFORMAT) { 360 if (!w.decorate) { 361 /* 362 * Disable decoration loading if the format will not 363 * show them anyway. 364 */ 365 cfg->decoration_style = 0; 366 } else if (!cfg->decoration_style) { 367 /* 368 * If we are going to show them, make sure we do load 369 * them here, but taking care not to override a 370 * specific style set by config or --decorate. 371 */ 372 cfg->decoration_style = DECORATE_SHORT_REFS; 373 } 374 } 375 376 if (cfg->decoration_style || rev->simplify_by_decoration) { 377 set_default_decoration_filter(&decoration_filter); 378 379 if (cfg->decoration_style) 380 rev->show_decorations = 1; 381 382 load_ref_decorations(&decoration_filter, cfg->decoration_style); 383 } 384 385 if (rev->line_level_traverse) 386 line_log_init(rev, line_cb.prefix, &line_cb.args); 387 388 setup_pager(the_repository); 389 } 390 391 static void cmd_log_init(int argc, const char **argv, const char *prefix, 392 struct rev_info *rev, struct setup_revision_opt *opt, 393 struct log_config *cfg) 394 { 395 cmd_log_init_defaults(rev, cfg); 396 cmd_log_init_finish(argc, argv, prefix, rev, opt, cfg); 397 } 398 399 static int cmd_log_walk_no_free(struct rev_info *rev) 400 { 401 struct commit *commit; 402 int saved_nrl = 0; 403 int saved_dcctc = 0; 404 int result; 405 406 if (prepare_revision_walk(rev)) 407 die(_("revision walk setup failed")); 408 409 /* 410 * For --check and --exit-code, the exit code is based on CHECK_FAILED 411 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to 412 * retain that state information if replacing rev->diffopt in this loop 413 */ 414 while ((commit = get_revision(rev)) != NULL) { 415 if (!log_tree_commit(rev, commit) && rev->max_count >= 0) 416 /* 417 * We decremented max_count in get_revision, 418 * but we didn't actually show the commit. 419 */ 420 rev->max_count++; 421 if (!rev->reflog_info && !rev->remerge_diff) { 422 /* 423 * We may show a given commit multiple times when 424 * walking the reflogs. Therefore we still need it. 425 * 426 * Likewise, we potentially still need the parents 427 * of * already shown commits to determine merge 428 * bases when showing remerge diffs. 429 */ 430 free_commit_buffer(the_repository->parsed_objects, 431 commit); 432 commit_list_free(commit->parents); 433 commit->parents = NULL; 434 } 435 if (saved_nrl < rev->diffopt.needed_rename_limit) 436 saved_nrl = rev->diffopt.needed_rename_limit; 437 if (rev->diffopt.degraded_cc_to_c) 438 saved_dcctc = 1; 439 } 440 rev->diffopt.degraded_cc_to_c = saved_dcctc; 441 rev->diffopt.needed_rename_limit = saved_nrl; 442 443 result = diff_result_code(rev); 444 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && 445 rev->diffopt.flags.check_failed) { 446 result = 02; 447 } 448 return result; 449 } 450 451 static int cmd_log_walk(struct rev_info *rev) 452 { 453 int retval; 454 455 rev->diffopt.no_free = 1; 456 retval = cmd_log_walk_no_free(rev); 457 rev->diffopt.no_free = 0; 458 diff_free(&rev->diffopt); 459 return retval; 460 } 461 462 static int git_log_config(const char *var, const char *value, 463 const struct config_context *ctx, void *cb) 464 { 465 struct log_config *cfg = cb; 466 const char *slot_name; 467 468 if (!strcmp(var, "format.pretty")) { 469 FREE_AND_NULL(cfg->fmt_pretty); 470 return git_config_string(&cfg->fmt_pretty, var, value); 471 } 472 if (!strcmp(var, "format.subjectprefix")) { 473 FREE_AND_NULL(cfg->fmt_patch_subject_prefix); 474 return git_config_string(&cfg->fmt_patch_subject_prefix, var, value); 475 } 476 if (!strcmp(var, "format.filenamemaxlength")) { 477 cfg->fmt_patch_name_max = git_config_int(var, value, ctx->kvi); 478 return 0; 479 } 480 if (!strcmp(var, "format.encodeemailheaders")) { 481 cfg->default_encode_email_headers = git_config_bool(var, value); 482 return 0; 483 } 484 if (!strcmp(var, "log.abbrevcommit")) { 485 cfg->default_abbrev_commit = git_config_bool(var, value); 486 return 0; 487 } 488 if (!strcmp(var, "log.date")) { 489 FREE_AND_NULL(cfg->default_date_mode); 490 return git_config_string(&cfg->default_date_mode, var, value); 491 } 492 if (!strcmp(var, "log.decorate")) { 493 cfg->decoration_style = parse_decoration_style(value); 494 if (cfg->decoration_style < 0) 495 cfg->decoration_style = 0; /* maybe warn? */ 496 return 0; 497 } 498 if (!strcmp(var, "log.diffmerges")) { 499 if (!value) 500 return config_error_nonbool(var); 501 return diff_merges_config(value); 502 } 503 if (!strcmp(var, "log.showroot")) { 504 cfg->default_show_root = git_config_bool(var, value); 505 return 0; 506 } 507 if (!strcmp(var, "log.follow")) { 508 cfg->default_follow = git_config_bool(var, value); 509 return 0; 510 } 511 if (skip_prefix(var, "color.decorate.", &slot_name)) 512 return parse_decorate_color_config(var, slot_name, value); 513 if (!strcmp(var, "log.mailmap")) { 514 cfg->use_mailmap_config = git_config_bool(var, value); 515 return 0; 516 } 517 if (!strcmp(var, "log.showsignature")) { 518 cfg->default_show_signature = git_config_bool(var, value); 519 return 0; 520 } 521 522 return git_diff_ui_config(var, value, ctx, cb); 523 } 524 525 #ifndef WITH_BREAKING_CHANGES 526 int cmd_whatchanged(int argc, 527 const char **argv, 528 const char *prefix, 529 struct repository *repo UNUSED) 530 { 531 struct log_config cfg; 532 struct rev_info rev; 533 struct setup_revision_opt opt; 534 int ret; 535 536 log_config_init(&cfg); 537 init_diff_ui_defaults(); 538 repo_config(the_repository, git_log_config, &cfg); 539 540 repo_init_revisions(the_repository, &rev, prefix); 541 repo_config(the_repository, grep_config, &rev.grep_filter); 542 543 rev.diff = 1; 544 rev.simplify_history = 0; 545 memset(&opt, 0, sizeof(opt)); 546 opt.def = "HEAD"; 547 opt.revarg_opt = REVARG_COMMITTISH; 548 cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); 549 550 if (!cfg.i_still_use_this) 551 you_still_use_that("git whatchanged", 552 _("\n" 553 "hint: You can replace 'git whatchanged ' with:\n" 554 "hint:\tgit log --raw --no-merges\n" 555 "hint: Or make an alias:\n" 556 "hint:\tgit config set --global alias.whatchanged 'log --raw --no-merges'\n" 557 "\n")); 558 559 if (!rev.diffopt.output_format) 560 rev.diffopt.output_format = DIFF_FORMAT_RAW; 561 562 ret = cmd_log_walk(&rev); 563 564 release_revisions(&rev); 565 log_config_release(&cfg); 566 return ret; 567 } 568 #endif 569 570 static void show_tagger(const char *buf, struct rev_info *rev) 571 { 572 struct strbuf out = STRBUF_INIT; 573 struct pretty_print_context pp = {0}; 574 575 pp.fmt = rev->commit_format; 576 pp.date_mode = rev->date_mode; 577 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding()); 578 fprintf(rev->diffopt.file, "%s", out.buf); 579 strbuf_release(&out); 580 } 581 582 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name) 583 { 584 struct object_id oidc; 585 struct object_context obj_context = {0}; 586 char *buf; 587 unsigned long size; 588 589 fflush(rev->diffopt.file); 590 if (!rev->diffopt.flags.textconv_set_via_cmdline || 591 !rev->diffopt.flags.allow_textconv) 592 return odb_stream_blob_to_fd(the_repository->objects, 1, oid, NULL, 0); 593 594 if (get_oid_with_context(the_repository, obj_name, 595 GET_OID_RECORD_PATH, 596 &oidc, &obj_context)) 597 die(_("not a valid object name %s"), obj_name); 598 if (!obj_context.path || 599 !textconv_object(the_repository, obj_context.path, 600 obj_context.mode, &oidc, 1, &buf, &size)) { 601 object_context_release(&obj_context); 602 return odb_stream_blob_to_fd(the_repository->objects, 1, oid, NULL, 0); 603 } 604 605 if (!buf) 606 die(_("git show %s: bad file"), obj_name); 607 608 write_or_die(1, buf, size); 609 object_context_release(&obj_context); 610 free(buf); 611 return 0; 612 } 613 614 static int show_tag_object(const struct object_id *oid, struct rev_info *rev) 615 { 616 size_t size; 617 enum object_type type; 618 char *buf = odb_read_object(the_repository->objects, oid, &type, &size); 619 unsigned long offset = 0; 620 621 if (!buf) 622 return error(_("could not read object %s"), oid_to_hex(oid)); 623 624 assert(type == OBJ_TAG); 625 while (offset < size && buf[offset] != '\n') { 626 unsigned long new_offset = offset + 1; 627 const char *ident; 628 while (new_offset < size && buf[new_offset++] != '\n') 629 ; /* do nothing */ 630 if (skip_prefix(buf + offset, "tagger ", &ident)) 631 show_tagger(ident, rev); 632 offset = new_offset; 633 } 634 635 if (offset < size) 636 fwrite(buf + offset, size - offset, 1, rev->diffopt.file); 637 free(buf); 638 return 0; 639 } 640 641 static int show_tree_object(const struct object_id *oid UNUSED, 642 struct strbuf *base UNUSED, 643 const char *pathname, unsigned mode, 644 void *context) 645 { 646 FILE *file = context; 647 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); 648 return 0; 649 } 650 651 static void show_setup_revisions_tweak(struct rev_info *rev) 652 { 653 if (rev->first_parent_only) 654 diff_merges_default_to_first_parent(rev); 655 else 656 diff_merges_default_to_dense_combined(rev); 657 if (!rev->diffopt.output_format) 658 rev->diffopt.output_format = DIFF_FORMAT_PATCH; 659 } 660 661 int cmd_show(int argc, 662 const char **argv, 663 const char *prefix, 664 struct repository *repo UNUSED) 665 { 666 struct log_config cfg; 667 struct rev_info rev; 668 unsigned int i; 669 struct setup_revision_opt opt; 670 struct pathspec match_all; 671 int ret = 0; 672 673 log_config_init(&cfg); 674 init_diff_ui_defaults(); 675 repo_config(the_repository, git_log_config, &cfg); 676 677 if (the_repository->gitdir) { 678 prepare_repo_settings(the_repository); 679 the_repository->settings.command_requires_full_index = 0; 680 } 681 682 memset(&match_all, 0, sizeof(match_all)); 683 repo_init_revisions(the_repository, &rev, prefix); 684 repo_config(the_repository, grep_config, &rev.grep_filter); 685 686 rev.diff = 1; 687 rev.always_show_header = 1; 688 rev.no_walk = 1; 689 rev.diffopt.stat_width = -1; /* Scale to real terminal size */ 690 691 memset(&opt, 0, sizeof(opt)); 692 opt.def = "HEAD"; 693 opt.tweak = show_setup_revisions_tweak; 694 cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); 695 696 if (!rev.no_walk) { 697 ret = cmd_log_walk(&rev); 698 release_revisions(&rev); 699 log_config_release(&cfg); 700 return ret; 701 } 702 703 rev.diffopt.no_free = 1; 704 for (i = 0; i < rev.pending.nr && !ret; i++) { 705 struct object *o = rev.pending.objects[i].item; 706 const char *name = rev.pending.objects[i].name; 707 switch (o->type) { 708 case OBJ_BLOB: 709 ret = show_blob_object(&o->oid, &rev, name); 710 break; 711 case OBJ_TAG: { 712 struct tag *t = (struct tag *)o; 713 struct object_id *oid = get_tagged_oid(t); 714 715 if (rev.shown_one) 716 putchar('\n'); 717 fprintf(rev.diffopt.file, "%stag %s%s\n", 718 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), 719 t->tag, 720 diff_get_color_opt(&rev.diffopt, DIFF_RESET)); 721 ret = show_tag_object(&o->oid, &rev); 722 rev.shown_one = 1; 723 if (ret) 724 break; 725 o = parse_object(the_repository, oid); 726 if (!o) 727 ret = error(_("could not read object %s"), 728 oid_to_hex(oid)); 729 rev.pending.objects[i].item = o; 730 i--; 731 break; 732 } 733 case OBJ_TREE: 734 if (rev.shown_one) 735 putchar('\n'); 736 fprintf(rev.diffopt.file, "%stree %s%s\n\n", 737 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), 738 name, 739 diff_get_color_opt(&rev.diffopt, DIFF_RESET)); 740 read_tree(the_repository, (struct tree *)o, 741 &match_all, show_tree_object, 742 rev.diffopt.file); 743 rev.shown_one = 1; 744 break; 745 case OBJ_COMMIT: 746 { 747 struct object_array old; 748 struct object_array blank = OBJECT_ARRAY_INIT; 749 750 memcpy(&old, &rev.pending, sizeof(old)); 751 memcpy(&rev.pending, &blank, sizeof(rev.pending)); 752 753 add_object_array(o, name, &rev.pending); 754 ret = cmd_log_walk_no_free(&rev); 755 756 /* 757 * No need for 758 * object_array_clear(&pending). It was 759 * cleared already in prepare_revision_walk() 760 */ 761 memcpy(&rev.pending, &old, sizeof(rev.pending)); 762 break; 763 } 764 default: 765 ret = error(_("unknown type: %d"), o->type); 766 } 767 } 768 769 rev.diffopt.no_free = 0; 770 diff_free(&rev.diffopt); 771 release_revisions(&rev); 772 log_config_release(&cfg); 773 774 return ret; 775 } 776 777 /* 778 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" 779 */ 780 int cmd_log_reflog(int argc, 781 const char **argv, 782 const char *prefix, 783 struct repository *repo UNUSED) 784 { 785 struct log_config cfg; 786 struct rev_info rev; 787 struct setup_revision_opt opt; 788 int ret; 789 790 log_config_init(&cfg); 791 init_diff_ui_defaults(); 792 repo_config(the_repository, git_log_config, &cfg); 793 794 repo_init_revisions(the_repository, &rev, prefix); 795 init_reflog_walk(&rev.reflog_info); 796 repo_config(the_repository, grep_config, &rev.grep_filter); 797 798 rev.verbose_header = 1; 799 memset(&opt, 0, sizeof(opt)); 800 opt.def = "HEAD"; 801 cmd_log_init_defaults(&rev, &cfg); 802 rev.abbrev_commit = 1; 803 rev.commit_format = CMIT_FMT_ONELINE; 804 rev.use_terminator = 1; 805 rev.always_show_header = 1; 806 cmd_log_init_finish(argc, argv, prefix, &rev, &opt, &cfg); 807 808 ret = cmd_log_walk(&rev); 809 810 release_revisions(&rev); 811 log_config_release(&cfg); 812 return ret; 813 } 814 815 static void log_setup_revisions_tweak(struct rev_info *rev) 816 { 817 if (rev->diffopt.flags.default_follow_renames && 818 diff_check_follow_pathspec(&rev->prune_data, 0)) 819 rev->diffopt.flags.follow_renames = 1; 820 821 if (rev->first_parent_only) 822 diff_merges_default_to_first_parent(rev); 823 } 824 825 int cmd_log(int argc, 826 const char **argv, 827 const char *prefix, 828 struct repository *repo UNUSED) 829 { 830 struct log_config cfg; 831 struct rev_info rev; 832 struct setup_revision_opt opt; 833 int ret; 834 835 log_config_init(&cfg); 836 init_diff_ui_defaults(); 837 repo_config(the_repository, git_log_config, &cfg); 838 839 repo_init_revisions(the_repository, &rev, prefix); 840 repo_config(the_repository, grep_config, &rev.grep_filter); 841 842 rev.always_show_header = 1; 843 memset(&opt, 0, sizeof(opt)); 844 opt.def = "HEAD"; 845 opt.revarg_opt = REVARG_COMMITTISH; 846 opt.tweak = log_setup_revisions_tweak; 847 cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); 848 849 ret = cmd_log_walk(&rev); 850 851 release_revisions(&rev); 852 log_config_release(&cfg); 853 return ret; 854 } 855 856 /* format-patch */ 857 858 enum cover_setting { 859 COVER_UNSET, 860 COVER_OFF, 861 COVER_ON, 862 COVER_AUTO 863 }; 864 865 enum thread_level { 866 THREAD_UNSET, 867 THREAD_SHALLOW, 868 THREAD_DEEP 869 }; 870 871 enum cover_from_description { 872 COVER_FROM_NONE, 873 COVER_FROM_MESSAGE, 874 COVER_FROM_SUBJECT, 875 COVER_FROM_AUTO 876 }; 877 878 enum auto_base_setting { 879 AUTO_BASE_NEVER, 880 AUTO_BASE_ALWAYS, 881 AUTO_BASE_WHEN_ABLE 882 }; 883 884 struct format_config { 885 struct log_config log; 886 enum thread_level thread; 887 int do_signoff; 888 enum auto_base_setting auto_base; 889 char *base_commit; 890 char *from; 891 char *signature; 892 char *signature_file; 893 enum cover_setting config_cover_letter; 894 char *fmt_cover_letter_commit_list; 895 char *config_output_directory; 896 enum cover_from_description cover_from_description_mode; 897 int show_notes; 898 struct display_notes_opt notes_opt; 899 int numbered_cmdline_opt; 900 int numbered; 901 int auto_number; 902 char *default_attach; 903 struct string_list extra_hdr; 904 struct string_list extra_to; 905 struct string_list extra_cc; 906 int keep_subject; 907 int subject_prefix; 908 struct strbuf sprefix; 909 char *fmt_patch_suffix; 910 }; 911 912 static void format_config_init(struct format_config *cfg) 913 { 914 memset(cfg, 0, sizeof(*cfg)); 915 log_config_init(&cfg->log); 916 cfg->cover_from_description_mode = COVER_FROM_MESSAGE; 917 cfg->auto_number = 1; 918 string_list_init_dup(&cfg->extra_hdr); 919 string_list_init_dup(&cfg->extra_to); 920 string_list_init_dup(&cfg->extra_cc); 921 strbuf_init(&cfg->sprefix, 0); 922 cfg->fmt_patch_suffix = xstrdup(".patch"); 923 } 924 925 static void format_config_release(struct format_config *cfg) 926 { 927 log_config_release(&cfg->log); 928 free(cfg->base_commit); 929 free(cfg->from); 930 free(cfg->signature); 931 free(cfg->signature_file); 932 free(cfg->config_output_directory); 933 free(cfg->default_attach); 934 string_list_clear(&cfg->extra_hdr, 0); 935 string_list_clear(&cfg->extra_to, 0); 936 string_list_clear(&cfg->extra_cc, 0); 937 strbuf_release(&cfg->sprefix); 938 free(cfg->fmt_patch_suffix); 939 free(cfg->fmt_cover_letter_commit_list); 940 } 941 942 static enum cover_from_description parse_cover_from_description(const char *arg) 943 { 944 if (!arg || !strcmp(arg, "default")) 945 return COVER_FROM_MESSAGE; 946 else if (!strcmp(arg, "none")) 947 return COVER_FROM_NONE; 948 else if (!strcmp(arg, "message")) 949 return COVER_FROM_MESSAGE; 950 else if (!strcmp(arg, "subject")) 951 return COVER_FROM_SUBJECT; 952 else if (!strcmp(arg, "auto")) 953 return COVER_FROM_AUTO; 954 else 955 die(_("%s: invalid cover from description mode"), arg); 956 } 957 958 static void add_header(struct format_config *cfg, const char *value) 959 { 960 struct string_list_item *item; 961 int len = strlen(value); 962 while (len && value[len - 1] == '\n') 963 len--; 964 965 if (!strncasecmp(value, "to: ", 4)) { 966 item = string_list_append(&cfg->extra_to, value + 4); 967 len -= 4; 968 } else if (!strncasecmp(value, "cc: ", 4)) { 969 item = string_list_append(&cfg->extra_cc, value + 4); 970 len -= 4; 971 } else { 972 item = string_list_append(&cfg->extra_hdr, value); 973 } 974 975 item->string[len] = '\0'; 976 } 977 978 static int git_format_config(const char *var, const char *value, 979 const struct config_context *ctx, void *cb) 980 { 981 struct format_config *cfg = cb; 982 983 if (!strcmp(var, "format.headers")) { 984 if (!value) 985 die(_("format.headers without value")); 986 add_header(cfg, value); 987 return 0; 988 } 989 if (!strcmp(var, "format.suffix")) { 990 FREE_AND_NULL(cfg->fmt_patch_suffix); 991 return git_config_string(&cfg->fmt_patch_suffix, var, value); 992 } 993 if (!strcmp(var, "format.to")) { 994 if (!value) 995 return config_error_nonbool(var); 996 string_list_append(&cfg->extra_to, value); 997 return 0; 998 } 999 if (!strcmp(var, "format.cc")) { 1000 if (!value) 1001 return config_error_nonbool(var); 1002 string_list_append(&cfg->extra_cc, value); 1003 return 0; 1004 } 1005 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") || 1006 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) { 1007 return 0; 1008 } 1009 if (!strcmp(var, "format.numbered")) { 1010 if (value && !strcasecmp(value, "auto")) { 1011 cfg->auto_number = 1; 1012 return 0; 1013 } 1014 cfg->numbered = git_config_bool(var, value); 1015 cfg->auto_number = cfg->auto_number && cfg->numbered; 1016 return 0; 1017 } 1018 if (!strcmp(var, "format.attach")) { 1019 if (value && *value) { 1020 FREE_AND_NULL(cfg->default_attach); 1021 cfg->default_attach = xstrdup(value); 1022 } else if (value && !*value) { 1023 FREE_AND_NULL(cfg->default_attach); 1024 } else { 1025 FREE_AND_NULL(cfg->default_attach); 1026 cfg->default_attach = xstrdup(git_version_string); 1027 } 1028 return 0; 1029 } 1030 if (!strcmp(var, "format.thread")) { 1031 if (value && !strcasecmp(value, "deep")) { 1032 cfg->thread = THREAD_DEEP; 1033 return 0; 1034 } 1035 if (value && !strcasecmp(value, "shallow")) { 1036 cfg->thread = THREAD_SHALLOW; 1037 return 0; 1038 } 1039 cfg->thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET; 1040 return 0; 1041 } 1042 if (!strcmp(var, "format.signoff")) { 1043 cfg->do_signoff = git_config_bool(var, value); 1044 return 0; 1045 } 1046 if (!strcmp(var, "format.signature")) { 1047 FREE_AND_NULL(cfg->signature); 1048 return git_config_string(&cfg->signature, var, value); 1049 } 1050 if (!strcmp(var, "format.signaturefile")) { 1051 FREE_AND_NULL(cfg->signature_file); 1052 return git_config_pathname(&cfg->signature_file, var, value); 1053 } 1054 if (!strcmp(var, "format.coverletter")) { 1055 if (value && !strcasecmp(value, "auto")) { 1056 cfg->config_cover_letter = COVER_AUTO; 1057 return 0; 1058 } 1059 cfg->config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF; 1060 return 0; 1061 } 1062 if (!strcmp(var, "format.commitlistformat")) { 1063 FREE_AND_NULL(cfg->fmt_cover_letter_commit_list); 1064 return git_config_string(&cfg->fmt_cover_letter_commit_list, var, value); 1065 } 1066 if (!strcmp(var, "format.outputdirectory")) { 1067 FREE_AND_NULL(cfg->config_output_directory); 1068 return git_config_string(&cfg->config_output_directory, var, value); 1069 } 1070 if (!strcmp(var, "format.useautobase")) { 1071 if (value && !strcasecmp(value, "whenAble")) { 1072 cfg->auto_base = AUTO_BASE_WHEN_ABLE; 1073 return 0; 1074 } 1075 cfg->auto_base = git_config_bool(var, value) ? AUTO_BASE_ALWAYS : AUTO_BASE_NEVER; 1076 return 0; 1077 } 1078 if (!strcmp(var, "format.from")) { 1079 int b = git_parse_maybe_bool(value); 1080 FREE_AND_NULL(cfg->from); 1081 if (b < 0) 1082 cfg->from = xstrdup(value); 1083 else if (b) 1084 cfg->from = xstrdup(git_committer_info(IDENT_NO_DATE)); 1085 return 0; 1086 } 1087 if (!strcmp(var, "format.forceinbodyfrom")) { 1088 force_in_body_from = git_config_bool(var, value); 1089 return 0; 1090 } 1091 if (!strcmp(var, "format.notes")) { 1092 int b = git_parse_maybe_bool(value); 1093 if (b < 0) 1094 enable_ref_display_notes(&cfg->notes_opt, &cfg->show_notes, value); 1095 else if (b) 1096 enable_default_display_notes(&cfg->notes_opt, &cfg->show_notes); 1097 else 1098 disable_display_notes(&cfg->notes_opt, &cfg->show_notes); 1099 return 0; 1100 } 1101 if (!strcmp(var, "format.coverfromdescription")) { 1102 cfg->cover_from_description_mode = parse_cover_from_description(value); 1103 return 0; 1104 } 1105 if (!strcmp(var, "format.mboxrd")) { 1106 stdout_mboxrd = git_config_bool(var, value); 1107 return 0; 1108 } 1109 if (!strcmp(var, "format.noprefix")) { 1110 format_no_prefix = git_parse_maybe_bool(value); 1111 if (format_no_prefix < 0) { 1112 int status = die_message( 1113 _("bad boolean config value '%s' for '%s'"), 1114 value, var); 1115 advise(_("'%s' used to accept any value and " 1116 "treat that as 'true'.\n" 1117 "Now it only accepts boolean values, " 1118 "like what '%s' does.\n"), 1119 var, "diff.noprefix"); 1120 exit(status); 1121 } 1122 return 0; 1123 } 1124 1125 /* 1126 * ignore some porcelain config which would otherwise be parsed by 1127 * git_diff_ui_config(), via git_log_config(); we can't just avoid 1128 * diff_ui_config completely, because we do care about some ui options 1129 * like color. 1130 */ 1131 if (!strcmp(var, "diff.noprefix")) 1132 return 0; 1133 1134 return git_log_config(var, value, ctx, &cfg->log); 1135 } 1136 1137 static const char *output_directory = NULL; 1138 static int outdir_offset; 1139 1140 static int open_next_file(struct commit *commit, const char *subject, 1141 struct rev_info *rev, int quiet) 1142 { 1143 struct strbuf filename = STRBUF_INIT; 1144 1145 if (output_directory) { 1146 strbuf_addstr(&filename, output_directory); 1147 strbuf_complete(&filename, '/'); 1148 } 1149 1150 if (rev->numbered_files) 1151 strbuf_addf(&filename, "%d", rev->nr); 1152 else if (commit) 1153 fmt_output_commit(&filename, commit, rev); 1154 else 1155 fmt_output_subject(&filename, subject, rev); 1156 1157 if (!quiet) 1158 printf("%s\n", filename.buf + outdir_offset); 1159 1160 if (!(rev->diffopt.file = fopen(filename.buf, "w"))) { 1161 error_errno(_("cannot open patch file %s"), filename.buf); 1162 strbuf_release(&filename); 1163 return -1; 1164 } 1165 1166 strbuf_release(&filename); 1167 return 0; 1168 } 1169 1170 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids) 1171 { 1172 struct rev_info check_rev; 1173 struct commit *commit, *c1, *c2; 1174 struct object *o1, *o2; 1175 unsigned flags1, flags2; 1176 1177 if (rev->pending.nr != 2) 1178 die(_("need exactly one range")); 1179 1180 o1 = rev->pending.objects[0].item; 1181 o2 = rev->pending.objects[1].item; 1182 flags1 = o1->flags; 1183 flags2 = o2->flags; 1184 c1 = lookup_commit_reference(the_repository, &o1->oid); 1185 c2 = lookup_commit_reference(the_repository, &o2->oid); 1186 1187 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) 1188 die(_("not a range")); 1189 1190 init_patch_ids(the_repository, ids); 1191 1192 /* given a range a..b get all patch ids for b..a */ 1193 repo_init_revisions(the_repository, &check_rev, rev->prefix); 1194 check_rev.max_parents = 1; 1195 o1->flags ^= UNINTERESTING; 1196 o2->flags ^= UNINTERESTING; 1197 add_pending_object(&check_rev, o1, "o1"); 1198 add_pending_object(&check_rev, o2, "o2"); 1199 if (prepare_revision_walk(&check_rev)) 1200 die(_("revision walk setup failed")); 1201 1202 while ((commit = get_revision(&check_rev)) != NULL) { 1203 add_commit_patch_id(commit, ids); 1204 } 1205 1206 /* reset for next revision walk */ 1207 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED); 1208 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED); 1209 o1->flags = flags1; 1210 o2->flags = flags2; 1211 } 1212 1213 static void gen_message_id(struct rev_info *info, const char *base) 1214 { 1215 struct strbuf buf = STRBUF_INIT; 1216 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base, 1217 (timestamp_t) time(NULL), 1218 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT)); 1219 info->message_id = strbuf_detach(&buf, NULL); 1220 } 1221 1222 static void print_signature(const char *signature, FILE *file) 1223 { 1224 if (!signature || !*signature) 1225 return; 1226 1227 fprintf(file, "-- \n%s", signature); 1228 if (signature[strlen(signature)-1] != '\n') 1229 putc('\n', file); 1230 putc('\n', file); 1231 } 1232 1233 static char *find_branch_name(struct rev_info *rev) 1234 { 1235 struct object_id branch_oid; 1236 const struct object_id *tip_oid; 1237 const char *ref, *v; 1238 char *full_ref, *branch = NULL; 1239 int interesting_found = 0; 1240 size_t idx; 1241 1242 for (size_t i = 0; i < rev->cmdline.nr; i++) { 1243 if (rev->cmdline.rev[i].flags & UNINTERESTING) 1244 continue; 1245 if (interesting_found) 1246 return NULL; 1247 interesting_found = 1; 1248 idx = i; 1249 } 1250 if (!interesting_found) 1251 return NULL; 1252 ref = rev->cmdline.rev[idx].name; 1253 tip_oid = &rev->cmdline.rev[idx].item->oid; 1254 if (repo_dwim_ref(the_repository, ref, strlen(ref), &branch_oid, 1255 &full_ref, 0) && 1256 skip_prefix(full_ref, "refs/heads/", &v) && 1257 oideq(tip_oid, &branch_oid)) 1258 branch = xstrdup(v); 1259 free(full_ref); 1260 return branch; 1261 } 1262 1263 static void show_diffstat(struct rev_info *rev, 1264 struct commit *origin, struct commit *head) 1265 { 1266 struct diff_options opts; 1267 1268 memcpy(&opts, &rev->diffopt, sizeof(opts)); 1269 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; 1270 diff_setup_done(&opts); 1271 1272 diff_tree_oid(get_commit_tree_oid(origin), 1273 get_commit_tree_oid(head), 1274 "", &opts); 1275 diffcore_std(&opts); 1276 diff_flush(&opts); 1277 1278 fprintf(rev->diffopt.file, "\n"); 1279 } 1280 1281 static void read_desc_file(struct strbuf *buf, const char *desc_file) 1282 { 1283 if (strbuf_read_file(buf, desc_file, 0) < 0) 1284 die_errno(_("unable to read branch description file '%s'"), 1285 desc_file); 1286 } 1287 1288 static void prepare_cover_text(struct pretty_print_context *pp, 1289 const char *description_file, 1290 const char *branch_name, 1291 struct strbuf *sb, 1292 const char *encoding, 1293 int need_8bit_cte, 1294 const struct format_config *cfg) 1295 { 1296 const char *subject = "*** SUBJECT HERE ***"; 1297 const char *body = "*** BLURB HERE ***"; 1298 struct strbuf description_sb = STRBUF_INIT; 1299 struct strbuf subject_sb = STRBUF_INIT; 1300 1301 if (cfg->cover_from_description_mode == COVER_FROM_NONE) 1302 goto do_pp; 1303 1304 if (description_file && *description_file) 1305 read_desc_file(&description_sb, description_file); 1306 else if (branch_name && *branch_name) 1307 read_branch_desc(&description_sb, branch_name); 1308 if (!description_sb.len) 1309 goto do_pp; 1310 1311 if (cfg->cover_from_description_mode == COVER_FROM_SUBJECT || 1312 cfg->cover_from_description_mode == COVER_FROM_AUTO) 1313 body = format_subject(&subject_sb, description_sb.buf, " "); 1314 1315 if (cfg->cover_from_description_mode == COVER_FROM_MESSAGE || 1316 (cfg->cover_from_description_mode == COVER_FROM_AUTO && 1317 subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN)) 1318 body = description_sb.buf; 1319 else 1320 subject = subject_sb.buf; 1321 1322 do_pp: 1323 pp_email_subject(pp, &subject, sb, encoding, need_8bit_cte); 1324 pp_remainder(pp, &body, sb, 0); 1325 1326 strbuf_release(&description_sb); 1327 strbuf_release(&subject_sb); 1328 } 1329 1330 struct rdiff_notes { 1331 /* 1332 * True if we want to override the notes behavior 1333 * of 'format-patch' 1334 */ 1335 bool override; 1336 struct string_list notes; 1337 }; 1338 1339 static int rdiff_notes_cb(const struct option *option, 1340 const char *arg, 1341 int unset) 1342 { 1343 struct option opt = *option; 1344 struct rdiff_notes *rdiff_notes = option->value; 1345 1346 rdiff_notes->override = 1; 1347 opt.value = &rdiff_notes->notes; 1348 return parse_opt_string_list(&opt, arg, unset); 1349 } 1350 1351 static int get_notes_refs(struct string_list_item *item, void *arg) 1352 { 1353 strvec_pushf(arg, "--notes=%s", item->string); 1354 return 0; 1355 } 1356 1357 static void get_notes_args(struct rdiff_notes *rdiff_notes, 1358 struct rev_info *rev) 1359 { 1360 if (rdiff_notes->override) { 1361 if (rdiff_notes->notes.nr) 1362 for_each_string_list(&rdiff_notes->notes, 1363 get_notes_refs, 1364 &rev->rdiff_log_arg); 1365 else 1366 strvec_push(&rev->rdiff_log_arg, "--no-notes"); 1367 } else if (!rev->show_notes) { 1368 strvec_push(&rev->rdiff_log_arg, "--no-notes"); 1369 } else if (rev->notes_opt.use_default_notes > 0 || 1370 (rev->notes_opt.use_default_notes == -1 && 1371 !rev->notes_opt.extra_notes_refs.nr)) { 1372 strvec_push(&rev->rdiff_log_arg, "--notes"); 1373 } else { 1374 for_each_string_list(&rev->notes_opt.extra_notes_refs, 1375 get_notes_refs, 1376 &rev->rdiff_log_arg); 1377 } 1378 } 1379 1380 static void generate_shortlog_cover_letter(struct shortlog *log, 1381 struct rev_info *rev, 1382 struct commit **list, 1383 int nr) 1384 { 1385 shortlog_init(log); 1386 log->wrap_lines = 1; 1387 log->wrap = MAIL_DEFAULT_WRAP; 1388 log->in1 = 2; 1389 log->in2 = 4; 1390 log->file = rev->diffopt.file; 1391 log->groups = SHORTLOG_GROUP_AUTHOR; 1392 shortlog_finish_setup(log); 1393 for (int i = 0; i < nr; i++) 1394 shortlog_add_commit(log, list[i]); 1395 1396 shortlog_output(log); 1397 } 1398 1399 static void generate_commit_list_cover(FILE *cover_file, const char *format, 1400 struct commit **list, int n) 1401 { 1402 struct strbuf commit_line = STRBUF_INIT; 1403 struct pretty_print_context ctx = {0}; 1404 struct rev_info rev = REV_INFO_INIT; 1405 1406 rev.total = n; 1407 ctx.rev = &rev; 1408 for (int i = 1; i <= n; i++) { 1409 rev.nr = i; 1410 repo_format_commit_message(the_repository, list[n - i], format, 1411 &commit_line, &ctx); 1412 fprintf(cover_file, "%s\n", commit_line.buf); 1413 strbuf_reset(&commit_line); 1414 } 1415 fprintf(cover_file, "\n"); 1416 1417 strbuf_release(&commit_line); 1418 } 1419 1420 static void make_cover_letter(struct rev_info *rev, int use_separate_file, 1421 struct commit *origin, 1422 int nr, struct commit **list, 1423 const char *description_file, 1424 const char *branch_name, 1425 int quiet, 1426 const struct format_config *cfg, 1427 const char *format) 1428 { 1429 const char *from; 1430 struct shortlog log; 1431 struct strbuf sb = STRBUF_INIT; 1432 int i; 1433 const char *encoding = "UTF-8"; 1434 int need_8bit_cte = 0; 1435 struct pretty_print_context pp = {0}; 1436 struct commit *head = list[0]; 1437 char *to_free = NULL; 1438 1439 if (!cmit_fmt_is_mail(rev->commit_format)) 1440 die(_("cover letter needs email format")); 1441 1442 from = cfg->from ? cfg->from : git_committer_info(0); 1443 1444 if (use_separate_file && 1445 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet)) 1446 die(_("failed to create cover-letter file")); 1447 1448 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0); 1449 1450 for (i = 0; !need_8bit_cte && i < nr; i++) { 1451 const char *buf = repo_get_commit_buffer(the_repository, 1452 list[i], NULL); 1453 if (has_non_ascii(buf)) 1454 need_8bit_cte = 1; 1455 repo_unuse_commit_buffer(the_repository, list[i], buf); 1456 } 1457 1458 if (!branch_name) 1459 branch_name = to_free = find_branch_name(rev); 1460 1461 pp.fmt = CMIT_FMT_EMAIL; 1462 pp.date_mode.type = DATE_RFC2822; 1463 pp.rev = rev; 1464 pp.encode_email_headers = rev->encode_email_headers; 1465 pp_user_info(&pp, NULL, &sb, from, encoding); 1466 prepare_cover_text(&pp, description_file, branch_name, &sb, 1467 encoding, need_8bit_cte, cfg); 1468 fprintf(rev->diffopt.file, "%s\n", sb.buf); 1469 1470 free(to_free); 1471 free(pp.after_subject); 1472 strbuf_release(&sb); 1473 1474 if (skip_prefix(format, "log:", &format)) 1475 generate_commit_list_cover(rev->diffopt.file, format, list, nr); 1476 else if (!strcmp(format, "shortlog")) 1477 generate_shortlog_cover_letter(&log, rev, list, nr); 1478 else if (!strcmp(format, "modern")) 1479 generate_commit_list_cover(rev->diffopt.file, "%w(72)[%(count)/%(total)] %s", 1480 list, nr); 1481 else if (strchr(format, '%')) 1482 generate_commit_list_cover(rev->diffopt.file, format, list, nr); 1483 else 1484 die(_("'%s' is not a valid format string"), format); 1485 1486 /* We can only do diffstat with a unique reference point */ 1487 if (origin) 1488 show_diffstat(rev, origin, head); 1489 1490 if (rev->idiff_oid1) { 1491 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title); 1492 show_interdiff(rev->idiff_oid1, rev->idiff_oid2, 0, 1493 &rev->diffopt); 1494 } 1495 1496 if (rev->rdiff1) { 1497 /* 1498 * Pass minimum required diff-options to range-diff; others 1499 * can be added later if deemed desirable. 1500 */ 1501 struct diff_options opts; 1502 struct range_diff_options range_diff_opts = { 1503 .creation_factor = rev->creation_factor, 1504 .dual_color = 1, 1505 .max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT, 1506 .diffopt = &opts, 1507 .log_arg = &rev->rdiff_log_arg 1508 }; 1509 1510 repo_diff_setup(the_repository, &opts); 1511 opts.file = rev->diffopt.file; 1512 opts.use_color = rev->diffopt.use_color; 1513 diff_setup_done(&opts); 1514 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title); 1515 show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts); 1516 } 1517 } 1518 1519 static char *clean_message_id(const char *msg_id) 1520 { 1521 char ch; 1522 const char *a, *z, *m; 1523 1524 m = msg_id; 1525 while ((ch = *m) && (isspace(ch) || (ch == '<'))) 1526 m++; 1527 a = m; 1528 z = NULL; 1529 while ((ch = *m)) { 1530 if (!isspace(ch) && (ch != '>')) 1531 z = m; 1532 m++; 1533 } 1534 if (!z) 1535 die(_("insane in-reply-to: %s"), msg_id); 1536 if (++z == m) 1537 return xstrdup(a); 1538 return xmemdupz(a, z - a); 1539 } 1540 1541 static const char *set_outdir(const char *prefix, const char *output_directory) 1542 { 1543 if (output_directory && is_absolute_path(output_directory)) 1544 return output_directory; 1545 1546 if (!prefix || !*prefix) { 1547 if (output_directory) 1548 return output_directory; 1549 /* The user did not explicitly ask for "./" */ 1550 outdir_offset = 2; 1551 return "./"; 1552 } 1553 1554 outdir_offset = strlen(prefix); 1555 if (!output_directory) 1556 return prefix; 1557 1558 return prefix_filename(prefix, output_directory); 1559 } 1560 1561 static const char * const builtin_format_patch_usage[] = { 1562 N_("git format-patch [] [ | ]"), 1563 NULL 1564 }; 1565 1566 struct keep_callback_data { 1567 struct format_config *cfg; 1568 struct rev_info *revs; 1569 }; 1570 1571 static int keep_callback(const struct option *opt, const char *arg, int unset) 1572 { 1573 struct keep_callback_data *data = opt->value; 1574 BUG_ON_OPT_NEG(unset); 1575 BUG_ON_OPT_ARG(arg); 1576 data->revs->total = -1; 1577 data->cfg->keep_subject = 1; 1578 return 0; 1579 } 1580 1581 static int subject_prefix_callback(const struct option *opt, const char *arg, 1582 int unset) 1583 { 1584 struct format_config *cfg = opt->value; 1585 1586 BUG_ON_OPT_NEG(unset); 1587 cfg->subject_prefix = 1; 1588 strbuf_reset(&cfg->sprefix); 1589 strbuf_addstr(&cfg->sprefix, arg); 1590 return 0; 1591 } 1592 1593 static int rfc_callback(const struct option *opt, const char *arg, 1594 int unset) 1595 { 1596 const char **rfc = opt->value; 1597 1598 *rfc = opt->value; 1599 if (unset) 1600 *rfc = NULL; 1601 else 1602 *rfc = arg ? arg : "RFC"; 1603 return 0; 1604 } 1605 1606 static int numbered_callback(const struct option *opt, const char *arg, 1607 int unset) 1608 { 1609 struct format_config *cfg = opt->value; 1610 BUG_ON_OPT_ARG(arg); 1611 cfg->numbered = cfg->numbered_cmdline_opt = unset ? 0 : 1; 1612 if (unset) 1613 cfg->auto_number = 0; 1614 return 0; 1615 } 1616 1617 static int no_numbered_callback(const struct option *opt, const char *arg, 1618 int unset) 1619 { 1620 BUG_ON_OPT_NEG(unset); 1621 return numbered_callback(opt, arg, 1); 1622 } 1623 1624 static int output_directory_callback(const struct option *opt, const char *arg, 1625 int unset) 1626 { 1627 const char **dir = (const char **)opt->value; 1628 BUG_ON_OPT_NEG(unset); 1629 if (*dir) 1630 die(_("two output directories?")); 1631 *dir = arg; 1632 return 0; 1633 } 1634 1635 static int thread_callback(const struct option *opt, const char *arg, int unset) 1636 { 1637 struct format_config *cfg = opt->value; 1638 1639 if (unset) 1640 cfg->thread = THREAD_UNSET; 1641 else if (!arg || !strcmp(arg, "shallow")) 1642 cfg->thread = THREAD_SHALLOW; 1643 else if (!strcmp(arg, "deep")) 1644 cfg->thread = THREAD_DEEP; 1645 /* 1646 * Please update _git_formatpatch() in git-completion.bash 1647 * when you add new options. 1648 */ 1649 else 1650 return 1; 1651 return 0; 1652 } 1653 1654 static int attach_callback(const struct option *opt, const char *arg, int unset) 1655 { 1656 struct rev_info *rev = (struct rev_info *)opt->value; 1657 if (unset) 1658 rev->mime_boundary = NULL; 1659 else if (arg) 1660 rev->mime_boundary = arg; 1661 else 1662 rev->mime_boundary = git_version_string; 1663 rev->no_inline = unset ? 0 : 1; 1664 return 0; 1665 } 1666 1667 static int inline_callback(const struct option *opt, const char *arg, int unset) 1668 { 1669 struct rev_info *rev = (struct rev_info *)opt->value; 1670 if (unset) 1671 rev->mime_boundary = NULL; 1672 else if (arg) 1673 rev->mime_boundary = arg; 1674 else 1675 rev->mime_boundary = git_version_string; 1676 rev->no_inline = 0; 1677 return 0; 1678 } 1679 1680 static int header_callback(const struct option *opt, const char *arg, 1681 int unset) 1682 { 1683 struct format_config *cfg = opt->value; 1684 1685 if (unset) { 1686 string_list_clear(&cfg->extra_hdr, 0); 1687 string_list_clear(&cfg->extra_to, 0); 1688 string_list_clear(&cfg->extra_cc, 0); 1689 } else { 1690 add_header(cfg, arg); 1691 } 1692 return 0; 1693 } 1694 1695 static int from_callback(const struct option *opt, const char *arg, int unset) 1696 { 1697 char **from = opt->value; 1698 1699 free(*from); 1700 1701 if (unset) 1702 *from = NULL; 1703 else if (arg) 1704 *from = xstrdup(arg); 1705 else 1706 *from = xstrdup(git_committer_info(IDENT_NO_DATE)); 1707 return 0; 1708 } 1709 1710 static int base_callback(const struct option *opt, const char *arg, int unset) 1711 { 1712 struct format_config *cfg = opt->value; 1713 1714 if (unset) { 1715 cfg->auto_base = AUTO_BASE_NEVER; 1716 FREE_AND_NULL(cfg->base_commit); 1717 } else if (!strcmp(arg, "auto")) { 1718 cfg->auto_base = AUTO_BASE_ALWAYS; 1719 FREE_AND_NULL(cfg->base_commit); 1720 } else { 1721 cfg->auto_base = AUTO_BASE_NEVER; 1722 cfg->base_commit = xstrdup(arg); 1723 } 1724 return 0; 1725 } 1726 1727 struct base_tree_info { 1728 struct object_id base_commit; 1729 int nr_patch_id, alloc_patch_id; 1730 struct object_id *patch_id; 1731 }; 1732 1733 static struct commit *get_base_commit(const struct format_config *cfg, 1734 struct commit **list, 1735 size_t total) 1736 { 1737 struct commit *base = NULL; 1738 struct commit **rev; 1739 int auto_select, die_on_failure, ret; 1740 size_t i = 0, rev_nr = 0; 1741 1742 switch (cfg->auto_base) { 1743 case AUTO_BASE_NEVER: 1744 if (cfg->base_commit) { 1745 auto_select = 0; 1746 die_on_failure = 1; 1747 } else { 1748 /* no base information is requested */ 1749 return NULL; 1750 } 1751 break; 1752 case AUTO_BASE_ALWAYS: 1753 case AUTO_BASE_WHEN_ABLE: 1754 if (cfg->base_commit) { 1755 BUG("requested automatic base selection but a commit was provided"); 1756 } else { 1757 auto_select = 1; 1758 die_on_failure = cfg->auto_base == AUTO_BASE_ALWAYS; 1759 } 1760 break; 1761 default: 1762 BUG("unexpected automatic base selection method"); 1763 } 1764 1765 if (!auto_select) { 1766 base = lookup_commit_reference_by_name(cfg->base_commit); 1767 if (!base) 1768 die(_("unknown commit %s"), cfg->base_commit); 1769 } else { 1770 struct branch *curr_branch = branch_get(NULL); 1771 const char *upstream = branch_get_upstream(curr_branch, NULL); 1772 if (upstream) { 1773 struct commit_list *base_list = NULL; 1774 struct commit *commit; 1775 struct object_id oid; 1776 1777 if (repo_get_oid(the_repository, upstream, &oid)) { 1778 if (die_on_failure) 1779 die(_("failed to resolve '%s' as a valid ref"), upstream); 1780 else 1781 return NULL; 1782 } 1783 commit = lookup_commit_or_die(&oid, "upstream base"); 1784 if (repo_get_merge_bases_many(the_repository, 1785 commit, total, 1786 list, 1787 &base_list) < 0 || 1788 /* There should be one and only one merge base. */ 1789 !base_list || base_list->next) { 1790 if (die_on_failure) { 1791 die(_("could not find exact merge base")); 1792 } else { 1793 commit_list_free(base_list); 1794 return NULL; 1795 } 1796 } 1797 base = base_list->item; 1798 commit_list_free(base_list); 1799 } else { 1800 if (die_on_failure) 1801 die(_("failed to get upstream, if you want to record base commit automatically,\n" 1802 "please use git branch --set-upstream-to to track a remote branch.\n" 1803 "Or you could specify base commit by --base= manually")); 1804 else 1805 return NULL; 1806 } 1807 } 1808 1809 ALLOC_ARRAY(rev, total); 1810 for (i = 0; i < total; i++) 1811 rev[i] = list[i]; 1812 1813 rev_nr = total; 1814 /* 1815 * Get merge base through pair-wise computations 1816 * and store it in rev[0]. 1817 */ 1818 while (rev_nr > 1) { 1819 for (i = 0; i < rev_nr / 2; i++) { 1820 struct commit_list *merge_base = NULL; 1821 if (repo_get_merge_bases(the_repository, 1822 rev[2 * i], 1823 rev[2 * i + 1], &merge_base) < 0 || 1824 !merge_base || merge_base->next) { 1825 if (die_on_failure) { 1826 die(_("failed to find exact merge base")); 1827 } else { 1828 commit_list_free(merge_base); 1829 free(rev); 1830 return NULL; 1831 } 1832 } 1833 1834 rev[i] = merge_base->item; 1835 commit_list_free(merge_base); 1836 } 1837 1838 if (rev_nr % 2) 1839 rev[i] = rev[2 * i]; 1840 rev_nr = DIV_ROUND_UP(rev_nr, 2); 1841 } 1842 1843 ret = repo_in_merge_bases(the_repository, base, rev[0]); 1844 if (ret < 0) 1845 exit(128); 1846 if (!ret) { 1847 if (die_on_failure) { 1848 die(_("base commit should be the ancestor of revision list")); 1849 } else { 1850 free(rev); 1851 return NULL; 1852 } 1853 } 1854 1855 for (i = 0; i < total; i++) { 1856 if (base == list[i]) { 1857 if (die_on_failure) { 1858 die(_("base commit shouldn't be in revision list")); 1859 } else { 1860 free(rev); 1861 return NULL; 1862 } 1863 } 1864 } 1865 1866 free(rev); 1867 return base; 1868 } 1869 1870 define_commit_slab(commit_base, int); 1871 1872 static void prepare_bases(struct base_tree_info *bases, 1873 struct commit *base, 1874 struct commit **list, 1875 size_t total) 1876 { 1877 struct commit *commit; 1878 struct rev_info revs; 1879 struct diff_options diffopt; 1880 struct commit_base commit_base; 1881 1882 if (!base) 1883 return; 1884 1885 init_commit_base(&commit_base); 1886 repo_diff_setup(the_repository, &diffopt); 1887 diffopt.flags.recursive = 1; 1888 diff_setup_done(&diffopt); 1889 1890 oidcpy(&bases->base_commit, &base->object.oid); 1891 1892 repo_init_revisions(the_repository, &revs, NULL); 1893 revs.max_parents = 1; 1894 revs.topo_order = 1; 1895 for (size_t i = 0; i < total; i++) { 1896 list[i]->object.flags &= ~UNINTERESTING; 1897 add_pending_object(&revs, &list[i]->object, "rev_list"); 1898 *commit_base_at(&commit_base, list[i]) = 1; 1899 } 1900 base->object.flags |= UNINTERESTING; 1901 add_pending_object(&revs, &base->object, "base"); 1902 1903 if (prepare_revision_walk(&revs)) 1904 die(_("revision walk setup failed")); 1905 /* 1906 * Traverse the commits list, get prerequisite patch ids 1907 * and stuff them in bases structure. 1908 */ 1909 while ((commit = get_revision(&revs)) != NULL) { 1910 struct object_id oid; 1911 struct object_id *patch_id; 1912 if (*commit_base_at(&commit_base, commit)) 1913 continue; 1914 if (commit_patch_id(commit, &diffopt, &oid, 0)) 1915 die(_("cannot get patch id")); 1916 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id); 1917 patch_id = bases->patch_id + bases->nr_patch_id; 1918 oidcpy(patch_id, &oid); 1919 bases->nr_patch_id++; 1920 } 1921 clear_commit_base(&commit_base); 1922 release_revisions(&revs); 1923 } 1924 1925 static void print_bases(struct base_tree_info *bases, FILE *file) 1926 { 1927 int i; 1928 1929 /* Only do this once, either for the cover or for the first one */ 1930 if (is_null_oid(&bases->base_commit)) 1931 return; 1932 1933 /* Show the base commit */ 1934 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit)); 1935 1936 /* Show the prerequisite patches */ 1937 for (i = bases->nr_patch_id - 1; i >= 0; i--) 1938 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i])); 1939 1940 free(bases->patch_id); 1941 bases->nr_patch_id = 0; 1942 bases->alloc_patch_id = 0; 1943 oidclr(&bases->base_commit, the_repository->hash_algo); 1944 } 1945 1946 static const char *diff_title(struct strbuf *sb, 1947 const char *reroll_count, 1948 const char *generic, 1949 const char *rerolled) 1950 { 1951 int v; 1952 1953 /* RFC may be v0, so allow -v1 to diff against v0 */ 1954 if (reroll_count && !strtol_i(reroll_count, 10, &v) && 1955 v >= 1) 1956 strbuf_addf(sb, rerolled, v - 1); 1957 else 1958 strbuf_addstr(sb, generic); 1959 return sb->buf; 1960 } 1961 1962 static void infer_range_diff_ranges(struct strbuf *r1, 1963 struct strbuf *r2, 1964 const char *prev, 1965 struct commit *origin, 1966 struct commit *head) 1967 { 1968 const char *head_oid = oid_to_hex(&head->object.oid); 1969 int prev_is_range = is_range_diff_range(prev); 1970 1971 if (prev_is_range) 1972 strbuf_addstr(r1, prev); 1973 else 1974 strbuf_addf(r1, "%s..%s", head_oid, prev); 1975 1976 if (origin) 1977 strbuf_addf(r2, "%s..%s", oid_to_hex(&origin->object.oid), head_oid); 1978 else if (prev_is_range) 1979 die(_("failed to infer range-diff origin of current series")); 1980 else { 1981 warning(_("using '%s' as range-diff origin of current series"), prev); 1982 strbuf_addf(r2, "%s..%s", prev, head_oid); 1983 } 1984 } 1985 1986 int cmd_format_patch(int argc, 1987 const char **argv, 1988 const char *prefix, 1989 struct repository *repo UNUSED) 1990 { 1991 struct format_config cfg; 1992 struct commit *commit; 1993 struct commit_stack list = COMMIT_STACK_INIT; 1994 struct rev_info rev; 1995 char *to_free = NULL; 1996 struct setup_revision_opt s_r_opt; 1997 size_t total, i; 1998 int use_stdout = 0; 1999 int start_number = -1; 2000 int just_numbers = 0; 2001 int ignore_if_in_upstream = 0; 2002 int cover_letter = -1; 2003 const char *cover_letter_fmt = NULL; 2004 int boundary_count = 0; 2005 int no_binary_diff = 0; 2006 int zero_commit = 0; 2007 struct commit *origin = NULL; 2008 const char *in_reply_to = NULL; 2009 struct patch_ids ids; 2010 struct strbuf buf = STRBUF_INIT; 2011 int use_patch_format = 0; 2012 int quiet = 0; 2013 const char *reroll_count = NULL; 2014 char *cover_from_description_arg = NULL; 2015 char *description_file = NULL; 2016 char *branch_name = NULL; 2017 struct base_tree_info bases; 2018 struct commit *base; 2019 int show_progress = 0; 2020 struct progress *progress = NULL; 2021 struct oid_array idiff_prev = OID_ARRAY_INIT; 2022 struct strbuf idiff_title = STRBUF_INIT; 2023 const char *rdiff_prev = NULL; 2024 struct strbuf rdiff1 = STRBUF_INIT; 2025 struct strbuf rdiff2 = STRBUF_INIT; 2026 struct strbuf rdiff_title = STRBUF_INIT; 2027 struct rdiff_notes rdiff_notes = { 2028 .notes = STRING_LIST_INIT_NODUP, 2029 }; 2030 const char *rfc = NULL; 2031 int creation_factor = -1; 2032 const char *signature = git_version_string; 2033 char *signature_to_free = NULL; 2034 char *signature_file_arg = NULL; 2035 struct keep_callback_data keep_callback_data = { 2036 .cfg = &cfg, 2037 .revs = &rev, 2038 }; 2039 const char *fmt_patch_suffix = NULL; 2040 2041 const struct option builtin_format_patch_options[] = { 2042 OPT_CALLBACK_F('n', "numbered", &cfg, NULL, 2043 N_("use [PATCH n/m] even with a single patch"), 2044 PARSE_OPT_NOARG, numbered_callback), 2045 OPT_CALLBACK_F('N', "no-numbered", &cfg, NULL, 2046 N_("use [PATCH] even with multiple patches"), 2047 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback), 2048 OPT_BOOL('s', "signoff", &cfg.do_signoff, N_("add a Signed-off-by trailer")), 2049 OPT_BOOL(0, "stdout", &use_stdout, 2050 N_("print patches to standard out")), 2051 OPT_BOOL(0, "cover-letter", &cover_letter, 2052 N_("generate a cover letter")), 2053 OPT_STRING(0, "commit-list-format", &cover_letter_fmt, N_("format-spec"), 2054 N_("format spec used for the commit list in the cover letter")), 2055 OPT_BOOL(0, "numbered-files", &just_numbers, 2056 N_("use simple number sequence for output file names")), 2057 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"), 2058 N_("use instead of '.patch'")), 2059 OPT_INTEGER(0, "start-number", &start_number, 2060 N_("start numbering patches at instead of 1")), 2061 OPT_STRING('v', "reroll-count", &reroll_count, N_("reroll-count"), 2062 N_("mark the series as Nth re-roll")), 2063 OPT_INTEGER(0, "filename-max-length", &cfg.log.fmt_patch_name_max, 2064 N_("max length of output filename")), 2065 OPT_CALLBACK_F(0, "rfc", &rfc, N_("rfc"), 2066 N_("add (default 'RFC') before 'PATCH'"), 2067 PARSE_OPT_OPTARG, rfc_callback), 2068 OPT_STRING(0, "cover-from-description", &cover_from_description_arg, 2069 N_("cover-from-description-mode"), 2070 N_("generate parts of a cover letter based on a branch's description")), 2071 OPT_FILENAME(0, "description-file", &description_file, 2072 N_("use branch description from file")), 2073 OPT_CALLBACK_F(0, "subject-prefix", &cfg, N_("prefix"), 2074 N_("use [] instead of [PATCH]"), 2075 PARSE_OPT_NONEG, subject_prefix_callback), 2076 OPT_CALLBACK_F('o', "output-directory", &output_directory, 2077 N_("dir"), N_("store resulting files in "), 2078 PARSE_OPT_NONEG, output_directory_callback), 2079 OPT_CALLBACK_F('k', "keep-subject", &keep_callback_data, NULL, 2080 N_("don't strip/add [PATCH]"), 2081 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback), 2082 OPT_BOOL(0, "no-binary", &no_binary_diff, 2083 N_("don't output binary diffs")), 2084 OPT_BOOL(0, "zero-commit", &zero_commit, 2085 N_("output all-zero hash in From header")), 2086 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream, 2087 N_("don't include a patch matching a commit upstream")), 2088 OPT_SET_INT_F('p', "no-stat", &use_patch_format, 2089 N_("show patch format instead of default (patch + stat)"), 2090 1, PARSE_OPT_NONEG), 2091 OPT_GROUP(N_("Messaging")), 2092 OPT_CALLBACK(0, "add-header", &cfg, N_("header"), 2093 N_("add email header"), header_callback), 2094 OPT_STRING_LIST(0, "to", &cfg.extra_to, N_("email"), N_("add To: header")), 2095 OPT_STRING_LIST(0, "cc", &cfg.extra_cc, N_("email"), N_("add Cc: header")), 2096 OPT_CALLBACK_F(0, "from", &cfg.from, N_("ident"), 2097 N_("set From address to (or committer ident if absent)"), 2098 PARSE_OPT_OPTARG, from_callback), 2099 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"), 2100 N_("make first mail a reply to ")), 2101 OPT_CALLBACK_F(0, "attach", &rev, N_("boundary"), 2102 N_("attach the patch"), PARSE_OPT_OPTARG, 2103 attach_callback), 2104 OPT_CALLBACK_F(0, "inline", &rev, N_("boundary"), 2105 N_("inline the patch"), 2106 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, 2107 inline_callback), 2108 OPT_CALLBACK_F(0, "thread", &cfg, N_("style"), 2109 N_("enable message threading, styles: shallow, deep"), 2110 PARSE_OPT_OPTARG, thread_callback), 2111 OPT_STRING(0, "signature", &signature, N_("signature"), 2112 N_("add a signature")), 2113 OPT_CALLBACK_F(0, "base", &cfg, N_("base-commit"), 2114 N_("add prerequisite tree info to the patch series"), 2115 0, base_callback), 2116 OPT_FILENAME(0, "signature-file", &signature_file_arg, 2117 N_("add a signature from a file")), 2118 OPT__QUIET(&quiet, N_("don't print the patch filenames")), 2119 OPT_BOOL(0, "progress", &show_progress, 2120 N_("show progress while generating patches")), 2121 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"), 2122 N_("show changes against in cover letter or single patch"), 2123 parse_opt_object_name), 2124 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"), 2125 N_("show changes against in cover letter or single patch")), 2126 OPT_CALLBACK_F(0, "range-diff-notes", &rdiff_notes, N_("note"), 2127 N_("override notes behavior for the range diff"), 2128 0, rdiff_notes_cb), 2129 OPT_INTEGER(0, "creation-factor", &creation_factor, 2130 N_("percentage by which creation is weighted")), 2131 OPT_BOOL(0, "force-in-body-from", &force_in_body_from, 2132 N_("show in-body From: even if identical to the e-mail header")), 2133 OPT_END() 2134 }; 2135 2136 format_config_init(&cfg); 2137 init_diff_ui_defaults(); 2138 init_display_notes(&cfg.notes_opt); 2139 repo_config(the_repository, git_format_config, &cfg); 2140 repo_init_revisions(the_repository, &rev, prefix); 2141 repo_config(the_repository, grep_config, &rev.grep_filter); 2142 2143 rev.show_notes = cfg.show_notes; 2144 memcpy(&rev.notes_opt, &cfg.notes_opt, sizeof(cfg.notes_opt)); 2145 rev.commit_format = CMIT_FMT_EMAIL; 2146 rev.encode_email_headers = cfg.log.default_encode_email_headers; 2147 rev.expand_tabs_in_log_default = 0; 2148 rev.verbose_header = 1; 2149 rev.diff = 1; 2150 rev.max_parents = 1; 2151 rev.diffopt.flags.recursive = 1; 2152 rev.diffopt.no_free = 1; 2153 memset(&s_r_opt, 0, sizeof(s_r_opt)); 2154 s_r_opt.def = "HEAD"; 2155 s_r_opt.revarg_opt = REVARG_COMMITTISH; 2156 2157 strbuf_addstr(&cfg.sprefix, cfg.log.fmt_patch_subject_prefix); 2158 if (format_no_prefix) 2159 diff_set_noprefix(&rev.diffopt); 2160 2161 if (cfg.default_attach) { 2162 rev.mime_boundary = cfg.default_attach; 2163 rev.no_inline = 1; 2164 } 2165 2166 /* 2167 * Parse the arguments before setup_revisions(), or something 2168 * like "git format-patch -o a123 HEAD^.." may fail; a123 is 2169 * possibly a valid SHA1. 2170 */ 2171 argc = parse_options(argc, argv, prefix, builtin_format_patch_options, 2172 builtin_format_patch_usage, 2173 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | 2174 PARSE_OPT_KEEP_DASHDASH); 2175 2176 rev.force_in_body_from = force_in_body_from; 2177 2178 if (!fmt_patch_suffix) 2179 fmt_patch_suffix = cfg.fmt_patch_suffix; 2180 2181 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */ 2182 if (cfg.log.fmt_patch_name_max <= cast_size_t_to_int(strlen("0000-") + strlen(fmt_patch_suffix))) 2183 cfg.log.fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix); 2184 2185 if (cover_from_description_arg) 2186 cfg.cover_from_description_mode = parse_cover_from_description(cover_from_description_arg); 2187 2188 if (rfc && rfc[0]) { 2189 cfg.subject_prefix = 1; 2190 if (rfc[0] == '-') 2191 strbuf_addf(&cfg.sprefix, " %s", rfc + 1); 2192 else 2193 strbuf_insertf(&cfg.sprefix, 0, "%s ", rfc); 2194 } 2195 2196 if (reroll_count) { 2197 strbuf_addf(&cfg.sprefix, " v%s", reroll_count); 2198 rev.reroll_count = reroll_count; 2199 } 2200 2201 rev.subject_prefix = cfg.sprefix.buf; 2202 2203 for (i = 0; i < cfg.extra_hdr.nr; i++) { 2204 strbuf_addstr(&buf, cfg.extra_hdr.items[i].string); 2205 strbuf_addch(&buf, '\n'); 2206 } 2207 2208 if (cfg.extra_to.nr) 2209 strbuf_addstr(&buf, "To: "); 2210 for (i = 0; i < cfg.extra_to.nr; i++) { 2211 if (i) 2212 strbuf_addstr(&buf, " "); 2213 strbuf_addstr(&buf, cfg.extra_to.items[i].string); 2214 if (i + 1 < cfg.extra_to.nr) 2215 strbuf_addch(&buf, ','); 2216 strbuf_addch(&buf, '\n'); 2217 } 2218 2219 if (cfg.extra_cc.nr) 2220 strbuf_addstr(&buf, "Cc: "); 2221 for (i = 0; i < cfg.extra_cc.nr; i++) { 2222 if (i) 2223 strbuf_addstr(&buf, " "); 2224 strbuf_addstr(&buf, cfg.extra_cc.items[i].string); 2225 if (i + 1 < cfg.extra_cc.nr) 2226 strbuf_addch(&buf, ','); 2227 strbuf_addch(&buf, '\n'); 2228 } 2229 2230 rev.extra_headers = to_free = strbuf_detach(&buf, NULL); 2231 2232 if (cfg.from) { 2233 if (split_ident_line(&rev.from_ident, cfg.from, strlen(cfg.from))) 2234 die(_("invalid ident line: %s"), cfg.from); 2235 } 2236 2237 if (start_number < 0) 2238 start_number = 1; 2239 2240 /* 2241 * If numbered is set solely due to format.numbered in config, 2242 * and it would conflict with --keep-subject (-k) from the 2243 * command line, reset "numbered". 2244 */ 2245 if (cfg.numbered && cfg.keep_subject && !cfg.numbered_cmdline_opt) 2246 cfg.numbered = 0; 2247 2248 if (cfg.numbered && cfg.keep_subject) 2249 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k"); 2250 if (cfg.keep_subject && cfg.subject_prefix) 2251 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k"); 2252 rev.preserve_subject = cfg.keep_subject; 2253 2254 argc = setup_revisions(argc, argv, &rev, &s_r_opt); 2255 if (argc > 1) 2256 die(_("unrecognized argument: %s"), argv[1]); 2257 2258 if (rev.diffopt.output_format & DIFF_FORMAT_NAME) 2259 die(_("--name-only does not make sense")); 2260 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) 2261 die(_("--name-status does not make sense")); 2262 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) 2263 die(_("--check does not make sense")); 2264 if (rev.remerge_diff) 2265 die(_("--remerge-diff does not make sense")); 2266 2267 if (!use_patch_format && 2268 (!rev.diffopt.output_format || 2269 rev.diffopt.output_format == DIFF_FORMAT_PATCH)) 2270 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; 2271 if (!rev.diffopt.stat_width) 2272 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP; 2273 2274 /* Always generate a patch */ 2275 rev.diffopt.output_format |= DIFF_FORMAT_PATCH; 2276 rev.always_show_header = 1; 2277 2278 rev.zero_commit = zero_commit; 2279 rev.patch_name_max = cfg.log.fmt_patch_name_max; 2280 2281 if (!rev.diffopt.flags.text && !no_binary_diff) 2282 rev.diffopt.flags.binary = 1; 2283 2284 if (rev.show_notes) 2285 load_display_notes(&rev.notes_opt); 2286 2287 die_for_incompatible_opt3(use_stdout, "--stdout", 2288 rev.diffopt.close_file, "--output", 2289 !!output_directory, "--output-directory"); 2290 2291 if (use_stdout && stdout_mboxrd) 2292 rev.commit_format = CMIT_FMT_MBOXRD; 2293 2294 if (use_stdout) { 2295 setup_pager(the_repository); 2296 } else if (!rev.diffopt.close_file) { 2297 int saved; 2298 2299 if (!output_directory) 2300 output_directory = cfg.config_output_directory; 2301 output_directory = set_outdir(prefix, output_directory); 2302 2303 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS) 2304 rev.diffopt.use_color = GIT_COLOR_NEVER; 2305 /* 2306 * We consider as 'outside of gitdir', therefore avoid 2307 * applying adjust_shared_perm in s-c-l-d. 2308 */ 2309 saved = repo_settings_get_shared_repository(the_repository); 2310 repo_settings_set_shared_repository(the_repository, 0); 2311 switch (safe_create_leading_directories_const(the_repository, output_directory)) { 2312 case SCLD_OK: 2313 case SCLD_EXISTS: 2314 break; 2315 default: 2316 die(_("could not create leading directories " 2317 "of '%s'"), output_directory); 2318 } 2319 repo_settings_set_shared_repository(the_repository, saved); 2320 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) 2321 die_errno(_("could not create directory '%s'"), 2322 output_directory); 2323 } 2324 2325 if (rev.pending.nr == 1) { 2326 int check_head = 0; 2327 2328 if (rev.max_count < 0 && !rev.show_root_diff) { 2329 /* 2330 * This is traditional behaviour of "git format-patch 2331 * origin" that prepares what the origin side still 2332 * does not have. 2333 */ 2334 rev.pending.objects[0].item->flags |= UNINTERESTING; 2335 add_head_to_pending(&rev); 2336 check_head = 1; 2337 } 2338 /* 2339 * Otherwise, it is "format-patch -22 HEAD", and/or 2340 * "format-patch --root HEAD". The user wants 2341 * get_revision() to do the usual traversal. 2342 */ 2343 2344 if (!strcmp(rev.pending.objects[0].name, "HEAD")) 2345 check_head = 1; 2346 2347 if (check_head) { 2348 const char *ref, *v; 2349 ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 2350 "HEAD", 2351 RESOLVE_REF_READING, 2352 NULL, NULL); 2353 if (ref && skip_prefix(ref, "refs/heads/", &v)) 2354 branch_name = xstrdup(v); 2355 else 2356 branch_name = xstrdup(""); /* no branch */ 2357 } 2358 } 2359 2360 /* 2361 * We cannot move this anywhere earlier because we do want to 2362 * know if --root was given explicitly from the command line. 2363 */ 2364 rev.show_root_diff = 1; 2365 2366 if (ignore_if_in_upstream) { 2367 /* Don't say anything if head and upstream are the same. */ 2368 if (rev.pending.nr == 2) { 2369 struct object_array_entry *o = rev.pending.objects; 2370 if (oideq(&o[0].item->oid, &o[1].item->oid)) 2371 goto done; 2372 } 2373 get_patch_ids(&rev, &ids); 2374 } 2375 2376 if (prepare_revision_walk(&rev)) 2377 die(_("revision walk setup failed")); 2378 rev.boundary = 1; 2379 while ((commit = get_revision(&rev)) != NULL) { 2380 if (commit->object.flags & BOUNDARY) { 2381 boundary_count++; 2382 origin = (boundary_count == 1) ? commit : NULL; 2383 continue; 2384 } 2385 2386 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids)) 2387 continue; 2388 2389 commit_stack_push(&list, commit); 2390 } 2391 if (!list.nr) 2392 /* nothing to do */ 2393 goto done; 2394 total = list.nr; 2395 2396 if (!cover_letter_fmt) { 2397 cover_letter_fmt = cfg.fmt_cover_letter_commit_list; 2398 if (!cover_letter_fmt) 2399 cover_letter_fmt = "shortlog"; 2400 } else if (cover_letter == -1) { 2401 cover_letter = 1; 2402 } 2403 2404 if (cover_letter == -1) { 2405 if (cfg.config_cover_letter == COVER_AUTO) 2406 cover_letter = (total > 1); 2407 else if ((idiff_prev.nr || rdiff_prev) && (total > 1)) 2408 cover_letter = (cfg.config_cover_letter != COVER_OFF); 2409 else 2410 cover_letter = (cfg.config_cover_letter == COVER_ON); 2411 } 2412 if (!cfg.keep_subject && cfg.auto_number && (total > 1 || cover_letter)) 2413 cfg.numbered = 1; 2414 if (cfg.numbered) 2415 rev.total = total + start_number - 1; 2416 2417 if (idiff_prev.nr) { 2418 if (!cover_letter && total != 1) 2419 die(_("--interdiff requires --cover-letter or single patch")); 2420 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1]; 2421 rev.idiff_oid2 = get_commit_tree_oid(list.items[0]); 2422 rev.idiff_title = diff_title(&idiff_title, reroll_count, 2423 _("Interdiff:"), 2424 _("Interdiff against v%d:")); 2425 } 2426 2427 if (creation_factor < 0) 2428 creation_factor = CREATION_FACTOR_FOR_THE_SAME_SERIES; 2429 else if (!rdiff_prev) 2430 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff"); 2431 2432 if (rdiff_prev) { 2433 if (!cover_letter && total != 1) 2434 die(_("--range-diff requires --cover-letter or single patch")); 2435 2436 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev, 2437 origin, list.items[0]); 2438 rev.rdiff1 = rdiff1.buf; 2439 rev.rdiff2 = rdiff2.buf; 2440 rev.creation_factor = creation_factor; 2441 rev.rdiff_title = diff_title(&rdiff_title, reroll_count, 2442 _("Range-diff:"), 2443 _("Range-diff against v%d:")); 2444 get_notes_args(&rdiff_notes, &rev); 2445 } 2446 2447 /* 2448 * The order of precedence is: 2449 * 2450 * 1. The `--signature` and `--no-signature` options. 2451 * 2. The `--signature-file` option. 2452 * 3. The `format.signature` config. 2453 * 4. The `format.signatureFile` config. 2454 * 5. Default `git_version_string`. 2455 */ 2456 if (!signature) { 2457 ; /* --no-signature inhibits all signatures */ 2458 } else if (signature && signature != git_version_string) { 2459 ; /* non-default signature already set */ 2460 } else if (signature_file_arg || (cfg.signature_file && !cfg.signature)) { 2461 struct strbuf buf = STRBUF_INIT; 2462 const char *signature_file = signature_file_arg ? 2463 signature_file_arg : cfg.signature_file; 2464 2465 if (strbuf_read_file(&buf, signature_file, 128) < 0) 2466 die_errno(_("unable to read signature file '%s'"), signature_file); 2467 signature = signature_to_free = strbuf_detach(&buf, NULL); 2468 } else if (cfg.signature) { 2469 signature = cfg.signature; 2470 } 2471 2472 memset(&bases, 0, sizeof(bases)); 2473 base = get_base_commit(&cfg, list.items, list.nr); 2474 if (base) { 2475 reset_revision_walk(); 2476 clear_object_flags(the_repository, UNINTERESTING); 2477 prepare_bases(&bases, base, list.items, list.nr); 2478 } 2479 2480 if (in_reply_to || cfg.thread || cover_letter) { 2481 rev.ref_message_ids = xmalloc(sizeof(*rev.ref_message_ids)); 2482 string_list_init_dup(rev.ref_message_ids); 2483 } 2484 if (in_reply_to) { 2485 char *msgid = clean_message_id(in_reply_to); 2486 string_list_append_nodup(rev.ref_message_ids, msgid); 2487 } 2488 rev.numbered_files = just_numbers; 2489 rev.patch_suffix = fmt_patch_suffix; 2490 2491 if (cover_letter) { 2492 if (cfg.thread) 2493 gen_message_id(&rev, "cover"); 2494 make_cover_letter(&rev, !!output_directory, 2495 origin, list.nr, list.items, 2496 description_file, branch_name, quiet, &cfg, 2497 cover_letter_fmt); 2498 print_bases(&bases, rev.diffopt.file); 2499 print_signature(signature, rev.diffopt.file); 2500 total++; 2501 start_number--; 2502 /* interdiff/range-diff in cover-letter; omit from patches */ 2503 rev.idiff_oid1 = NULL; 2504 rev.rdiff1 = NULL; 2505 } 2506 rev.add_signoff = cfg.do_signoff; 2507 2508 if (show_progress) 2509 progress = start_delayed_progress(the_repository, 2510 _("Generating patches"), total); 2511 while (list.nr) { 2512 size_t idx = list.nr - 1; 2513 int shown; 2514 2515 display_progress(progress, total - idx); 2516 commit = commit_stack_pop(&list); 2517 rev.nr = total - idx + (start_number - 1); 2518 2519 /* Make the second and subsequent mails replies to the first */ 2520 if (cfg.thread) { 2521 /* Have we already had a message ID? */ 2522 if (rev.message_id) { 2523 /* 2524 * For deep threading: make every mail 2525 * a reply to the previous one, no 2526 * matter what other options are set. 2527 * 2528 * For shallow threading: 2529 * 2530 * Without --cover-letter and 2531 * --in-reply-to, make every mail a 2532 * reply to the one before. 2533 * 2534 * With --in-reply-to but no 2535 * --cover-letter, make every mail a 2536 * reply to the . 2537 * 2538 * With --cover-letter, make every 2539 * mail but the cover letter a reply 2540 * to the cover letter. The cover 2541 * letter is a reply to the 2542 * --in-reply-to, if specified. 2543 */ 2544 if (cfg.thread == THREAD_SHALLOW 2545 && rev.ref_message_ids->nr > 0 2546 && (!cover_letter || rev.nr > 1)) 2547 free(rev.message_id); 2548 else 2549 string_list_append_nodup(rev.ref_message_ids, 2550 rev.message_id); 2551 } 2552 gen_message_id(&rev, oid_to_hex(&commit->object.oid)); 2553 } 2554 2555 if (output_directory && 2556 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet)) 2557 die(_("failed to create output files")); 2558 shown = log_tree_commit(&rev, commit); 2559 free_commit_buffer(the_repository->parsed_objects, 2560 commit); 2561 2562 /* We put one extra blank line between formatted 2563 * patches and this flag is used by log-tree code 2564 * to see if it needs to emit a LF before showing 2565 * the log; when using one file per patch, we do 2566 * not want the extra blank line. 2567 */ 2568 if (output_directory) 2569 rev.shown_one = 0; 2570 if (shown) { 2571 print_bases(&bases, rev.diffopt.file); 2572 if (rev.mime_boundary) 2573 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n", 2574 mime_boundary_leader, 2575 rev.mime_boundary); 2576 else 2577 print_signature(signature, rev.diffopt.file); 2578 } 2579 if (output_directory) { 2580 fclose(rev.diffopt.file); 2581 rev.diffopt.file = NULL; 2582 } 2583 } 2584 stop_progress(&progress); 2585 commit_stack_clear(&list); 2586 if (ignore_if_in_upstream) 2587 free_patch_ids(&ids); 2588 2589 done: 2590 oid_array_clear(&idiff_prev); 2591 strbuf_release(&idiff_title); 2592 strbuf_release(&rdiff1); 2593 strbuf_release(&rdiff2); 2594 strbuf_release(&rdiff_title); 2595 free(description_file); 2596 free(signature_file_arg); 2597 free(signature_to_free); 2598 free(branch_name); 2599 free(to_free); 2600 free(rev.message_id); 2601 if (rev.ref_message_ids) 2602 string_list_clear(rev.ref_message_ids, 0); 2603 free(rev.ref_message_ids); 2604 rev.diffopt.no_free = 0; 2605 release_revisions(&rev); 2606 format_config_release(&cfg); 2607 strvec_clear(&rev.rdiff_log_arg); 2608 string_list_clear(&rdiff_notes.notes, 0); 2609 return 0; 2610 } 2611 2612 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) 2613 { 2614 struct object_id oid; 2615 if (repo_get_oid(the_repository, arg, &oid) == 0) { 2616 struct commit *commit = lookup_commit_reference(the_repository, 2617 &oid); 2618 if (commit) { 2619 commit->object.flags |= flags; 2620 add_pending_object(revs, &commit->object, arg); 2621 return 0; 2622 } 2623 } 2624 return -1; 2625 } 2626 2627 static const char * const cherry_usage[] = { 2628 N_("git cherry [-v] [ [ []]]"), 2629 NULL 2630 }; 2631 2632 static void print_commit(char sign, struct commit *commit, int verbose, 2633 int abbrev, FILE *file) 2634 { 2635 if (!verbose) { 2636 fprintf(file, "%c %s\n", sign, 2637 repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev)); 2638 } else { 2639 struct strbuf buf = STRBUF_INIT; 2640 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); 2641 fprintf(file, "%c %s %s\n", sign, 2642 repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev), 2643 buf.buf); 2644 strbuf_release(&buf); 2645 } 2646 } 2647 2648 /* 2649 * Enumerate blob OIDs from a single commit's diff, inserting them into blobs. 2650 * Skips files whose userdiff driver explicitly declares binary status 2651 * (drv->binary > 0), since patch-ID uses oid_to_hex() for those and 2652 * never reads blob content. Use userdiff_find_by_path() since 2653 * diff_filespec_load_driver() is static in diff.c. 2654 * 2655 * Clean up with diff_queue_clear() (from diffcore.h). 2656 */ 2657 static void collect_diff_blob_oids(struct commit *commit, 2658 struct diff_options *opts, 2659 struct oidset *blobs) 2660 { 2661 struct diff_queue_struct *q; 2662 2663 /* 2664 * Merge commits are filtered out by patch_id_defined() in patch-ids.c, 2665 * so we'll never be called with one. 2666 */ 2667 assert(!commit->parents || !commit->parents->next); 2668 2669 if (commit->parents) 2670 diff_tree_oid(&commit->parents->item->object.oid, 2671 &commit->object.oid, "", opts); 2672 else 2673 diff_root_tree_oid(&commit->object.oid, "", opts); 2674 diffcore_std(opts); 2675 2676 q = &diff_queued_diff; 2677 for (int i = 0; i < q->nr; i++) { 2678 struct diff_filepair *p = q->queue[i]; 2679 struct userdiff_driver *drv; 2680 2681 /* Skip binary files */ 2682 drv = userdiff_find_by_path(opts->repo->index, p->one->path); 2683 if (drv && drv->binary > 0) 2684 continue; 2685 2686 if (DIFF_FILE_VALID(p->one) && 2687 odb_read_object_info_extended(opts->repo->objects, 2688 &p->one->oid, NULL, 2689 OBJECT_INFO_FOR_PREFETCH)) 2690 oidset_insert(blobs, &p->one->oid); 2691 if (DIFF_FILE_VALID(p->two) && 2692 odb_read_object_info_extended(opts->repo->objects, 2693 &p->two->oid, NULL, 2694 OBJECT_INFO_FOR_PREFETCH)) 2695 oidset_insert(blobs, &p->two->oid); 2696 } 2697 diff_queue_clear(q); 2698 } 2699 2700 static int always_match(const void *cmp_data UNUSED, 2701 const struct hashmap_entry *entry1 UNUSED, 2702 const struct hashmap_entry *entry2 UNUSED, 2703 const void *keydata UNUSED) 2704 { 2705 return 0; 2706 } 2707 2708 /* 2709 * Prefetch blobs for git cherry in partial clones. 2710 * 2711 * Called between the revision walk (which builds the head-side 2712 * commit list) and the has_commit_patch_id() comparison loop. 2713 * 2714 * Uses a cmpfn-swap trick to avoid reading blobs: temporarily 2715 * replaces the hashmap's comparison function with a trivial 2716 * always-match function, so hashmap_get()/hashmap_get_next() match 2717 * any entry with the same oidhash bucket. These are the set of oids 2718 * that would trigger patch_id_neq() during normal lookup and cause 2719 * blobs to be read on demand, and we want to prefetch them all at 2720 * once instead. 2721 */ 2722 static void prefetch_cherry_blobs(struct repository *repo, 2723 struct commit_list *list, 2724 struct patch_ids *ids) 2725 { 2726 struct oidset blobs = OIDSET_INIT; 2727 hashmap_cmp_fn original_cmpfn; 2728 2729 /* Exit if we're not in a partial clone */ 2730 if (!repo_has_promisor_remote(repo)) 2731 return; 2732 2733 /* Save original cmpfn, replace with always_match */ 2734 original_cmpfn = ids->patches.cmpfn; 2735 ids->patches.cmpfn = always_match; 2736 2737 /* Find header-only collisions, gather blobs from those commits */ 2738 for (struct commit_list *l = list; l; l = l->next) { 2739 struct commit *c = l->item; 2740 bool match_found = false; 2741 for (struct patch_id *cur = patch_id_iter_first(c, ids); 2742 cur; 2743 cur = patch_id_iter_next(cur, ids)) { 2744 match_found = true; 2745 collect_diff_blob_oids(cur->commit, &ids->diffopts, 2746 &blobs); 2747 } 2748 if (match_found) 2749 collect_diff_blob_oids(c, &ids->diffopts, &blobs); 2750 } 2751 2752 /* Restore original cmpfn */ 2753 ids->patches.cmpfn = original_cmpfn; 2754 2755 /* If we have any blobs to fetch, fetch them */ 2756 if (oidset_size(&blobs)) { 2757 struct oid_array to_fetch = OID_ARRAY_INIT; 2758 struct oidset_iter iter; 2759 const struct object_id *oid; 2760 2761 oidset_iter_init(&blobs, &iter); 2762 while ((oid = oidset_iter_next(&iter))) 2763 oid_array_append(&to_fetch, oid); 2764 2765 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr); 2766 2767 oid_array_clear(&to_fetch); 2768 } 2769 2770 oidset_clear(&blobs); 2771 } 2772 2773 int cmd_cherry(int argc, 2774 const char **argv, 2775 const char *prefix, 2776 struct repository *repo UNUSED) 2777 { 2778 struct rev_info revs; 2779 struct patch_ids ids; 2780 struct commit *commit; 2781 struct commit_list *list = NULL; 2782 struct branch *current_branch; 2783 const char *upstream; 2784 const char *head = "HEAD"; 2785 const char *limit = NULL; 2786 int verbose = 0, abbrev = 0; 2787 2788 struct option options[] = { 2789 OPT__ABBREV(&abbrev), 2790 OPT__VERBOSE(&verbose, N_("be verbose")), 2791 OPT_END() 2792 }; 2793 2794 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); 2795 2796 switch (argc) { 2797 case 3: 2798 limit = argv[2]; 2799 /* FALLTHROUGH */ 2800 case 2: 2801 head = argv[1]; 2802 /* FALLTHROUGH */ 2803 case 1: 2804 upstream = argv[0]; 2805 break; 2806 default: 2807 current_branch = branch_get(NULL); 2808 upstream = branch_get_upstream(current_branch, NULL); 2809 if (!upstream) { 2810 fprintf(stderr, _("Could not find a tracked" 2811 " remote branch, please" 2812 " specify manually.\n")); 2813 usage_with_options(cherry_usage, options); 2814 } 2815 } 2816 2817 repo_init_revisions(the_repository, &revs, prefix); 2818 revs.max_parents = 1; 2819 2820 if (add_pending_commit(head, &revs, 0)) 2821 die(_("unknown commit %s"), head); 2822 if (add_pending_commit(upstream, &revs, UNINTERESTING)) 2823 die(_("unknown commit %s"), upstream); 2824 2825 /* Don't say anything if head and upstream are the same. */ 2826 if (revs.pending.nr == 2) { 2827 struct object_array_entry *o = revs.pending.objects; 2828 if (oideq(&o[0].item->oid, &o[1].item->oid)) 2829 return 0; 2830 } 2831 2832 get_patch_ids(&revs, &ids); 2833 2834 if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) 2835 die(_("unknown commit %s"), limit); 2836 2837 /* reverse the list of commits */ 2838 if (prepare_revision_walk(&revs)) 2839 die(_("revision walk setup failed")); 2840 while ((commit = get_revision(&revs)) != NULL) { 2841 commit_list_insert(commit, &list); 2842 } 2843 2844 prefetch_cherry_blobs(the_repository, list, &ids); 2845 2846 for (struct commit_list *l = list; l; l = l->next) { 2847 char sign = '+'; 2848 2849 commit = l->item; 2850 if (has_commit_patch_id(commit, &ids)) 2851 sign = '-'; 2852 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file); 2853 } 2854 2855 commit_list_free(list); 2856 free_patch_ids(&ids); 2857 return 0; 2858 }