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