Git fork
at reftables-rust 642 lines 18 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#define DISABLE_SIGN_COMPARE_WARNINGS 3 4#include "git-compat-util.h" 5#include "environment.h" 6#include "gettext.h" 7#include "range-diff.h" 8#include "object-name.h" 9#include "string-list.h" 10#include "run-command.h" 11#include "strvec.h" 12#include "hashmap.h" 13#include "xdiff-interface.h" 14#include "linear-assignment.h" 15#include "diffcore.h" 16#include "commit.h" 17#include "pager.h" 18#include "pretty.h" 19#include "repository.h" 20#include "userdiff.h" 21#include "apply.h" 22#include "revision.h" 23 24struct patch_util { 25 /* For the search for an exact match */ 26 struct hashmap_entry e; 27 const char *diff, *patch; 28 29 int i, shown; 30 int diffsize; 31 size_t diff_offset; 32 /* the index of the matching item in the other branch, or -1 */ 33 int matching; 34 struct object_id oid; 35}; 36 37/* 38 * Reads the patches into a string list, with the `util` field being populated 39 * as struct object_id (will need to be free()d). 40 */ 41static int read_patches(const char *range, struct string_list *list, 42 const struct strvec *log_arg, 43 unsigned int include_merges) 44{ 45 struct child_process cp = CHILD_PROCESS_INIT; 46 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT; 47 struct patch_util *util = NULL; 48 int in_header = 1; 49 char *line, *current_filename = NULL; 50 ssize_t len; 51 size_t size; 52 int ret = -1; 53 54 strvec_pushl(&cp.args, "log", "--no-color", "-p", 55 "--reverse", "--date-order", "--decorate=no", 56 "--no-prefix", "--submodule=short", 57 /* 58 * Choose indicators that are not used anywhere 59 * else in diffs, but still look reasonable 60 * (e.g. will not be confusing when debugging) 61 */ 62 "--output-indicator-new=>", 63 "--output-indicator-old=<", 64 "--output-indicator-context=#", 65 "--no-abbrev-commit", 66 "--pretty=medium", 67 "--show-notes-by-default", 68 NULL); 69 if (!include_merges) 70 strvec_push(&cp.args, "--no-merges"); 71 strvec_push(&cp.args, range); 72 if (log_arg) 73 strvec_pushv(&cp.args, log_arg->v); 74 cp.out = -1; 75 cp.no_stdin = 1; 76 cp.git_cmd = 1; 77 78 if (start_command(&cp)) 79 return error_errno(_("could not start `log`")); 80 if (strbuf_read(&contents, cp.out, 0) < 0) { 81 error_errno(_("could not read `log` output")); 82 finish_command(&cp); 83 goto cleanup; 84 } 85 if (finish_command(&cp)) 86 goto cleanup; 87 88 line = contents.buf; 89 size = contents.len; 90 for (; size > 0; size -= len, line += len) { 91 const char *p; 92 char *eol; 93 94 eol = memchr(line, '\n', size); 95 if (eol) { 96 *eol = '\0'; 97 len = eol + 1 - line; 98 } else { 99 len = size; 100 } 101 102 if (skip_prefix(line, "commit ", &p)) { 103 char *q; 104 if (util) { 105 string_list_append(list, buf.buf)->util = util; 106 strbuf_reset(&buf); 107 } 108 CALLOC_ARRAY(util, 1); 109 if (include_merges && (q = strstr(p, " (from "))) 110 *q = '\0'; 111 if (repo_get_oid(the_repository, p, &util->oid)) { 112 error(_("could not parse commit '%s'"), p); 113 FREE_AND_NULL(util); 114 string_list_clear(list, 1); 115 goto cleanup; 116 } 117 util->matching = -1; 118 in_header = 1; 119 continue; 120 } 121 122 if (!util) { 123 error(_("could not parse first line of `log` output: " 124 "did not start with 'commit ': '%s'"), 125 line); 126 string_list_clear(list, 1); 127 goto cleanup; 128 } 129 130 if (starts_with(line, "diff --git")) { 131 struct patch patch = { 0 }; 132 struct strbuf root = STRBUF_INIT; 133 int linenr = 0; 134 int orig_len; 135 136 in_header = 0; 137 strbuf_addch(&buf, '\n'); 138 if (!util->diff_offset) 139 util->diff_offset = buf.len; 140 if (eol) 141 *eol = '\n'; 142 orig_len = len; 143 len = parse_git_diff_header(&root, &linenr, 0, line, 144 len, size, &patch); 145 if (len < 0) { 146 error(_("could not parse git header '%.*s'"), 147 orig_len, line); 148 FREE_AND_NULL(util); 149 string_list_clear(list, 1); 150 goto cleanup; 151 } 152 strbuf_addstr(&buf, " ## "); 153 if (patch.is_new > 0) 154 strbuf_addf(&buf, "%s (new)", patch.new_name); 155 else if (patch.is_delete > 0) 156 strbuf_addf(&buf, "%s (deleted)", patch.old_name); 157 else if (patch.is_rename) 158 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name); 159 else 160 strbuf_addstr(&buf, patch.new_name); 161 162 free(current_filename); 163 if (patch.is_delete > 0) 164 current_filename = xstrdup(patch.old_name); 165 else 166 current_filename = xstrdup(patch.new_name); 167 168 if (patch.new_mode && patch.old_mode && 169 patch.old_mode != patch.new_mode) 170 strbuf_addf(&buf, " (mode change %06o => %06o)", 171 patch.old_mode, patch.new_mode); 172 173 strbuf_addstr(&buf, " ##"); 174 release_patch(&patch); 175 } else if (in_header) { 176 if (starts_with(line, "Author: ")) { 177 strbuf_addstr(&buf, " ## Metadata ##\n"); 178 strbuf_addstr(&buf, line); 179 strbuf_addstr(&buf, "\n\n"); 180 strbuf_addstr(&buf, " ## Commit message ##\n"); 181 } else if (starts_with(line, "Notes") && 182 line[strlen(line) - 1] == ':') { 183 strbuf_addstr(&buf, "\n\n"); 184 /* strip the trailing colon */ 185 strbuf_addf(&buf, " ## %.*s ##\n", 186 (int)(strlen(line) - 1), line); 187 } else if (starts_with(line, " ")) { 188 p = line + len - 2; 189 while (isspace(*p) && p >= line) 190 p--; 191 strbuf_add(&buf, line, p - line + 1); 192 strbuf_addch(&buf, '\n'); 193 } 194 continue; 195 } else if (skip_prefix(line, "@@ ", &p)) { 196 p = strstr(p, "@@"); 197 strbuf_addstr(&buf, "@@"); 198 if (current_filename && p[2]) 199 strbuf_addf(&buf, " %s:", current_filename); 200 if (p) 201 strbuf_addstr(&buf, p + 2); 202 } else if (!line[0]) 203 /* 204 * A completely blank (not ' \n', which is context) 205 * line is not valid in a diff. We skip it 206 * silently, because this neatly handles the blank 207 * separator line between commits in git-log 208 * output. 209 */ 210 continue; 211 else if (line[0] == '>') { 212 strbuf_addch(&buf, '+'); 213 strbuf_addstr(&buf, line + 1); 214 } else if (line[0] == '<') { 215 strbuf_addch(&buf, '-'); 216 strbuf_addstr(&buf, line + 1); 217 } else if (line[0] == '#') { 218 strbuf_addch(&buf, ' '); 219 strbuf_addstr(&buf, line + 1); 220 } else { 221 strbuf_addch(&buf, ' '); 222 strbuf_addstr(&buf, line); 223 } 224 225 strbuf_addch(&buf, '\n'); 226 util->diffsize++; 227 } 228 229 ret = 0; 230cleanup: 231 strbuf_release(&contents); 232 233 if (util) 234 string_list_append(list, buf.buf)->util = util; 235 strbuf_release(&buf); 236 free(current_filename); 237 238 return ret; 239} 240 241static int patch_util_cmp(const void *cmp_data UNUSED, 242 const struct hashmap_entry *ha, 243 const struct hashmap_entry *hb, 244 const void *keydata) 245{ 246 const struct patch_util 247 *a = container_of(ha, const struct patch_util, e), 248 *b = container_of(hb, const struct patch_util, e); 249 return strcmp(a->diff, keydata ? keydata : b->diff); 250} 251 252static void find_exact_matches(struct string_list *a, struct string_list *b) 253{ 254 struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL); 255 int i; 256 257 /* First, add the patches of a to a hash map */ 258 for (i = 0; i < a->nr; i++) { 259 struct patch_util *util = a->items[i].util; 260 261 util->i = i; 262 util->patch = a->items[i].string; 263 util->diff = util->patch + util->diff_offset; 264 hashmap_entry_init(&util->e, strhash(util->diff)); 265 hashmap_add(&map, &util->e); 266 } 267 268 /* Now try to find exact matches in b */ 269 for (i = 0; i < b->nr; i++) { 270 struct patch_util *util = b->items[i].util, *other; 271 272 util->i = i; 273 util->patch = b->items[i].string; 274 util->diff = util->patch + util->diff_offset; 275 hashmap_entry_init(&util->e, strhash(util->diff)); 276 other = hashmap_remove_entry(&map, util, e, NULL); 277 if (other) { 278 if (other->matching >= 0) 279 BUG("already assigned!"); 280 281 other->matching = i; 282 util->matching = other->i; 283 } 284 } 285 286 hashmap_clear(&map); 287} 288 289static int diffsize_consume(void *data, 290 char *line UNUSED, 291 unsigned long len UNUSED) 292{ 293 (*(int *)data)++; 294 return 0; 295} 296 297static void diffsize_hunk(void *data, 298 long ob UNUSED, long on UNUSED, 299 long nb UNUSED, long nn UNUSED, 300 const char *func UNUSED, long funclen UNUSED) 301{ 302 diffsize_consume(data, NULL, 0); 303} 304 305static int diffsize(const char *a, const char *b) 306{ 307 xpparam_t pp = { 0 }; 308 xdemitconf_t cfg = { 0 }; 309 mmfile_t mf1, mf2; 310 int count = 0; 311 312 mf1.ptr = (char *)a; 313 mf1.size = strlen(a); 314 mf2.ptr = (char *)b; 315 mf2.size = strlen(b); 316 317 cfg.ctxlen = 3; 318 if (!xdi_diff_outf(&mf1, &mf2, 319 diffsize_hunk, diffsize_consume, &count, 320 &pp, &cfg)) 321 return count; 322 323 error(_("failed to generate diff")); 324 return COST_MAX; 325} 326 327static void get_correspondences(struct string_list *a, struct string_list *b, 328 int creation_factor, size_t max_memory) 329{ 330 int n = a->nr + b->nr; 331 int *cost, c, *a2b, *b2a; 332 int i, j; 333 size_t cost_size = st_mult(n, n); 334 size_t cost_bytes = st_mult(sizeof(int), cost_size); 335 if (cost_bytes >= max_memory) { 336 struct strbuf cost_str = STRBUF_INIT; 337 struct strbuf max_str = STRBUF_INIT; 338 strbuf_humanise_bytes(&cost_str, cost_bytes); 339 strbuf_humanise_bytes(&max_str, max_memory); 340 die(_("range-diff: unable to compute the range-diff, since it " 341 "exceeds the maximum memory for the cost matrix: %s " 342 "(%"PRIuMAX" bytes) needed, limited to %s (%"PRIuMAX" bytes)"), 343 cost_str.buf, (uintmax_t)cost_bytes, max_str.buf, (uintmax_t)max_memory); 344 } 345 ALLOC_ARRAY(cost, cost_size); 346 ALLOC_ARRAY(a2b, n); 347 ALLOC_ARRAY(b2a, n); 348 349 for (i = 0; i < a->nr; i++) { 350 struct patch_util *a_util = a->items[i].util; 351 352 for (j = 0; j < b->nr; j++) { 353 struct patch_util *b_util = b->items[j].util; 354 355 if (a_util->matching == j) 356 c = 0; 357 else if (a_util->matching < 0 && b_util->matching < 0) 358 c = diffsize(a_util->diff, b_util->diff); 359 else 360 c = COST_MAX; 361 cost[i + n * j] = c; 362 } 363 364 c = a_util->matching < 0 ? 365 a_util->diffsize * creation_factor / 100 : COST_MAX; 366 for (j = b->nr; j < n; j++) 367 cost[i + n * j] = c; 368 } 369 370 for (j = 0; j < b->nr; j++) { 371 struct patch_util *util = b->items[j].util; 372 373 c = util->matching < 0 ? 374 util->diffsize * creation_factor / 100 : COST_MAX; 375 for (i = a->nr; i < n; i++) 376 cost[i + n * j] = c; 377 } 378 379 for (i = a->nr; i < n; i++) 380 for (j = b->nr; j < n; j++) 381 cost[i + n * j] = 0; 382 383 compute_assignment(n, n, cost, a2b, b2a); 384 385 for (i = 0; i < a->nr; i++) 386 if (a2b[i] >= 0 && a2b[i] < b->nr) { 387 struct patch_util *a_util = a->items[i].util; 388 struct patch_util *b_util = b->items[a2b[i]].util; 389 390 a_util->matching = a2b[i]; 391 b_util->matching = i; 392 } 393 394 free(cost); 395 free(a2b); 396 free(b2a); 397} 398 399static void output_pair_header(struct diff_options *diffopt, 400 int patch_no_width, 401 struct strbuf *buf, 402 struct strbuf *dashes, 403 struct patch_util *a_util, 404 struct patch_util *b_util) 405{ 406 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; 407 struct commit *commit; 408 char status; 409 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); 410 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); 411 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); 412 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); 413 const char *color; 414 int abbrev = diffopt->abbrev; 415 416 if (abbrev < 0) 417 abbrev = DEFAULT_ABBREV; 418 419 if (!dashes->len) 420 strbuf_addchars(dashes, '-', 421 strlen(repo_find_unique_abbrev(the_repository, oid, abbrev))); 422 423 if (!b_util) { 424 color = color_old; 425 status = '<'; 426 } else if (!a_util) { 427 color = color_new; 428 status = '>'; 429 } else if (strcmp(a_util->patch, b_util->patch)) { 430 color = color_commit; 431 status = '!'; 432 } else { 433 color = color_commit; 434 status = '='; 435 } 436 437 strbuf_reset(buf); 438 strbuf_addstr(buf, status == '!' ? color_old : color); 439 if (!a_util) 440 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); 441 else 442 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, 443 repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev)); 444 445 if (status == '!') 446 strbuf_addf(buf, "%s%s", color_reset, color); 447 strbuf_addch(buf, status); 448 if (status == '!') 449 strbuf_addf(buf, "%s%s", color_reset, color_new); 450 451 if (!b_util) 452 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); 453 else 454 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, 455 repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev)); 456 457 commit = lookup_commit_reference(the_repository, oid); 458 if (commit) { 459 if (status == '!') 460 strbuf_addf(buf, "%s%s", color_reset, color); 461 462 strbuf_addch(buf, ' '); 463 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); 464 } 465 strbuf_addf(buf, "%s\n", color_reset); 466 467 fwrite(buf->buf, buf->len, 1, diffopt->file); 468} 469 470static struct userdiff_driver section_headers = { 471 .funcname = { 472 .pattern = "^ ## (.*) ##$\n^.?@@ (.*)$", 473 .cflags = REG_EXTENDED, 474 }, 475}; 476 477static struct diff_filespec *get_filespec(const char *name, const char *p) 478{ 479 struct diff_filespec *spec = alloc_filespec(name); 480 481 fill_filespec(spec, null_oid(the_hash_algo), 0, 0100644); 482 spec->data = (char *)p; 483 spec->size = strlen(p); 484 spec->should_munmap = 0; 485 spec->is_stdin = 1; 486 spec->driver = &section_headers; 487 488 return spec; 489} 490 491static void patch_diff(const char *a, const char *b, 492 struct diff_options *diffopt) 493{ 494 diff_queue(&diff_queued_diff, 495 get_filespec("a", a), get_filespec("b", b)); 496 497 diffcore_std(diffopt); 498 diff_flush(diffopt); 499} 500 501static const char *output_prefix_cb(struct diff_options *opt UNUSED, void *data) 502{ 503 return data; 504} 505 506static void output(struct string_list *a, struct string_list *b, 507 struct range_diff_options *range_diff_opts) 508{ 509 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; 510 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); 511 int i = 0, j = 0; 512 struct diff_options opts; 513 struct strbuf indent = STRBUF_INIT; 514 515 if (range_diff_opts->diffopt) 516 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts)); 517 else 518 repo_diff_setup(the_repository, &opts); 519 520 opts.no_free = 1; 521 if (!opts.output_format) 522 opts.output_format = DIFF_FORMAT_PATCH; 523 opts.flags.suppress_diff_headers = 1; 524 opts.flags.dual_color_diffed_diffs = 525 range_diff_opts->dual_color; 526 opts.flags.suppress_hunk_header_line_count = 1; 527 opts.output_prefix = output_prefix_cb; 528 strbuf_addstr(&indent, " "); 529 opts.output_prefix_data = indent.buf; 530 diff_setup_done(&opts); 531 532 /* 533 * We assume the user is really more interested in the second argument 534 * ("newer" version). To that end, we print the output in the order of 535 * the RHS (the `b` parameter). To put the LHS (the `a` parameter) 536 * commits that are no longer in the RHS into a good place, we place 537 * them once we have shown all of their predecessors in the LHS. 538 */ 539 540 while (i < a->nr || j < b->nr) { 541 struct patch_util *a_util, *b_util; 542 a_util = i < a->nr ? a->items[i].util : NULL; 543 b_util = j < b->nr ? b->items[j].util : NULL; 544 545 /* Skip all the already-shown commits from the LHS. */ 546 while (i < a->nr && a_util->shown) 547 a_util = ++i < a->nr ? a->items[i].util : NULL; 548 549 /* Show unmatched LHS commit whose predecessors were shown. */ 550 if (i < a->nr && a_util->matching < 0) { 551 if (!range_diff_opts->right_only) 552 output_pair_header(&opts, patch_no_width, 553 &buf, &dashes, a_util, NULL); 554 i++; 555 continue; 556 } 557 558 /* Show unmatched RHS commits. */ 559 while (j < b->nr && b_util->matching < 0) { 560 if (!range_diff_opts->left_only) 561 output_pair_header(&opts, patch_no_width, 562 &buf, &dashes, NULL, b_util); 563 b_util = ++j < b->nr ? b->items[j].util : NULL; 564 } 565 566 /* Show matching LHS/RHS pair. */ 567 if (j < b->nr) { 568 a_util = a->items[b_util->matching].util; 569 output_pair_header(&opts, patch_no_width, 570 &buf, &dashes, a_util, b_util); 571 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT)) 572 patch_diff(a->items[b_util->matching].string, 573 b->items[j].string, &opts); 574 a_util->shown = 1; 575 j++; 576 } 577 } 578 strbuf_release(&buf); 579 strbuf_release(&dashes); 580 strbuf_release(&indent); 581 opts.no_free = 0; 582 diff_free(&opts); 583} 584 585int show_range_diff(const char *range1, const char *range2, 586 struct range_diff_options *range_diff_opts) 587{ 588 int res = 0; 589 590 struct string_list branch1 = STRING_LIST_INIT_DUP; 591 struct string_list branch2 = STRING_LIST_INIT_DUP; 592 unsigned int include_merges = range_diff_opts->include_merges; 593 594 if (range_diff_opts->left_only && range_diff_opts->right_only) 595 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only"); 596 597 if (!res && read_patches(range1, &branch1, range_diff_opts->log_arg, include_merges)) 598 res = error(_("could not parse log for '%s'"), range1); 599 if (!res && read_patches(range2, &branch2, range_diff_opts->log_arg, include_merges)) 600 res = error(_("could not parse log for '%s'"), range2); 601 602 if (!res) { 603 find_exact_matches(&branch1, &branch2); 604 get_correspondences(&branch1, &branch2, 605 range_diff_opts->creation_factor, 606 range_diff_opts->max_memory); 607 output(&branch1, &branch2, range_diff_opts); 608 } 609 610 string_list_clear(&branch1, 1); 611 string_list_clear(&branch2, 1); 612 613 return res; 614} 615 616int is_range_diff_range(const char *arg) 617{ 618 char *copy = xstrdup(arg); /* setup_revisions() modifies it */ 619 const char *argv[] = { "", copy, "--", NULL }; 620 int i, positive = 0, negative = 0; 621 struct rev_info revs; 622 623 repo_init_revisions(the_repository, &revs, NULL); 624 if (setup_revisions(3, argv, &revs, NULL) == 1) { 625 for (i = 0; i < revs.pending.nr; i++) 626 if (revs.pending.objects[i].item->flags & UNINTERESTING) 627 negative++; 628 else 629 positive++; 630 for (i = 0; i < revs.pending.nr; i++) { 631 struct object *obj = revs.pending.objects[i].item; 632 633 if (obj->type == OBJ_COMMIT) 634 clear_commit_marks((struct commit *)obj, 635 ALL_REV_FLAGS); 636 } 637 } 638 639 free(copy); 640 release_revisions(&revs); 641 return negative > 0 && positive > 0; 642}