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