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 12voidblame_origin_decref(struct blame_origin *o) 13{ 14if(o && --o->refcnt <=0) { 15struct blame_origin *p, *l = NULL; 16if(o->previous) 17blame_origin_decref(o->previous); 18free(o->file.ptr); 19/* Should be present exactly once in commit chain */ 20for(p = o->commit->util; p; l = p, p = p->next) { 21if(p == o) { 22if(l) 23 l->next = p->next; 24else 25 o->commit->util = p->next; 26free(o); 27return; 28} 29} 30die("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{ 42struct blame_origin *o; 43FLEX_ALLOC_STR(o, path, path); 44 o->commit = commit; 45 o->refcnt =1; 46 o->next = commit->util; 47 commit->util = o; 48return 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{ 57struct blame_origin *o, *l; 58 59for(o = commit->util, l = NULL; o; l = o, o = o->next) { 60if(!strcmp(o->path, path)) { 61/* bump to front */ 62if(l) { 63 l->next = o->next; 64 o->next = commit->util; 65 commit->util = o; 66} 67returnblame_origin_incref(o); 68} 69} 70returnmake_origin(commit, path); 71} 72 73 74 75static voidverify_working_tree_path(struct commit *work_tree,const char*path) 76{ 77struct commit_list *parents; 78int pos; 79 80for(parents = work_tree->parents; parents; parents = parents->next) { 81const struct object_id *commit_oid = &parents->item->object.oid; 82struct object_id blob_oid; 83unsigned mode; 84 85if(!get_tree_entry(commit_oid, path, &blob_oid, &mode) && 86oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) 87return; 88} 89 90 pos =cache_name_pos(path,strlen(path)); 91if(pos >=0) 92;/* path is in the index */ 93else if(-1- pos < active_nr && 94!strcmp(active_cache[-1- pos]->name, path)) 95;/* path is in the index, unmerged */ 96else 97die("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{ 102struct commit *parent; 103 104 parent =lookup_commit_reference(oid); 105if(!parent) 106die("no such commit%s",oid_to_hex(oid)); 107return&commit_list_insert(parent, tail)->next; 108} 109 110static voidappend_merge_parents(struct commit_list **tail) 111{ 112int merge_head; 113struct strbuf line = STRBUF_INIT; 114 115 merge_head =open(git_path_merge_head(the_repository), O_RDONLY); 116if(merge_head <0) { 117if(errno == ENOENT) 118return; 119die("cannot open '%s' for reading", 120git_path_merge_head(the_repository)); 121} 122 123while(!strbuf_getwholeline_fd(&line, merge_head,'\n')) { 124struct object_id oid; 125if(line.len < GIT_SHA1_HEXSZ ||get_oid_hex(line.buf, &oid)) 126die("unknown line in '%s':%s", 127git_path_merge_head(the_repository), line.buf); 128 tail =append_parent(tail, &oid); 129} 130close(merge_head); 131strbuf_release(&line); 132} 133 134/* 135 * This isn't as simple as passing sb->buf and sb->len, because we 136 * want to transfer ownership of the buffer to the commit (so we 137 * must use detach). 138 */ 139static voidset_commit_buffer_from_strbuf(struct commit *c,struct strbuf *sb) 140{ 141size_t len; 142void*buf =strbuf_detach(sb, &len); 143set_commit_buffer(c, buf, len); 144} 145 146/* 147 * Prepare a dummy commit that represents the work tree (or staged) item. 148 * Note that annotating work tree item never works in the reverse. 149 */ 150static struct commit *fake_working_tree_commit(struct diff_options *opt, 151const char*path, 152const char*contents_from) 153{ 154struct commit *commit; 155struct blame_origin *origin; 156struct commit_list **parent_tail, *parent; 157struct object_id head_oid; 158struct strbuf buf = STRBUF_INIT; 159const char*ident; 160time_t now; 161int size, len; 162struct cache_entry *ce; 163unsigned mode; 164struct strbuf msg = STRBUF_INIT; 165 166read_cache(); 167time(&now); 168 commit =alloc_commit_node(the_repository); 169 commit->object.parsed =1; 170 commit->date = now; 171 parent_tail = &commit->parents; 172 173if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) 174die("no such ref: HEAD"); 175 176 parent_tail =append_parent(parent_tail, &head_oid); 177append_merge_parents(parent_tail); 178verify_working_tree_path(commit, path); 179 180 origin =make_origin(commit, path); 181 182 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0); 183strbuf_addstr(&msg,"tree 0000000000000000000000000000000000000000\n"); 184for(parent = commit->parents; parent; parent = parent->next) 185strbuf_addf(&msg,"parent%s\n", 186oid_to_hex(&parent->item->object.oid)); 187strbuf_addf(&msg, 188"author%s\n" 189"committer%s\n\n" 190"Version of%sfrom%s\n", 191 ident, ident, path, 192(!contents_from ? path : 193(!strcmp(contents_from,"-") ?"standard input": contents_from))); 194set_commit_buffer_from_strbuf(commit, &msg); 195 196if(!contents_from ||strcmp("-", contents_from)) { 197struct stat st; 198const char*read_from; 199char*buf_ptr; 200unsigned long buf_len; 201 202if(contents_from) { 203if(stat(contents_from, &st) <0) 204die_errno("Cannot stat '%s'", contents_from); 205 read_from = contents_from; 206} 207else{ 208if(lstat(path, &st) <0) 209die_errno("Cannot lstat '%s'", path); 210 read_from = path; 211} 212 mode =canon_mode(st.st_mode); 213 214switch(st.st_mode & S_IFMT) { 215case S_IFREG: 216if(opt->flags.allow_textconv && 217textconv_object(read_from, mode, &null_oid,0, &buf_ptr, &buf_len)) 218strbuf_attach(&buf, buf_ptr, buf_len, buf_len +1); 219else if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) 220die_errno("cannot open or read '%s'", read_from); 221break; 222case S_IFLNK: 223if(strbuf_readlink(&buf, read_from, st.st_size) <0) 224die_errno("cannot readlink '%s'", read_from); 225break; 226default: 227die("unsupported file type%s", read_from); 228} 229} 230else{ 231/* Reading from stdin */ 232 mode =0; 233if(strbuf_read(&buf,0,0) <0) 234die_errno("failed to read from stdin"); 235} 236convert_to_git(&the_index, path, buf.buf, buf.len, &buf,0); 237 origin->file.ptr = buf.buf; 238 origin->file.size = buf.len; 239pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); 240 241/* 242 * Read the current index, replace the path entry with 243 * origin->blob_sha1 without mucking with its mode or type 244 * bits; we are not going to write this index out -- we just 245 * want to run "diff-index --cached". 246 */ 247discard_cache(); 248read_cache(); 249 250 len =strlen(path); 251if(!mode) { 252int pos =cache_name_pos(path, len); 253if(0<= pos) 254 mode = active_cache[pos]->ce_mode; 255else 256/* Let's not bother reading from HEAD tree */ 257 mode = S_IFREG |0644; 258} 259 size =cache_entry_size(len); 260 ce =xcalloc(1, size); 261oidcpy(&ce->oid, &origin->blob_oid); 262memcpy(ce->name, path, len); 263 ce->ce_flags =create_ce_flags(0); 264 ce->ce_namelen = len; 265 ce->ce_mode =create_ce_mode(mode); 266add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 267 268cache_tree_invalidate_path(&the_index, path); 269 270return commit; 271} 272 273 274 275static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 276 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 277{ 278 xpparam_t xpp = {0}; 279 xdemitconf_t xecfg = {0}; 280 xdemitcb_t ecb = {NULL}; 281 282 xpp.flags = xdl_opts; 283 xecfg.hunk_func = hunk_func; 284 ecb.priv = cb_data; 285returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 286} 287 288/* 289 * Given an origin, prepare mmfile_t structure to be used by the 290 * diff machinery 291 */ 292static voidfill_origin_blob(struct diff_options *opt, 293struct blame_origin *o, mmfile_t *file,int*num_read_blob) 294{ 295if(!o->file.ptr) { 296enum object_type type; 297unsigned long file_size; 298 299(*num_read_blob)++; 300if(opt->flags.allow_textconv && 301textconv_object(o->path, o->mode, &o->blob_oid,1, &file->ptr, &file_size)) 302; 303else 304 file->ptr =read_object_file(&o->blob_oid, &type, 305&file_size); 306 file->size = file_size; 307 308if(!file->ptr) 309die("Cannot read blob%sfor path%s", 310oid_to_hex(&o->blob_oid), 311 o->path); 312 o->file = *file; 313} 314else 315*file = o->file; 316} 317 318static voiddrop_origin_blob(struct blame_origin *o) 319{ 320if(o->file.ptr) { 321FREE_AND_NULL(o->file.ptr); 322} 323} 324 325/* 326 * Any merge of blames happens on lists of blames that arrived via 327 * different parents in a single suspect. In this case, we want to 328 * sort according to the suspect line numbers as opposed to the final 329 * image line numbers. The function body is somewhat longish because 330 * it avoids unnecessary writes. 331 */ 332 333static struct blame_entry *blame_merge(struct blame_entry *list1, 334struct blame_entry *list2) 335{ 336struct blame_entry *p1 = list1, *p2 = list2, 337**tail = &list1; 338 339if(!p1) 340return p2; 341if(!p2) 342return p1; 343 344if(p1->s_lno <= p2->s_lno) { 345do{ 346 tail = &p1->next; 347if((p1 = *tail) == NULL) { 348*tail = p2; 349return list1; 350} 351}while(p1->s_lno <= p2->s_lno); 352} 353for(;;) { 354*tail = p2; 355do{ 356 tail = &p2->next; 357if((p2 = *tail) == NULL) { 358*tail = p1; 359return list1; 360} 361}while(p1->s_lno > p2->s_lno); 362*tail = p1; 363do{ 364 tail = &p1->next; 365if((p1 = *tail) == NULL) { 366*tail = p2; 367return list1; 368} 369}while(p1->s_lno <= p2->s_lno); 370} 371} 372 373static void*get_next_blame(const void*p) 374{ 375return((struct blame_entry *)p)->next; 376} 377 378static voidset_next_blame(void*p1,void*p2) 379{ 380((struct blame_entry *)p1)->next = p2; 381} 382 383/* 384 * Final image line numbers are all different, so we don't need a 385 * three-way comparison here. 386 */ 387 388static intcompare_blame_final(const void*p1,const void*p2) 389{ 390return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 391?1: -1; 392} 393 394static intcompare_blame_suspect(const void*p1,const void*p2) 395{ 396const struct blame_entry *s1 = p1, *s2 = p2; 397/* 398 * to allow for collating suspects, we sort according to the 399 * respective pointer value as the primary sorting criterion. 400 * The actual relation is pretty unimportant as long as it 401 * establishes a total order. Comparing as integers gives us 402 * that. 403 */ 404if(s1->suspect != s2->suspect) 405return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 406if(s1->s_lno == s2->s_lno) 407return0; 408return s1->s_lno > s2->s_lno ?1: -1; 409} 410 411voidblame_sort_final(struct blame_scoreboard *sb) 412{ 413 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 414 compare_blame_final); 415} 416 417static intcompare_commits_by_reverse_commit_date(const void*a, 418const void*b, 419void*c) 420{ 421return-compare_commits_by_commit_date(a, b, c); 422} 423 424/* 425 * For debugging -- origin is refcounted, and this asserts that 426 * we do not underflow. 427 */ 428static voidsanity_check_refcnt(struct blame_scoreboard *sb) 429{ 430int baa =0; 431struct blame_entry *ent; 432 433for(ent = sb->ent; ent; ent = ent->next) { 434/* Nobody should have zero or negative refcnt */ 435if(ent->suspect->refcnt <=0) { 436fprintf(stderr,"%sin%shas negative refcnt%d\n", 437 ent->suspect->path, 438oid_to_hex(&ent->suspect->commit->object.oid), 439 ent->suspect->refcnt); 440 baa =1; 441} 442} 443if(baa) 444 sb->on_sanity_fail(sb, baa); 445} 446 447/* 448 * If two blame entries that are next to each other came from 449 * contiguous lines in the same origin (i.e. <commit, path> pair), 450 * merge them together. 451 */ 452voidblame_coalesce(struct blame_scoreboard *sb) 453{ 454struct blame_entry *ent, *next; 455 456for(ent = sb->ent; ent && (next = ent->next); ent = next) { 457if(ent->suspect == next->suspect && 458 ent->s_lno + ent->num_lines == next->s_lno) { 459 ent->num_lines += next->num_lines; 460 ent->next = next->next; 461blame_origin_decref(next->suspect); 462free(next); 463 ent->score =0; 464 next = ent;/* again */ 465} 466} 467 468if(sb->debug)/* sanity */ 469sanity_check_refcnt(sb); 470} 471 472/* 473 * Merge the given sorted list of blames into a preexisting origin. 474 * If there were no previous blames to that commit, it is entered into 475 * the commit priority queue of the score board. 476 */ 477 478static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 479struct blame_entry *sorted) 480{ 481if(porigin->suspects) 482 porigin->suspects =blame_merge(porigin->suspects, sorted); 483else{ 484struct blame_origin *o; 485for(o = porigin->commit->util; o; o = o->next) { 486if(o->suspects) { 487 porigin->suspects = sorted; 488return; 489} 490} 491 porigin->suspects = sorted; 492prio_queue_put(&sb->commits, porigin->commit); 493} 494} 495 496/* 497 * Fill the blob_sha1 field of an origin if it hasn't, so that later 498 * call to fill_origin_blob() can use it to locate the data. blob_sha1 499 * for an origin is also used to pass the blame for the entire file to 500 * the parent to detect the case where a child's blob is identical to 501 * that of its parent's. 502 * 503 * This also fills origin->mode for corresponding tree path. 504 */ 505static intfill_blob_sha1_and_mode(struct blame_origin *origin) 506{ 507if(!is_null_oid(&origin->blob_oid)) 508return0; 509if(get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 510goto error_out; 511if(oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB) 512goto error_out; 513return0; 514 error_out: 515oidclr(&origin->blob_oid); 516 origin->mode = S_IFINVALID; 517return-1; 518} 519 520/* 521 * We have an origin -- check if the same path exists in the 522 * parent and return an origin structure to represent it. 523 */ 524static struct blame_origin *find_origin(struct commit *parent, 525struct blame_origin *origin) 526{ 527struct blame_origin *porigin; 528struct diff_options diff_opts; 529const char*paths[2]; 530 531/* First check any existing origins */ 532for(porigin = parent->util; porigin; porigin = porigin->next) 533if(!strcmp(porigin->path, origin->path)) { 534/* 535 * The same path between origin and its parent 536 * without renaming -- the most common case. 537 */ 538returnblame_origin_incref(porigin); 539} 540 541/* See if the origin->path is different between parent 542 * and origin first. Most of the time they are the 543 * same and diff-tree is fairly efficient about this. 544 */ 545diff_setup(&diff_opts); 546 diff_opts.flags.recursive =1; 547 diff_opts.detect_rename =0; 548 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 549 paths[0] = origin->path; 550 paths[1] = NULL; 551 552parse_pathspec(&diff_opts.pathspec, 553 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 554 PATHSPEC_LITERAL_PATH,"", paths); 555diff_setup_done(&diff_opts); 556 557if(is_null_oid(&origin->commit->object.oid)) 558do_diff_cache(&parent->tree->object.oid, &diff_opts); 559else 560diff_tree_oid(&parent->tree->object.oid, 561&origin->commit->tree->object.oid, 562"", &diff_opts); 563diffcore_std(&diff_opts); 564 565if(!diff_queued_diff.nr) { 566/* The path is the same as parent */ 567 porigin =get_origin(parent, origin->path); 568oidcpy(&porigin->blob_oid, &origin->blob_oid); 569 porigin->mode = origin->mode; 570}else{ 571/* 572 * Since origin->path is a pathspec, if the parent 573 * commit had it as a directory, we will see a whole 574 * bunch of deletion of files in the directory that we 575 * do not care about. 576 */ 577int i; 578struct diff_filepair *p = NULL; 579for(i =0; i < diff_queued_diff.nr; i++) { 580const char*name; 581 p = diff_queued_diff.queue[i]; 582 name = p->one->path ? p->one->path : p->two->path; 583if(!strcmp(name, origin->path)) 584break; 585} 586if(!p) 587die("internal error in blame::find_origin"); 588switch(p->status) { 589default: 590die("internal error in blame::find_origin (%c)", 591 p->status); 592case'M': 593 porigin =get_origin(parent, origin->path); 594oidcpy(&porigin->blob_oid, &p->one->oid); 595 porigin->mode = p->one->mode; 596break; 597case'A': 598case'T': 599/* Did not exist in parent, or type changed */ 600break; 601} 602} 603diff_flush(&diff_opts); 604clear_pathspec(&diff_opts.pathspec); 605return porigin; 606} 607 608/* 609 * We have an origin -- find the path that corresponds to it in its 610 * parent and return an origin structure to represent it. 611 */ 612static struct blame_origin *find_rename(struct commit *parent, 613struct blame_origin *origin) 614{ 615struct blame_origin *porigin = NULL; 616struct diff_options diff_opts; 617int i; 618 619diff_setup(&diff_opts); 620 diff_opts.flags.recursive =1; 621 diff_opts.detect_rename = DIFF_DETECT_RENAME; 622 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 623 diff_opts.single_follow = origin->path; 624diff_setup_done(&diff_opts); 625 626if(is_null_oid(&origin->commit->object.oid)) 627do_diff_cache(&parent->tree->object.oid, &diff_opts); 628else 629diff_tree_oid(&parent->tree->object.oid, 630&origin->commit->tree->object.oid, 631"", &diff_opts); 632diffcore_std(&diff_opts); 633 634for(i =0; i < diff_queued_diff.nr; i++) { 635struct diff_filepair *p = diff_queued_diff.queue[i]; 636if((p->status =='R'|| p->status =='C') && 637!strcmp(p->two->path, origin->path)) { 638 porigin =get_origin(parent, p->one->path); 639oidcpy(&porigin->blob_oid, &p->one->oid); 640 porigin->mode = p->one->mode; 641break; 642} 643} 644diff_flush(&diff_opts); 645clear_pathspec(&diff_opts.pathspec); 646return porigin; 647} 648 649/* 650 * Append a new blame entry to a given output queue. 651 */ 652static voidadd_blame_entry(struct blame_entry ***queue, 653const struct blame_entry *src) 654{ 655struct blame_entry *e =xmalloc(sizeof(*e)); 656memcpy(e, src,sizeof(*e)); 657blame_origin_incref(e->suspect); 658 659 e->next = **queue; 660**queue = e; 661*queue = &e->next; 662} 663 664/* 665 * src typically is on-stack; we want to copy the information in it to 666 * a malloced blame_entry that gets added to the given queue. The 667 * origin of dst loses a refcnt. 668 */ 669static voiddup_entry(struct blame_entry ***queue, 670struct blame_entry *dst,struct blame_entry *src) 671{ 672blame_origin_incref(src->suspect); 673blame_origin_decref(dst->suspect); 674memcpy(dst, src,sizeof(*src)); 675 dst->next = **queue; 676**queue = dst; 677*queue = &dst->next; 678} 679 680const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 681{ 682return sb->final_buf + sb->lineno[lno]; 683} 684 685/* 686 * It is known that lines between tlno to same came from parent, and e 687 * has an overlap with that range. it also is known that parent's 688 * line plno corresponds to e's line tlno. 689 * 690 * <---- e -----> 691 * <------> 692 * <------------> 693 * <------------> 694 * <------------------> 695 * 696 * Split e into potentially three parts; before this chunk, the chunk 697 * to be blamed for the parent, and after that portion. 698 */ 699static voidsplit_overlap(struct blame_entry *split, 700struct blame_entry *e, 701int tlno,int plno,int same, 702struct blame_origin *parent) 703{ 704int chunk_end_lno; 705memset(split,0,sizeof(struct blame_entry [3])); 706 707if(e->s_lno < tlno) { 708/* there is a pre-chunk part not blamed on parent */ 709 split[0].suspect =blame_origin_incref(e->suspect); 710 split[0].lno = e->lno; 711 split[0].s_lno = e->s_lno; 712 split[0].num_lines = tlno - e->s_lno; 713 split[1].lno = e->lno + tlno - e->s_lno; 714 split[1].s_lno = plno; 715} 716else{ 717 split[1].lno = e->lno; 718 split[1].s_lno = plno + (e->s_lno - tlno); 719} 720 721if(same < e->s_lno + e->num_lines) { 722/* there is a post-chunk part not blamed on parent */ 723 split[2].suspect =blame_origin_incref(e->suspect); 724 split[2].lno = e->lno + (same - e->s_lno); 725 split[2].s_lno = e->s_lno + (same - e->s_lno); 726 split[2].num_lines = e->s_lno + e->num_lines - same; 727 chunk_end_lno = split[2].lno; 728} 729else 730 chunk_end_lno = e->lno + e->num_lines; 731 split[1].num_lines = chunk_end_lno - split[1].lno; 732 733/* 734 * if it turns out there is nothing to blame the parent for, 735 * forget about the splitting. !split[1].suspect signals this. 736 */ 737if(split[1].num_lines <1) 738return; 739 split[1].suspect =blame_origin_incref(parent); 740} 741 742/* 743 * split_overlap() divided an existing blame e into up to three parts 744 * in split. Any assigned blame is moved to queue to 745 * reflect the split. 746 */ 747static voidsplit_blame(struct blame_entry ***blamed, 748struct blame_entry ***unblamed, 749struct blame_entry *split, 750struct blame_entry *e) 751{ 752if(split[0].suspect && split[2].suspect) { 753/* The first part (reuse storage for the existing entry e) */ 754dup_entry(unblamed, e, &split[0]); 755 756/* The last part -- me */ 757add_blame_entry(unblamed, &split[2]); 758 759/* ... and the middle part -- parent */ 760add_blame_entry(blamed, &split[1]); 761} 762else if(!split[0].suspect && !split[2].suspect) 763/* 764 * The parent covers the entire area; reuse storage for 765 * e and replace it with the parent. 766 */ 767dup_entry(blamed, e, &split[1]); 768else if(split[0].suspect) { 769/* me and then parent */ 770dup_entry(unblamed, e, &split[0]); 771add_blame_entry(blamed, &split[1]); 772} 773else{ 774/* parent and then me */ 775dup_entry(blamed, e, &split[1]); 776add_blame_entry(unblamed, &split[2]); 777} 778} 779 780/* 781 * After splitting the blame, the origins used by the 782 * on-stack blame_entry should lose one refcnt each. 783 */ 784static voiddecref_split(struct blame_entry *split) 785{ 786int i; 787 788for(i =0; i <3; i++) 789blame_origin_decref(split[i].suspect); 790} 791 792/* 793 * reverse_blame reverses the list given in head, appending tail. 794 * That allows us to build lists in reverse order, then reverse them 795 * afterwards. This can be faster than building the list in proper 796 * order right away. The reason is that building in proper order 797 * requires writing a link in the _previous_ element, while building 798 * in reverse order just requires placing the list head into the 799 * _current_ element. 800 */ 801 802static struct blame_entry *reverse_blame(struct blame_entry *head, 803struct blame_entry *tail) 804{ 805while(head) { 806struct blame_entry *next = head->next; 807 head->next = tail; 808 tail = head; 809 head = next; 810} 811return tail; 812} 813 814/* 815 * Process one hunk from the patch between the current suspect for 816 * blame_entry e and its parent. This first blames any unfinished 817 * entries before the chunk (which is where target and parent start 818 * differing) on the parent, and then splits blame entries at the 819 * start and at the end of the difference region. Since use of -M and 820 * -C options may lead to overlapping/duplicate source line number 821 * ranges, all we can rely on from sorting/merging is the order of the 822 * first suspect line number. 823 */ 824static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 825int tlno,int offset,int same, 826struct blame_origin *parent) 827{ 828struct blame_entry *e = **srcq; 829struct blame_entry *samep = NULL, *diffp = NULL; 830 831while(e && e->s_lno < tlno) { 832struct blame_entry *next = e->next; 833/* 834 * current record starts before differing portion. If 835 * it reaches into it, we need to split it up and 836 * examine the second part separately. 837 */ 838if(e->s_lno + e->num_lines > tlno) { 839/* Move second half to a new record */ 840int len = tlno - e->s_lno; 841struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 842 n->suspect = e->suspect; 843 n->lno = e->lno + len; 844 n->s_lno = e->s_lno + len; 845 n->num_lines = e->num_lines - len; 846 e->num_lines = len; 847 e->score =0; 848/* Push new record to diffp */ 849 n->next = diffp; 850 diffp = n; 851}else 852blame_origin_decref(e->suspect); 853/* Pass blame for everything before the differing 854 * chunk to the parent */ 855 e->suspect =blame_origin_incref(parent); 856 e->s_lno += offset; 857 e->next = samep; 858 samep = e; 859 e = next; 860} 861/* 862 * As we don't know how much of a common stretch after this 863 * diff will occur, the currently blamed parts are all that we 864 * can assign to the parent for now. 865 */ 866 867if(samep) { 868**dstq =reverse_blame(samep, **dstq); 869*dstq = &samep->next; 870} 871/* 872 * Prepend the split off portions: everything after e starts 873 * after the blameable portion. 874 */ 875 e =reverse_blame(diffp, e); 876 877/* 878 * Now retain records on the target while parts are different 879 * from the parent. 880 */ 881 samep = NULL; 882 diffp = NULL; 883while(e && e->s_lno < same) { 884struct blame_entry *next = e->next; 885 886/* 887 * If current record extends into sameness, need to split. 888 */ 889if(e->s_lno + e->num_lines > same) { 890/* 891 * Move second half to a new record to be 892 * processed by later chunks 893 */ 894int len = same - e->s_lno; 895struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 896 n->suspect =blame_origin_incref(e->suspect); 897 n->lno = e->lno + len; 898 n->s_lno = e->s_lno + len; 899 n->num_lines = e->num_lines - len; 900 e->num_lines = len; 901 e->score =0; 902/* Push new record to samep */ 903 n->next = samep; 904 samep = n; 905} 906 e->next = diffp; 907 diffp = e; 908 e = next; 909} 910**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 911/* Move across elements that are in the unblamable portion */ 912if(diffp) 913*srcq = &diffp->next; 914} 915 916struct blame_chunk_cb_data { 917struct blame_origin *parent; 918long offset; 919struct blame_entry **dstq; 920struct blame_entry **srcq; 921}; 922 923/* diff chunks are from parent to target */ 924static intblame_chunk_cb(long start_a,long count_a, 925long start_b,long count_b,void*data) 926{ 927struct blame_chunk_cb_data *d = data; 928if(start_a - start_b != d->offset) 929die("internal error in blame::blame_chunk_cb"); 930blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 931 start_b + count_b, d->parent); 932 d->offset = start_a + count_a - (start_b + count_b); 933return0; 934} 935 936/* 937 * We are looking at the origin 'target' and aiming to pass blame 938 * for the lines it is suspected to its parent. Run diff to find 939 * which lines came from parent and pass blame for them. 940 */ 941static voidpass_blame_to_parent(struct blame_scoreboard *sb, 942struct blame_origin *target, 943struct blame_origin *parent) 944{ 945 mmfile_t file_p, file_o; 946struct blame_chunk_cb_data d; 947struct blame_entry *newdest = NULL; 948 949if(!target->suspects) 950return;/* nothing remains for this target */ 951 952 d.parent = parent; 953 d.offset =0; 954 d.dstq = &newdest; d.srcq = &target->suspects; 955 956fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 957fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 958 sb->num_get_patch++; 959 960if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 961die("unable to generate diff (%s->%s)", 962oid_to_hex(&parent->commit->object.oid), 963oid_to_hex(&target->commit->object.oid)); 964/* The rest are the same as the parent */ 965blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 966*d.dstq = NULL; 967queue_blames(sb, parent, newdest); 968 969return; 970} 971 972/* 973 * The lines in blame_entry after splitting blames many times can become 974 * very small and trivial, and at some point it becomes pointless to 975 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 976 * ordinary C program, and it is not worth to say it was copied from 977 * totally unrelated file in the parent. 978 * 979 * Compute how trivial the lines in the blame_entry are. 980 */ 981unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e) 982{ 983unsigned score; 984const char*cp, *ep; 985 986if(e->score) 987return e->score; 988 989 score =1; 990 cp =blame_nth_line(sb, e->lno); 991 ep =blame_nth_line(sb, e->lno + e->num_lines); 992while(cp < ep) { 993unsigned ch = *((unsigned char*)cp); 994if(isalnum(ch)) 995 score++; 996 cp++; 997} 998 e->score = score; 999return score;1000}10011002/*1003 * best_so_far[] and potential[] are both a split of an existing blame_entry1004 * that passes blame to the parent. Maintain best_so_far the best split so1005 * far, by comparing potential and best_so_far and copying potential into1006 * bst_so_far as needed.1007 */1008static voidcopy_split_if_better(struct blame_scoreboard *sb,1009struct blame_entry *best_so_far,1010struct blame_entry *potential)1011{1012int i;10131014if(!potential[1].suspect)1015return;1016if(best_so_far[1].suspect) {1017if(blame_entry_score(sb, &potential[1]) <1018blame_entry_score(sb, &best_so_far[1]))1019return;1020}10211022for(i =0; i <3; i++)1023blame_origin_incref(potential[i].suspect);1024decref_split(best_so_far);1025memcpy(best_so_far, potential,sizeof(struct blame_entry[3]));1026}10271028/*1029 * We are looking at a part of the final image represented by1030 * ent (tlno and same are offset by ent->s_lno).1031 * tlno is where we are looking at in the final image.1032 * up to (but not including) same match preimage.1033 * plno is where we are looking at in the preimage.1034 *1035 * <-------------- final image ---------------------->1036 * <------ent------>1037 * ^tlno ^same1038 * <---------preimage----->1039 * ^plno1040 *1041 * All line numbers are 0-based.1042 */1043static voidhandle_split(struct blame_scoreboard *sb,1044struct blame_entry *ent,1045int tlno,int plno,int same,1046struct blame_origin *parent,1047struct blame_entry *split)1048{1049if(ent->num_lines <= tlno)1050return;1051if(tlno < same) {1052struct blame_entry potential[3];1053 tlno += ent->s_lno;1054 same += ent->s_lno;1055split_overlap(potential, ent, tlno, plno, same, parent);1056copy_split_if_better(sb, split, potential);1057decref_split(potential);1058}1059}10601061struct handle_split_cb_data {1062struct blame_scoreboard *sb;1063struct blame_entry *ent;1064struct blame_origin *parent;1065struct blame_entry *split;1066long plno;1067long tlno;1068};10691070static inthandle_split_cb(long start_a,long count_a,1071long start_b,long count_b,void*data)1072{1073struct handle_split_cb_data *d = data;1074handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1075 d->split);1076 d->plno = start_a + count_a;1077 d->tlno = start_b + count_b;1078return0;1079}10801081/*1082 * Find the lines from parent that are the same as ent so that1083 * we can pass blames to it. file_p has the blob contents for1084 * the parent.1085 */1086static voidfind_copy_in_blob(struct blame_scoreboard *sb,1087struct blame_entry *ent,1088struct blame_origin *parent,1089struct blame_entry *split,1090 mmfile_t *file_p)1091{1092const char*cp;1093 mmfile_t file_o;1094struct handle_split_cb_data d;10951096memset(&d,0,sizeof(d));1097 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1098/*1099 * Prepare mmfile that contains only the lines in ent.1100 */1101 cp =blame_nth_line(sb, ent->lno);1102 file_o.ptr = (char*) cp;1103 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11041105/*1106 * file_o is a part of final image we are annotating.1107 * file_p partially may match that image.1108 */1109memset(split,0,sizeof(struct blame_entry [3]));1110if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1111die("unable to generate diff (%s)",1112oid_to_hex(&parent->commit->object.oid));1113/* remainder, if any, all match the preimage */1114handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1115}11161117/* Move all blame entries from list *source that have a score smaller1118 * than score_min to the front of list *small.1119 * Returns a pointer to the link pointing to the old head of the small list.1120 */11211122static struct blame_entry **filter_small(struct blame_scoreboard *sb,1123struct blame_entry **small,1124struct blame_entry **source,1125unsigned score_min)1126{1127struct blame_entry *p = *source;1128struct blame_entry *oldsmall = *small;1129while(p) {1130if(blame_entry_score(sb, p) <= score_min) {1131*small = p;1132 small = &p->next;1133 p = *small;1134}else{1135*source = p;1136 source = &p->next;1137 p = *source;1138}1139}1140*small = oldsmall;1141*source = NULL;1142return small;1143}11441145/*1146 * See if lines currently target is suspected for can be attributed to1147 * parent.1148 */1149static voidfind_move_in_parent(struct blame_scoreboard *sb,1150struct blame_entry ***blamed,1151struct blame_entry **toosmall,1152struct blame_origin *target,1153struct blame_origin *parent)1154{1155struct blame_entry *e, split[3];1156struct blame_entry *unblamed = target->suspects;1157struct blame_entry *leftover = NULL;1158 mmfile_t file_p;11591160if(!unblamed)1161return;/* nothing remains for this target */11621163fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1164if(!file_p.ptr)1165return;11661167/* At each iteration, unblamed has a NULL-terminated list of1168 * entries that have not yet been tested for blame. leftover1169 * contains the reversed list of entries that have been tested1170 * without being assignable to the parent.1171 */1172do{1173struct blame_entry **unblamedtail = &unblamed;1174struct blame_entry *next;1175for(e = unblamed; e; e = next) {1176 next = e->next;1177find_copy_in_blob(sb, e, parent, split, &file_p);1178if(split[1].suspect &&1179 sb->move_score <blame_entry_score(sb, &split[1])) {1180split_blame(blamed, &unblamedtail, split, e);1181}else{1182 e->next = leftover;1183 leftover = e;1184}1185decref_split(split);1186}1187*unblamedtail = NULL;1188 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1189}while(unblamed);1190 target->suspects =reverse_blame(leftover, NULL);1191}11921193struct blame_list {1194struct blame_entry *ent;1195struct blame_entry split[3];1196};11971198/*1199 * Count the number of entries the target is suspected for,1200 * and prepare a list of entry and the best split.1201 */1202static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1203int*num_ents_p)1204{1205struct blame_entry *e;1206int num_ents, i;1207struct blame_list *blame_list = NULL;12081209for(e = unblamed, num_ents =0; e; e = e->next)1210 num_ents++;1211if(num_ents) {1212 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1213for(e = unblamed, i =0; e; e = e->next)1214 blame_list[i++].ent = e;1215}1216*num_ents_p = num_ents;1217return blame_list;1218}12191220/*1221 * For lines target is suspected for, see if we can find code movement1222 * across file boundary from the parent commit. porigin is the path1223 * in the parent we already tried.1224 */1225static voidfind_copy_in_parent(struct blame_scoreboard *sb,1226struct blame_entry ***blamed,1227struct blame_entry **toosmall,1228struct blame_origin *target,1229struct commit *parent,1230struct blame_origin *porigin,1231int opt)1232{1233struct diff_options diff_opts;1234int i, j;1235struct blame_list *blame_list;1236int num_ents;1237struct blame_entry *unblamed = target->suspects;1238struct blame_entry *leftover = NULL;12391240if(!unblamed)1241return;/* nothing remains for this target */12421243diff_setup(&diff_opts);1244 diff_opts.flags.recursive =1;1245 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12461247diff_setup_done(&diff_opts);12481249/* Try "find copies harder" on new path if requested;1250 * we do not want to use diffcore_rename() actually to1251 * match things up; find_copies_harder is set only to1252 * force diff_tree_oid() to feed all filepairs to diff_queue,1253 * and this code needs to be after diff_setup_done(), which1254 * usually makes find-copies-harder imply copy detection.1255 */1256if((opt & PICKAXE_BLAME_COPY_HARDEST)1257|| ((opt & PICKAXE_BLAME_COPY_HARDER)1258&& (!porigin ||strcmp(target->path, porigin->path))))1259 diff_opts.flags.find_copies_harder =1;12601261if(is_null_oid(&target->commit->object.oid))1262do_diff_cache(&parent->tree->object.oid, &diff_opts);1263else1264diff_tree_oid(&parent->tree->object.oid,1265&target->commit->tree->object.oid,1266"", &diff_opts);12671268if(!diff_opts.flags.find_copies_harder)1269diffcore_std(&diff_opts);12701271do{1272struct blame_entry **unblamedtail = &unblamed;1273 blame_list =setup_blame_list(unblamed, &num_ents);12741275for(i =0; i < diff_queued_diff.nr; i++) {1276struct diff_filepair *p = diff_queued_diff.queue[i];1277struct blame_origin *norigin;1278 mmfile_t file_p;1279struct blame_entry potential[3];12801281if(!DIFF_FILE_VALID(p->one))1282continue;/* does not exist in parent */1283if(S_ISGITLINK(p->one->mode))1284continue;/* ignore git links */1285if(porigin && !strcmp(p->one->path, porigin->path))1286/* find_move already dealt with this path */1287continue;12881289 norigin =get_origin(parent, p->one->path);1290oidcpy(&norigin->blob_oid, &p->one->oid);1291 norigin->mode = p->one->mode;1292fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1293if(!file_p.ptr)1294continue;12951296for(j =0; j < num_ents; j++) {1297find_copy_in_blob(sb, blame_list[j].ent,1298 norigin, potential, &file_p);1299copy_split_if_better(sb, blame_list[j].split,1300 potential);1301decref_split(potential);1302}1303blame_origin_decref(norigin);1304}13051306for(j =0; j < num_ents; j++) {1307struct blame_entry *split = blame_list[j].split;1308if(split[1].suspect &&1309 sb->copy_score <blame_entry_score(sb, &split[1])) {1310split_blame(blamed, &unblamedtail, split,1311 blame_list[j].ent);1312}else{1313 blame_list[j].ent->next = leftover;1314 leftover = blame_list[j].ent;1315}1316decref_split(split);1317}1318free(blame_list);1319*unblamedtail = NULL;1320 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1321}while(unblamed);1322 target->suspects =reverse_blame(leftover, NULL);1323diff_flush(&diff_opts);1324clear_pathspec(&diff_opts.pathspec);1325}13261327/*1328 * The blobs of origin and porigin exactly match, so everything1329 * origin is suspected for can be blamed on the parent.1330 */1331static voidpass_whole_blame(struct blame_scoreboard *sb,1332struct blame_origin *origin,struct blame_origin *porigin)1333{1334struct blame_entry *e, *suspects;13351336if(!porigin->file.ptr && origin->file.ptr) {1337/* Steal its file */1338 porigin->file = origin->file;1339 origin->file.ptr = NULL;1340}1341 suspects = origin->suspects;1342 origin->suspects = NULL;1343for(e = suspects; e; e = e->next) {1344blame_origin_incref(porigin);1345blame_origin_decref(e->suspect);1346 e->suspect = porigin;1347}1348queue_blames(sb, porigin, suspects);1349}13501351/*1352 * We pass blame from the current commit to its parents. We keep saying1353 * "parent" (and "porigin"), but what we mean is to find scapegoat to1354 * exonerate ourselves.1355 */1356static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1357int reverse)1358{1359if(!reverse) {1360if(revs->first_parent_only &&1361 commit->parents &&1362 commit->parents->next) {1363free_commit_list(commit->parents->next);1364 commit->parents->next = NULL;1365}1366return commit->parents;1367}1368returnlookup_decoration(&revs->children, &commit->object);1369}13701371static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1372{1373struct commit_list *l =first_scapegoat(revs, commit, reverse);1374returncommit_list_count(l);1375}13761377/* Distribute collected unsorted blames to the respected sorted lists1378 * in the various origins.1379 */1380static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1381{1382 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1383 compare_blame_suspect);1384while(blamed)1385{1386struct blame_origin *porigin = blamed->suspect;1387struct blame_entry *suspects = NULL;1388do{1389struct blame_entry *next = blamed->next;1390 blamed->next = suspects;1391 suspects = blamed;1392 blamed = next;1393}while(blamed && blamed->suspect == porigin);1394 suspects =reverse_blame(suspects, NULL);1395queue_blames(sb, porigin, suspects);1396}1397}13981399#define MAXSG 1614001401static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1402{1403struct rev_info *revs = sb->revs;1404int i, pass, num_sg;1405struct commit *commit = origin->commit;1406struct commit_list *sg;1407struct blame_origin *sg_buf[MAXSG];1408struct blame_origin *porigin, **sg_origin = sg_buf;1409struct blame_entry *toosmall = NULL;1410struct blame_entry *blames, **blametail = &blames;14111412 num_sg =num_scapegoats(revs, commit, sb->reverse);1413if(!num_sg)1414goto finish;1415else if(num_sg <ARRAY_SIZE(sg_buf))1416memset(sg_buf,0,sizeof(sg_buf));1417else1418 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14191420/*1421 * The first pass looks for unrenamed path to optimize for1422 * common cases, then we look for renames in the second pass.1423 */1424for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1425struct blame_origin *(*find)(struct commit *,struct blame_origin *);1426 find = pass ? find_rename : find_origin;14271428for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1429 i < num_sg && sg;1430 sg = sg->next, i++) {1431struct commit *p = sg->item;1432int j, same;14331434if(sg_origin[i])1435continue;1436if(parse_commit(p))1437continue;1438 porigin =find(p, origin);1439if(!porigin)1440continue;1441if(!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1442pass_whole_blame(sb, origin, porigin);1443blame_origin_decref(porigin);1444goto finish;1445}1446for(j = same =0; j < i; j++)1447if(sg_origin[j] &&1448!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1449 same =1;1450break;1451}1452if(!same)1453 sg_origin[i] = porigin;1454else1455blame_origin_decref(porigin);1456}1457}14581459 sb->num_commits++;1460for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1461 i < num_sg && sg;1462 sg = sg->next, i++) {1463struct blame_origin *porigin = sg_origin[i];1464if(!porigin)1465continue;1466if(!origin->previous) {1467blame_origin_incref(porigin);1468 origin->previous = porigin;1469}1470pass_blame_to_parent(sb, origin, porigin);1471if(!origin->suspects)1472goto finish;1473}14741475/*1476 * Optionally find moves in parents' files.1477 */1478if(opt & PICKAXE_BLAME_MOVE) {1479filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1480if(origin->suspects) {1481for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1482 i < num_sg && sg;1483 sg = sg->next, i++) {1484struct blame_origin *porigin = sg_origin[i];1485if(!porigin)1486continue;1487find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1488if(!origin->suspects)1489break;1490}1491}1492}14931494/*1495 * Optionally find copies from parents' files.1496 */1497if(opt & PICKAXE_BLAME_COPY) {1498if(sb->copy_score > sb->move_score)1499filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1500else if(sb->copy_score < sb->move_score) {1501 origin->suspects =blame_merge(origin->suspects, toosmall);1502 toosmall = NULL;1503filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1504}1505if(!origin->suspects)1506goto finish;15071508for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1509 i < num_sg && sg;1510 sg = sg->next, i++) {1511struct blame_origin *porigin = sg_origin[i];1512find_copy_in_parent(sb, &blametail, &toosmall,1513 origin, sg->item, porigin, opt);1514if(!origin->suspects)1515goto finish;1516}1517}15181519finish:1520*blametail = NULL;1521distribute_blame(sb, blames);1522/*1523 * prepend toosmall to origin->suspects1524 *1525 * There is no point in sorting: this ends up on a big1526 * unsorted list in the caller anyway.1527 */1528if(toosmall) {1529struct blame_entry **tail = &toosmall;1530while(*tail)1531 tail = &(*tail)->next;1532*tail = origin->suspects;1533 origin->suspects = toosmall;1534}1535for(i =0; i < num_sg; i++) {1536if(sg_origin[i]) {1537drop_origin_blob(sg_origin[i]);1538blame_origin_decref(sg_origin[i]);1539}1540}1541drop_origin_blob(origin);1542if(sg_buf != sg_origin)1543free(sg_origin);1544}15451546/*1547 * The main loop -- while we have blobs with lines whose true origin1548 * is still unknown, pick one blob, and allow its lines to pass blames1549 * to its parents. */1550voidassign_blame(struct blame_scoreboard *sb,int opt)1551{1552struct rev_info *revs = sb->revs;1553struct commit *commit =prio_queue_get(&sb->commits);15541555while(commit) {1556struct blame_entry *ent;1557struct blame_origin *suspect = commit->util;15581559/* find one suspect to break down */1560while(suspect && !suspect->suspects)1561 suspect = suspect->next;15621563if(!suspect) {1564 commit =prio_queue_get(&sb->commits);1565continue;1566}15671568assert(commit == suspect->commit);15691570/*1571 * We will use this suspect later in the loop,1572 * so hold onto it in the meantime.1573 */1574blame_origin_incref(suspect);1575parse_commit(commit);1576if(sb->reverse ||1577(!(commit->object.flags & UNINTERESTING) &&1578!(revs->max_age != -1&& commit->date < revs->max_age)))1579pass_blame(sb, suspect, opt);1580else{1581 commit->object.flags |= UNINTERESTING;1582if(commit->object.parsed)1583mark_parents_uninteresting(commit);1584}1585/* treat root commit as boundary */1586if(!commit->parents && !sb->show_root)1587 commit->object.flags |= UNINTERESTING;15881589/* Take responsibility for the remaining entries */1590 ent = suspect->suspects;1591if(ent) {1592 suspect->guilty =1;1593for(;;) {1594struct blame_entry *next = ent->next;1595if(sb->found_guilty_entry)1596 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1597if(next) {1598 ent = next;1599continue;1600}1601 ent->next = sb->ent;1602 sb->ent = suspect->suspects;1603 suspect->suspects = NULL;1604break;1605}1606}1607blame_origin_decref(suspect);16081609if(sb->debug)/* sanity */1610sanity_check_refcnt(sb);1611}1612}16131614static const char*get_next_line(const char*start,const char*end)1615{1616const char*nl =memchr(start,'\n', end - start);1617return nl ? nl +1: end;1618}16191620/*1621 * To allow quick access to the contents of nth line in the1622 * final image, prepare an index in the scoreboard.1623 */1624static intprepare_lines(struct blame_scoreboard *sb)1625{1626const char*buf = sb->final_buf;1627unsigned long len = sb->final_buf_size;1628const char*end = buf + len;1629const char*p;1630int*lineno;1631int num =0;16321633for(p = buf; p < end; p =get_next_line(p, end))1634 num++;16351636ALLOC_ARRAY(sb->lineno, num +1);1637 lineno = sb->lineno;16381639for(p = buf; p < end; p =get_next_line(p, end))1640*lineno++ = p - buf;16411642*lineno = len;16431644 sb->num_lines = num;1645return sb->num_lines;1646}16471648static struct commit *find_single_final(struct rev_info *revs,1649const char**name_p)1650{1651int i;1652struct commit *found = NULL;1653const char*name = NULL;16541655for(i =0; i < revs->pending.nr; i++) {1656struct object *obj = revs->pending.objects[i].item;1657if(obj->flags & UNINTERESTING)1658continue;1659 obj =deref_tag(obj, NULL,0);1660if(obj->type != OBJ_COMMIT)1661die("Non commit%s?", revs->pending.objects[i].name);1662if(found)1663die("More than one commit to dig from%sand%s?",1664 revs->pending.objects[i].name, name);1665 found = (struct commit *)obj;1666 name = revs->pending.objects[i].name;1667}1668if(name_p)1669*name_p =xstrdup_or_null(name);1670return found;1671}16721673static struct commit *dwim_reverse_initial(struct rev_info *revs,1674const char**name_p)1675{1676/*1677 * DWIM "git blame --reverse ONE -- PATH" as1678 * "git blame --reverse ONE..HEAD -- PATH" but only do so1679 * when it makes sense.1680 */1681struct object *obj;1682struct commit *head_commit;1683struct object_id head_oid;16841685if(revs->pending.nr !=1)1686return NULL;16871688/* Is that sole rev a committish? */1689 obj = revs->pending.objects[0].item;1690 obj =deref_tag(obj, NULL,0);1691if(obj->type != OBJ_COMMIT)1692return NULL;16931694/* Do we have HEAD? */1695if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1696return NULL;1697 head_commit =lookup_commit_reference_gently(&head_oid,1);1698if(!head_commit)1699return NULL;17001701/* Turn "ONE" into "ONE..HEAD" then */1702 obj->flags |= UNINTERESTING;1703add_pending_object(revs, &head_commit->object,"HEAD");17041705if(name_p)1706*name_p = revs->pending.objects[0].name;1707return(struct commit *)obj;1708}17091710static struct commit *find_single_initial(struct rev_info *revs,1711const char**name_p)1712{1713int i;1714struct commit *found = NULL;1715const char*name = NULL;17161717/*1718 * There must be one and only one negative commit, and it must be1719 * the boundary.1720 */1721for(i =0; i < revs->pending.nr; i++) {1722struct object *obj = revs->pending.objects[i].item;1723if(!(obj->flags & UNINTERESTING))1724continue;1725 obj =deref_tag(obj, NULL,0);1726if(obj->type != OBJ_COMMIT)1727die("Non commit%s?", revs->pending.objects[i].name);1728if(found)1729die("More than one commit to dig up from,%sand%s?",1730 revs->pending.objects[i].name, name);1731 found = (struct commit *) obj;1732 name = revs->pending.objects[i].name;1733}17341735if(!name)1736 found =dwim_reverse_initial(revs, &name);1737if(!name)1738die("No commit to dig up from?");17391740if(name_p)1741*name_p =xstrdup(name);1742return found;1743}17441745voidinit_scoreboard(struct blame_scoreboard *sb)1746{1747memset(sb,0,sizeof(struct blame_scoreboard));1748 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;1749 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;1750}17511752voidsetup_scoreboard(struct blame_scoreboard *sb,const char*path,struct blame_origin **orig)1753{1754const char*final_commit_name = NULL;1755struct blame_origin *o;1756struct commit *final_commit = NULL;1757enum object_type type;17581759if(sb->reverse && sb->contents_from)1760die(_("--contents and --reverse do not blend well."));17611762if(!sb->reverse) {1763 sb->final =find_single_final(sb->revs, &final_commit_name);1764 sb->commits.compare = compare_commits_by_commit_date;1765}else{1766 sb->final =find_single_initial(sb->revs, &final_commit_name);1767 sb->commits.compare = compare_commits_by_reverse_commit_date;1768}17691770if(sb->final && sb->contents_from)1771die(_("cannot use --contents with final commit object name"));17721773if(sb->reverse && sb->revs->first_parent_only)1774 sb->revs->children.name = NULL;17751776if(!sb->final) {1777/*1778 * "--not A B -- path" without anything positive;1779 * do not default to HEAD, but use the working tree1780 * or "--contents".1781 */1782setup_work_tree();1783 sb->final =fake_working_tree_commit(&sb->revs->diffopt,1784 path, sb->contents_from);1785add_pending_object(sb->revs, &(sb->final->object),":");1786}17871788if(sb->reverse && sb->revs->first_parent_only) {1789 final_commit =find_single_final(sb->revs, NULL);1790if(!final_commit)1791die(_("--reverse and --first-parent together require specified latest commit"));1792}17931794/*1795 * If we have bottom, this will mark the ancestors of the1796 * bottom commits we would reach while traversing as1797 * uninteresting.1798 */1799if(prepare_revision_walk(sb->revs))1800die(_("revision walk setup failed"));18011802if(sb->reverse && sb->revs->first_parent_only) {1803struct commit *c = final_commit;18041805 sb->revs->children.name ="children";1806while(c->parents &&1807oidcmp(&c->object.oid, &sb->final->object.oid)) {1808struct commit_list *l =xcalloc(1,sizeof(*l));18091810 l->item = c;1811if(add_decoration(&sb->revs->children,1812&c->parents->item->object, l))1813die("BUG: not unique item in first-parent chain");1814 c = c->parents->item;1815}18161817if(oidcmp(&c->object.oid, &sb->final->object.oid))1818die(_("--reverse --first-parent together require range along first-parent chain"));1819}18201821if(is_null_oid(&sb->final->object.oid)) {1822 o = sb->final->util;1823 sb->final_buf =xmemdupz(o->file.ptr, o->file.size);1824 sb->final_buf_size = o->file.size;1825}1826else{1827 o =get_origin(sb->final, path);1828if(fill_blob_sha1_and_mode(o))1829die(_("no such path%sin%s"), path, final_commit_name);18301831if(sb->revs->diffopt.flags.allow_textconv &&1832textconv_object(path, o->mode, &o->blob_oid,1, (char**) &sb->final_buf,1833&sb->final_buf_size))1834;1835else1836 sb->final_buf =read_object_file(&o->blob_oid, &type,1837&sb->final_buf_size);18381839if(!sb->final_buf)1840die(_("cannot read blob%sfor path%s"),1841oid_to_hex(&o->blob_oid),1842 path);1843}1844 sb->num_read_blob++;1845prepare_lines(sb);18461847if(orig)1848*orig = o;18491850free((char*)final_commit_name);1851}1852185318541855struct blame_entry *blame_entry_prepend(struct blame_entry *head,1856long start,long end,1857struct blame_origin *o)1858{1859struct blame_entry *new_head =xcalloc(1,sizeof(struct blame_entry));1860 new_head->lno = start;1861 new_head->num_lines = end - start;1862 new_head->suspect = o;1863 new_head->s_lno = start;1864 new_head->next = head;1865blame_origin_incref(o);1866return new_head;1867}