Git fork
at 3da4413dbcdb8df021739ca6f20fe4f0bcd1fd3c 630 lines 17 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 *other_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 (other_arg) 73 strvec_pushv(&cp.args, other_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) 329{ 330 int n = a->nr + b->nr; 331 int *cost, c, *a2b, *b2a; 332 int i, j; 333 334 ALLOC_ARRAY(cost, st_mult(n, n)); 335 ALLOC_ARRAY(a2b, n); 336 ALLOC_ARRAY(b2a, n); 337 338 for (i = 0; i < a->nr; i++) { 339 struct patch_util *a_util = a->items[i].util; 340 341 for (j = 0; j < b->nr; j++) { 342 struct patch_util *b_util = b->items[j].util; 343 344 if (a_util->matching == j) 345 c = 0; 346 else if (a_util->matching < 0 && b_util->matching < 0) 347 c = diffsize(a_util->diff, b_util->diff); 348 else 349 c = COST_MAX; 350 cost[i + n * j] = c; 351 } 352 353 c = a_util->matching < 0 ? 354 a_util->diffsize * creation_factor / 100 : COST_MAX; 355 for (j = b->nr; j < n; j++) 356 cost[i + n * j] = c; 357 } 358 359 for (j = 0; j < b->nr; j++) { 360 struct patch_util *util = b->items[j].util; 361 362 c = util->matching < 0 ? 363 util->diffsize * creation_factor / 100 : COST_MAX; 364 for (i = a->nr; i < n; i++) 365 cost[i + n * j] = c; 366 } 367 368 for (i = a->nr; i < n; i++) 369 for (j = b->nr; j < n; j++) 370 cost[i + n * j] = 0; 371 372 compute_assignment(n, n, cost, a2b, b2a); 373 374 for (i = 0; i < a->nr; i++) 375 if (a2b[i] >= 0 && a2b[i] < b->nr) { 376 struct patch_util *a_util = a->items[i].util; 377 struct patch_util *b_util = b->items[a2b[i]].util; 378 379 a_util->matching = a2b[i]; 380 b_util->matching = i; 381 } 382 383 free(cost); 384 free(a2b); 385 free(b2a); 386} 387 388static void output_pair_header(struct diff_options *diffopt, 389 int patch_no_width, 390 struct strbuf *buf, 391 struct strbuf *dashes, 392 struct patch_util *a_util, 393 struct patch_util *b_util) 394{ 395 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; 396 struct commit *commit; 397 char status; 398 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); 399 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); 400 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); 401 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); 402 const char *color; 403 int abbrev = diffopt->abbrev; 404 405 if (abbrev < 0) 406 abbrev = DEFAULT_ABBREV; 407 408 if (!dashes->len) 409 strbuf_addchars(dashes, '-', 410 strlen(repo_find_unique_abbrev(the_repository, oid, abbrev))); 411 412 if (!b_util) { 413 color = color_old; 414 status = '<'; 415 } else if (!a_util) { 416 color = color_new; 417 status = '>'; 418 } else if (strcmp(a_util->patch, b_util->patch)) { 419 color = color_commit; 420 status = '!'; 421 } else { 422 color = color_commit; 423 status = '='; 424 } 425 426 strbuf_reset(buf); 427 strbuf_addstr(buf, status == '!' ? color_old : color); 428 if (!a_util) 429 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); 430 else 431 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, 432 repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev)); 433 434 if (status == '!') 435 strbuf_addf(buf, "%s%s", color_reset, color); 436 strbuf_addch(buf, status); 437 if (status == '!') 438 strbuf_addf(buf, "%s%s", color_reset, color_new); 439 440 if (!b_util) 441 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); 442 else 443 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, 444 repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev)); 445 446 commit = lookup_commit_reference(the_repository, oid); 447 if (commit) { 448 if (status == '!') 449 strbuf_addf(buf, "%s%s", color_reset, color); 450 451 strbuf_addch(buf, ' '); 452 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); 453 } 454 strbuf_addf(buf, "%s\n", color_reset); 455 456 fwrite(buf->buf, buf->len, 1, diffopt->file); 457} 458 459static struct userdiff_driver section_headers = { 460 .funcname = { 461 .pattern = "^ ## (.*) ##$\n^.?@@ (.*)$", 462 .cflags = REG_EXTENDED, 463 }, 464}; 465 466static struct diff_filespec *get_filespec(const char *name, const char *p) 467{ 468 struct diff_filespec *spec = alloc_filespec(name); 469 470 fill_filespec(spec, null_oid(the_hash_algo), 0, 0100644); 471 spec->data = (char *)p; 472 spec->size = strlen(p); 473 spec->should_munmap = 0; 474 spec->is_stdin = 1; 475 spec->driver = &section_headers; 476 477 return spec; 478} 479 480static void patch_diff(const char *a, const char *b, 481 struct diff_options *diffopt) 482{ 483 diff_queue(&diff_queued_diff, 484 get_filespec("a", a), get_filespec("b", b)); 485 486 diffcore_std(diffopt); 487 diff_flush(diffopt); 488} 489 490static const char *output_prefix_cb(struct diff_options *opt UNUSED, void *data) 491{ 492 return data; 493} 494 495static void output(struct string_list *a, struct string_list *b, 496 struct range_diff_options *range_diff_opts) 497{ 498 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; 499 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); 500 int i = 0, j = 0; 501 struct diff_options opts; 502 struct strbuf indent = STRBUF_INIT; 503 504 if (range_diff_opts->diffopt) 505 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts)); 506 else 507 repo_diff_setup(the_repository, &opts); 508 509 opts.no_free = 1; 510 if (!opts.output_format) 511 opts.output_format = DIFF_FORMAT_PATCH; 512 opts.flags.suppress_diff_headers = 1; 513 opts.flags.dual_color_diffed_diffs = 514 range_diff_opts->dual_color; 515 opts.flags.suppress_hunk_header_line_count = 1; 516 opts.output_prefix = output_prefix_cb; 517 strbuf_addstr(&indent, " "); 518 opts.output_prefix_data = indent.buf; 519 diff_setup_done(&opts); 520 521 /* 522 * We assume the user is really more interested in the second argument 523 * ("newer" version). To that end, we print the output in the order of 524 * the RHS (the `b` parameter). To put the LHS (the `a` parameter) 525 * commits that are no longer in the RHS into a good place, we place 526 * them once we have shown all of their predecessors in the LHS. 527 */ 528 529 while (i < a->nr || j < b->nr) { 530 struct patch_util *a_util, *b_util; 531 a_util = i < a->nr ? a->items[i].util : NULL; 532 b_util = j < b->nr ? b->items[j].util : NULL; 533 534 /* Skip all the already-shown commits from the LHS. */ 535 while (i < a->nr && a_util->shown) 536 a_util = ++i < a->nr ? a->items[i].util : NULL; 537 538 /* Show unmatched LHS commit whose predecessors were shown. */ 539 if (i < a->nr && a_util->matching < 0) { 540 if (!range_diff_opts->right_only) 541 output_pair_header(&opts, patch_no_width, 542 &buf, &dashes, a_util, NULL); 543 i++; 544 continue; 545 } 546 547 /* Show unmatched RHS commits. */ 548 while (j < b->nr && b_util->matching < 0) { 549 if (!range_diff_opts->left_only) 550 output_pair_header(&opts, patch_no_width, 551 &buf, &dashes, NULL, b_util); 552 b_util = ++j < b->nr ? b->items[j].util : NULL; 553 } 554 555 /* Show matching LHS/RHS pair. */ 556 if (j < b->nr) { 557 a_util = a->items[b_util->matching].util; 558 output_pair_header(&opts, patch_no_width, 559 &buf, &dashes, a_util, b_util); 560 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT)) 561 patch_diff(a->items[b_util->matching].string, 562 b->items[j].string, &opts); 563 a_util->shown = 1; 564 j++; 565 } 566 } 567 strbuf_release(&buf); 568 strbuf_release(&dashes); 569 strbuf_release(&indent); 570 opts.no_free = 0; 571 diff_free(&opts); 572} 573 574int show_range_diff(const char *range1, const char *range2, 575 struct range_diff_options *range_diff_opts) 576{ 577 int res = 0; 578 579 struct string_list branch1 = STRING_LIST_INIT_DUP; 580 struct string_list branch2 = STRING_LIST_INIT_DUP; 581 unsigned int include_merges = range_diff_opts->include_merges; 582 583 if (range_diff_opts->left_only && range_diff_opts->right_only) 584 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only"); 585 586 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg, include_merges)) 587 res = error(_("could not parse log for '%s'"), range1); 588 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg, include_merges)) 589 res = error(_("could not parse log for '%s'"), range2); 590 591 if (!res) { 592 find_exact_matches(&branch1, &branch2); 593 get_correspondences(&branch1, &branch2, 594 range_diff_opts->creation_factor); 595 output(&branch1, &branch2, range_diff_opts); 596 } 597 598 string_list_clear(&branch1, 1); 599 string_list_clear(&branch2, 1); 600 601 return res; 602} 603 604int is_range_diff_range(const char *arg) 605{ 606 char *copy = xstrdup(arg); /* setup_revisions() modifies it */ 607 const char *argv[] = { "", copy, "--", NULL }; 608 int i, positive = 0, negative = 0; 609 struct rev_info revs; 610 611 repo_init_revisions(the_repository, &revs, NULL); 612 if (setup_revisions(3, argv, &revs, NULL) == 1) { 613 for (i = 0; i < revs.pending.nr; i++) 614 if (revs.pending.objects[i].item->flags & UNINTERESTING) 615 negative++; 616 else 617 positive++; 618 for (i = 0; i < revs.pending.nr; i++) { 619 struct object *obj = revs.pending.objects[i].item; 620 621 if (obj->type == OBJ_COMMIT) 622 clear_commit_marks((struct commit *)obj, 623 ALL_REV_FLAGS); 624 } 625 } 626 627 free(copy); 628 release_revisions(&revs); 629 return negative > 0 && positive > 0; 630}