Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/line-log.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / line-log.c` 1210 lines ยท 29775 bytes Raw Blame History 1 #define DISABLE_SIGN_COMPARE_WARNINGS 2 3 #include "git-compat-util.h" 4 #include "diffcore.h" 5 #include "line-range.h" 6 #include "hex.h" 7 #include "tag.h" 8 #include "tree.h" 9 #include "diff.h" 10 #include "commit.h" 11 #include "decorate.h" 12 #include "repository.h" 13 #include "revision.h" 14 #include "xdiff-interface.h" 15 #include "strbuf.h" 16 #include "line-log.h" 17 #include "setup.h" 18 #include "strvec.h" 19 #include "bloom.h" 20 #include "tree-walk.h" 21 22 static void range_set_grow(struct range_set *rs, size_t extra) 23 { 24 ALLOC_GROW(rs->ranges, rs->nr + extra, rs->alloc); 25 } 26 27 /* Either initialization would be fine */ 28 #define RANGE_SET_INIT {0} 29 30 void range_set_init(struct range_set *rs, size_t prealloc) 31 { 32 rs->alloc = rs->nr = 0; 33 rs->ranges = NULL; 34 if (prealloc) 35 range_set_grow(rs, prealloc); 36 } 37 38 void range_set_release(struct range_set *rs) 39 { 40 FREE_AND_NULL(rs->ranges); 41 rs->alloc = rs->nr = 0; 42 } 43 44 /* dst must be uninitialized! */ 45 static void range_set_copy(struct range_set *dst, struct range_set *src) 46 { 47 range_set_init(dst, src->nr); 48 COPY_ARRAY(dst->ranges, src->ranges, src->nr); 49 dst->nr = src->nr; 50 } 51 52 static void range_set_move(struct range_set *dst, struct range_set *src) 53 { 54 range_set_release(dst); 55 dst->ranges = src->ranges; 56 dst->nr = src->nr; 57 dst->alloc = src->alloc; 58 src->ranges = NULL; 59 src->alloc = src->nr = 0; 60 } 61 62 /* tack on a _new_ range _at the end_ */ 63 void range_set_append_unsafe(struct range_set *rs, long a, long b) 64 { 65 assert(a <= b); 66 range_set_grow(rs, 1); 67 rs->ranges[rs->nr].start = a; 68 rs->ranges[rs->nr].end = b; 69 rs->nr++; 70 } 71 72 void range_set_append(struct range_set *rs, long a, long b) 73 { 74 assert(rs->nr == 0 || rs->ranges[rs->nr-1].end <= a); 75 range_set_append_unsafe(rs, a, b); 76 } 77 78 static int range_cmp(const void *_r, const void *_s) 79 { 80 const struct range *r = _r; 81 const struct range *s = _s; 82 83 /* this could be simply 'return r.start-s.start', but for the types */ 84 if (r->start == s->start) 85 return 0; 86 if (r->start < s->start) 87 return -1; 88 return 1; 89 } 90 91 /* 92 * Check that the ranges are non-empty, sorted and non-overlapping 93 */ 94 static void range_set_check_invariants(struct range_set *rs) 95 { 96 unsigned int i; 97 98 if (!rs) 99 return; 100 101 if (rs->nr) 102 assert(rs->ranges[0].start < rs->ranges[0].end); 103 104 for (i = 1; i < rs->nr; i++) { 105 assert(rs->ranges[i-1].end < rs->ranges[i].start); 106 assert(rs->ranges[i].start < rs->ranges[i].end); 107 } 108 } 109 110 /* 111 * In-place pass of sorting and merging the ranges in the range set, 112 * to establish the invariants when we get the ranges from the user 113 */ 114 void sort_and_merge_range_set(struct range_set *rs) 115 { 116 unsigned int i; 117 unsigned int o = 0; /* output cursor */ 118 119 QSORT(rs->ranges, rs->nr, range_cmp); 120 121 for (i = 0; i < rs->nr; i++) { 122 if (rs->ranges[i].start == rs->ranges[i].end) 123 continue; 124 if (o > 0 && rs->ranges[i].start <= rs->ranges[o-1].end) { 125 if (rs->ranges[o-1].end < rs->ranges[i].end) 126 rs->ranges[o-1].end = rs->ranges[i].end; 127 } else { 128 rs->ranges[o].start = rs->ranges[i].start; 129 rs->ranges[o].end = rs->ranges[i].end; 130 o++; 131 } 132 } 133 assert(o <= rs->nr); 134 rs->nr = o; 135 136 range_set_check_invariants(rs); 137 } 138 139 /* 140 * Union of range sets (i.e., sets of line numbers). Used to merge 141 * them when searches meet at a common ancestor. 142 * 143 * This is also where the ranges are consolidated into canonical form: 144 * overlapping and adjacent ranges are merged, and empty ranges are 145 * removed. 146 */ 147 static void range_set_union(struct range_set *out, 148 struct range_set *a, struct range_set *b) 149 { 150 unsigned int i = 0, j = 0; 151 struct range *ra = a->ranges; 152 struct range *rb = b->ranges; 153 /* cannot make an alias of out->ranges: it may change during grow */ 154 155 assert(out->nr == 0); 156 while (i < a->nr || j < b->nr) { 157 struct range *new_range; 158 if (i < a->nr && j < b->nr) { 159 if (ra[i].start < rb[j].start) 160 new_range = &ra[i++]; 161 else if (ra[i].start > rb[j].start) 162 new_range = &rb[j++]; 163 else if (ra[i].end < rb[j].end) 164 new_range = &ra[i++]; 165 else 166 new_range = &rb[j++]; 167 } else if (i < a->nr) /* b exhausted */ 168 new_range = &ra[i++]; 169 else /* a exhausted */ 170 new_range = &rb[j++]; 171 if (new_range->start == new_range->end) 172 ; /* empty range */ 173 else if (!out->nr || out->ranges[out->nr-1].end < new_range->start) { 174 range_set_grow(out, 1); 175 out->ranges[out->nr].start = new_range->start; 176 out->ranges[out->nr].end = new_range->end; 177 out->nr++; 178 } else if (out->ranges[out->nr-1].end < new_range->end) { 179 out->ranges[out->nr-1].end = new_range->end; 180 } 181 } 182 } 183 184 /* 185 * Difference of range sets (out = a \ b). Pass the "interesting" 186 * ranges as 'a' and the target side of the diff as 'b': it removes 187 * the ranges for which the commit is responsible. 188 */ 189 static void range_set_difference(struct range_set *out, 190 struct range_set *a, struct range_set *b) 191 { 192 unsigned int i, j = 0; 193 for (i = 0; i < a->nr; i++) { 194 long start = a->ranges[i].start; 195 long end = a->ranges[i].end; 196 while (start < end) { 197 while (j < b->nr && start >= b->ranges[j].end) 198 /* 199 * a: |------- 200 * b: ------| 201 */ 202 j++; 203 if (j >= b->nr || end <= b->ranges[j].start) { 204 /* 205 * b exhausted, or 206 * a: ----| 207 * b: |---- 208 */ 209 range_set_append(out, start, end); 210 break; 211 } 212 if (start >= b->ranges[j].start) { 213 /* 214 * a: |--???? 215 * b: |------| 216 */ 217 start = b->ranges[j].end; 218 } else if (end > b->ranges[j].start) { 219 /* 220 * a: |-----| 221 * b: |--????? 222 */ 223 if (start < b->ranges[j].start) 224 range_set_append(out, start, b->ranges[j].start); 225 start = b->ranges[j].end; 226 } 227 } 228 } 229 } 230 231 static void diff_ranges_init(struct diff_ranges *diff) 232 { 233 range_set_init(&diff->parent, 0); 234 range_set_init(&diff->target, 0); 235 } 236 237 static void diff_ranges_release(struct diff_ranges *diff) 238 { 239 range_set_release(&diff->parent); 240 range_set_release(&diff->target); 241 } 242 243 static void line_log_data_init(struct line_log_data *r) 244 { 245 memset(r, 0, sizeof(struct line_log_data)); 246 range_set_init(&r->ranges, 0); 247 } 248 249 static void line_log_data_clear(struct line_log_data *r) 250 { 251 range_set_release(&r->ranges); 252 free(r->path); 253 if (r->pair) 254 diff_free_filepair(r->pair); 255 diff_ranges_release(&r->diff); 256 } 257 258 static void free_line_log_data(struct line_log_data *r) 259 { 260 while (r) { 261 struct line_log_data *next = r->next; 262 line_log_data_clear(r); 263 free(r); 264 r = next; 265 } 266 } 267 268 static struct line_log_data * 269 search_line_log_data(struct line_log_data *list, const char *path, 270 struct line_log_data **insertion_point) 271 { 272 struct line_log_data *p = list; 273 if (insertion_point) 274 *insertion_point = NULL; 275 while (p) { 276 int cmp = strcmp(p->path, path); 277 if (!cmp) 278 return p; 279 if (insertion_point && cmp < 0) 280 *insertion_point = p; 281 p = p->next; 282 } 283 return NULL; 284 } 285 286 /* 287 * Note: takes ownership of 'path', which happens to be what the only 288 * caller needs. 289 */ 290 static void line_log_data_insert(struct line_log_data **list, 291 char *path, 292 long begin, long end) 293 { 294 struct line_log_data *ip; 295 struct line_log_data *p = search_line_log_data(*list, path, &ip); 296 297 if (p) { 298 range_set_append_unsafe(&p->ranges, begin, end); 299 free(path); 300 return; 301 } 302 303 CALLOC_ARRAY(p, 1); 304 p->path = path; 305 range_set_append(&p->ranges, begin, end); 306 if (ip) { 307 p->next = ip->next; 308 ip->next = p; 309 } else { 310 p->next = *list; 311 *list = p; 312 } 313 } 314 315 struct collect_diff_cbdata { 316 struct diff_ranges *diff; 317 }; 318 319 static int collect_diff_cb(long start_a, long count_a, 320 long start_b, long count_b, 321 void *data) 322 { 323 struct collect_diff_cbdata *d = data; 324 325 if (count_a >= 0) 326 range_set_append(&d->diff->parent, start_a, start_a + count_a); 327 if (count_b >= 0) 328 range_set_append(&d->diff->target, start_b, start_b + count_b); 329 330 return 0; 331 } 332 333 static int collect_diff(mmfile_t *parent, mmfile_t *target, struct diff_ranges *out) 334 { 335 struct collect_diff_cbdata cbdata = {NULL}; 336 xpparam_t xpp; 337 xdemitconf_t xecfg; 338 xdemitcb_t ecb; 339 340 memset(&xpp, 0, sizeof(xpp)); 341 memset(&xecfg, 0, sizeof(xecfg)); 342 xecfg.ctxlen = xecfg.interhunkctxlen = 0; 343 344 cbdata.diff = out; 345 xecfg.hunk_func = collect_diff_cb; 346 memset(&ecb, 0, sizeof(ecb)); 347 ecb.priv = &cbdata; 348 return xdi_diff(parent, target, &xpp, &xecfg, &ecb); 349 } 350 351 /* 352 * These are handy for debugging. Removing them with #if 0 silences 353 * the "unused function" warning. 354 */ 355 #if 0 356 static void dump_range_set(struct range_set *rs, const char *desc) 357 { 358 int i; 359 printf("range set %s (%d items):\n", desc, rs->nr); 360 for (i = 0; i < rs->nr; i++) 361 printf("\t[%ld,%ld]\n", rs->ranges[i].start, rs->ranges[i].end); 362 } 363 364 static void dump_line_log_data(struct line_log_data *r) 365 { 366 char buf[4096]; 367 while (r) { 368 snprintf(buf, 4096, "file %s\n", r->path); 369 dump_range_set(&r->ranges, buf); 370 r = r->next; 371 } 372 } 373 374 static void dump_diff_ranges(struct diff_ranges *diff, const char *desc) 375 { 376 int i; 377 assert(diff->parent.nr == diff->target.nr); 378 printf("diff ranges %s (%d items):\n", desc, diff->parent.nr); 379 printf("\tparent\ttarget\n"); 380 for (i = 0; i < diff->parent.nr; i++) { 381 printf("\t[%ld,%ld]\t[%ld,%ld]\n", 382 diff->parent.ranges[i].start, 383 diff->parent.ranges[i].end, 384 diff->target.ranges[i].start, 385 diff->target.ranges[i].end); 386 } 387 } 388 #endif 389 390 391 static int ranges_overlap(struct range *a, struct range *b) 392 { 393 return !(a->end <= b->start || b->end <= a->start); 394 } 395 396 /* 397 * Given a diff and the set of interesting ranges, determine all hunks 398 * of the diff which touch (overlap) at least one of the interesting 399 * ranges in the target. 400 */ 401 static void diff_ranges_filter_touched(struct diff_ranges *out, 402 struct diff_ranges *diff, 403 struct range_set *rs) 404 { 405 unsigned int i, j = 0; 406 407 assert(out->target.nr == 0); 408 409 for (i = 0; i < diff->target.nr; i++) { 410 while (diff->target.ranges[i].start >= rs->ranges[j].end) { 411 j++; 412 if (j == rs->nr) 413 return; 414 } 415 if (ranges_overlap(&diff->target.ranges[i], &rs->ranges[j])) { 416 range_set_append(&out->parent, 417 diff->parent.ranges[i].start, 418 diff->parent.ranges[i].end); 419 range_set_append(&out->target, 420 diff->target.ranges[i].start, 421 diff->target.ranges[i].end); 422 } 423 } 424 } 425 426 /* 427 * Adjust the line counts in 'rs' to account for the lines 428 * added/removed in the diff. 429 */ 430 static void range_set_shift_diff(struct range_set *out, 431 struct range_set *rs, 432 struct diff_ranges *diff) 433 { 434 unsigned int i, j = 0; 435 long offset = 0; 436 struct range *src = rs->ranges; 437 struct range *target = diff->target.ranges; 438 struct range *parent = diff->parent.ranges; 439 440 for (i = 0; i < rs->nr; i++) { 441 while (j < diff->target.nr && src[i].start >= target[j].start) { 442 offset += (parent[j].end-parent[j].start) 443 - (target[j].end-target[j].start); 444 j++; 445 } 446 range_set_append(out, src[i].start+offset, src[i].end+offset); 447 } 448 } 449 450 /* 451 * Given a diff and the set of interesting ranges, map the ranges 452 * across the diff. That is: observe that the target commit takes 453 * blame for all the + (target-side) ranges. So for every pair of 454 * ranges in the diff that was touched, we remove the latter and add 455 * its parent side. 456 */ 457 static void range_set_map_across_diff(struct range_set *out, 458 struct range_set *rs, 459 struct diff_ranges *diff, 460 struct diff_ranges **touched_out) 461 { 462 struct diff_ranges *touched = xmalloc(sizeof(*touched)); 463 struct range_set tmp1 = RANGE_SET_INIT; 464 struct range_set tmp2 = RANGE_SET_INIT; 465 466 diff_ranges_init(touched); 467 diff_ranges_filter_touched(touched, diff, rs); 468 range_set_difference(&tmp1, rs, &touched->target); 469 range_set_shift_diff(&tmp2, &tmp1, diff); 470 range_set_union(out, &tmp2, &touched->parent); 471 range_set_release(&tmp1); 472 range_set_release(&tmp2); 473 474 *touched_out = touched; 475 } 476 477 static struct commit *check_single_commit(struct rev_info *revs) 478 { 479 struct object *commit = NULL; 480 int found = -1; 481 int i; 482 483 for (i = 0; i < revs->pending.nr; i++) { 484 struct object *obj = revs->pending.objects[i].item; 485 if (obj->flags & UNINTERESTING) 486 continue; 487 obj = deref_tag(revs->repo, obj, NULL, 0); 488 if (!obj || obj->type != OBJ_COMMIT) 489 die("Non commit %s?", revs->pending.objects[i].name); 490 if (commit) 491 die("More than one commit to dig from: %s and %s?", 492 revs->pending.objects[i].name, 493 revs->pending.objects[found].name); 494 commit = obj; 495 found = i; 496 } 497 498 if (!commit) 499 die("No commit specified?"); 500 501 return (struct commit *) commit; 502 } 503 504 static void fill_blob_sha1(struct repository *r, struct commit *commit, 505 struct diff_filespec *spec) 506 { 507 unsigned short mode; 508 struct object_id oid; 509 510 if (get_tree_entry(r, &commit->object.oid, spec->path, &oid, &mode)) 511 die("There is no path %s in the commit", spec->path); 512 fill_filespec(spec, &oid, 1, mode); 513 514 return; 515 } 516 517 static void fill_line_ends(struct repository *r, 518 struct diff_filespec *spec, 519 long *lines, 520 unsigned long **line_ends) 521 { 522 int num = 0, size = 50; 523 long cur = 0; 524 unsigned long *ends = NULL; 525 char *data = NULL; 526 527 if (diff_populate_filespec(r, spec, NULL)) 528 die("Cannot read blob %s", oid_to_hex(&spec->oid)); 529 530 ALLOC_ARRAY(ends, size); 531 ends[cur++] = 0; 532 data = spec->data; 533 while (num < spec->size) { 534 if (data[num] == '\n' || num == spec->size - 1) { 535 ALLOC_GROW(ends, (cur + 1), size); 536 ends[cur++] = num; 537 } 538 num++; 539 } 540 541 /* shrink the array to fit the elements */ 542 REALLOC_ARRAY(ends, cur); 543 *lines = cur-1; 544 *line_ends = ends; 545 } 546 547 struct nth_line_cb { 548 struct diff_filespec *spec; 549 long lines; 550 unsigned long *line_ends; 551 }; 552 553 static const char *nth_line(void *data, long line) 554 { 555 struct nth_line_cb *d = data; 556 assert(d && line <= d->lines); 557 assert(d->spec && d->spec->data); 558 559 if (line == 0) 560 return (char *)d->spec->data; 561 else 562 return (char *)d->spec->data + d->line_ends[line] + 1; 563 } 564 565 static struct line_log_data * 566 parse_lines(struct repository *r, struct commit *commit, 567 const char *prefix, struct string_list *args) 568 { 569 long lines = 0; 570 unsigned long *ends = NULL; 571 struct nth_line_cb cb_data; 572 struct string_list_item *item; 573 struct line_log_data *ranges = NULL; 574 struct line_log_data *p; 575 576 for_each_string_list_item(item, args) { 577 const char *name_part; 578 char *range_part; 579 char *full_name; 580 struct diff_filespec *spec; 581 long begin = 0, end = 0; 582 long anchor; 583 584 name_part = skip_range_arg(item->string, r->index); 585 if (!name_part || *name_part != ':' || !name_part[1]) 586 die("-L argument not 'start,end:file' or ':funcname:file': %s", 587 item->string); 588 range_part = xstrndup(item->string, name_part - item->string); 589 name_part++; 590 591 full_name = prefix_path(r, prefix, prefix ? strlen(prefix) : 0, 592 name_part); 593 594 spec = alloc_filespec(full_name); 595 fill_blob_sha1(r, commit, spec); 596 fill_line_ends(r, spec, &lines, &ends); 597 cb_data.spec = spec; 598 cb_data.lines = lines; 599 cb_data.line_ends = ends; 600 601 p = search_line_log_data(ranges, full_name, NULL); 602 if (p && p->ranges.nr) 603 anchor = p->ranges.ranges[p->ranges.nr - 1].end + 1; 604 else 605 anchor = 1; 606 607 if (parse_range_arg(range_part, nth_line, &cb_data, 608 lines, anchor, &begin, &end, 609 full_name, r->index)) 610 die("malformed -L argument '%s'", range_part); 611 if ((!lines && (begin || end)) || lines < begin) 612 die("file %s has only %lu lines", name_part, lines); 613 if (begin < 1) 614 begin = 1; 615 if (end < 1 || lines < end) 616 end = lines; 617 begin--; 618 line_log_data_insert(&ranges, full_name, begin, end); 619 620 free_filespec(spec); 621 FREE_AND_NULL(ends); 622 free(range_part); 623 } 624 625 for (p = ranges; p; p = p->next) 626 sort_and_merge_range_set(&p->ranges); 627 628 return ranges; 629 } 630 631 static struct line_log_data *line_log_data_copy_one(struct line_log_data *r) 632 { 633 struct line_log_data *ret = xmalloc(sizeof(*ret)); 634 635 assert(r); 636 line_log_data_init(ret); 637 range_set_copy(&ret->ranges, &r->ranges); 638 639 ret->path = xstrdup(r->path); 640 641 return ret; 642 } 643 644 static struct line_log_data * 645 line_log_data_copy(struct line_log_data *r) 646 { 647 struct line_log_data *ret = NULL; 648 struct line_log_data *tmp = NULL, *prev = NULL; 649 650 assert(r); 651 ret = tmp = prev = line_log_data_copy_one(r); 652 r = r->next; 653 while (r) { 654 tmp = line_log_data_copy_one(r); 655 prev->next = tmp; 656 prev = tmp; 657 r = r->next; 658 } 659 660 return ret; 661 } 662 663 /* merge two range sets across files */ 664 static struct line_log_data *line_log_data_merge(struct line_log_data *a, 665 struct line_log_data *b) 666 { 667 struct line_log_data *head = NULL, **pp = &head; 668 669 while (a || b) { 670 struct line_log_data *src; 671 struct line_log_data *src2 = NULL; 672 struct line_log_data *d; 673 int cmp; 674 if (!a) 675 cmp = 1; 676 else if (!b) 677 cmp = -1; 678 else 679 cmp = strcmp(a->path, b->path); 680 if (cmp < 0) { 681 src = a; 682 a = a->next; 683 } else if (cmp == 0) { 684 src = a; 685 a = a->next; 686 src2 = b; 687 b = b->next; 688 } else { 689 src = b; 690 b = b->next; 691 } 692 d = xmalloc(sizeof(struct line_log_data)); 693 line_log_data_init(d); 694 d->path = xstrdup(src->path); 695 *pp = d; 696 pp = &d->next; 697 if (src2) 698 range_set_union(&d->ranges, &src->ranges, &src2->ranges); 699 else 700 range_set_copy(&d->ranges, &src->ranges); 701 } 702 703 return head; 704 } 705 706 static void add_line_range(struct rev_info *revs, struct commit *commit, 707 struct line_log_data *range) 708 { 709 struct line_log_data *old_line = NULL; 710 struct line_log_data *new_line = NULL; 711 712 old_line = lookup_decoration(&revs->line_log_data, &commit->object); 713 if (old_line && range) { 714 new_line = line_log_data_merge(old_line, range); 715 free_line_log_data(old_line); 716 } else if (range) 717 new_line = line_log_data_copy(range); 718 719 if (new_line) 720 add_decoration(&revs->line_log_data, &commit->object, new_line); 721 } 722 723 static void clear_commit_line_range(struct rev_info *revs, struct commit *commit) 724 { 725 struct line_log_data *r; 726 r = lookup_decoration(&revs->line_log_data, &commit->object); 727 if (!r) 728 return; 729 free_line_log_data(r); 730 add_decoration(&revs->line_log_data, &commit->object, NULL); 731 } 732 733 static struct line_log_data *lookup_line_range(struct rev_info *revs, 734 struct commit *commit) 735 { 736 struct line_log_data *ret = NULL; 737 struct line_log_data *d; 738 739 ret = lookup_decoration(&revs->line_log_data, &commit->object); 740 741 for (d = ret; d; d = d->next) 742 range_set_check_invariants(&d->ranges); 743 744 return ret; 745 } 746 747 static int same_paths_in_pathspec_and_range(struct pathspec *pathspec, 748 struct line_log_data *range) 749 { 750 int i; 751 struct line_log_data *r; 752 753 for (i = 0, r = range; i < pathspec->nr && r; i++, r = r->next) 754 if (strcmp(pathspec->items[i].match, r->path)) 755 return 0; 756 if (i < pathspec->nr || r) 757 /* different number of pathspec items and ranges */ 758 return 0; 759 760 return 1; 761 } 762 763 static void parse_pathspec_from_ranges(struct pathspec *pathspec, 764 struct line_log_data *range) 765 { 766 struct line_log_data *r; 767 struct strvec array = STRVEC_INIT; 768 769 for (r = range; r; r = r->next) 770 strvec_push(&array, r->path); 771 772 parse_pathspec(pathspec, 0, PATHSPEC_PREFER_FULL, "", array.v); 773 774 strvec_clear(&array); 775 } 776 777 void line_log_init(struct rev_info *rev, const char *prefix, struct string_list *args) 778 { 779 struct commit *commit = NULL; 780 struct line_log_data *range; 781 782 commit = check_single_commit(rev); 783 range = parse_lines(rev->diffopt.repo, commit, prefix, args); 784 add_line_range(rev, commit, range); 785 786 parse_pathspec_from_ranges(&rev->diffopt.pathspec, range); 787 788 free_line_log_data(range); 789 } 790 791 static void move_diff_queue(struct diff_queue_struct *dst, 792 struct diff_queue_struct *src) 793 { 794 assert(src != dst); 795 memcpy(dst, src, sizeof(*dst)); 796 diff_queue_init(src); 797 } 798 799 static void filter_diffs_for_paths(struct line_log_data *range, int keep_deletions) 800 { 801 int i; 802 struct diff_queue_struct outq = DIFF_QUEUE_INIT; 803 804 for (i = 0; i < diff_queued_diff.nr; i++) { 805 struct diff_filepair *p = diff_queued_diff.queue[i]; 806 struct line_log_data *rg = NULL; 807 808 if (!DIFF_FILE_VALID(p->two)) { 809 if (keep_deletions) 810 diff_q(&outq, p); 811 else 812 diff_free_filepair(p); 813 continue; 814 } 815 for (rg = range; rg; rg = rg->next) { 816 if (!strcmp(rg->path, p->two->path)) 817 break; 818 } 819 if (rg) 820 diff_q(&outq, p); 821 else 822 diff_free_filepair(p); 823 } 824 free(diff_queued_diff.queue); 825 diff_queued_diff = outq; 826 } 827 828 static inline int diff_might_be_rename(void) 829 { 830 int i; 831 for (i = 0; i < diff_queued_diff.nr; i++) 832 if (!DIFF_FILE_VALID(diff_queued_diff.queue[i]->one)) { 833 /* fprintf(stderr, "diff_might_be_rename found creation of: %s\n", */ 834 /* diff_queued_diff.queue[i]->two->path); */ 835 return 1; 836 } 837 return 0; 838 } 839 840 static void queue_diffs(struct line_log_data *range, 841 struct diff_options *opt, 842 struct diff_queue_struct *queue, 843 struct commit *commit, struct commit *parent) 844 { 845 struct object_id *tree_oid, *parent_tree_oid; 846 847 assert(commit); 848 849 tree_oid = get_commit_tree_oid(commit); 850 parent_tree_oid = parent ? get_commit_tree_oid(parent) : NULL; 851 852 if (opt->detect_rename && 853 !same_paths_in_pathspec_and_range(&opt->pathspec, range)) { 854 clear_pathspec(&opt->pathspec); 855 parse_pathspec_from_ranges(&opt->pathspec, range); 856 } 857 diff_queue_clear(&diff_queued_diff); 858 diff_tree_oid(parent_tree_oid, tree_oid, "", opt); 859 if (opt->detect_rename && diff_might_be_rename()) { 860 struct diff_options rename_opts; 861 862 /* 863 * Build a private diff_options for rename detection so 864 * that any user-specified options on the original opts 865 * (e.g. pickaxe) cannot discard diff pairs needed for 866 * rename tracking. Similar to blame's find_rename(). 867 */ 868 repo_diff_setup(opt->repo, &rename_opts); 869 rename_opts.flags.recursive = 1; 870 rename_opts.detect_rename = opt->detect_rename; 871 rename_opts.rename_score = opt->rename_score; 872 rename_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 873 diff_setup_done(&rename_opts); 874 875 /* must look at the full tree diff to detect renames */ 876 diff_queue_clear(&diff_queued_diff); 877 diff_tree_oid(parent_tree_oid, tree_oid, "", &rename_opts); 878 879 filter_diffs_for_paths(range, 1); 880 diffcore_std(&rename_opts); 881 filter_diffs_for_paths(range, 0); 882 diff_free(&rename_opts); 883 } 884 move_diff_queue(queue, &diff_queued_diff); 885 } 886 887 /* 888 * Unlike most other functions, this destructively operates on 889 * 'range'. 890 */ 891 static int process_diff_filepair(struct rev_info *rev, 892 struct diff_filepair *pair, 893 struct line_log_data *range, 894 struct diff_ranges **diff_out) 895 { 896 struct line_log_data *rg = range; 897 struct range_set tmp; 898 struct diff_ranges diff; 899 mmfile_t file_parent, file_target; 900 char *parent_data_to_free = NULL; 901 902 assert(pair->two->path); 903 while (rg) { 904 assert(rg->path); 905 if (!strcmp(rg->path, pair->two->path)) 906 break; 907 rg = rg->next; 908 } 909 910 if (!rg) 911 return 0; 912 if (rg->ranges.nr == 0) 913 return 0; 914 915 assert(pair->two->oid_valid); 916 diff_populate_filespec(rev->diffopt.repo, pair->two, NULL); 917 file_target.ptr = pair->two->data; 918 file_target.size = pair->two->size; 919 920 if (pair->one->oid_valid) { 921 diff_populate_filespec(rev->diffopt.repo, pair->one, NULL); 922 file_parent.ptr = pair->one->data; 923 file_parent.size = pair->one->size; 924 } else { 925 file_parent.ptr = parent_data_to_free = xstrdup(""); 926 file_parent.size = 0; 927 } 928 929 diff_ranges_init(&diff); 930 if (collect_diff(&file_parent, &file_target, &diff)) 931 die("unable to generate diff for %s", pair->one->path); 932 933 /* NEEDSWORK should apply some heuristics to prevent mismatches */ 934 free(rg->path); 935 rg->path = xstrdup(pair->one->path); 936 937 range_set_init(&tmp, 0); 938 range_set_map_across_diff(&tmp, &rg->ranges, &diff, diff_out); 939 range_set_release(&rg->ranges); 940 range_set_move(&rg->ranges, &tmp); 941 942 diff_ranges_release(&diff); 943 944 free(parent_data_to_free); 945 return ((*diff_out)->parent.nr > 0); 946 } 947 948 static struct diff_filepair *diff_filepair_dup(struct diff_filepair *pair) 949 { 950 struct diff_filepair *new_filepair = xcalloc(1, sizeof(struct diff_filepair)); 951 new_filepair->one = pair->one; 952 new_filepair->two = pair->two; 953 new_filepair->one->count++; 954 new_filepair->two->count++; 955 return new_filepair; 956 } 957 958 static int process_all_files(struct line_log_data **range_out, 959 struct rev_info *rev, 960 struct diff_queue_struct *queue, 961 struct line_log_data *range) 962 { 963 int i, changed = 0; 964 965 *range_out = line_log_data_copy(range); 966 967 for (i = 0; i < queue->nr; i++) { 968 struct diff_ranges *pairdiff = NULL; 969 struct diff_filepair *pair = queue->queue[i]; 970 if (process_diff_filepair(rev, pair, *range_out, &pairdiff)) { 971 /* 972 * Store away the diff for later output. We 973 * tuck it in the ranges we got as _input_, 974 * since that's the commit that caused the 975 * diff. 976 * 977 * NEEDSWORK not enough when we get around to 978 * doing something interesting with merges; 979 * currently each invocation on a merge parent 980 * trashes the previous one's diff. 981 * 982 * NEEDSWORK tramples over data structures not owned here 983 */ 984 struct line_log_data *rg = range; 985 changed++; 986 while (rg && strcmp(rg->path, pair->two->path)) 987 rg = rg->next; 988 assert(rg); 989 if (rg->pair) 990 diff_free_filepair(rg->pair); 991 rg->pair = diff_filepair_dup(queue->queue[i]); 992 diff_ranges_release(&rg->diff); 993 memcpy(&rg->diff, pairdiff, sizeof(struct diff_ranges)); 994 FREE_AND_NULL(pairdiff); 995 } 996 997 if (pairdiff) { 998 diff_ranges_release(pairdiff); 999 free(pairdiff); 1000 } 1001 } 1002 1003 return changed; 1004 } 1005 1006 void line_log_queue_pairs(struct rev_info *rev, struct commit *commit) 1007 { 1008 struct line_log_data *range = lookup_line_range(rev, commit); 1009 struct line_log_data *r; 1010 1011 for (r = range; r; r = r->next) { 1012 if (r->pair) { 1013 struct diff_filepair *p = diff_filepair_dup(r->pair); 1014 p->line_ranges = &r->ranges; 1015 diff_q(&diff_queued_diff, p); 1016 } 1017 } 1018 } 1019 1020 static int bloom_filter_check(struct rev_info *rev, 1021 struct commit *commit, 1022 struct line_log_data *range) 1023 { 1024 struct bloom_filter *filter; 1025 struct bloom_key key; 1026 int result = 0; 1027 1028 if (!commit->parents) 1029 return 1; 1030 1031 if (!rev->bloom_filter_settings || 1032 !(filter = get_bloom_filter(rev->repo, commit))) 1033 return 1; 1034 1035 if (!range) 1036 return 0; 1037 1038 while (!result && range) { 1039 bloom_key_fill(&key, range->path, strlen(range->path), 1040 rev->bloom_filter_settings); 1041 1042 if (bloom_filter_contains(filter, &key, rev->bloom_filter_settings)) 1043 result = 1; 1044 1045 bloom_key_clear(&key); 1046 range = range->next; 1047 } 1048 1049 return result; 1050 } 1051 1052 static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *commit, 1053 struct line_log_data *range) 1054 { 1055 struct commit *parent = NULL; 1056 struct diff_queue_struct queue = DIFF_QUEUE_INIT; 1057 struct line_log_data *parent_range; 1058 int changed; 1059 1060 if (commit->parents) 1061 parent = commit->parents->item; 1062 1063 queue_diffs(range, &rev->diffopt, &queue, commit, parent); 1064 changed = process_all_files(&parent_range, rev, &queue, range); 1065 1066 if (parent) 1067 add_line_range(rev, parent, parent_range); 1068 free_line_log_data(parent_range); 1069 diff_queue_clear(&queue); 1070 return changed; 1071 } 1072 1073 static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commit, 1074 struct line_log_data *range) 1075 { 1076 struct line_log_data **cand; 1077 struct commit_list *p; 1078 int i; 1079 int nparents = commit_list_count(commit->parents); 1080 int ret; 1081 1082 if (nparents > 1 && rev->first_parent_only) 1083 nparents = 1; 1084 1085 CALLOC_ARRAY(cand, nparents); 1086 1087 for (p = commit->parents, i = 0; 1088 p && i < nparents; 1089 p = p->next, i++) { 1090 struct commit *parent = p->item; 1091 struct diff_queue_struct diffqueue = DIFF_QUEUE_INIT; 1092 int changed; 1093 1094 queue_diffs(range, &rev->diffopt, &diffqueue, commit, parent); 1095 1096 changed = process_all_files(&cand[i], rev, &diffqueue, range); 1097 diff_queue_clear(&diffqueue); 1098 if (!changed) { 1099 /* 1100 * This parent can take all the blame, so we 1101 * don't follow any other path in history 1102 */ 1103 add_line_range(rev, parent, cand[i]); 1104 commit_list_free(commit->parents); 1105 commit_list_append(parent, &commit->parents); 1106 1107 ret = 0; 1108 goto out; 1109 } 1110 } 1111 1112 /* 1113 * No single parent took the blame. We add the candidates 1114 * from the above loop to the parents. 1115 */ 1116 for (p = commit->parents, i = 0; 1117 p && i < nparents; 1118 p = p->next, i++) 1119 add_line_range(rev, p->item, cand[i]); 1120 1121 ret = 1; 1122 1123 out: 1124 clear_commit_line_range(rev, commit); 1125 for (i = 0; i < nparents; i++) { 1126 if (!cand[i]) 1127 continue; 1128 line_log_data_clear(cand[i]); 1129 free(cand[i]); 1130 } 1131 free(cand); 1132 return ret; 1133 1134 /* NEEDSWORK evil merge detection stuff */ 1135 } 1136 1137 int line_log_process_ranges_arbitrary_commit(struct rev_info *rev, struct commit *commit) 1138 { 1139 struct line_log_data *range = lookup_line_range(rev, commit); 1140 int changed = 0; 1141 1142 if (range) { 1143 if (commit->parents && !bloom_filter_check(rev, commit, range)) { 1144 add_line_range(rev, commit->parents->item, range); 1145 clear_commit_line_range(rev, commit); 1146 } else if (commit->parents && commit->parents->next) 1147 changed = process_ranges_merge_commit(rev, commit, range); 1148 else 1149 changed = process_ranges_ordinary_commit(rev, commit, range); 1150 } 1151 1152 if (!changed) 1153 commit->object.flags |= TREESAME; 1154 1155 return changed; 1156 } 1157 1158 static enum rewrite_result line_log_rewrite_one(struct rev_info *rev UNUSED, 1159 struct commit **pp) 1160 { 1161 for (;;) { 1162 struct commit *p = *pp; 1163 if (p->parents && p->parents->next) 1164 return rewrite_one_ok; 1165 if (p->object.flags & UNINTERESTING) 1166 return rewrite_one_ok; 1167 if (!(p->object.flags & TREESAME)) 1168 return rewrite_one_ok; 1169 if (!p->parents) 1170 return rewrite_one_noparents; 1171 *pp = p->parents->item; 1172 } 1173 } 1174 1175 int line_log_filter(struct rev_info *rev) 1176 { 1177 struct commit *commit; 1178 struct commit_list *list = rev->commits; 1179 struct commit_list *out = NULL, **pp = &out; 1180 1181 while (list) { 1182 struct commit_list *to_free = NULL; 1183 commit = list->item; 1184 if (line_log_process_ranges_arbitrary_commit(rev, commit)) { 1185 *pp = list; 1186 pp = &list->next; 1187 } else 1188 to_free = list; 1189 list = list->next; 1190 free(to_free); 1191 } 1192 *pp = NULL; 1193 1194 for (list = out; list; list = list->next) 1195 rewrite_parents(rev, list->item, line_log_rewrite_one); 1196 1197 rev->commits = out; 1198 1199 return 0; 1200 } 1201 1202 static void free_void_line_log_data(void *data) 1203 { 1204 free_line_log_data(data); 1205 } 1206 1207 void line_log_free(struct rev_info *rev) 1208 { 1209 clear_decoration(&rev->line_log_data, free_void_line_log_data); 1210 }