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