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#include"commit-slab.h" 12 13define_commit_slab(blame_suspects,struct blame_origin *); 14static struct blame_suspects blame_suspects; 15 16struct blame_origin *get_blame_suspects(struct commit *commit) 17{ 18struct blame_origin **result; 19 20 result =blame_suspects_peek(&blame_suspects, commit); 21 22return result ? *result : NULL; 23} 24 25static voidset_blame_suspects(struct commit *commit,struct blame_origin *origin) 26{ 27*blame_suspects_at(&blame_suspects, commit) = origin; 28} 29 30voidblame_origin_decref(struct blame_origin *o) 31{ 32if(o && --o->refcnt <=0) { 33struct blame_origin *p, *l = NULL; 34if(o->previous) 35blame_origin_decref(o->previous); 36free(o->file.ptr); 37/* Should be present exactly once in commit chain */ 38for(p =get_blame_suspects(o->commit); p; l = p, p = p->next) { 39if(p == o) { 40if(l) 41 l->next = p->next; 42else 43set_blame_suspects(o->commit, p->next); 44free(o); 45return; 46} 47} 48die("internal error in blame_origin_decref"); 49} 50} 51 52/* 53 * Given a commit and a path in it, create a new origin structure. 54 * The callers that add blame to the scoreboard should use 55 * get_origin() to obtain shared, refcounted copy instead of calling 56 * this function directly. 57 */ 58static struct blame_origin *make_origin(struct commit *commit,const char*path) 59{ 60struct blame_origin *o; 61FLEX_ALLOC_STR(o, path, path); 62 o->commit = commit; 63 o->refcnt =1; 64 o->next =get_blame_suspects(commit); 65set_blame_suspects(commit, o); 66return o; 67} 68 69/* 70 * Locate an existing origin or create a new one. 71 * This moves the origin to front position in the commit util list. 72 */ 73static struct blame_origin *get_origin(struct commit *commit,const char*path) 74{ 75struct blame_origin *o, *l; 76 77for(o =get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) { 78if(!strcmp(o->path, path)) { 79/* bump to front */ 80if(l) { 81 l->next = o->next; 82 o->next =get_blame_suspects(commit); 83set_blame_suspects(commit, o); 84} 85returnblame_origin_incref(o); 86} 87} 88returnmake_origin(commit, path); 89} 90 91 92 93static voidverify_working_tree_path(struct repository *repo, 94struct commit *work_tree,const char*path) 95{ 96struct commit_list *parents; 97int pos; 98 99for(parents = work_tree->parents; parents; parents = parents->next) { 100const struct object_id *commit_oid = &parents->item->object.oid; 101struct object_id blob_oid; 102unsigned mode; 103 104if(!get_tree_entry(commit_oid, path, &blob_oid, &mode) && 105oid_object_info(repo, &blob_oid, NULL) == OBJ_BLOB) 106return; 107} 108 109 pos =index_name_pos(repo->index, path,strlen(path)); 110if(pos >=0) 111;/* path is in the index */ 112else if(-1- pos < repo->index->cache_nr && 113!strcmp(repo->index->cache[-1- pos]->name, path)) 114;/* path is in the index, unmerged */ 115else 116die("no such path '%s' in HEAD", path); 117} 118 119static struct commit_list **append_parent(struct commit_list **tail,const struct object_id *oid) 120{ 121struct commit *parent; 122 123 parent =lookup_commit_reference(the_repository, oid); 124if(!parent) 125die("no such commit%s",oid_to_hex(oid)); 126return&commit_list_insert(parent, tail)->next; 127} 128 129static voidappend_merge_parents(struct commit_list **tail) 130{ 131int merge_head; 132struct strbuf line = STRBUF_INIT; 133 134 merge_head =open(git_path_merge_head(the_repository), O_RDONLY); 135if(merge_head <0) { 136if(errno == ENOENT) 137return; 138die("cannot open '%s' for reading", 139git_path_merge_head(the_repository)); 140} 141 142while(!strbuf_getwholeline_fd(&line, merge_head,'\n')) { 143struct object_id oid; 144if(line.len < GIT_SHA1_HEXSZ ||get_oid_hex(line.buf, &oid)) 145die("unknown line in '%s':%s", 146git_path_merge_head(the_repository), line.buf); 147 tail =append_parent(tail, &oid); 148} 149close(merge_head); 150strbuf_release(&line); 151} 152 153/* 154 * This isn't as simple as passing sb->buf and sb->len, because we 155 * want to transfer ownership of the buffer to the commit (so we 156 * must use detach). 157 */ 158static voidset_commit_buffer_from_strbuf(struct commit *c,struct strbuf *sb) 159{ 160size_t len; 161void*buf =strbuf_detach(sb, &len); 162set_commit_buffer(the_repository, c, buf, len); 163} 164 165/* 166 * Prepare a dummy commit that represents the work tree (or staged) item. 167 * Note that annotating work tree item never works in the reverse. 168 */ 169static struct commit *fake_working_tree_commit(struct repository *repo, 170struct diff_options *opt, 171const char*path, 172const char*contents_from) 173{ 174struct commit *commit; 175struct blame_origin *origin; 176struct commit_list **parent_tail, *parent; 177struct object_id head_oid; 178struct strbuf buf = STRBUF_INIT; 179const char*ident; 180time_t now; 181int len; 182struct cache_entry *ce; 183unsigned mode; 184struct strbuf msg = STRBUF_INIT; 185 186read_index(repo->index); 187time(&now); 188 commit =alloc_commit_node(the_repository); 189 commit->object.parsed =1; 190 commit->date = now; 191 parent_tail = &commit->parents; 192 193if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) 194die("no such ref: HEAD"); 195 196 parent_tail =append_parent(parent_tail, &head_oid); 197append_merge_parents(parent_tail); 198verify_working_tree_path(repo, commit, path); 199 200 origin =make_origin(commit, path); 201 202 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0); 203strbuf_addstr(&msg,"tree 0000000000000000000000000000000000000000\n"); 204for(parent = commit->parents; parent; parent = parent->next) 205strbuf_addf(&msg,"parent%s\n", 206oid_to_hex(&parent->item->object.oid)); 207strbuf_addf(&msg, 208"author%s\n" 209"committer%s\n\n" 210"Version of%sfrom%s\n", 211 ident, ident, path, 212(!contents_from ? path : 213(!strcmp(contents_from,"-") ?"standard input": contents_from))); 214set_commit_buffer_from_strbuf(commit, &msg); 215 216if(!contents_from ||strcmp("-", contents_from)) { 217struct stat st; 218const char*read_from; 219char*buf_ptr; 220unsigned long buf_len; 221 222if(contents_from) { 223if(stat(contents_from, &st) <0) 224die_errno("Cannot stat '%s'", contents_from); 225 read_from = contents_from; 226} 227else{ 228if(lstat(path, &st) <0) 229die_errno("Cannot lstat '%s'", path); 230 read_from = path; 231} 232 mode =canon_mode(st.st_mode); 233 234switch(st.st_mode & S_IFMT) { 235case S_IFREG: 236if(opt->flags.allow_textconv && 237textconv_object(read_from, mode, &null_oid,0, &buf_ptr, &buf_len)) 238strbuf_attach(&buf, buf_ptr, buf_len, buf_len +1); 239else if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) 240die_errno("cannot open or read '%s'", read_from); 241break; 242case S_IFLNK: 243if(strbuf_readlink(&buf, read_from, st.st_size) <0) 244die_errno("cannot readlink '%s'", read_from); 245break; 246default: 247die("unsupported file type%s", read_from); 248} 249} 250else{ 251/* Reading from stdin */ 252 mode =0; 253if(strbuf_read(&buf,0,0) <0) 254die_errno("failed to read from stdin"); 255} 256convert_to_git(repo->index, path, buf.buf, buf.len, &buf,0); 257 origin->file.ptr = buf.buf; 258 origin->file.size = buf.len; 259pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); 260 261/* 262 * Read the current index, replace the path entry with 263 * origin->blob_sha1 without mucking with its mode or type 264 * bits; we are not going to write this index out -- we just 265 * want to run "diff-index --cached". 266 */ 267discard_index(repo->index); 268read_index(repo->index); 269 270 len =strlen(path); 271if(!mode) { 272int pos =index_name_pos(repo->index, path, len); 273if(0<= pos) 274 mode = repo->index->cache[pos]->ce_mode; 275else 276/* Let's not bother reading from HEAD tree */ 277 mode = S_IFREG |0644; 278} 279 ce =make_empty_cache_entry(repo->index, len); 280oidcpy(&ce->oid, &origin->blob_oid); 281memcpy(ce->name, path, len); 282 ce->ce_flags =create_ce_flags(0); 283 ce->ce_namelen = len; 284 ce->ce_mode =create_ce_mode(mode); 285add_index_entry(repo->index, ce, 286 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 287 288cache_tree_invalidate_path(repo->index, path); 289 290return commit; 291} 292 293 294 295static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 296 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 297{ 298 xpparam_t xpp = {0}; 299 xdemitconf_t xecfg = {0}; 300 xdemitcb_t ecb = {NULL}; 301 302 xpp.flags = xdl_opts; 303 xecfg.hunk_func = hunk_func; 304 ecb.priv = cb_data; 305returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 306} 307 308/* 309 * Given an origin, prepare mmfile_t structure to be used by the 310 * diff machinery 311 */ 312static voidfill_origin_blob(struct diff_options *opt, 313struct blame_origin *o, mmfile_t *file,int*num_read_blob) 314{ 315if(!o->file.ptr) { 316enum object_type type; 317unsigned long file_size; 318 319(*num_read_blob)++; 320if(opt->flags.allow_textconv && 321textconv_object(o->path, o->mode, &o->blob_oid,1, &file->ptr, &file_size)) 322; 323else 324 file->ptr =read_object_file(&o->blob_oid, &type, 325&file_size); 326 file->size = file_size; 327 328if(!file->ptr) 329die("Cannot read blob%sfor path%s", 330oid_to_hex(&o->blob_oid), 331 o->path); 332 o->file = *file; 333} 334else 335*file = o->file; 336} 337 338static voiddrop_origin_blob(struct blame_origin *o) 339{ 340if(o->file.ptr) { 341FREE_AND_NULL(o->file.ptr); 342} 343} 344 345/* 346 * Any merge of blames happens on lists of blames that arrived via 347 * different parents in a single suspect. In this case, we want to 348 * sort according to the suspect line numbers as opposed to the final 349 * image line numbers. The function body is somewhat longish because 350 * it avoids unnecessary writes. 351 */ 352 353static struct blame_entry *blame_merge(struct blame_entry *list1, 354struct blame_entry *list2) 355{ 356struct blame_entry *p1 = list1, *p2 = list2, 357**tail = &list1; 358 359if(!p1) 360return p2; 361if(!p2) 362return p1; 363 364if(p1->s_lno <= p2->s_lno) { 365do{ 366 tail = &p1->next; 367if((p1 = *tail) == NULL) { 368*tail = p2; 369return list1; 370} 371}while(p1->s_lno <= p2->s_lno); 372} 373for(;;) { 374*tail = p2; 375do{ 376 tail = &p2->next; 377if((p2 = *tail) == NULL) { 378*tail = p1; 379return list1; 380} 381}while(p1->s_lno > p2->s_lno); 382*tail = p1; 383do{ 384 tail = &p1->next; 385if((p1 = *tail) == NULL) { 386*tail = p2; 387return list1; 388} 389}while(p1->s_lno <= p2->s_lno); 390} 391} 392 393static void*get_next_blame(const void*p) 394{ 395return((struct blame_entry *)p)->next; 396} 397 398static voidset_next_blame(void*p1,void*p2) 399{ 400((struct blame_entry *)p1)->next = p2; 401} 402 403/* 404 * Final image line numbers are all different, so we don't need a 405 * three-way comparison here. 406 */ 407 408static intcompare_blame_final(const void*p1,const void*p2) 409{ 410return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 411?1: -1; 412} 413 414static intcompare_blame_suspect(const void*p1,const void*p2) 415{ 416const struct blame_entry *s1 = p1, *s2 = p2; 417/* 418 * to allow for collating suspects, we sort according to the 419 * respective pointer value as the primary sorting criterion. 420 * The actual relation is pretty unimportant as long as it 421 * establishes a total order. Comparing as integers gives us 422 * that. 423 */ 424if(s1->suspect != s2->suspect) 425return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 426if(s1->s_lno == s2->s_lno) 427return0; 428return s1->s_lno > s2->s_lno ?1: -1; 429} 430 431voidblame_sort_final(struct blame_scoreboard *sb) 432{ 433 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 434 compare_blame_final); 435} 436 437static intcompare_commits_by_reverse_commit_date(const void*a, 438const void*b, 439void*c) 440{ 441return-compare_commits_by_commit_date(a, b, c); 442} 443 444/* 445 * For debugging -- origin is refcounted, and this asserts that 446 * we do not underflow. 447 */ 448static voidsanity_check_refcnt(struct blame_scoreboard *sb) 449{ 450int baa =0; 451struct blame_entry *ent; 452 453for(ent = sb->ent; ent; ent = ent->next) { 454/* Nobody should have zero or negative refcnt */ 455if(ent->suspect->refcnt <=0) { 456fprintf(stderr,"%sin%shas negative refcnt%d\n", 457 ent->suspect->path, 458oid_to_hex(&ent->suspect->commit->object.oid), 459 ent->suspect->refcnt); 460 baa =1; 461} 462} 463if(baa) 464 sb->on_sanity_fail(sb, baa); 465} 466 467/* 468 * If two blame entries that are next to each other came from 469 * contiguous lines in the same origin (i.e. <commit, path> pair), 470 * merge them together. 471 */ 472voidblame_coalesce(struct blame_scoreboard *sb) 473{ 474struct blame_entry *ent, *next; 475 476for(ent = sb->ent; ent && (next = ent->next); ent = next) { 477if(ent->suspect == next->suspect && 478 ent->s_lno + ent->num_lines == next->s_lno) { 479 ent->num_lines += next->num_lines; 480 ent->next = next->next; 481blame_origin_decref(next->suspect); 482free(next); 483 ent->score =0; 484 next = ent;/* again */ 485} 486} 487 488if(sb->debug)/* sanity */ 489sanity_check_refcnt(sb); 490} 491 492/* 493 * Merge the given sorted list of blames into a preexisting origin. 494 * If there were no previous blames to that commit, it is entered into 495 * the commit priority queue of the score board. 496 */ 497 498static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 499struct blame_entry *sorted) 500{ 501if(porigin->suspects) 502 porigin->suspects =blame_merge(porigin->suspects, sorted); 503else{ 504struct blame_origin *o; 505for(o =get_blame_suspects(porigin->commit); o; o = o->next) { 506if(o->suspects) { 507 porigin->suspects = sorted; 508return; 509} 510} 511 porigin->suspects = sorted; 512prio_queue_put(&sb->commits, porigin->commit); 513} 514} 515 516/* 517 * Fill the blob_sha1 field of an origin if it hasn't, so that later 518 * call to fill_origin_blob() can use it to locate the data. blob_sha1 519 * for an origin is also used to pass the blame for the entire file to 520 * the parent to detect the case where a child's blob is identical to 521 * that of its parent's. 522 * 523 * This also fills origin->mode for corresponding tree path. 524 */ 525static intfill_blob_sha1_and_mode(struct repository *repo, 526struct blame_origin *origin) 527{ 528if(!is_null_oid(&origin->blob_oid)) 529return0; 530if(get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 531goto error_out; 532if(oid_object_info(repo, &origin->blob_oid, NULL) != OBJ_BLOB) 533goto error_out; 534return0; 535 error_out: 536oidclr(&origin->blob_oid); 537 origin->mode = S_IFINVALID; 538return-1; 539} 540 541/* 542 * We have an origin -- check if the same path exists in the 543 * parent and return an origin structure to represent it. 544 */ 545static struct blame_origin *find_origin(struct commit *parent, 546struct blame_origin *origin) 547{ 548struct blame_origin *porigin; 549struct diff_options diff_opts; 550const char*paths[2]; 551 552/* First check any existing origins */ 553for(porigin =get_blame_suspects(parent); porigin; porigin = porigin->next) 554if(!strcmp(porigin->path, origin->path)) { 555/* 556 * The same path between origin and its parent 557 * without renaming -- the most common case. 558 */ 559returnblame_origin_incref(porigin); 560} 561 562/* See if the origin->path is different between parent 563 * and origin first. Most of the time they are the 564 * same and diff-tree is fairly efficient about this. 565 */ 566diff_setup(&diff_opts); 567 diff_opts.flags.recursive =1; 568 diff_opts.detect_rename =0; 569 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 570 paths[0] = origin->path; 571 paths[1] = NULL; 572 573parse_pathspec(&diff_opts.pathspec, 574 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 575 PATHSPEC_LITERAL_PATH,"", paths); 576diff_setup_done(&diff_opts); 577 578if(is_null_oid(&origin->commit->object.oid)) 579do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 580else 581diff_tree_oid(get_commit_tree_oid(parent), 582get_commit_tree_oid(origin->commit), 583"", &diff_opts); 584diffcore_std(&diff_opts); 585 586if(!diff_queued_diff.nr) { 587/* The path is the same as parent */ 588 porigin =get_origin(parent, origin->path); 589oidcpy(&porigin->blob_oid, &origin->blob_oid); 590 porigin->mode = origin->mode; 591}else{ 592/* 593 * Since origin->path is a pathspec, if the parent 594 * commit had it as a directory, we will see a whole 595 * bunch of deletion of files in the directory that we 596 * do not care about. 597 */ 598int i; 599struct diff_filepair *p = NULL; 600for(i =0; i < diff_queued_diff.nr; i++) { 601const char*name; 602 p = diff_queued_diff.queue[i]; 603 name = p->one->path ? p->one->path : p->two->path; 604if(!strcmp(name, origin->path)) 605break; 606} 607if(!p) 608die("internal error in blame::find_origin"); 609switch(p->status) { 610default: 611die("internal error in blame::find_origin (%c)", 612 p->status); 613case'M': 614 porigin =get_origin(parent, origin->path); 615oidcpy(&porigin->blob_oid, &p->one->oid); 616 porigin->mode = p->one->mode; 617break; 618case'A': 619case'T': 620/* Did not exist in parent, or type changed */ 621break; 622} 623} 624diff_flush(&diff_opts); 625clear_pathspec(&diff_opts.pathspec); 626return porigin; 627} 628 629/* 630 * We have an origin -- find the path that corresponds to it in its 631 * parent and return an origin structure to represent it. 632 */ 633static struct blame_origin *find_rename(struct commit *parent, 634struct blame_origin *origin) 635{ 636struct blame_origin *porigin = NULL; 637struct diff_options diff_opts; 638int i; 639 640diff_setup(&diff_opts); 641 diff_opts.flags.recursive =1; 642 diff_opts.detect_rename = DIFF_DETECT_RENAME; 643 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 644 diff_opts.single_follow = origin->path; 645diff_setup_done(&diff_opts); 646 647if(is_null_oid(&origin->commit->object.oid)) 648do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 649else 650diff_tree_oid(get_commit_tree_oid(parent), 651get_commit_tree_oid(origin->commit), 652"", &diff_opts); 653diffcore_std(&diff_opts); 654 655for(i =0; i < diff_queued_diff.nr; i++) { 656struct diff_filepair *p = diff_queued_diff.queue[i]; 657if((p->status =='R'|| p->status =='C') && 658!strcmp(p->two->path, origin->path)) { 659 porigin =get_origin(parent, p->one->path); 660oidcpy(&porigin->blob_oid, &p->one->oid); 661 porigin->mode = p->one->mode; 662break; 663} 664} 665diff_flush(&diff_opts); 666clear_pathspec(&diff_opts.pathspec); 667return porigin; 668} 669 670/* 671 * Append a new blame entry to a given output queue. 672 */ 673static voidadd_blame_entry(struct blame_entry ***queue, 674const struct blame_entry *src) 675{ 676struct blame_entry *e =xmalloc(sizeof(*e)); 677memcpy(e, src,sizeof(*e)); 678blame_origin_incref(e->suspect); 679 680 e->next = **queue; 681**queue = e; 682*queue = &e->next; 683} 684 685/* 686 * src typically is on-stack; we want to copy the information in it to 687 * a malloced blame_entry that gets added to the given queue. The 688 * origin of dst loses a refcnt. 689 */ 690static voiddup_entry(struct blame_entry ***queue, 691struct blame_entry *dst,struct blame_entry *src) 692{ 693blame_origin_incref(src->suspect); 694blame_origin_decref(dst->suspect); 695memcpy(dst, src,sizeof(*src)); 696 dst->next = **queue; 697**queue = dst; 698*queue = &dst->next; 699} 700 701const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 702{ 703return sb->final_buf + sb->lineno[lno]; 704} 705 706/* 707 * It is known that lines between tlno to same came from parent, and e 708 * has an overlap with that range. it also is known that parent's 709 * line plno corresponds to e's line tlno. 710 * 711 * <---- e -----> 712 * <------> 713 * <------------> 714 * <------------> 715 * <------------------> 716 * 717 * Split e into potentially three parts; before this chunk, the chunk 718 * to be blamed for the parent, and after that portion. 719 */ 720static voidsplit_overlap(struct blame_entry *split, 721struct blame_entry *e, 722int tlno,int plno,int same, 723struct blame_origin *parent) 724{ 725int chunk_end_lno; 726memset(split,0,sizeof(struct blame_entry [3])); 727 728if(e->s_lno < tlno) { 729/* there is a pre-chunk part not blamed on parent */ 730 split[0].suspect =blame_origin_incref(e->suspect); 731 split[0].lno = e->lno; 732 split[0].s_lno = e->s_lno; 733 split[0].num_lines = tlno - e->s_lno; 734 split[1].lno = e->lno + tlno - e->s_lno; 735 split[1].s_lno = plno; 736} 737else{ 738 split[1].lno = e->lno; 739 split[1].s_lno = plno + (e->s_lno - tlno); 740} 741 742if(same < e->s_lno + e->num_lines) { 743/* there is a post-chunk part not blamed on parent */ 744 split[2].suspect =blame_origin_incref(e->suspect); 745 split[2].lno = e->lno + (same - e->s_lno); 746 split[2].s_lno = e->s_lno + (same - e->s_lno); 747 split[2].num_lines = e->s_lno + e->num_lines - same; 748 chunk_end_lno = split[2].lno; 749} 750else 751 chunk_end_lno = e->lno + e->num_lines; 752 split[1].num_lines = chunk_end_lno - split[1].lno; 753 754/* 755 * if it turns out there is nothing to blame the parent for, 756 * forget about the splitting. !split[1].suspect signals this. 757 */ 758if(split[1].num_lines <1) 759return; 760 split[1].suspect =blame_origin_incref(parent); 761} 762 763/* 764 * split_overlap() divided an existing blame e into up to three parts 765 * in split. Any assigned blame is moved to queue to 766 * reflect the split. 767 */ 768static voidsplit_blame(struct blame_entry ***blamed, 769struct blame_entry ***unblamed, 770struct blame_entry *split, 771struct blame_entry *e) 772{ 773if(split[0].suspect && split[2].suspect) { 774/* The first part (reuse storage for the existing entry e) */ 775dup_entry(unblamed, e, &split[0]); 776 777/* The last part -- me */ 778add_blame_entry(unblamed, &split[2]); 779 780/* ... and the middle part -- parent */ 781add_blame_entry(blamed, &split[1]); 782} 783else if(!split[0].suspect && !split[2].suspect) 784/* 785 * The parent covers the entire area; reuse storage for 786 * e and replace it with the parent. 787 */ 788dup_entry(blamed, e, &split[1]); 789else if(split[0].suspect) { 790/* me and then parent */ 791dup_entry(unblamed, e, &split[0]); 792add_blame_entry(blamed, &split[1]); 793} 794else{ 795/* parent and then me */ 796dup_entry(blamed, e, &split[1]); 797add_blame_entry(unblamed, &split[2]); 798} 799} 800 801/* 802 * After splitting the blame, the origins used by the 803 * on-stack blame_entry should lose one refcnt each. 804 */ 805static voiddecref_split(struct blame_entry *split) 806{ 807int i; 808 809for(i =0; i <3; i++) 810blame_origin_decref(split[i].suspect); 811} 812 813/* 814 * reverse_blame reverses the list given in head, appending tail. 815 * That allows us to build lists in reverse order, then reverse them 816 * afterwards. This can be faster than building the list in proper 817 * order right away. The reason is that building in proper order 818 * requires writing a link in the _previous_ element, while building 819 * in reverse order just requires placing the list head into the 820 * _current_ element. 821 */ 822 823static struct blame_entry *reverse_blame(struct blame_entry *head, 824struct blame_entry *tail) 825{ 826while(head) { 827struct blame_entry *next = head->next; 828 head->next = tail; 829 tail = head; 830 head = next; 831} 832return tail; 833} 834 835/* 836 * Process one hunk from the patch between the current suspect for 837 * blame_entry e and its parent. This first blames any unfinished 838 * entries before the chunk (which is where target and parent start 839 * differing) on the parent, and then splits blame entries at the 840 * start and at the end of the difference region. Since use of -M and 841 * -C options may lead to overlapping/duplicate source line number 842 * ranges, all we can rely on from sorting/merging is the order of the 843 * first suspect line number. 844 */ 845static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 846int tlno,int offset,int same, 847struct blame_origin *parent) 848{ 849struct blame_entry *e = **srcq; 850struct blame_entry *samep = NULL, *diffp = NULL; 851 852while(e && e->s_lno < tlno) { 853struct blame_entry *next = e->next; 854/* 855 * current record starts before differing portion. If 856 * it reaches into it, we need to split it up and 857 * examine the second part separately. 858 */ 859if(e->s_lno + e->num_lines > tlno) { 860/* Move second half to a new record */ 861int len = tlno - e->s_lno; 862struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 863 n->suspect = e->suspect; 864 n->lno = e->lno + len; 865 n->s_lno = e->s_lno + len; 866 n->num_lines = e->num_lines - len; 867 e->num_lines = len; 868 e->score =0; 869/* Push new record to diffp */ 870 n->next = diffp; 871 diffp = n; 872}else 873blame_origin_decref(e->suspect); 874/* Pass blame for everything before the differing 875 * chunk to the parent */ 876 e->suspect =blame_origin_incref(parent); 877 e->s_lno += offset; 878 e->next = samep; 879 samep = e; 880 e = next; 881} 882/* 883 * As we don't know how much of a common stretch after this 884 * diff will occur, the currently blamed parts are all that we 885 * can assign to the parent for now. 886 */ 887 888if(samep) { 889**dstq =reverse_blame(samep, **dstq); 890*dstq = &samep->next; 891} 892/* 893 * Prepend the split off portions: everything after e starts 894 * after the blameable portion. 895 */ 896 e =reverse_blame(diffp, e); 897 898/* 899 * Now retain records on the target while parts are different 900 * from the parent. 901 */ 902 samep = NULL; 903 diffp = NULL; 904while(e && e->s_lno < same) { 905struct blame_entry *next = e->next; 906 907/* 908 * If current record extends into sameness, need to split. 909 */ 910if(e->s_lno + e->num_lines > same) { 911/* 912 * Move second half to a new record to be 913 * processed by later chunks 914 */ 915int len = same - e->s_lno; 916struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 917 n->suspect =blame_origin_incref(e->suspect); 918 n->lno = e->lno + len; 919 n->s_lno = e->s_lno + len; 920 n->num_lines = e->num_lines - len; 921 e->num_lines = len; 922 e->score =0; 923/* Push new record to samep */ 924 n->next = samep; 925 samep = n; 926} 927 e->next = diffp; 928 diffp = e; 929 e = next; 930} 931**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 932/* Move across elements that are in the unblamable portion */ 933if(diffp) 934*srcq = &diffp->next; 935} 936 937struct blame_chunk_cb_data { 938struct blame_origin *parent; 939long offset; 940struct blame_entry **dstq; 941struct blame_entry **srcq; 942}; 943 944/* diff chunks are from parent to target */ 945static intblame_chunk_cb(long start_a,long count_a, 946long start_b,long count_b,void*data) 947{ 948struct blame_chunk_cb_data *d = data; 949if(start_a - start_b != d->offset) 950die("internal error in blame::blame_chunk_cb"); 951blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 952 start_b + count_b, d->parent); 953 d->offset = start_a + count_a - (start_b + count_b); 954return0; 955} 956 957/* 958 * We are looking at the origin 'target' and aiming to pass blame 959 * for the lines it is suspected to its parent. Run diff to find 960 * which lines came from parent and pass blame for them. 961 */ 962static voidpass_blame_to_parent(struct blame_scoreboard *sb, 963struct blame_origin *target, 964struct blame_origin *parent) 965{ 966 mmfile_t file_p, file_o; 967struct blame_chunk_cb_data d; 968struct blame_entry *newdest = NULL; 969 970if(!target->suspects) 971return;/* nothing remains for this target */ 972 973 d.parent = parent; 974 d.offset =0; 975 d.dstq = &newdest; d.srcq = &target->suspects; 976 977fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 978fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 979 sb->num_get_patch++; 980 981if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 982die("unable to generate diff (%s->%s)", 983oid_to_hex(&parent->commit->object.oid), 984oid_to_hex(&target->commit->object.oid)); 985/* The rest are the same as the parent */ 986blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 987*d.dstq = NULL; 988queue_blames(sb, parent, newdest); 989 990return; 991} 992 993/* 994 * The lines in blame_entry after splitting blames many times can become 995 * very small and trivial, and at some point it becomes pointless to 996 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 997 * ordinary C program, and it is not worth to say it was copied from 998 * totally unrelated file in the parent. 999 *1000 * Compute how trivial the lines in the blame_entry are.1001 */1002unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e)1003{1004unsigned score;1005const char*cp, *ep;10061007if(e->score)1008return e->score;10091010 score =1;1011 cp =blame_nth_line(sb, e->lno);1012 ep =blame_nth_line(sb, e->lno + e->num_lines);1013while(cp < ep) {1014unsigned ch = *((unsigned char*)cp);1015if(isalnum(ch))1016 score++;1017 cp++;1018}1019 e->score = score;1020return score;1021}10221023/*1024 * best_so_far[] and potential[] are both a split of an existing blame_entry1025 * that passes blame to the parent. Maintain best_so_far the best split so1026 * far, by comparing potential and best_so_far and copying potential into1027 * bst_so_far as needed.1028 */1029static voidcopy_split_if_better(struct blame_scoreboard *sb,1030struct blame_entry *best_so_far,1031struct blame_entry *potential)1032{1033int i;10341035if(!potential[1].suspect)1036return;1037if(best_so_far[1].suspect) {1038if(blame_entry_score(sb, &potential[1]) <1039blame_entry_score(sb, &best_so_far[1]))1040return;1041}10421043for(i =0; i <3; i++)1044blame_origin_incref(potential[i].suspect);1045decref_split(best_so_far);1046memcpy(best_so_far, potential,sizeof(struct blame_entry[3]));1047}10481049/*1050 * We are looking at a part of the final image represented by1051 * ent (tlno and same are offset by ent->s_lno).1052 * tlno is where we are looking at in the final image.1053 * up to (but not including) same match preimage.1054 * plno is where we are looking at in the preimage.1055 *1056 * <-------------- final image ---------------------->1057 * <------ent------>1058 * ^tlno ^same1059 * <---------preimage----->1060 * ^plno1061 *1062 * All line numbers are 0-based.1063 */1064static voidhandle_split(struct blame_scoreboard *sb,1065struct blame_entry *ent,1066int tlno,int plno,int same,1067struct blame_origin *parent,1068struct blame_entry *split)1069{1070if(ent->num_lines <= tlno)1071return;1072if(tlno < same) {1073struct blame_entry potential[3];1074 tlno += ent->s_lno;1075 same += ent->s_lno;1076split_overlap(potential, ent, tlno, plno, same, parent);1077copy_split_if_better(sb, split, potential);1078decref_split(potential);1079}1080}10811082struct handle_split_cb_data {1083struct blame_scoreboard *sb;1084struct blame_entry *ent;1085struct blame_origin *parent;1086struct blame_entry *split;1087long plno;1088long tlno;1089};10901091static inthandle_split_cb(long start_a,long count_a,1092long start_b,long count_b,void*data)1093{1094struct handle_split_cb_data *d = data;1095handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1096 d->split);1097 d->plno = start_a + count_a;1098 d->tlno = start_b + count_b;1099return0;1100}11011102/*1103 * Find the lines from parent that are the same as ent so that1104 * we can pass blames to it. file_p has the blob contents for1105 * the parent.1106 */1107static voidfind_copy_in_blob(struct blame_scoreboard *sb,1108struct blame_entry *ent,1109struct blame_origin *parent,1110struct blame_entry *split,1111 mmfile_t *file_p)1112{1113const char*cp;1114 mmfile_t file_o;1115struct handle_split_cb_data d;11161117memset(&d,0,sizeof(d));1118 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1119/*1120 * Prepare mmfile that contains only the lines in ent.1121 */1122 cp =blame_nth_line(sb, ent->lno);1123 file_o.ptr = (char*) cp;1124 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11251126/*1127 * file_o is a part of final image we are annotating.1128 * file_p partially may match that image.1129 */1130memset(split,0,sizeof(struct blame_entry [3]));1131if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1132die("unable to generate diff (%s)",1133oid_to_hex(&parent->commit->object.oid));1134/* remainder, if any, all match the preimage */1135handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1136}11371138/* Move all blame entries from list *source that have a score smaller1139 * than score_min to the front of list *small.1140 * Returns a pointer to the link pointing to the old head of the small list.1141 */11421143static struct blame_entry **filter_small(struct blame_scoreboard *sb,1144struct blame_entry **small,1145struct blame_entry **source,1146unsigned score_min)1147{1148struct blame_entry *p = *source;1149struct blame_entry *oldsmall = *small;1150while(p) {1151if(blame_entry_score(sb, p) <= score_min) {1152*small = p;1153 small = &p->next;1154 p = *small;1155}else{1156*source = p;1157 source = &p->next;1158 p = *source;1159}1160}1161*small = oldsmall;1162*source = NULL;1163return small;1164}11651166/*1167 * See if lines currently target is suspected for can be attributed to1168 * parent.1169 */1170static voidfind_move_in_parent(struct blame_scoreboard *sb,1171struct blame_entry ***blamed,1172struct blame_entry **toosmall,1173struct blame_origin *target,1174struct blame_origin *parent)1175{1176struct blame_entry *e, split[3];1177struct blame_entry *unblamed = target->suspects;1178struct blame_entry *leftover = NULL;1179 mmfile_t file_p;11801181if(!unblamed)1182return;/* nothing remains for this target */11831184fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1185if(!file_p.ptr)1186return;11871188/* At each iteration, unblamed has a NULL-terminated list of1189 * entries that have not yet been tested for blame. leftover1190 * contains the reversed list of entries that have been tested1191 * without being assignable to the parent.1192 */1193do{1194struct blame_entry **unblamedtail = &unblamed;1195struct blame_entry *next;1196for(e = unblamed; e; e = next) {1197 next = e->next;1198find_copy_in_blob(sb, e, parent, split, &file_p);1199if(split[1].suspect &&1200 sb->move_score <blame_entry_score(sb, &split[1])) {1201split_blame(blamed, &unblamedtail, split, e);1202}else{1203 e->next = leftover;1204 leftover = e;1205}1206decref_split(split);1207}1208*unblamedtail = NULL;1209 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1210}while(unblamed);1211 target->suspects =reverse_blame(leftover, NULL);1212}12131214struct blame_list {1215struct blame_entry *ent;1216struct blame_entry split[3];1217};12181219/*1220 * Count the number of entries the target is suspected for,1221 * and prepare a list of entry and the best split.1222 */1223static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1224int*num_ents_p)1225{1226struct blame_entry *e;1227int num_ents, i;1228struct blame_list *blame_list = NULL;12291230for(e = unblamed, num_ents =0; e; e = e->next)1231 num_ents++;1232if(num_ents) {1233 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1234for(e = unblamed, i =0; e; e = e->next)1235 blame_list[i++].ent = e;1236}1237*num_ents_p = num_ents;1238return blame_list;1239}12401241/*1242 * For lines target is suspected for, see if we can find code movement1243 * across file boundary from the parent commit. porigin is the path1244 * in the parent we already tried.1245 */1246static voidfind_copy_in_parent(struct blame_scoreboard *sb,1247struct blame_entry ***blamed,1248struct blame_entry **toosmall,1249struct blame_origin *target,1250struct commit *parent,1251struct blame_origin *porigin,1252int opt)1253{1254struct diff_options diff_opts;1255int i, j;1256struct blame_list *blame_list;1257int num_ents;1258struct blame_entry *unblamed = target->suspects;1259struct blame_entry *leftover = NULL;12601261if(!unblamed)1262return;/* nothing remains for this target */12631264diff_setup(&diff_opts);1265 diff_opts.flags.recursive =1;1266 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12671268diff_setup_done(&diff_opts);12691270/* Try "find copies harder" on new path if requested;1271 * we do not want to use diffcore_rename() actually to1272 * match things up; find_copies_harder is set only to1273 * force diff_tree_oid() to feed all filepairs to diff_queue,1274 * and this code needs to be after diff_setup_done(), which1275 * usually makes find-copies-harder imply copy detection.1276 */1277if((opt & PICKAXE_BLAME_COPY_HARDEST)1278|| ((opt & PICKAXE_BLAME_COPY_HARDER)1279&& (!porigin ||strcmp(target->path, porigin->path))))1280 diff_opts.flags.find_copies_harder =1;12811282if(is_null_oid(&target->commit->object.oid))1283do_diff_cache(get_commit_tree_oid(parent), &diff_opts);1284else1285diff_tree_oid(get_commit_tree_oid(parent),1286get_commit_tree_oid(target->commit),1287"", &diff_opts);12881289if(!diff_opts.flags.find_copies_harder)1290diffcore_std(&diff_opts);12911292do{1293struct blame_entry **unblamedtail = &unblamed;1294 blame_list =setup_blame_list(unblamed, &num_ents);12951296for(i =0; i < diff_queued_diff.nr; i++) {1297struct diff_filepair *p = diff_queued_diff.queue[i];1298struct blame_origin *norigin;1299 mmfile_t file_p;1300struct blame_entry potential[3];13011302if(!DIFF_FILE_VALID(p->one))1303continue;/* does not exist in parent */1304if(S_ISGITLINK(p->one->mode))1305continue;/* ignore git links */1306if(porigin && !strcmp(p->one->path, porigin->path))1307/* find_move already dealt with this path */1308continue;13091310 norigin =get_origin(parent, p->one->path);1311oidcpy(&norigin->blob_oid, &p->one->oid);1312 norigin->mode = p->one->mode;1313fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1314if(!file_p.ptr)1315continue;13161317for(j =0; j < num_ents; j++) {1318find_copy_in_blob(sb, blame_list[j].ent,1319 norigin, potential, &file_p);1320copy_split_if_better(sb, blame_list[j].split,1321 potential);1322decref_split(potential);1323}1324blame_origin_decref(norigin);1325}13261327for(j =0; j < num_ents; j++) {1328struct blame_entry *split = blame_list[j].split;1329if(split[1].suspect &&1330 sb->copy_score <blame_entry_score(sb, &split[1])) {1331split_blame(blamed, &unblamedtail, split,1332 blame_list[j].ent);1333}else{1334 blame_list[j].ent->next = leftover;1335 leftover = blame_list[j].ent;1336}1337decref_split(split);1338}1339free(blame_list);1340*unblamedtail = NULL;1341 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1342}while(unblamed);1343 target->suspects =reverse_blame(leftover, NULL);1344diff_flush(&diff_opts);1345clear_pathspec(&diff_opts.pathspec);1346}13471348/*1349 * The blobs of origin and porigin exactly match, so everything1350 * origin is suspected for can be blamed on the parent.1351 */1352static voidpass_whole_blame(struct blame_scoreboard *sb,1353struct blame_origin *origin,struct blame_origin *porigin)1354{1355struct blame_entry *e, *suspects;13561357if(!porigin->file.ptr && origin->file.ptr) {1358/* Steal its file */1359 porigin->file = origin->file;1360 origin->file.ptr = NULL;1361}1362 suspects = origin->suspects;1363 origin->suspects = NULL;1364for(e = suspects; e; e = e->next) {1365blame_origin_incref(porigin);1366blame_origin_decref(e->suspect);1367 e->suspect = porigin;1368}1369queue_blames(sb, porigin, suspects);1370}13711372/*1373 * We pass blame from the current commit to its parents. We keep saying1374 * "parent" (and "porigin"), but what we mean is to find scapegoat to1375 * exonerate ourselves.1376 */1377static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1378int reverse)1379{1380if(!reverse) {1381if(revs->first_parent_only &&1382 commit->parents &&1383 commit->parents->next) {1384free_commit_list(commit->parents->next);1385 commit->parents->next = NULL;1386}1387return commit->parents;1388}1389returnlookup_decoration(&revs->children, &commit->object);1390}13911392static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1393{1394struct commit_list *l =first_scapegoat(revs, commit, reverse);1395returncommit_list_count(l);1396}13971398/* Distribute collected unsorted blames to the respected sorted lists1399 * in the various origins.1400 */1401static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1402{1403 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1404 compare_blame_suspect);1405while(blamed)1406{1407struct blame_origin *porigin = blamed->suspect;1408struct blame_entry *suspects = NULL;1409do{1410struct blame_entry *next = blamed->next;1411 blamed->next = suspects;1412 suspects = blamed;1413 blamed = next;1414}while(blamed && blamed->suspect == porigin);1415 suspects =reverse_blame(suspects, NULL);1416queue_blames(sb, porigin, suspects);1417}1418}14191420#define MAXSG 1614211422static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1423{1424struct rev_info *revs = sb->revs;1425int i, pass, num_sg;1426struct commit *commit = origin->commit;1427struct commit_list *sg;1428struct blame_origin *sg_buf[MAXSG];1429struct blame_origin *porigin, **sg_origin = sg_buf;1430struct blame_entry *toosmall = NULL;1431struct blame_entry *blames, **blametail = &blames;14321433 num_sg =num_scapegoats(revs, commit, sb->reverse);1434if(!num_sg)1435goto finish;1436else if(num_sg <ARRAY_SIZE(sg_buf))1437memset(sg_buf,0,sizeof(sg_buf));1438else1439 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14401441/*1442 * The first pass looks for unrenamed path to optimize for1443 * common cases, then we look for renames in the second pass.1444 */1445for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1446struct blame_origin *(*find)(struct commit *,struct blame_origin *);1447 find = pass ? find_rename : find_origin;14481449for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1450 i < num_sg && sg;1451 sg = sg->next, i++) {1452struct commit *p = sg->item;1453int j, same;14541455if(sg_origin[i])1456continue;1457if(parse_commit(p))1458continue;1459 porigin =find(p, origin);1460if(!porigin)1461continue;1462if(!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1463pass_whole_blame(sb, origin, porigin);1464blame_origin_decref(porigin);1465goto finish;1466}1467for(j = same =0; j < i; j++)1468if(sg_origin[j] &&1469!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1470 same =1;1471break;1472}1473if(!same)1474 sg_origin[i] = porigin;1475else1476blame_origin_decref(porigin);1477}1478}14791480 sb->num_commits++;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;1487if(!origin->previous) {1488blame_origin_incref(porigin);1489 origin->previous = porigin;1490}1491pass_blame_to_parent(sb, origin, porigin);1492if(!origin->suspects)1493goto finish;1494}14951496/*1497 * Optionally find moves in parents' files.1498 */1499if(opt & PICKAXE_BLAME_MOVE) {1500filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1501if(origin->suspects) {1502for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1503 i < num_sg && sg;1504 sg = sg->next, i++) {1505struct blame_origin *porigin = sg_origin[i];1506if(!porigin)1507continue;1508find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1509if(!origin->suspects)1510break;1511}1512}1513}15141515/*1516 * Optionally find copies from parents' files.1517 */1518if(opt & PICKAXE_BLAME_COPY) {1519if(sb->copy_score > sb->move_score)1520filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1521else if(sb->copy_score < sb->move_score) {1522 origin->suspects =blame_merge(origin->suspects, toosmall);1523 toosmall = NULL;1524filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1525}1526if(!origin->suspects)1527goto finish;15281529for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1530 i < num_sg && sg;1531 sg = sg->next, i++) {1532struct blame_origin *porigin = sg_origin[i];1533find_copy_in_parent(sb, &blametail, &toosmall,1534 origin, sg->item, porigin, opt);1535if(!origin->suspects)1536goto finish;1537}1538}15391540finish:1541*blametail = NULL;1542distribute_blame(sb, blames);1543/*1544 * prepend toosmall to origin->suspects1545 *1546 * There is no point in sorting: this ends up on a big1547 * unsorted list in the caller anyway.1548 */1549if(toosmall) {1550struct blame_entry **tail = &toosmall;1551while(*tail)1552 tail = &(*tail)->next;1553*tail = origin->suspects;1554 origin->suspects = toosmall;1555}1556for(i =0; i < num_sg; i++) {1557if(sg_origin[i]) {1558drop_origin_blob(sg_origin[i]);1559blame_origin_decref(sg_origin[i]);1560}1561}1562drop_origin_blob(origin);1563if(sg_buf != sg_origin)1564free(sg_origin);1565}15661567/*1568 * The main loop -- while we have blobs with lines whose true origin1569 * is still unknown, pick one blob, and allow its lines to pass blames1570 * to its parents. */1571voidassign_blame(struct blame_scoreboard *sb,int opt)1572{1573struct rev_info *revs = sb->revs;1574struct commit *commit =prio_queue_get(&sb->commits);15751576while(commit) {1577struct blame_entry *ent;1578struct blame_origin *suspect =get_blame_suspects(commit);15791580/* find one suspect to break down */1581while(suspect && !suspect->suspects)1582 suspect = suspect->next;15831584if(!suspect) {1585 commit =prio_queue_get(&sb->commits);1586continue;1587}15881589assert(commit == suspect->commit);15901591/*1592 * We will use this suspect later in the loop,1593 * so hold onto it in the meantime.1594 */1595blame_origin_incref(suspect);1596parse_commit(commit);1597if(sb->reverse ||1598(!(commit->object.flags & UNINTERESTING) &&1599!(revs->max_age != -1&& commit->date < revs->max_age)))1600pass_blame(sb, suspect, opt);1601else{1602 commit->object.flags |= UNINTERESTING;1603if(commit->object.parsed)1604mark_parents_uninteresting(commit);1605}1606/* treat root commit as boundary */1607if(!commit->parents && !sb->show_root)1608 commit->object.flags |= UNINTERESTING;16091610/* Take responsibility for the remaining entries */1611 ent = suspect->suspects;1612if(ent) {1613 suspect->guilty =1;1614for(;;) {1615struct blame_entry *next = ent->next;1616if(sb->found_guilty_entry)1617 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1618if(next) {1619 ent = next;1620continue;1621}1622 ent->next = sb->ent;1623 sb->ent = suspect->suspects;1624 suspect->suspects = NULL;1625break;1626}1627}1628blame_origin_decref(suspect);16291630if(sb->debug)/* sanity */1631sanity_check_refcnt(sb);1632}1633}16341635static const char*get_next_line(const char*start,const char*end)1636{1637const char*nl =memchr(start,'\n', end - start);1638return nl ? nl +1: end;1639}16401641/*1642 * To allow quick access to the contents of nth line in the1643 * final image, prepare an index in the scoreboard.1644 */1645static intprepare_lines(struct blame_scoreboard *sb)1646{1647const char*buf = sb->final_buf;1648unsigned long len = sb->final_buf_size;1649const char*end = buf + len;1650const char*p;1651int*lineno;1652int num =0;16531654for(p = buf; p < end; p =get_next_line(p, end))1655 num++;16561657ALLOC_ARRAY(sb->lineno, num +1);1658 lineno = sb->lineno;16591660for(p = buf; p < end; p =get_next_line(p, end))1661*lineno++ = p - buf;16621663*lineno = len;16641665 sb->num_lines = num;1666return sb->num_lines;1667}16681669static struct commit *find_single_final(struct rev_info *revs,1670const char**name_p)1671{1672int i;1673struct commit *found = NULL;1674const char*name = NULL;16751676for(i =0; i < revs->pending.nr; i++) {1677struct object *obj = revs->pending.objects[i].item;1678if(obj->flags & UNINTERESTING)1679continue;1680 obj =deref_tag(the_repository, obj, NULL,0);1681if(obj->type != OBJ_COMMIT)1682die("Non commit%s?", revs->pending.objects[i].name);1683if(found)1684die("More than one commit to dig from%sand%s?",1685 revs->pending.objects[i].name, name);1686 found = (struct commit *)obj;1687 name = revs->pending.objects[i].name;1688}1689if(name_p)1690*name_p =xstrdup_or_null(name);1691return found;1692}16931694static struct commit *dwim_reverse_initial(struct rev_info *revs,1695const char**name_p)1696{1697/*1698 * DWIM "git blame --reverse ONE -- PATH" as1699 * "git blame --reverse ONE..HEAD -- PATH" but only do so1700 * when it makes sense.1701 */1702struct object *obj;1703struct commit *head_commit;1704struct object_id head_oid;17051706if(revs->pending.nr !=1)1707return NULL;17081709/* Is that sole rev a committish? */1710 obj = revs->pending.objects[0].item;1711 obj =deref_tag(the_repository, obj, NULL,0);1712if(obj->type != OBJ_COMMIT)1713return NULL;17141715/* Do we have HEAD? */1716if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1717return NULL;1718 head_commit =lookup_commit_reference_gently(the_repository,1719&head_oid,1);1720if(!head_commit)1721return NULL;17221723/* Turn "ONE" into "ONE..HEAD" then */1724 obj->flags |= UNINTERESTING;1725add_pending_object(revs, &head_commit->object,"HEAD");17261727if(name_p)1728*name_p = revs->pending.objects[0].name;1729return(struct commit *)obj;1730}17311732static struct commit *find_single_initial(struct rev_info *revs,1733const char**name_p)1734{1735int i;1736struct commit *found = NULL;1737const char*name = NULL;17381739/*1740 * There must be one and only one negative commit, and it must be1741 * the boundary.1742 */1743for(i =0; i < revs->pending.nr; i++) {1744struct object *obj = revs->pending.objects[i].item;1745if(!(obj->flags & UNINTERESTING))1746continue;1747 obj =deref_tag(the_repository, obj, NULL,0);1748if(obj->type != OBJ_COMMIT)1749die("Non commit%s?", revs->pending.objects[i].name);1750if(found)1751die("More than one commit to dig up from,%sand%s?",1752 revs->pending.objects[i].name, name);1753 found = (struct commit *) obj;1754 name = revs->pending.objects[i].name;1755}17561757if(!name)1758 found =dwim_reverse_initial(revs, &name);1759if(!name)1760die("No commit to dig up from?");17611762if(name_p)1763*name_p =xstrdup(name);1764return found;1765}17661767voidinit_scoreboard(struct blame_scoreboard *sb)1768{1769memset(sb,0,sizeof(struct blame_scoreboard));1770 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;1771 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;1772}17731774voidsetup_scoreboard(struct blame_scoreboard *sb,1775const char*path,1776struct blame_origin **orig)1777{1778const char*final_commit_name = NULL;1779struct blame_origin *o;1780struct commit *final_commit = NULL;1781enum object_type type;17821783init_blame_suspects(&blame_suspects);17841785if(sb->reverse && sb->contents_from)1786die(_("--contents and --reverse do not blend well."));17871788if(!sb->repo)1789BUG("repo is NULL");17901791if(!sb->reverse) {1792 sb->final =find_single_final(sb->revs, &final_commit_name);1793 sb->commits.compare = compare_commits_by_commit_date;1794}else{1795 sb->final =find_single_initial(sb->revs, &final_commit_name);1796 sb->commits.compare = compare_commits_by_reverse_commit_date;1797}17981799if(sb->final && sb->contents_from)1800die(_("cannot use --contents with final commit object name"));18011802if(sb->reverse && sb->revs->first_parent_only)1803 sb->revs->children.name = NULL;18041805if(!sb->final) {1806/*1807 * "--not A B -- path" without anything positive;1808 * do not default to HEAD, but use the working tree1809 * or "--contents".1810 */1811setup_work_tree();1812 sb->final =fake_working_tree_commit(sb->repo,1813&sb->revs->diffopt,1814 path, sb->contents_from);1815add_pending_object(sb->revs, &(sb->final->object),":");1816}18171818if(sb->reverse && sb->revs->first_parent_only) {1819 final_commit =find_single_final(sb->revs, NULL);1820if(!final_commit)1821die(_("--reverse and --first-parent together require specified latest commit"));1822}18231824/*1825 * If we have bottom, this will mark the ancestors of the1826 * bottom commits we would reach while traversing as1827 * uninteresting.1828 */1829if(prepare_revision_walk(sb->revs))1830die(_("revision walk setup failed"));18311832if(sb->reverse && sb->revs->first_parent_only) {1833struct commit *c = final_commit;18341835 sb->revs->children.name ="children";1836while(c->parents &&1837oidcmp(&c->object.oid, &sb->final->object.oid)) {1838struct commit_list *l =xcalloc(1,sizeof(*l));18391840 l->item = c;1841if(add_decoration(&sb->revs->children,1842&c->parents->item->object, l))1843BUG("not unique item in first-parent chain");1844 c = c->parents->item;1845}18461847if(oidcmp(&c->object.oid, &sb->final->object.oid))1848die(_("--reverse --first-parent together require range along first-parent chain"));1849}18501851if(is_null_oid(&sb->final->object.oid)) {1852 o =get_blame_suspects(sb->final);1853 sb->final_buf =xmemdupz(o->file.ptr, o->file.size);1854 sb->final_buf_size = o->file.size;1855}1856else{1857 o =get_origin(sb->final, path);1858if(fill_blob_sha1_and_mode(sb->repo, o))1859die(_("no such path%sin%s"), path, final_commit_name);18601861if(sb->revs->diffopt.flags.allow_textconv &&1862textconv_object(path, o->mode, &o->blob_oid,1, (char**) &sb->final_buf,1863&sb->final_buf_size))1864;1865else1866 sb->final_buf =read_object_file(&o->blob_oid, &type,1867&sb->final_buf_size);18681869if(!sb->final_buf)1870die(_("cannot read blob%sfor path%s"),1871oid_to_hex(&o->blob_oid),1872 path);1873}1874 sb->num_read_blob++;1875prepare_lines(sb);18761877if(orig)1878*orig = o;18791880free((char*)final_commit_name);1881}1882188318841885struct blame_entry *blame_entry_prepend(struct blame_entry *head,1886long start,long end,1887struct blame_origin *o)1888{1889struct blame_entry *new_head =xcalloc(1,sizeof(struct blame_entry));1890 new_head->lno = start;1891 new_head->num_lines = end - start;1892 new_head->suspect = o;1893 new_head->s_lno = start;1894 new_head->next = head;1895blame_origin_incref(o);1896return new_head;1897}