1#include "cache.h" 2#include "refs.h" 3#include "object-store.h" 4#include "cache-tree.h" 5#include "mergesort.h" 6#include "diff.h" 7#include "diffcore.h" 8#include "tag.h" 9#include "blame.h" 10#include "alloc.h" 11 12void blame_origin_decref(struct blame_origin *o) 13{ 14 if (o && --o->refcnt <= 0) { 15 struct blame_origin *p, *l = NULL; 16 if (o->previous) 17 blame_origin_decref(o->previous); 18 free(o->file.ptr); 19 /* Should be present exactly once in commit chain */ 20 for (p = o->commit->util; p; l = p, p = p->next) { 21 if (p == o) { 22 if (l) 23 l->next = p->next; 24 else 25 o->commit->util = p->next; 26 free(o); 27 return; 28 } 29 } 30 die("internal error in blame_origin_decref"); 31 } 32} 33 34/* 35 * Given a commit and a path in it, create a new origin structure. 36 * The callers that add blame to the scoreboard should use 37 * get_origin() to obtain shared, refcounted copy instead of calling 38 * this function directly. 39 */ 40static struct blame_origin *make_origin(struct commit *commit, const char *path) 41{ 42 struct blame_origin *o; 43 FLEX_ALLOC_STR(o, path, path); 44 o->commit = commit; 45 o->refcnt = 1; 46 o->next = commit->util; 47 commit->util = o; 48 return o; 49} 50 51/* 52 * Locate an existing origin or create a new one. 53 * This moves the origin to front position in the commit util list. 54 */ 55static struct blame_origin *get_origin(struct commit *commit, const char *path) 56{ 57 struct blame_origin *o, *l; 58 59 for (o = commit->util, l = NULL; o; l = o, o = o->next) { 60 if (!strcmp(o->path, path)) { 61 /* bump to front */ 62 if (l) { 63 l->next = o->next; 64 o->next = commit->util; 65 commit->util = o; 66 } 67 return blame_origin_incref(o); 68 } 69 } 70 return make_origin(commit, path); 71} 72 73 74 75static void verify_working_tree_path(struct commit *work_tree, const char *path) 76{ 77 struct commit_list *parents; 78 int pos; 79 80 for (parents = work_tree->parents; parents; parents = parents->next) { 81 const struct object_id *commit_oid = &parents->item->object.oid; 82 struct object_id blob_oid; 83 unsigned mode; 84 85 if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) && 86 oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) 87 return; 88 } 89 90 pos = cache_name_pos(path, strlen(path)); 91 if (pos >= 0) 92 ; /* path is in the index */ 93 else if (-1 - pos < active_nr && 94 !strcmp(active_cache[-1 - pos]->name, path)) 95 ; /* path is in the index, unmerged */ 96 else 97 die("no such path '%s' in HEAD", path); 98} 99 100static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid) 101{ 102 struct commit *parent; 103 104 parent = lookup_commit_reference(oid); 105 if (!parent) 106 die("no such commit %s", oid_to_hex(oid)); 107 return &commit_list_insert(parent, tail)->next; 108} 109 110static void append_merge_parents(struct commit_list **tail) 111{ 112 int merge_head; 113 struct strbuf line = STRBUF_INIT; 114 115 merge_head = open(git_path_merge_head(), O_RDONLY); 116 if (merge_head < 0) { 117 if (errno == ENOENT) 118 return; 119 die("cannot open '%s' for reading", git_path_merge_head()); 120 } 121 122 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) { 123 struct object_id oid; 124 if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid)) 125 die("unknown line in '%s': %s", git_path_merge_head(), line.buf); 126 tail = append_parent(tail, &oid); 127 } 128 close(merge_head); 129 strbuf_release(&line); 130} 131 132/* 133 * This isn't as simple as passing sb->buf and sb->len, because we 134 * want to transfer ownership of the buffer to the commit (so we 135 * must use detach). 136 */ 137static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb) 138{ 139 size_t len; 140 void *buf = strbuf_detach(sb, &len); 141 set_commit_buffer(c, buf, len); 142} 143 144/* 145 * Prepare a dummy commit that represents the work tree (or staged) item. 146 * Note that annotating work tree item never works in the reverse. 147 */ 148static struct commit *fake_working_tree_commit(struct diff_options *opt, 149 const char *path, 150 const char *contents_from) 151{ 152 struct commit *commit; 153 struct blame_origin *origin; 154 struct commit_list **parent_tail, *parent; 155 struct object_id head_oid; 156 struct strbuf buf = STRBUF_INIT; 157 const char *ident; 158 time_t now; 159 int size, len; 160 struct cache_entry *ce; 161 unsigned mode; 162 struct strbuf msg = STRBUF_INIT; 163 164 read_cache(); 165 time(&now); 166 commit = alloc_commit_node(the_repository); 167 commit->object.parsed = 1; 168 commit->date = now; 169 parent_tail = &commit->parents; 170 171 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) 172 die("no such ref: HEAD"); 173 174 parent_tail = append_parent(parent_tail, &head_oid); 175 append_merge_parents(parent_tail); 176 verify_working_tree_path(commit, path); 177 178 origin = make_origin(commit, path); 179 180 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); 181 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n"); 182 for (parent = commit->parents; parent; parent = parent->next) 183 strbuf_addf(&msg, "parent %s\n", 184 oid_to_hex(&parent->item->object.oid)); 185 strbuf_addf(&msg, 186 "author %s\n" 187 "committer %s\n\n" 188 "Version of %s from %s\n", 189 ident, ident, path, 190 (!contents_from ? path : 191 (!strcmp(contents_from, "-") ? "standard input" : contents_from))); 192 set_commit_buffer_from_strbuf(commit, &msg); 193 194 if (!contents_from || strcmp("-", contents_from)) { 195 struct stat st; 196 const char *read_from; 197 char *buf_ptr; 198 unsigned long buf_len; 199 200 if (contents_from) { 201 if (stat(contents_from, &st) < 0) 202 die_errno("Cannot stat '%s'", contents_from); 203 read_from = contents_from; 204 } 205 else { 206 if (lstat(path, &st) < 0) 207 die_errno("Cannot lstat '%s'", path); 208 read_from = path; 209 } 210 mode = canon_mode(st.st_mode); 211 212 switch (st.st_mode & S_IFMT) { 213 case S_IFREG: 214 if (opt->flags.allow_textconv && 215 textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len)) 216 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); 217 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) 218 die_errno("cannot open or read '%s'", read_from); 219 break; 220 case S_IFLNK: 221 if (strbuf_readlink(&buf, read_from, st.st_size) < 0) 222 die_errno("cannot readlink '%s'", read_from); 223 break; 224 default: 225 die("unsupported file type %s", read_from); 226 } 227 } 228 else { 229 /* Reading from stdin */ 230 mode = 0; 231 if (strbuf_read(&buf, 0, 0) < 0) 232 die_errno("failed to read from stdin"); 233 } 234 convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0); 235 origin->file.ptr = buf.buf; 236 origin->file.size = buf.len; 237 pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); 238 239 /* 240 * Read the current index, replace the path entry with 241 * origin->blob_sha1 without mucking with its mode or type 242 * bits; we are not going to write this index out -- we just 243 * want to run "diff-index --cached". 244 */ 245 discard_cache(); 246 read_cache(); 247 248 len = strlen(path); 249 if (!mode) { 250 int pos = cache_name_pos(path, len); 251 if (0 <= pos) 252 mode = active_cache[pos]->ce_mode; 253 else 254 /* Let's not bother reading from HEAD tree */ 255 mode = S_IFREG | 0644; 256 } 257 size = cache_entry_size(len); 258 ce = xcalloc(1, size); 259 oidcpy(&ce->oid, &origin->blob_oid); 260 memcpy(ce->name, path, len); 261 ce->ce_flags = create_ce_flags(0); 262 ce->ce_namelen = len; 263 ce->ce_mode = create_ce_mode(mode); 264 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 265 266 cache_tree_invalidate_path(&the_index, path); 267 268 return commit; 269} 270 271 272 273static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, 274 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts) 275{ 276 xpparam_t xpp = {0}; 277 xdemitconf_t xecfg = {0}; 278 xdemitcb_t ecb = {NULL}; 279 280 xpp.flags = xdl_opts; 281 xecfg.hunk_func = hunk_func; 282 ecb.priv = cb_data; 283 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 284} 285 286/* 287 * Given an origin, prepare mmfile_t structure to be used by the 288 * diff machinery 289 */ 290static void fill_origin_blob(struct diff_options *opt, 291 struct blame_origin *o, mmfile_t *file, int *num_read_blob) 292{ 293 if (!o->file.ptr) { 294 enum object_type type; 295 unsigned long file_size; 296 297 (*num_read_blob)++; 298 if (opt->flags.allow_textconv && 299 textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size)) 300 ; 301 else 302 file->ptr = read_object_file(&o->blob_oid, &type, 303 &file_size); 304 file->size = file_size; 305 306 if (!file->ptr) 307 die("Cannot read blob %s for path %s", 308 oid_to_hex(&o->blob_oid), 309 o->path); 310 o->file = *file; 311 } 312 else 313 *file = o->file; 314} 315 316static void drop_origin_blob(struct blame_origin *o) 317{ 318 if (o->file.ptr) { 319 FREE_AND_NULL(o->file.ptr); 320 } 321} 322 323/* 324 * Any merge of blames happens on lists of blames that arrived via 325 * different parents in a single suspect. In this case, we want to 326 * sort according to the suspect line numbers as opposed to the final 327 * image line numbers. The function body is somewhat longish because 328 * it avoids unnecessary writes. 329 */ 330 331static struct blame_entry *blame_merge(struct blame_entry *list1, 332 struct blame_entry *list2) 333{ 334 struct blame_entry *p1 = list1, *p2 = list2, 335 **tail = &list1; 336 337 if (!p1) 338 return p2; 339 if (!p2) 340 return p1; 341 342 if (p1->s_lno <= p2->s_lno) { 343 do { 344 tail = &p1->next; 345 if ((p1 = *tail) == NULL) { 346 *tail = p2; 347 return list1; 348 } 349 } while (p1->s_lno <= p2->s_lno); 350 } 351 for (;;) { 352 *tail = p2; 353 do { 354 tail = &p2->next; 355 if ((p2 = *tail) == NULL) { 356 *tail = p1; 357 return list1; 358 } 359 } while (p1->s_lno > p2->s_lno); 360 *tail = p1; 361 do { 362 tail = &p1->next; 363 if ((p1 = *tail) == NULL) { 364 *tail = p2; 365 return list1; 366 } 367 } while (p1->s_lno <= p2->s_lno); 368 } 369} 370 371static void *get_next_blame(const void *p) 372{ 373 return ((struct blame_entry *)p)->next; 374} 375 376static void set_next_blame(void *p1, void *p2) 377{ 378 ((struct blame_entry *)p1)->next = p2; 379} 380 381/* 382 * Final image line numbers are all different, so we don't need a 383 * three-way comparison here. 384 */ 385 386static int compare_blame_final(const void *p1, const void *p2) 387{ 388 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 389 ? 1 : -1; 390} 391 392static int compare_blame_suspect(const void *p1, const void *p2) 393{ 394 const struct blame_entry *s1 = p1, *s2 = p2; 395 /* 396 * to allow for collating suspects, we sort according to the 397 * respective pointer value as the primary sorting criterion. 398 * The actual relation is pretty unimportant as long as it 399 * establishes a total order. Comparing as integers gives us 400 * that. 401 */ 402 if (s1->suspect != s2->suspect) 403 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1; 404 if (s1->s_lno == s2->s_lno) 405 return 0; 406 return s1->s_lno > s2->s_lno ? 1 : -1; 407} 408 409void blame_sort_final(struct blame_scoreboard *sb) 410{ 411 sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame, 412 compare_blame_final); 413} 414 415static int compare_commits_by_reverse_commit_date(const void *a, 416 const void *b, 417 void *c) 418{ 419 return -compare_commits_by_commit_date(a, b, c); 420} 421 422/* 423 * For debugging -- origin is refcounted, and this asserts that 424 * we do not underflow. 425 */ 426static void sanity_check_refcnt(struct blame_scoreboard *sb) 427{ 428 int baa = 0; 429 struct blame_entry *ent; 430 431 for (ent = sb->ent; ent; ent = ent->next) { 432 /* Nobody should have zero or negative refcnt */ 433 if (ent->suspect->refcnt <= 0) { 434 fprintf(stderr, "%s in %s has negative refcnt %d\n", 435 ent->suspect->path, 436 oid_to_hex(&ent->suspect->commit->object.oid), 437 ent->suspect->refcnt); 438 baa = 1; 439 } 440 } 441 if (baa) 442 sb->on_sanity_fail(sb, baa); 443} 444 445/* 446 * If two blame entries that are next to each other came from 447 * contiguous lines in the same origin (i.e. <commit, path> pair), 448 * merge them together. 449 */ 450void blame_coalesce(struct blame_scoreboard *sb) 451{ 452 struct blame_entry *ent, *next; 453 454 for (ent = sb->ent; ent && (next = ent->next); ent = next) { 455 if (ent->suspect == next->suspect && 456 ent->s_lno + ent->num_lines == next->s_lno) { 457 ent->num_lines += next->num_lines; 458 ent->next = next->next; 459 blame_origin_decref(next->suspect); 460 free(next); 461 ent->score = 0; 462 next = ent; /* again */ 463 } 464 } 465 466 if (sb->debug) /* sanity */ 467 sanity_check_refcnt(sb); 468} 469 470/* 471 * Merge the given sorted list of blames into a preexisting origin. 472 * If there were no previous blames to that commit, it is entered into 473 * the commit priority queue of the score board. 474 */ 475 476static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin, 477 struct blame_entry *sorted) 478{ 479 if (porigin->suspects) 480 porigin->suspects = blame_merge(porigin->suspects, sorted); 481 else { 482 struct blame_origin *o; 483 for (o = porigin->commit->util; o; o = o->next) { 484 if (o->suspects) { 485 porigin->suspects = sorted; 486 return; 487 } 488 } 489 porigin->suspects = sorted; 490 prio_queue_put(&sb->commits, porigin->commit); 491 } 492} 493 494/* 495 * Fill the blob_sha1 field of an origin if it hasn't, so that later 496 * call to fill_origin_blob() can use it to locate the data. blob_sha1 497 * for an origin is also used to pass the blame for the entire file to 498 * the parent to detect the case where a child's blob is identical to 499 * that of its parent's. 500 * 501 * This also fills origin->mode for corresponding tree path. 502 */ 503static int fill_blob_sha1_and_mode(struct blame_origin *origin) 504{ 505 if (!is_null_oid(&origin->blob_oid)) 506 return 0; 507 if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 508 goto error_out; 509 if (oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB) 510 goto error_out; 511 return 0; 512 error_out: 513 oidclr(&origin->blob_oid); 514 origin->mode = S_IFINVALID; 515 return -1; 516} 517 518/* 519 * We have an origin -- check if the same path exists in the 520 * parent and return an origin structure to represent it. 521 */ 522static struct blame_origin *find_origin(struct commit *parent, 523 struct blame_origin *origin) 524{ 525 struct blame_origin *porigin; 526 struct diff_options diff_opts; 527 const char *paths[2]; 528 529 /* First check any existing origins */ 530 for (porigin = parent->util; porigin; porigin = porigin->next) 531 if (!strcmp(porigin->path, origin->path)) { 532 /* 533 * The same path between origin and its parent 534 * without renaming -- the most common case. 535 */ 536 return blame_origin_incref (porigin); 537 } 538 539 /* See if the origin->path is different between parent 540 * and origin first. Most of the time they are the 541 * same and diff-tree is fairly efficient about this. 542 */ 543 diff_setup(&diff_opts); 544 diff_opts.flags.recursive = 1; 545 diff_opts.detect_rename = 0; 546 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 547 paths[0] = origin->path; 548 paths[1] = NULL; 549 550 parse_pathspec(&diff_opts.pathspec, 551 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 552 PATHSPEC_LITERAL_PATH, "", paths); 553 diff_setup_done(&diff_opts); 554 555 if (is_null_oid(&origin->commit->object.oid)) 556 do_diff_cache(&parent->tree->object.oid, &diff_opts); 557 else 558 diff_tree_oid(&parent->tree->object.oid, 559 &origin->commit->tree->object.oid, 560 "", &diff_opts); 561 diffcore_std(&diff_opts); 562 563 if (!diff_queued_diff.nr) { 564 /* The path is the same as parent */ 565 porigin = get_origin(parent, origin->path); 566 oidcpy(&porigin->blob_oid, &origin->blob_oid); 567 porigin->mode = origin->mode; 568 } else { 569 /* 570 * Since origin->path is a pathspec, if the parent 571 * commit had it as a directory, we will see a whole 572 * bunch of deletion of files in the directory that we 573 * do not care about. 574 */ 575 int i; 576 struct diff_filepair *p = NULL; 577 for (i = 0; i < diff_queued_diff.nr; i++) { 578 const char *name; 579 p = diff_queued_diff.queue[i]; 580 name = p->one->path ? p->one->path : p->two->path; 581 if (!strcmp(name, origin->path)) 582 break; 583 } 584 if (!p) 585 die("internal error in blame::find_origin"); 586 switch (p->status) { 587 default: 588 die("internal error in blame::find_origin (%c)", 589 p->status); 590 case 'M': 591 porigin = get_origin(parent, origin->path); 592 oidcpy(&porigin->blob_oid, &p->one->oid); 593 porigin->mode = p->one->mode; 594 break; 595 case 'A': 596 case 'T': 597 /* Did not exist in parent, or type changed */ 598 break; 599 } 600 } 601 diff_flush(&diff_opts); 602 clear_pathspec(&diff_opts.pathspec); 603 return porigin; 604} 605 606/* 607 * We have an origin -- find the path that corresponds to it in its 608 * parent and return an origin structure to represent it. 609 */ 610static struct blame_origin *find_rename(struct commit *parent, 611 struct blame_origin *origin) 612{ 613 struct blame_origin *porigin = NULL; 614 struct diff_options diff_opts; 615 int i; 616 617 diff_setup(&diff_opts); 618 diff_opts.flags.recursive = 1; 619 diff_opts.detect_rename = DIFF_DETECT_RENAME; 620 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 621 diff_opts.single_follow = origin->path; 622 diff_setup_done(&diff_opts); 623 624 if (is_null_oid(&origin->commit->object.oid)) 625 do_diff_cache(&parent->tree->object.oid, &diff_opts); 626 else 627 diff_tree_oid(&parent->tree->object.oid, 628 &origin->commit->tree->object.oid, 629 "", &diff_opts); 630 diffcore_std(&diff_opts); 631 632 for (i = 0; i < diff_queued_diff.nr; i++) { 633 struct diff_filepair *p = diff_queued_diff.queue[i]; 634 if ((p->status == 'R' || p->status == 'C') && 635 !strcmp(p->two->path, origin->path)) { 636 porigin = get_origin(parent, p->one->path); 637 oidcpy(&porigin->blob_oid, &p->one->oid); 638 porigin->mode = p->one->mode; 639 break; 640 } 641 } 642 diff_flush(&diff_opts); 643 clear_pathspec(&diff_opts.pathspec); 644 return porigin; 645} 646 647/* 648 * Append a new blame entry to a given output queue. 649 */ 650static void add_blame_entry(struct blame_entry ***queue, 651 const struct blame_entry *src) 652{ 653 struct blame_entry *e = xmalloc(sizeof(*e)); 654 memcpy(e, src, sizeof(*e)); 655 blame_origin_incref(e->suspect); 656 657 e->next = **queue; 658 **queue = e; 659 *queue = &e->next; 660} 661 662/* 663 * src typically is on-stack; we want to copy the information in it to 664 * a malloced blame_entry that gets added to the given queue. The 665 * origin of dst loses a refcnt. 666 */ 667static void dup_entry(struct blame_entry ***queue, 668 struct blame_entry *dst, struct blame_entry *src) 669{ 670 blame_origin_incref(src->suspect); 671 blame_origin_decref(dst->suspect); 672 memcpy(dst, src, sizeof(*src)); 673 dst->next = **queue; 674 **queue = dst; 675 *queue = &dst->next; 676} 677 678const char *blame_nth_line(struct blame_scoreboard *sb, long lno) 679{ 680 return sb->final_buf + sb->lineno[lno]; 681} 682 683/* 684 * It is known that lines between tlno to same came from parent, and e 685 * has an overlap with that range. it also is known that parent's 686 * line plno corresponds to e's line tlno. 687 * 688 * <---- e -----> 689 * <------> 690 * <------------> 691 * <------------> 692 * <------------------> 693 * 694 * Split e into potentially three parts; before this chunk, the chunk 695 * to be blamed for the parent, and after that portion. 696 */ 697static void split_overlap(struct blame_entry *split, 698 struct blame_entry *e, 699 int tlno, int plno, int same, 700 struct blame_origin *parent) 701{ 702 int chunk_end_lno; 703 memset(split, 0, sizeof(struct blame_entry [3])); 704 705 if (e->s_lno < tlno) { 706 /* there is a pre-chunk part not blamed on parent */ 707 split[0].suspect = blame_origin_incref(e->suspect); 708 split[0].lno = e->lno; 709 split[0].s_lno = e->s_lno; 710 split[0].num_lines = tlno - e->s_lno; 711 split[1].lno = e->lno + tlno - e->s_lno; 712 split[1].s_lno = plno; 713 } 714 else { 715 split[1].lno = e->lno; 716 split[1].s_lno = plno + (e->s_lno - tlno); 717 } 718 719 if (same < e->s_lno + e->num_lines) { 720 /* there is a post-chunk part not blamed on parent */ 721 split[2].suspect = blame_origin_incref(e->suspect); 722 split[2].lno = e->lno + (same - e->s_lno); 723 split[2].s_lno = e->s_lno + (same - e->s_lno); 724 split[2].num_lines = e->s_lno + e->num_lines - same; 725 chunk_end_lno = split[2].lno; 726 } 727 else 728 chunk_end_lno = e->lno + e->num_lines; 729 split[1].num_lines = chunk_end_lno - split[1].lno; 730 731 /* 732 * if it turns out there is nothing to blame the parent for, 733 * forget about the splitting. !split[1].suspect signals this. 734 */ 735 if (split[1].num_lines < 1) 736 return; 737 split[1].suspect = blame_origin_incref(parent); 738} 739 740/* 741 * split_overlap() divided an existing blame e into up to three parts 742 * in split. Any assigned blame is moved to queue to 743 * reflect the split. 744 */ 745static void split_blame(struct blame_entry ***blamed, 746 struct blame_entry ***unblamed, 747 struct blame_entry *split, 748 struct blame_entry *e) 749{ 750 if (split[0].suspect && split[2].suspect) { 751 /* The first part (reuse storage for the existing entry e) */ 752 dup_entry(unblamed, e, &split[0]); 753 754 /* The last part -- me */ 755 add_blame_entry(unblamed, &split[2]); 756 757 /* ... and the middle part -- parent */ 758 add_blame_entry(blamed, &split[1]); 759 } 760 else if (!split[0].suspect && !split[2].suspect) 761 /* 762 * The parent covers the entire area; reuse storage for 763 * e and replace it with the parent. 764 */ 765 dup_entry(blamed, e, &split[1]); 766 else if (split[0].suspect) { 767 /* me and then parent */ 768 dup_entry(unblamed, e, &split[0]); 769 add_blame_entry(blamed, &split[1]); 770 } 771 else { 772 /* parent and then me */ 773 dup_entry(blamed, e, &split[1]); 774 add_blame_entry(unblamed, &split[2]); 775 } 776} 777 778/* 779 * After splitting the blame, the origins used by the 780 * on-stack blame_entry should lose one refcnt each. 781 */ 782static void decref_split(struct blame_entry *split) 783{ 784 int i; 785 786 for (i = 0; i < 3; i++) 787 blame_origin_decref(split[i].suspect); 788} 789 790/* 791 * reverse_blame reverses the list given in head, appending tail. 792 * That allows us to build lists in reverse order, then reverse them 793 * afterwards. This can be faster than building the list in proper 794 * order right away. The reason is that building in proper order 795 * requires writing a link in the _previous_ element, while building 796 * in reverse order just requires placing the list head into the 797 * _current_ element. 798 */ 799 800static struct blame_entry *reverse_blame(struct blame_entry *head, 801 struct blame_entry *tail) 802{ 803 while (head) { 804 struct blame_entry *next = head->next; 805 head->next = tail; 806 tail = head; 807 head = next; 808 } 809 return tail; 810} 811 812/* 813 * Process one hunk from the patch between the current suspect for 814 * blame_entry e and its parent. This first blames any unfinished 815 * entries before the chunk (which is where target and parent start 816 * differing) on the parent, and then splits blame entries at the 817 * start and at the end of the difference region. Since use of -M and 818 * -C options may lead to overlapping/duplicate source line number 819 * ranges, all we can rely on from sorting/merging is the order of the 820 * first suspect line number. 821 */ 822static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq, 823 int tlno, int offset, int same, 824 struct blame_origin *parent) 825{ 826 struct blame_entry *e = **srcq; 827 struct blame_entry *samep = NULL, *diffp = NULL; 828 829 while (e && e->s_lno < tlno) { 830 struct blame_entry *next = e->next; 831 /* 832 * current record starts before differing portion. If 833 * it reaches into it, we need to split it up and 834 * examine the second part separately. 835 */ 836 if (e->s_lno + e->num_lines > tlno) { 837 /* Move second half to a new record */ 838 int len = tlno - e->s_lno; 839 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry)); 840 n->suspect = e->suspect; 841 n->lno = e->lno + len; 842 n->s_lno = e->s_lno + len; 843 n->num_lines = e->num_lines - len; 844 e->num_lines = len; 845 e->score = 0; 846 /* Push new record to diffp */ 847 n->next = diffp; 848 diffp = n; 849 } else 850 blame_origin_decref(e->suspect); 851 /* Pass blame for everything before the differing 852 * chunk to the parent */ 853 e->suspect = blame_origin_incref(parent); 854 e->s_lno += offset; 855 e->next = samep; 856 samep = e; 857 e = next; 858 } 859 /* 860 * As we don't know how much of a common stretch after this 861 * diff will occur, the currently blamed parts are all that we 862 * can assign to the parent for now. 863 */ 864 865 if (samep) { 866 **dstq = reverse_blame(samep, **dstq); 867 *dstq = &samep->next; 868 } 869 /* 870 * Prepend the split off portions: everything after e starts 871 * after the blameable portion. 872 */ 873 e = reverse_blame(diffp, e); 874 875 /* 876 * Now retain records on the target while parts are different 877 * from the parent. 878 */ 879 samep = NULL; 880 diffp = NULL; 881 while (e && e->s_lno < same) { 882 struct blame_entry *next = e->next; 883 884 /* 885 * If current record extends into sameness, need to split. 886 */ 887 if (e->s_lno + e->num_lines > same) { 888 /* 889 * Move second half to a new record to be 890 * processed by later chunks 891 */ 892 int len = same - e->s_lno; 893 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry)); 894 n->suspect = blame_origin_incref(e->suspect); 895 n->lno = e->lno + len; 896 n->s_lno = e->s_lno + len; 897 n->num_lines = e->num_lines - len; 898 e->num_lines = len; 899 e->score = 0; 900 /* Push new record to samep */ 901 n->next = samep; 902 samep = n; 903 } 904 e->next = diffp; 905 diffp = e; 906 e = next; 907 } 908 **srcq = reverse_blame(diffp, reverse_blame(samep, e)); 909 /* Move across elements that are in the unblamable portion */ 910 if (diffp) 911 *srcq = &diffp->next; 912} 913 914struct blame_chunk_cb_data { 915 struct blame_origin *parent; 916 long offset; 917 struct blame_entry **dstq; 918 struct blame_entry **srcq; 919}; 920 921/* diff chunks are from parent to target */ 922static int blame_chunk_cb(long start_a, long count_a, 923 long start_b, long count_b, void *data) 924{ 925 struct blame_chunk_cb_data *d = data; 926 if (start_a - start_b != d->offset) 927 die("internal error in blame::blame_chunk_cb"); 928 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 929 start_b + count_b, d->parent); 930 d->offset = start_a + count_a - (start_b + count_b); 931 return 0; 932} 933 934/* 935 * We are looking at the origin 'target' and aiming to pass blame 936 * for the lines it is suspected to its parent. Run diff to find 937 * which lines came from parent and pass blame for them. 938 */ 939static void pass_blame_to_parent(struct blame_scoreboard *sb, 940 struct blame_origin *target, 941 struct blame_origin *parent) 942{ 943 mmfile_t file_p, file_o; 944 struct blame_chunk_cb_data d; 945 struct blame_entry *newdest = NULL; 946 947 if (!target->suspects) 948 return; /* nothing remains for this target */ 949 950 d.parent = parent; 951 d.offset = 0; 952 d.dstq = &newdest; d.srcq = &target->suspects; 953 954 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 955 fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 956 sb->num_get_patch++; 957 958 if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 959 die("unable to generate diff (%s -> %s)", 960 oid_to_hex(&parent->commit->object.oid), 961 oid_to_hex(&target->commit->object.oid)); 962 /* The rest are the same as the parent */ 963 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 964 *d.dstq = NULL; 965 queue_blames(sb, parent, newdest); 966 967 return; 968} 969 970/* 971 * The lines in blame_entry after splitting blames many times can become 972 * very small and trivial, and at some point it becomes pointless to 973 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 974 * ordinary C program, and it is not worth to say it was copied from 975 * totally unrelated file in the parent. 976 * 977 * Compute how trivial the lines in the blame_entry are. 978 */ 979unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e) 980{ 981 unsigned score; 982 const char *cp, *ep; 983 984 if (e->score) 985 return e->score; 986 987 score = 1; 988 cp = blame_nth_line(sb, e->lno); 989 ep = blame_nth_line(sb, e->lno + e->num_lines); 990 while (cp < ep) { 991 unsigned ch = *((unsigned char *)cp); 992 if (isalnum(ch)) 993 score++; 994 cp++; 995 } 996 e->score = score; 997 return score; 998} 9991000/*1001 * best_so_far[] and potential[] are both a split of an existing blame_entry1002 * that passes blame to the parent. Maintain best_so_far the best split so1003 * far, by comparing potential and best_so_far and copying potential into1004 * bst_so_far as needed.1005 */1006static void copy_split_if_better(struct blame_scoreboard *sb,1007 struct blame_entry *best_so_far,1008 struct blame_entry *potential)1009{1010 int i;10111012 if (!potential[1].suspect)1013 return;1014 if (best_so_far[1].suspect) {1015 if (blame_entry_score(sb, &potential[1]) <1016 blame_entry_score(sb, &best_so_far[1]))1017 return;1018 }10191020 for (i = 0; i < 3; i++)1021 blame_origin_incref(potential[i].suspect);1022 decref_split(best_so_far);1023 memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));1024}10251026/*1027 * We are looking at a part of the final image represented by1028 * ent (tlno and same are offset by ent->s_lno).1029 * tlno is where we are looking at in the final image.1030 * up to (but not including) same match preimage.1031 * plno is where we are looking at in the preimage.1032 *1033 * <-------------- final image ---------------------->1034 * <------ent------>1035 * ^tlno ^same1036 * <---------preimage----->1037 * ^plno1038 *1039 * All line numbers are 0-based.1040 */1041static void handle_split(struct blame_scoreboard *sb,1042 struct blame_entry *ent,1043 int tlno, int plno, int same,1044 struct blame_origin *parent,1045 struct blame_entry *split)1046{1047 if (ent->num_lines <= tlno)1048 return;1049 if (tlno < same) {1050 struct blame_entry potential[3];1051 tlno += ent->s_lno;1052 same += ent->s_lno;1053 split_overlap(potential, ent, tlno, plno, same, parent);1054 copy_split_if_better(sb, split, potential);1055 decref_split(potential);1056 }1057}10581059struct handle_split_cb_data {1060 struct blame_scoreboard *sb;1061 struct blame_entry *ent;1062 struct blame_origin *parent;1063 struct blame_entry *split;1064 long plno;1065 long tlno;1066};10671068static int handle_split_cb(long start_a, long count_a,1069 long start_b, long count_b, void *data)1070{1071 struct handle_split_cb_data *d = data;1072 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1073 d->split);1074 d->plno = start_a + count_a;1075 d->tlno = start_b + count_b;1076 return 0;1077}10781079/*1080 * Find the lines from parent that are the same as ent so that1081 * we can pass blames to it. file_p has the blob contents for1082 * the parent.1083 */1084static void find_copy_in_blob(struct blame_scoreboard *sb,1085 struct blame_entry *ent,1086 struct blame_origin *parent,1087 struct blame_entry *split,1088 mmfile_t *file_p)1089{1090 const char *cp;1091 mmfile_t file_o;1092 struct handle_split_cb_data d;10931094 memset(&d, 0, sizeof(d));1095 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1096 /*1097 * Prepare mmfile that contains only the lines in ent.1098 */1099 cp = blame_nth_line(sb, ent->lno);1100 file_o.ptr = (char *) cp;1101 file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11021103 /*1104 * file_o is a part of final image we are annotating.1105 * file_p partially may match that image.1106 */1107 memset(split, 0, sizeof(struct blame_entry [3]));1108 if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1109 die("unable to generate diff (%s)",1110 oid_to_hex(&parent->commit->object.oid));1111 /* remainder, if any, all match the preimage */1112 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1113}11141115/* Move all blame entries from list *source that have a score smaller1116 * than score_min to the front of list *small.1117 * Returns a pointer to the link pointing to the old head of the small list.1118 */11191120static struct blame_entry **filter_small(struct blame_scoreboard *sb,1121 struct blame_entry **small,1122 struct blame_entry **source,1123 unsigned score_min)1124{1125 struct blame_entry *p = *source;1126 struct blame_entry *oldsmall = *small;1127 while (p) {1128 if (blame_entry_score(sb, p) <= score_min) {1129 *small = p;1130 small = &p->next;1131 p = *small;1132 } else {1133 *source = p;1134 source = &p->next;1135 p = *source;1136 }1137 }1138 *small = oldsmall;1139 *source = NULL;1140 return small;1141}11421143/*1144 * See if lines currently target is suspected for can be attributed to1145 * parent.1146 */1147static void find_move_in_parent(struct blame_scoreboard *sb,1148 struct blame_entry ***blamed,1149 struct blame_entry **toosmall,1150 struct blame_origin *target,1151 struct blame_origin *parent)1152{1153 struct blame_entry *e, split[3];1154 struct blame_entry *unblamed = target->suspects;1155 struct blame_entry *leftover = NULL;1156 mmfile_t file_p;11571158 if (!unblamed)1159 return; /* nothing remains for this target */11601161 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1162 if (!file_p.ptr)1163 return;11641165 /* At each iteration, unblamed has a NULL-terminated list of1166 * entries that have not yet been tested for blame. leftover1167 * contains the reversed list of entries that have been tested1168 * without being assignable to the parent.1169 */1170 do {1171 struct blame_entry **unblamedtail = &unblamed;1172 struct blame_entry *next;1173 for (e = unblamed; e; e = next) {1174 next = e->next;1175 find_copy_in_blob(sb, e, parent, split, &file_p);1176 if (split[1].suspect &&1177 sb->move_score < blame_entry_score(sb, &split[1])) {1178 split_blame(blamed, &unblamedtail, split, e);1179 } else {1180 e->next = leftover;1181 leftover = e;1182 }1183 decref_split(split);1184 }1185 *unblamedtail = NULL;1186 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);1187 } while (unblamed);1188 target->suspects = reverse_blame(leftover, NULL);1189}11901191struct blame_list {1192 struct blame_entry *ent;1193 struct blame_entry split[3];1194};11951196/*1197 * Count the number of entries the target is suspected for,1198 * and prepare a list of entry and the best split.1199 */1200static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1201 int *num_ents_p)1202{1203 struct blame_entry *e;1204 int num_ents, i;1205 struct blame_list *blame_list = NULL;12061207 for (e = unblamed, num_ents = 0; e; e = e->next)1208 num_ents++;1209 if (num_ents) {1210 blame_list = xcalloc(num_ents, sizeof(struct blame_list));1211 for (e = unblamed, i = 0; e; e = e->next)1212 blame_list[i++].ent = e;1213 }1214 *num_ents_p = num_ents;1215 return blame_list;1216}12171218/*1219 * For lines target is suspected for, see if we can find code movement1220 * across file boundary from the parent commit. porigin is the path1221 * in the parent we already tried.1222 */1223static void find_copy_in_parent(struct blame_scoreboard *sb,1224 struct blame_entry ***blamed,1225 struct blame_entry **toosmall,1226 struct blame_origin *target,1227 struct commit *parent,1228 struct blame_origin *porigin,1229 int opt)1230{1231 struct diff_options diff_opts;1232 int i, j;1233 struct blame_list *blame_list;1234 int num_ents;1235 struct blame_entry *unblamed = target->suspects;1236 struct blame_entry *leftover = NULL;12371238 if (!unblamed)1239 return; /* nothing remains for this target */12401241 diff_setup(&diff_opts);1242 diff_opts.flags.recursive = 1;1243 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12441245 diff_setup_done(&diff_opts);12461247 /* Try "find copies harder" on new path if requested;1248 * we do not want to use diffcore_rename() actually to1249 * match things up; find_copies_harder is set only to1250 * force diff_tree_oid() to feed all filepairs to diff_queue,1251 * and this code needs to be after diff_setup_done(), which1252 * usually makes find-copies-harder imply copy detection.1253 */1254 if ((opt & PICKAXE_BLAME_COPY_HARDEST)1255 || ((opt & PICKAXE_BLAME_COPY_HARDER)1256 && (!porigin || strcmp(target->path, porigin->path))))1257 diff_opts.flags.find_copies_harder = 1;12581259 if (is_null_oid(&target->commit->object.oid))1260 do_diff_cache(&parent->tree->object.oid, &diff_opts);1261 else1262 diff_tree_oid(&parent->tree->object.oid,1263 &target->commit->tree->object.oid,1264 "", &diff_opts);12651266 if (!diff_opts.flags.find_copies_harder)1267 diffcore_std(&diff_opts);12681269 do {1270 struct blame_entry **unblamedtail = &unblamed;1271 blame_list = setup_blame_list(unblamed, &num_ents);12721273 for (i = 0; i < diff_queued_diff.nr; i++) {1274 struct diff_filepair *p = diff_queued_diff.queue[i];1275 struct blame_origin *norigin;1276 mmfile_t file_p;1277 struct blame_entry potential[3];12781279 if (!DIFF_FILE_VALID(p->one))1280 continue; /* does not exist in parent */1281 if (S_ISGITLINK(p->one->mode))1282 continue; /* ignore git links */1283 if (porigin && !strcmp(p->one->path, porigin->path))1284 /* find_move already dealt with this path */1285 continue;12861287 norigin = get_origin(parent, p->one->path);1288 oidcpy(&norigin->blob_oid, &p->one->oid);1289 norigin->mode = p->one->mode;1290 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1291 if (!file_p.ptr)1292 continue;12931294 for (j = 0; j < num_ents; j++) {1295 find_copy_in_blob(sb, blame_list[j].ent,1296 norigin, potential, &file_p);1297 copy_split_if_better(sb, blame_list[j].split,1298 potential);1299 decref_split(potential);1300 }1301 blame_origin_decref(norigin);1302 }13031304 for (j = 0; j < num_ents; j++) {1305 struct blame_entry *split = blame_list[j].split;1306 if (split[1].suspect &&1307 sb->copy_score < blame_entry_score(sb, &split[1])) {1308 split_blame(blamed, &unblamedtail, split,1309 blame_list[j].ent);1310 } else {1311 blame_list[j].ent->next = leftover;1312 leftover = blame_list[j].ent;1313 }1314 decref_split(split);1315 }1316 free(blame_list);1317 *unblamedtail = NULL;1318 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);1319 } while (unblamed);1320 target->suspects = reverse_blame(leftover, NULL);1321 diff_flush(&diff_opts);1322 clear_pathspec(&diff_opts.pathspec);1323}13241325/*1326 * The blobs of origin and porigin exactly match, so everything1327 * origin is suspected for can be blamed on the parent.1328 */1329static void pass_whole_blame(struct blame_scoreboard *sb,1330 struct blame_origin *origin, struct blame_origin *porigin)1331{1332 struct blame_entry *e, *suspects;13331334 if (!porigin->file.ptr && origin->file.ptr) {1335 /* Steal its file */1336 porigin->file = origin->file;1337 origin->file.ptr = NULL;1338 }1339 suspects = origin->suspects;1340 origin->suspects = NULL;1341 for (e = suspects; e; e = e->next) {1342 blame_origin_incref(porigin);1343 blame_origin_decref(e->suspect);1344 e->suspect = porigin;1345 }1346 queue_blames(sb, porigin, suspects);1347}13481349/*1350 * We pass blame from the current commit to its parents. We keep saying1351 * "parent" (and "porigin"), but what we mean is to find scapegoat to1352 * exonerate ourselves.1353 */1354static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,1355 int reverse)1356{1357 if (!reverse) {1358 if (revs->first_parent_only &&1359 commit->parents &&1360 commit->parents->next) {1361 free_commit_list(commit->parents->next);1362 commit->parents->next = NULL;1363 }1364 return commit->parents;1365 }1366 return lookup_decoration(&revs->children, &commit->object);1367}13681369static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)1370{1371 struct commit_list *l = first_scapegoat(revs, commit, reverse);1372 return commit_list_count(l);1373}13741375/* Distribute collected unsorted blames to the respected sorted lists1376 * in the various origins.1377 */1378static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)1379{1380 blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,1381 compare_blame_suspect);1382 while (blamed)1383 {1384 struct blame_origin *porigin = blamed->suspect;1385 struct blame_entry *suspects = NULL;1386 do {1387 struct blame_entry *next = blamed->next;1388 blamed->next = suspects;1389 suspects = blamed;1390 blamed = next;1391 } while (blamed && blamed->suspect == porigin);1392 suspects = reverse_blame(suspects, NULL);1393 queue_blames(sb, porigin, suspects);1394 }1395}13961397#define MAXSG 1613981399static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)1400{1401 struct rev_info *revs = sb->revs;1402 int i, pass, num_sg;1403 struct commit *commit = origin->commit;1404 struct commit_list *sg;1405 struct blame_origin *sg_buf[MAXSG];1406 struct blame_origin *porigin, **sg_origin = sg_buf;1407 struct blame_entry *toosmall = NULL;1408 struct blame_entry *blames, **blametail = &blames;14091410 num_sg = num_scapegoats(revs, commit, sb->reverse);1411 if (!num_sg)1412 goto finish;1413 else if (num_sg < ARRAY_SIZE(sg_buf))1414 memset(sg_buf, 0, sizeof(sg_buf));1415 else1416 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));14171418 /*1419 * The first pass looks for unrenamed path to optimize for1420 * common cases, then we look for renames in the second pass.1421 */1422 for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {1423 struct blame_origin *(*find)(struct commit *, struct blame_origin *);1424 find = pass ? find_rename : find_origin;14251426 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);1427 i < num_sg && sg;1428 sg = sg->next, i++) {1429 struct commit *p = sg->item;1430 int j, same;14311432 if (sg_origin[i])1433 continue;1434 if (parse_commit(p))1435 continue;1436 porigin = find(p, origin);1437 if (!porigin)1438 continue;1439 if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1440 pass_whole_blame(sb, origin, porigin);1441 blame_origin_decref(porigin);1442 goto finish;1443 }1444 for (j = same = 0; j < i; j++)1445 if (sg_origin[j] &&1446 !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1447 same = 1;1448 break;1449 }1450 if (!same)1451 sg_origin[i] = porigin;1452 else1453 blame_origin_decref(porigin);1454 }1455 }14561457 sb->num_commits++;1458 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);1459 i < num_sg && sg;1460 sg = sg->next, i++) {1461 struct blame_origin *porigin = sg_origin[i];1462 if (!porigin)1463 continue;1464 if (!origin->previous) {1465 blame_origin_incref(porigin);1466 origin->previous = porigin;1467 }1468 pass_blame_to_parent(sb, origin, porigin);1469 if (!origin->suspects)1470 goto finish;1471 }14721473 /*1474 * Optionally find moves in parents' files.1475 */1476 if (opt & PICKAXE_BLAME_MOVE) {1477 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1478 if (origin->suspects) {1479 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);1480 i < num_sg && sg;1481 sg = sg->next, i++) {1482 struct blame_origin *porigin = sg_origin[i];1483 if (!porigin)1484 continue;1485 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1486 if (!origin->suspects)1487 break;1488 }1489 }1490 }14911492 /*1493 * Optionally find copies from parents' files.1494 */1495 if (opt & PICKAXE_BLAME_COPY) {1496 if (sb->copy_score > sb->move_score)1497 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1498 else if (sb->copy_score < sb->move_score) {1499 origin->suspects = blame_merge(origin->suspects, toosmall);1500 toosmall = NULL;1501 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1502 }1503 if (!origin->suspects)1504 goto finish;15051506 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);1507 i < num_sg && sg;1508 sg = sg->next, i++) {1509 struct blame_origin *porigin = sg_origin[i];1510 find_copy_in_parent(sb, &blametail, &toosmall,1511 origin, sg->item, porigin, opt);1512 if (!origin->suspects)1513 goto finish;1514 }1515 }15161517finish:1518 *blametail = NULL;1519 distribute_blame(sb, blames);1520 /*1521 * prepend toosmall to origin->suspects1522 *1523 * There is no point in sorting: this ends up on a big1524 * unsorted list in the caller anyway.1525 */1526 if (toosmall) {1527 struct blame_entry **tail = &toosmall;1528 while (*tail)1529 tail = &(*tail)->next;1530 *tail = origin->suspects;1531 origin->suspects = toosmall;1532 }1533 for (i = 0; i < num_sg; i++) {1534 if (sg_origin[i]) {1535 drop_origin_blob(sg_origin[i]);1536 blame_origin_decref(sg_origin[i]);1537 }1538 }1539 drop_origin_blob(origin);1540 if (sg_buf != sg_origin)1541 free(sg_origin);1542}15431544/*1545 * The main loop -- while we have blobs with lines whose true origin1546 * is still unknown, pick one blob, and allow its lines to pass blames1547 * to its parents. */1548void assign_blame(struct blame_scoreboard *sb, int opt)1549{1550 struct rev_info *revs = sb->revs;1551 struct commit *commit = prio_queue_get(&sb->commits);15521553 while (commit) {1554 struct blame_entry *ent;1555 struct blame_origin *suspect = commit->util;15561557 /* find one suspect to break down */1558 while (suspect && !suspect->suspects)1559 suspect = suspect->next;15601561 if (!suspect) {1562 commit = prio_queue_get(&sb->commits);1563 continue;1564 }15651566 assert(commit == suspect->commit);15671568 /*1569 * We will use this suspect later in the loop,1570 * so hold onto it in the meantime.1571 */1572 blame_origin_incref(suspect);1573 parse_commit(commit);1574 if (sb->reverse ||1575 (!(commit->object.flags & UNINTERESTING) &&1576 !(revs->max_age != -1 && commit->date < revs->max_age)))1577 pass_blame(sb, suspect, opt);1578 else {1579 commit->object.flags |= UNINTERESTING;1580 if (commit->object.parsed)1581 mark_parents_uninteresting(commit);1582 }1583 /* treat root commit as boundary */1584 if (!commit->parents && !sb->show_root)1585 commit->object.flags |= UNINTERESTING;15861587 /* Take responsibility for the remaining entries */1588 ent = suspect->suspects;1589 if (ent) {1590 suspect->guilty = 1;1591 for (;;) {1592 struct blame_entry *next = ent->next;1593 if (sb->found_guilty_entry)1594 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1595 if (next) {1596 ent = next;1597 continue;1598 }1599 ent->next = sb->ent;1600 sb->ent = suspect->suspects;1601 suspect->suspects = NULL;1602 break;1603 }1604 }1605 blame_origin_decref(suspect);16061607 if (sb->debug) /* sanity */1608 sanity_check_refcnt(sb);1609 }1610}16111612static const char *get_next_line(const char *start, const char *end)1613{1614 const char *nl = memchr(start, '\n', end - start);1615 return nl ? nl + 1 : end;1616}16171618/*1619 * To allow quick access to the contents of nth line in the1620 * final image, prepare an index in the scoreboard.1621 */1622static int prepare_lines(struct blame_scoreboard *sb)1623{1624 const char *buf = sb->final_buf;1625 unsigned long len = sb->final_buf_size;1626 const char *end = buf + len;1627 const char *p;1628 int *lineno;1629 int num = 0;16301631 for (p = buf; p < end; p = get_next_line(p, end))1632 num++;16331634 ALLOC_ARRAY(sb->lineno, num + 1);1635 lineno = sb->lineno;16361637 for (p = buf; p < end; p = get_next_line(p, end))1638 *lineno++ = p - buf;16391640 *lineno = len;16411642 sb->num_lines = num;1643 return sb->num_lines;1644}16451646static struct commit *find_single_final(struct rev_info *revs,1647 const char **name_p)1648{1649 int i;1650 struct commit *found = NULL;1651 const char *name = NULL;16521653 for (i = 0; i < revs->pending.nr; i++) {1654 struct object *obj = revs->pending.objects[i].item;1655 if (obj->flags & UNINTERESTING)1656 continue;1657 obj = deref_tag(obj, NULL, 0);1658 if (obj->type != OBJ_COMMIT)1659 die("Non commit %s?", revs->pending.objects[i].name);1660 if (found)1661 die("More than one commit to dig from %s and %s?",1662 revs->pending.objects[i].name, name);1663 found = (struct commit *)obj;1664 name = revs->pending.objects[i].name;1665 }1666 if (name_p)1667 *name_p = xstrdup_or_null(name);1668 return found;1669}16701671static struct commit *dwim_reverse_initial(struct rev_info *revs,1672 const char **name_p)1673{1674 /*1675 * DWIM "git blame --reverse ONE -- PATH" as1676 * "git blame --reverse ONE..HEAD -- PATH" but only do so1677 * when it makes sense.1678 */1679 struct object *obj;1680 struct commit *head_commit;1681 struct object_id head_oid;16821683 if (revs->pending.nr != 1)1684 return NULL;16851686 /* Is that sole rev a committish? */1687 obj = revs->pending.objects[0].item;1688 obj = deref_tag(obj, NULL, 0);1689 if (obj->type != OBJ_COMMIT)1690 return NULL;16911692 /* Do we have HEAD? */1693 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1694 return NULL;1695 head_commit = lookup_commit_reference_gently(&head_oid, 1);1696 if (!head_commit)1697 return NULL;16981699 /* Turn "ONE" into "ONE..HEAD" then */1700 obj->flags |= UNINTERESTING;1701 add_pending_object(revs, &head_commit->object, "HEAD");17021703 if (name_p)1704 *name_p = revs->pending.objects[0].name;1705 return (struct commit *)obj;1706}17071708static struct commit *find_single_initial(struct rev_info *revs,1709 const char **name_p)1710{1711 int i;1712 struct commit *found = NULL;1713 const char *name = NULL;17141715 /*1716 * There must be one and only one negative commit, and it must be1717 * the boundary.1718 */1719 for (i = 0; i < revs->pending.nr; i++) {1720 struct object *obj = revs->pending.objects[i].item;1721 if (!(obj->flags & UNINTERESTING))1722 continue;1723 obj = deref_tag(obj, NULL, 0);1724 if (obj->type != OBJ_COMMIT)1725 die("Non commit %s?", revs->pending.objects[i].name);1726 if (found)1727 die("More than one commit to dig up from, %s and %s?",1728 revs->pending.objects[i].name, name);1729 found = (struct commit *) obj;1730 name = revs->pending.objects[i].name;1731 }17321733 if (!name)1734 found = dwim_reverse_initial(revs, &name);1735 if (!name)1736 die("No commit to dig up from?");17371738 if (name_p)1739 *name_p = xstrdup(name);1740 return found;1741}17421743void init_scoreboard(struct blame_scoreboard *sb)1744{1745 memset(sb, 0, sizeof(struct blame_scoreboard));1746 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;1747 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;1748}17491750void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)1751{1752 const char *final_commit_name = NULL;1753 struct blame_origin *o;1754 struct commit *final_commit = NULL;1755 enum object_type type;17561757 if (sb->reverse && sb->contents_from)1758 die(_("--contents and --reverse do not blend well."));17591760 if (!sb->reverse) {1761 sb->final = find_single_final(sb->revs, &final_commit_name);1762 sb->commits.compare = compare_commits_by_commit_date;1763 } else {1764 sb->final = find_single_initial(sb->revs, &final_commit_name);1765 sb->commits.compare = compare_commits_by_reverse_commit_date;1766 }17671768 if (sb->final && sb->contents_from)1769 die(_("cannot use --contents with final commit object name"));17701771 if (sb->reverse && sb->revs->first_parent_only)1772 sb->revs->children.name = NULL;17731774 if (!sb->final) {1775 /*1776 * "--not A B -- path" without anything positive;1777 * do not default to HEAD, but use the working tree1778 * or "--contents".1779 */1780 setup_work_tree();1781 sb->final = fake_working_tree_commit(&sb->revs->diffopt,1782 path, sb->contents_from);1783 add_pending_object(sb->revs, &(sb->final->object), ":");1784 }17851786 if (sb->reverse && sb->revs->first_parent_only) {1787 final_commit = find_single_final(sb->revs, NULL);1788 if (!final_commit)1789 die(_("--reverse and --first-parent together require specified latest commit"));1790 }17911792 /*1793 * If we have bottom, this will mark the ancestors of the1794 * bottom commits we would reach while traversing as1795 * uninteresting.1796 */1797 if (prepare_revision_walk(sb->revs))1798 die(_("revision walk setup failed"));17991800 if (sb->reverse && sb->revs->first_parent_only) {1801 struct commit *c = final_commit;18021803 sb->revs->children.name = "children";1804 while (c->parents &&1805 oidcmp(&c->object.oid, &sb->final->object.oid)) {1806 struct commit_list *l = xcalloc(1, sizeof(*l));18071808 l->item = c;1809 if (add_decoration(&sb->revs->children,1810 &c->parents->item->object, l))1811 die("BUG: not unique item in first-parent chain");1812 c = c->parents->item;1813 }18141815 if (oidcmp(&c->object.oid, &sb->final->object.oid))1816 die(_("--reverse --first-parent together require range along first-parent chain"));1817 }18181819 if (is_null_oid(&sb->final->object.oid)) {1820 o = sb->final->util;1821 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);1822 sb->final_buf_size = o->file.size;1823 }1824 else {1825 o = get_origin(sb->final, path);1826 if (fill_blob_sha1_and_mode(o))1827 die(_("no such path %s in %s"), path, final_commit_name);18281829 if (sb->revs->diffopt.flags.allow_textconv &&1830 textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,1831 &sb->final_buf_size))1832 ;1833 else1834 sb->final_buf = read_object_file(&o->blob_oid, &type,1835 &sb->final_buf_size);18361837 if (!sb->final_buf)1838 die(_("cannot read blob %s for path %s"),1839 oid_to_hex(&o->blob_oid),1840 path);1841 }1842 sb->num_read_blob++;1843 prepare_lines(sb);18441845 if (orig)1846 *orig = o;18471848 free((char *)final_commit_name);1849}1850185118521853struct blame_entry *blame_entry_prepend(struct blame_entry *head,1854 long start, long end,1855 struct blame_origin *o)1856{1857 struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));1858 new_head->lno = start;1859 new_head->num_lines = end - start;1860 new_head->suspect = o;1861 new_head->s_lno = start;1862 new_head->next = head;1863 blame_origin_incref(o);1864 return new_head;1865}