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