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 commit *work_tree,const char*path) 94{ 95struct commit_list *parents; 96int pos; 97 98for(parents = work_tree->parents; parents; parents = parents->next) { 99const struct object_id *commit_oid = &parents->item->object.oid; 100struct object_id blob_oid; 101unsigned mode; 102 103if(!get_tree_entry(commit_oid, path, &blob_oid, &mode) && 104oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB) 105return; 106} 107 108 pos =cache_name_pos(path,strlen(path)); 109if(pos >=0) 110;/* path is in the index */ 111else if(-1- pos < active_nr && 112!strcmp(active_cache[-1- pos]->name, path)) 113;/* path is in the index, unmerged */ 114else 115die("no such path '%s' in HEAD", path); 116} 117 118static struct commit_list **append_parent(struct commit_list **tail,const struct object_id *oid) 119{ 120struct commit *parent; 121 122 parent =lookup_commit_reference(oid); 123if(!parent) 124die("no such commit%s",oid_to_hex(oid)); 125return&commit_list_insert(parent, tail)->next; 126} 127 128static voidappend_merge_parents(struct commit_list **tail) 129{ 130int merge_head; 131struct strbuf line = STRBUF_INIT; 132 133 merge_head =open(git_path_merge_head(the_repository), O_RDONLY); 134if(merge_head <0) { 135if(errno == ENOENT) 136return; 137die("cannot open '%s' for reading", 138git_path_merge_head(the_repository)); 139} 140 141while(!strbuf_getwholeline_fd(&line, merge_head,'\n')) { 142struct object_id oid; 143if(line.len < GIT_SHA1_HEXSZ ||get_oid_hex(line.buf, &oid)) 144die("unknown line in '%s':%s", 145git_path_merge_head(the_repository), line.buf); 146 tail =append_parent(tail, &oid); 147} 148close(merge_head); 149strbuf_release(&line); 150} 151 152/* 153 * This isn't as simple as passing sb->buf and sb->len, because we 154 * want to transfer ownership of the buffer to the commit (so we 155 * must use detach). 156 */ 157static voidset_commit_buffer_from_strbuf(struct commit *c,struct strbuf *sb) 158{ 159size_t len; 160void*buf =strbuf_detach(sb, &len); 161set_commit_buffer(c, buf, len); 162} 163 164/* 165 * Prepare a dummy commit that represents the work tree (or staged) item. 166 * Note that annotating work tree item never works in the reverse. 167 */ 168static struct commit *fake_working_tree_commit(struct diff_options *opt, 169const char*path, 170const char*contents_from) 171{ 172struct commit *commit; 173struct blame_origin *origin; 174struct commit_list **parent_tail, *parent; 175struct object_id head_oid; 176struct strbuf buf = STRBUF_INIT; 177const char*ident; 178time_t now; 179int size, len; 180struct cache_entry *ce; 181unsigned mode; 182struct strbuf msg = STRBUF_INIT; 183 184read_cache(); 185time(&now); 186 commit =alloc_commit_node(the_repository); 187 commit->object.parsed =1; 188 commit->date = now; 189 parent_tail = &commit->parents; 190 191if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) 192die("no such ref: HEAD"); 193 194 parent_tail =append_parent(parent_tail, &head_oid); 195append_merge_parents(parent_tail); 196verify_working_tree_path(commit, path); 197 198 origin =make_origin(commit, path); 199 200 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0); 201strbuf_addstr(&msg,"tree 0000000000000000000000000000000000000000\n"); 202for(parent = commit->parents; parent; parent = parent->next) 203strbuf_addf(&msg,"parent%s\n", 204oid_to_hex(&parent->item->object.oid)); 205strbuf_addf(&msg, 206"author%s\n" 207"committer%s\n\n" 208"Version of%sfrom%s\n", 209 ident, ident, path, 210(!contents_from ? path : 211(!strcmp(contents_from,"-") ?"standard input": contents_from))); 212set_commit_buffer_from_strbuf(commit, &msg); 213 214if(!contents_from ||strcmp("-", contents_from)) { 215struct stat st; 216const char*read_from; 217char*buf_ptr; 218unsigned long buf_len; 219 220if(contents_from) { 221if(stat(contents_from, &st) <0) 222die_errno("Cannot stat '%s'", contents_from); 223 read_from = contents_from; 224} 225else{ 226if(lstat(path, &st) <0) 227die_errno("Cannot lstat '%s'", path); 228 read_from = path; 229} 230 mode =canon_mode(st.st_mode); 231 232switch(st.st_mode & S_IFMT) { 233case S_IFREG: 234if(opt->flags.allow_textconv && 235textconv_object(read_from, mode, &null_oid,0, &buf_ptr, &buf_len)) 236strbuf_attach(&buf, buf_ptr, buf_len, buf_len +1); 237else if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) 238die_errno("cannot open or read '%s'", read_from); 239break; 240case S_IFLNK: 241if(strbuf_readlink(&buf, read_from, st.st_size) <0) 242die_errno("cannot readlink '%s'", read_from); 243break; 244default: 245die("unsupported file type%s", read_from); 246} 247} 248else{ 249/* Reading from stdin */ 250 mode =0; 251if(strbuf_read(&buf,0,0) <0) 252die_errno("failed to read from stdin"); 253} 254convert_to_git(&the_index, path, buf.buf, buf.len, &buf,0); 255 origin->file.ptr = buf.buf; 256 origin->file.size = buf.len; 257pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); 258 259/* 260 * Read the current index, replace the path entry with 261 * origin->blob_sha1 without mucking with its mode or type 262 * bits; we are not going to write this index out -- we just 263 * want to run "diff-index --cached". 264 */ 265discard_cache(); 266read_cache(); 267 268 len =strlen(path); 269if(!mode) { 270int pos =cache_name_pos(path, len); 271if(0<= pos) 272 mode = active_cache[pos]->ce_mode; 273else 274/* Let's not bother reading from HEAD tree */ 275 mode = S_IFREG |0644; 276} 277 size =cache_entry_size(len); 278 ce =xcalloc(1, size); 279oidcpy(&ce->oid, &origin->blob_oid); 280memcpy(ce->name, path, len); 281 ce->ce_flags =create_ce_flags(0); 282 ce->ce_namelen = len; 283 ce->ce_mode =create_ce_mode(mode); 284add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 285 286cache_tree_invalidate_path(&the_index, path); 287 288return commit; 289} 290 291 292 293static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 294 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 295{ 296 xpparam_t xpp = {0}; 297 xdemitconf_t xecfg = {0}; 298 xdemitcb_t ecb = {NULL}; 299 300 xpp.flags = xdl_opts; 301 xecfg.hunk_func = hunk_func; 302 ecb.priv = cb_data; 303returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 304} 305 306/* 307 * Given an origin, prepare mmfile_t structure to be used by the 308 * diff machinery 309 */ 310static voidfill_origin_blob(struct diff_options *opt, 311struct blame_origin *o, mmfile_t *file,int*num_read_blob) 312{ 313if(!o->file.ptr) { 314enum object_type type; 315unsigned long file_size; 316 317(*num_read_blob)++; 318if(opt->flags.allow_textconv && 319textconv_object(o->path, o->mode, &o->blob_oid,1, &file->ptr, &file_size)) 320; 321else 322 file->ptr =read_object_file(&o->blob_oid, &type, 323&file_size); 324 file->size = file_size; 325 326if(!file->ptr) 327die("Cannot read blob%sfor path%s", 328oid_to_hex(&o->blob_oid), 329 o->path); 330 o->file = *file; 331} 332else 333*file = o->file; 334} 335 336static voiddrop_origin_blob(struct blame_origin *o) 337{ 338if(o->file.ptr) { 339FREE_AND_NULL(o->file.ptr); 340} 341} 342 343/* 344 * Any merge of blames happens on lists of blames that arrived via 345 * different parents in a single suspect. In this case, we want to 346 * sort according to the suspect line numbers as opposed to the final 347 * image line numbers. The function body is somewhat longish because 348 * it avoids unnecessary writes. 349 */ 350 351static struct blame_entry *blame_merge(struct blame_entry *list1, 352struct blame_entry *list2) 353{ 354struct blame_entry *p1 = list1, *p2 = list2, 355**tail = &list1; 356 357if(!p1) 358return p2; 359if(!p2) 360return p1; 361 362if(p1->s_lno <= p2->s_lno) { 363do{ 364 tail = &p1->next; 365if((p1 = *tail) == NULL) { 366*tail = p2; 367return list1; 368} 369}while(p1->s_lno <= p2->s_lno); 370} 371for(;;) { 372*tail = p2; 373do{ 374 tail = &p2->next; 375if((p2 = *tail) == NULL) { 376*tail = p1; 377return list1; 378} 379}while(p1->s_lno > p2->s_lno); 380*tail = p1; 381do{ 382 tail = &p1->next; 383if((p1 = *tail) == NULL) { 384*tail = p2; 385return list1; 386} 387}while(p1->s_lno <= p2->s_lno); 388} 389} 390 391static void*get_next_blame(const void*p) 392{ 393return((struct blame_entry *)p)->next; 394} 395 396static voidset_next_blame(void*p1,void*p2) 397{ 398((struct blame_entry *)p1)->next = p2; 399} 400 401/* 402 * Final image line numbers are all different, so we don't need a 403 * three-way comparison here. 404 */ 405 406static intcompare_blame_final(const void*p1,const void*p2) 407{ 408return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 409?1: -1; 410} 411 412static intcompare_blame_suspect(const void*p1,const void*p2) 413{ 414const struct blame_entry *s1 = p1, *s2 = p2; 415/* 416 * to allow for collating suspects, we sort according to the 417 * respective pointer value as the primary sorting criterion. 418 * The actual relation is pretty unimportant as long as it 419 * establishes a total order. Comparing as integers gives us 420 * that. 421 */ 422if(s1->suspect != s2->suspect) 423return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 424if(s1->s_lno == s2->s_lno) 425return0; 426return s1->s_lno > s2->s_lno ?1: -1; 427} 428 429voidblame_sort_final(struct blame_scoreboard *sb) 430{ 431 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 432 compare_blame_final); 433} 434 435static intcompare_commits_by_reverse_commit_date(const void*a, 436const void*b, 437void*c) 438{ 439return-compare_commits_by_commit_date(a, b, c); 440} 441 442/* 443 * For debugging -- origin is refcounted, and this asserts that 444 * we do not underflow. 445 */ 446static voidsanity_check_refcnt(struct blame_scoreboard *sb) 447{ 448int baa =0; 449struct blame_entry *ent; 450 451for(ent = sb->ent; ent; ent = ent->next) { 452/* Nobody should have zero or negative refcnt */ 453if(ent->suspect->refcnt <=0) { 454fprintf(stderr,"%sin%shas negative refcnt%d\n", 455 ent->suspect->path, 456oid_to_hex(&ent->suspect->commit->object.oid), 457 ent->suspect->refcnt); 458 baa =1; 459} 460} 461if(baa) 462 sb->on_sanity_fail(sb, baa); 463} 464 465/* 466 * If two blame entries that are next to each other came from 467 * contiguous lines in the same origin (i.e. <commit, path> pair), 468 * merge them together. 469 */ 470voidblame_coalesce(struct blame_scoreboard *sb) 471{ 472struct blame_entry *ent, *next; 473 474for(ent = sb->ent; ent && (next = ent->next); ent = next) { 475if(ent->suspect == next->suspect && 476 ent->s_lno + ent->num_lines == next->s_lno) { 477 ent->num_lines += next->num_lines; 478 ent->next = next->next; 479blame_origin_decref(next->suspect); 480free(next); 481 ent->score =0; 482 next = ent;/* again */ 483} 484} 485 486if(sb->debug)/* sanity */ 487sanity_check_refcnt(sb); 488} 489 490/* 491 * Merge the given sorted list of blames into a preexisting origin. 492 * If there were no previous blames to that commit, it is entered into 493 * the commit priority queue of the score board. 494 */ 495 496static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 497struct blame_entry *sorted) 498{ 499if(porigin->suspects) 500 porigin->suspects =blame_merge(porigin->suspects, sorted); 501else{ 502struct blame_origin *o; 503for(o =get_blame_suspects(porigin->commit); o; o = o->next) { 504if(o->suspects) { 505 porigin->suspects = sorted; 506return; 507} 508} 509 porigin->suspects = sorted; 510prio_queue_put(&sb->commits, porigin->commit); 511} 512} 513 514/* 515 * Fill the blob_sha1 field of an origin if it hasn't, so that later 516 * call to fill_origin_blob() can use it to locate the data. blob_sha1 517 * for an origin is also used to pass the blame for the entire file to 518 * the parent to detect the case where a child's blob is identical to 519 * that of its parent's. 520 * 521 * This also fills origin->mode for corresponding tree path. 522 */ 523static intfill_blob_sha1_and_mode(struct blame_origin *origin) 524{ 525if(!is_null_oid(&origin->blob_oid)) 526return0; 527if(get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 528goto error_out; 529if(oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB) 530goto error_out; 531return0; 532 error_out: 533oidclr(&origin->blob_oid); 534 origin->mode = S_IFINVALID; 535return-1; 536} 537 538/* 539 * We have an origin -- check if the same path exists in the 540 * parent and return an origin structure to represent it. 541 */ 542static struct blame_origin *find_origin(struct commit *parent, 543struct blame_origin *origin) 544{ 545struct blame_origin *porigin; 546struct diff_options diff_opts; 547const char*paths[2]; 548 549/* First check any existing origins */ 550for(porigin =get_blame_suspects(parent); porigin; porigin = porigin->next) 551if(!strcmp(porigin->path, origin->path)) { 552/* 553 * The same path between origin and its parent 554 * without renaming -- the most common case. 555 */ 556returnblame_origin_incref(porigin); 557} 558 559/* See if the origin->path is different between parent 560 * and origin first. Most of the time they are the 561 * same and diff-tree is fairly efficient about this. 562 */ 563diff_setup(&diff_opts); 564 diff_opts.flags.recursive =1; 565 diff_opts.detect_rename =0; 566 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 567 paths[0] = origin->path; 568 paths[1] = NULL; 569 570parse_pathspec(&diff_opts.pathspec, 571 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 572 PATHSPEC_LITERAL_PATH,"", paths); 573diff_setup_done(&diff_opts); 574 575if(is_null_oid(&origin->commit->object.oid)) 576do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 577else 578diff_tree_oid(get_commit_tree_oid(parent), 579get_commit_tree_oid(origin->commit), 580"", &diff_opts); 581diffcore_std(&diff_opts); 582 583if(!diff_queued_diff.nr) { 584/* The path is the same as parent */ 585 porigin =get_origin(parent, origin->path); 586oidcpy(&porigin->blob_oid, &origin->blob_oid); 587 porigin->mode = origin->mode; 588}else{ 589/* 590 * Since origin->path is a pathspec, if the parent 591 * commit had it as a directory, we will see a whole 592 * bunch of deletion of files in the directory that we 593 * do not care about. 594 */ 595int i; 596struct diff_filepair *p = NULL; 597for(i =0; i < diff_queued_diff.nr; i++) { 598const char*name; 599 p = diff_queued_diff.queue[i]; 600 name = p->one->path ? p->one->path : p->two->path; 601if(!strcmp(name, origin->path)) 602break; 603} 604if(!p) 605die("internal error in blame::find_origin"); 606switch(p->status) { 607default: 608die("internal error in blame::find_origin (%c)", 609 p->status); 610case'M': 611 porigin =get_origin(parent, origin->path); 612oidcpy(&porigin->blob_oid, &p->one->oid); 613 porigin->mode = p->one->mode; 614break; 615case'A': 616case'T': 617/* Did not exist in parent, or type changed */ 618break; 619} 620} 621diff_flush(&diff_opts); 622clear_pathspec(&diff_opts.pathspec); 623return porigin; 624} 625 626/* 627 * We have an origin -- find the path that corresponds to it in its 628 * parent and return an origin structure to represent it. 629 */ 630static struct blame_origin *find_rename(struct commit *parent, 631struct blame_origin *origin) 632{ 633struct blame_origin *porigin = NULL; 634struct diff_options diff_opts; 635int i; 636 637diff_setup(&diff_opts); 638 diff_opts.flags.recursive =1; 639 diff_opts.detect_rename = DIFF_DETECT_RENAME; 640 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 641 diff_opts.single_follow = origin->path; 642diff_setup_done(&diff_opts); 643 644if(is_null_oid(&origin->commit->object.oid)) 645do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 646else 647diff_tree_oid(get_commit_tree_oid(parent), 648get_commit_tree_oid(origin->commit), 649"", &diff_opts); 650diffcore_std(&diff_opts); 651 652for(i =0; i < diff_queued_diff.nr; i++) { 653struct diff_filepair *p = diff_queued_diff.queue[i]; 654if((p->status =='R'|| p->status =='C') && 655!strcmp(p->two->path, origin->path)) { 656 porigin =get_origin(parent, p->one->path); 657oidcpy(&porigin->blob_oid, &p->one->oid); 658 porigin->mode = p->one->mode; 659break; 660} 661} 662diff_flush(&diff_opts); 663clear_pathspec(&diff_opts.pathspec); 664return porigin; 665} 666 667/* 668 * Append a new blame entry to a given output queue. 669 */ 670static voidadd_blame_entry(struct blame_entry ***queue, 671const struct blame_entry *src) 672{ 673struct blame_entry *e =xmalloc(sizeof(*e)); 674memcpy(e, src,sizeof(*e)); 675blame_origin_incref(e->suspect); 676 677 e->next = **queue; 678**queue = e; 679*queue = &e->next; 680} 681 682/* 683 * src typically is on-stack; we want to copy the information in it to 684 * a malloced blame_entry that gets added to the given queue. The 685 * origin of dst loses a refcnt. 686 */ 687static voiddup_entry(struct blame_entry ***queue, 688struct blame_entry *dst,struct blame_entry *src) 689{ 690blame_origin_incref(src->suspect); 691blame_origin_decref(dst->suspect); 692memcpy(dst, src,sizeof(*src)); 693 dst->next = **queue; 694**queue = dst; 695*queue = &dst->next; 696} 697 698const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 699{ 700return sb->final_buf + sb->lineno[lno]; 701} 702 703/* 704 * It is known that lines between tlno to same came from parent, and e 705 * has an overlap with that range. it also is known that parent's 706 * line plno corresponds to e's line tlno. 707 * 708 * <---- e -----> 709 * <------> 710 * <------------> 711 * <------------> 712 * <------------------> 713 * 714 * Split e into potentially three parts; before this chunk, the chunk 715 * to be blamed for the parent, and after that portion. 716 */ 717static voidsplit_overlap(struct blame_entry *split, 718struct blame_entry *e, 719int tlno,int plno,int same, 720struct blame_origin *parent) 721{ 722int chunk_end_lno; 723memset(split,0,sizeof(struct blame_entry [3])); 724 725if(e->s_lno < tlno) { 726/* there is a pre-chunk part not blamed on parent */ 727 split[0].suspect =blame_origin_incref(e->suspect); 728 split[0].lno = e->lno; 729 split[0].s_lno = e->s_lno; 730 split[0].num_lines = tlno - e->s_lno; 731 split[1].lno = e->lno + tlno - e->s_lno; 732 split[1].s_lno = plno; 733} 734else{ 735 split[1].lno = e->lno; 736 split[1].s_lno = plno + (e->s_lno - tlno); 737} 738 739if(same < e->s_lno + e->num_lines) { 740/* there is a post-chunk part not blamed on parent */ 741 split[2].suspect =blame_origin_incref(e->suspect); 742 split[2].lno = e->lno + (same - e->s_lno); 743 split[2].s_lno = e->s_lno + (same - e->s_lno); 744 split[2].num_lines = e->s_lno + e->num_lines - same; 745 chunk_end_lno = split[2].lno; 746} 747else 748 chunk_end_lno = e->lno + e->num_lines; 749 split[1].num_lines = chunk_end_lno - split[1].lno; 750 751/* 752 * if it turns out there is nothing to blame the parent for, 753 * forget about the splitting. !split[1].suspect signals this. 754 */ 755if(split[1].num_lines <1) 756return; 757 split[1].suspect =blame_origin_incref(parent); 758} 759 760/* 761 * split_overlap() divided an existing blame e into up to three parts 762 * in split. Any assigned blame is moved to queue to 763 * reflect the split. 764 */ 765static voidsplit_blame(struct blame_entry ***blamed, 766struct blame_entry ***unblamed, 767struct blame_entry *split, 768struct blame_entry *e) 769{ 770if(split[0].suspect && split[2].suspect) { 771/* The first part (reuse storage for the existing entry e) */ 772dup_entry(unblamed, e, &split[0]); 773 774/* The last part -- me */ 775add_blame_entry(unblamed, &split[2]); 776 777/* ... and the middle part -- parent */ 778add_blame_entry(blamed, &split[1]); 779} 780else if(!split[0].suspect && !split[2].suspect) 781/* 782 * The parent covers the entire area; reuse storage for 783 * e and replace it with the parent. 784 */ 785dup_entry(blamed, e, &split[1]); 786else if(split[0].suspect) { 787/* me and then parent */ 788dup_entry(unblamed, e, &split[0]); 789add_blame_entry(blamed, &split[1]); 790} 791else{ 792/* parent and then me */ 793dup_entry(blamed, e, &split[1]); 794add_blame_entry(unblamed, &split[2]); 795} 796} 797 798/* 799 * After splitting the blame, the origins used by the 800 * on-stack blame_entry should lose one refcnt each. 801 */ 802static voiddecref_split(struct blame_entry *split) 803{ 804int i; 805 806for(i =0; i <3; i++) 807blame_origin_decref(split[i].suspect); 808} 809 810/* 811 * reverse_blame reverses the list given in head, appending tail. 812 * That allows us to build lists in reverse order, then reverse them 813 * afterwards. This can be faster than building the list in proper 814 * order right away. The reason is that building in proper order 815 * requires writing a link in the _previous_ element, while building 816 * in reverse order just requires placing the list head into the 817 * _current_ element. 818 */ 819 820static struct blame_entry *reverse_blame(struct blame_entry *head, 821struct blame_entry *tail) 822{ 823while(head) { 824struct blame_entry *next = head->next; 825 head->next = tail; 826 tail = head; 827 head = next; 828} 829return tail; 830} 831 832/* 833 * Process one hunk from the patch between the current suspect for 834 * blame_entry e and its parent. This first blames any unfinished 835 * entries before the chunk (which is where target and parent start 836 * differing) on the parent, and then splits blame entries at the 837 * start and at the end of the difference region. Since use of -M and 838 * -C options may lead to overlapping/duplicate source line number 839 * ranges, all we can rely on from sorting/merging is the order of the 840 * first suspect line number. 841 */ 842static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 843int tlno,int offset,int same, 844struct blame_origin *parent) 845{ 846struct blame_entry *e = **srcq; 847struct blame_entry *samep = NULL, *diffp = NULL; 848 849while(e && e->s_lno < tlno) { 850struct blame_entry *next = e->next; 851/* 852 * current record starts before differing portion. If 853 * it reaches into it, we need to split it up and 854 * examine the second part separately. 855 */ 856if(e->s_lno + e->num_lines > tlno) { 857/* Move second half to a new record */ 858int len = tlno - e->s_lno; 859struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 860 n->suspect = e->suspect; 861 n->lno = e->lno + len; 862 n->s_lno = e->s_lno + len; 863 n->num_lines = e->num_lines - len; 864 e->num_lines = len; 865 e->score =0; 866/* Push new record to diffp */ 867 n->next = diffp; 868 diffp = n; 869}else 870blame_origin_decref(e->suspect); 871/* Pass blame for everything before the differing 872 * chunk to the parent */ 873 e->suspect =blame_origin_incref(parent); 874 e->s_lno += offset; 875 e->next = samep; 876 samep = e; 877 e = next; 878} 879/* 880 * As we don't know how much of a common stretch after this 881 * diff will occur, the currently blamed parts are all that we 882 * can assign to the parent for now. 883 */ 884 885if(samep) { 886**dstq =reverse_blame(samep, **dstq); 887*dstq = &samep->next; 888} 889/* 890 * Prepend the split off portions: everything after e starts 891 * after the blameable portion. 892 */ 893 e =reverse_blame(diffp, e); 894 895/* 896 * Now retain records on the target while parts are different 897 * from the parent. 898 */ 899 samep = NULL; 900 diffp = NULL; 901while(e && e->s_lno < same) { 902struct blame_entry *next = e->next; 903 904/* 905 * If current record extends into sameness, need to split. 906 */ 907if(e->s_lno + e->num_lines > same) { 908/* 909 * Move second half to a new record to be 910 * processed by later chunks 911 */ 912int len = same - e->s_lno; 913struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 914 n->suspect =blame_origin_incref(e->suspect); 915 n->lno = e->lno + len; 916 n->s_lno = e->s_lno + len; 917 n->num_lines = e->num_lines - len; 918 e->num_lines = len; 919 e->score =0; 920/* Push new record to samep */ 921 n->next = samep; 922 samep = n; 923} 924 e->next = diffp; 925 diffp = e; 926 e = next; 927} 928**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 929/* Move across elements that are in the unblamable portion */ 930if(diffp) 931*srcq = &diffp->next; 932} 933 934struct blame_chunk_cb_data { 935struct blame_origin *parent; 936long offset; 937struct blame_entry **dstq; 938struct blame_entry **srcq; 939}; 940 941/* diff chunks are from parent to target */ 942static intblame_chunk_cb(long start_a,long count_a, 943long start_b,long count_b,void*data) 944{ 945struct blame_chunk_cb_data *d = data; 946if(start_a - start_b != d->offset) 947die("internal error in blame::blame_chunk_cb"); 948blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 949 start_b + count_b, d->parent); 950 d->offset = start_a + count_a - (start_b + count_b); 951return0; 952} 953 954/* 955 * We are looking at the origin 'target' and aiming to pass blame 956 * for the lines it is suspected to its parent. Run diff to find 957 * which lines came from parent and pass blame for them. 958 */ 959static voidpass_blame_to_parent(struct blame_scoreboard *sb, 960struct blame_origin *target, 961struct blame_origin *parent) 962{ 963 mmfile_t file_p, file_o; 964struct blame_chunk_cb_data d; 965struct blame_entry *newdest = NULL; 966 967if(!target->suspects) 968return;/* nothing remains for this target */ 969 970 d.parent = parent; 971 d.offset =0; 972 d.dstq = &newdest; d.srcq = &target->suspects; 973 974fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 975fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 976 sb->num_get_patch++; 977 978if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 979die("unable to generate diff (%s->%s)", 980oid_to_hex(&parent->commit->object.oid), 981oid_to_hex(&target->commit->object.oid)); 982/* The rest are the same as the parent */ 983blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 984*d.dstq = NULL; 985queue_blames(sb, parent, newdest); 986 987return; 988} 989 990/* 991 * The lines in blame_entry after splitting blames many times can become 992 * very small and trivial, and at some point it becomes pointless to 993 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 994 * ordinary C program, and it is not worth to say it was copied from 995 * totally unrelated file in the parent. 996 * 997 * Compute how trivial the lines in the blame_entry are. 998 */ 999unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e)1000{1001unsigned score;1002const char*cp, *ep;10031004if(e->score)1005return e->score;10061007 score =1;1008 cp =blame_nth_line(sb, e->lno);1009 ep =blame_nth_line(sb, e->lno + e->num_lines);1010while(cp < ep) {1011unsigned ch = *((unsigned char*)cp);1012if(isalnum(ch))1013 score++;1014 cp++;1015}1016 e->score = score;1017return score;1018}10191020/*1021 * best_so_far[] and potential[] are both a split of an existing blame_entry1022 * that passes blame to the parent. Maintain best_so_far the best split so1023 * far, by comparing potential and best_so_far and copying potential into1024 * bst_so_far as needed.1025 */1026static voidcopy_split_if_better(struct blame_scoreboard *sb,1027struct blame_entry *best_so_far,1028struct blame_entry *potential)1029{1030int i;10311032if(!potential[1].suspect)1033return;1034if(best_so_far[1].suspect) {1035if(blame_entry_score(sb, &potential[1]) <1036blame_entry_score(sb, &best_so_far[1]))1037return;1038}10391040for(i =0; i <3; i++)1041blame_origin_incref(potential[i].suspect);1042decref_split(best_so_far);1043memcpy(best_so_far, potential,sizeof(struct blame_entry[3]));1044}10451046/*1047 * We are looking at a part of the final image represented by1048 * ent (tlno and same are offset by ent->s_lno).1049 * tlno is where we are looking at in the final image.1050 * up to (but not including) same match preimage.1051 * plno is where we are looking at in the preimage.1052 *1053 * <-------------- final image ---------------------->1054 * <------ent------>1055 * ^tlno ^same1056 * <---------preimage----->1057 * ^plno1058 *1059 * All line numbers are 0-based.1060 */1061static voidhandle_split(struct blame_scoreboard *sb,1062struct blame_entry *ent,1063int tlno,int plno,int same,1064struct blame_origin *parent,1065struct blame_entry *split)1066{1067if(ent->num_lines <= tlno)1068return;1069if(tlno < same) {1070struct blame_entry potential[3];1071 tlno += ent->s_lno;1072 same += ent->s_lno;1073split_overlap(potential, ent, tlno, plno, same, parent);1074copy_split_if_better(sb, split, potential);1075decref_split(potential);1076}1077}10781079struct handle_split_cb_data {1080struct blame_scoreboard *sb;1081struct blame_entry *ent;1082struct blame_origin *parent;1083struct blame_entry *split;1084long plno;1085long tlno;1086};10871088static inthandle_split_cb(long start_a,long count_a,1089long start_b,long count_b,void*data)1090{1091struct handle_split_cb_data *d = data;1092handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1093 d->split);1094 d->plno = start_a + count_a;1095 d->tlno = start_b + count_b;1096return0;1097}10981099/*1100 * Find the lines from parent that are the same as ent so that1101 * we can pass blames to it. file_p has the blob contents for1102 * the parent.1103 */1104static voidfind_copy_in_blob(struct blame_scoreboard *sb,1105struct blame_entry *ent,1106struct blame_origin *parent,1107struct blame_entry *split,1108 mmfile_t *file_p)1109{1110const char*cp;1111 mmfile_t file_o;1112struct handle_split_cb_data d;11131114memset(&d,0,sizeof(d));1115 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1116/*1117 * Prepare mmfile that contains only the lines in ent.1118 */1119 cp =blame_nth_line(sb, ent->lno);1120 file_o.ptr = (char*) cp;1121 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11221123/*1124 * file_o is a part of final image we are annotating.1125 * file_p partially may match that image.1126 */1127memset(split,0,sizeof(struct blame_entry [3]));1128if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1129die("unable to generate diff (%s)",1130oid_to_hex(&parent->commit->object.oid));1131/* remainder, if any, all match the preimage */1132handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1133}11341135/* Move all blame entries from list *source that have a score smaller1136 * than score_min to the front of list *small.1137 * Returns a pointer to the link pointing to the old head of the small list.1138 */11391140static struct blame_entry **filter_small(struct blame_scoreboard *sb,1141struct blame_entry **small,1142struct blame_entry **source,1143unsigned score_min)1144{1145struct blame_entry *p = *source;1146struct blame_entry *oldsmall = *small;1147while(p) {1148if(blame_entry_score(sb, p) <= score_min) {1149*small = p;1150 small = &p->next;1151 p = *small;1152}else{1153*source = p;1154 source = &p->next;1155 p = *source;1156}1157}1158*small = oldsmall;1159*source = NULL;1160return small;1161}11621163/*1164 * See if lines currently target is suspected for can be attributed to1165 * parent.1166 */1167static voidfind_move_in_parent(struct blame_scoreboard *sb,1168struct blame_entry ***blamed,1169struct blame_entry **toosmall,1170struct blame_origin *target,1171struct blame_origin *parent)1172{1173struct blame_entry *e, split[3];1174struct blame_entry *unblamed = target->suspects;1175struct blame_entry *leftover = NULL;1176 mmfile_t file_p;11771178if(!unblamed)1179return;/* nothing remains for this target */11801181fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1182if(!file_p.ptr)1183return;11841185/* At each iteration, unblamed has a NULL-terminated list of1186 * entries that have not yet been tested for blame. leftover1187 * contains the reversed list of entries that have been tested1188 * without being assignable to the parent.1189 */1190do{1191struct blame_entry **unblamedtail = &unblamed;1192struct blame_entry *next;1193for(e = unblamed; e; e = next) {1194 next = e->next;1195find_copy_in_blob(sb, e, parent, split, &file_p);1196if(split[1].suspect &&1197 sb->move_score <blame_entry_score(sb, &split[1])) {1198split_blame(blamed, &unblamedtail, split, e);1199}else{1200 e->next = leftover;1201 leftover = e;1202}1203decref_split(split);1204}1205*unblamedtail = NULL;1206 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1207}while(unblamed);1208 target->suspects =reverse_blame(leftover, NULL);1209}12101211struct blame_list {1212struct blame_entry *ent;1213struct blame_entry split[3];1214};12151216/*1217 * Count the number of entries the target is suspected for,1218 * and prepare a list of entry and the best split.1219 */1220static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1221int*num_ents_p)1222{1223struct blame_entry *e;1224int num_ents, i;1225struct blame_list *blame_list = NULL;12261227for(e = unblamed, num_ents =0; e; e = e->next)1228 num_ents++;1229if(num_ents) {1230 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1231for(e = unblamed, i =0; e; e = e->next)1232 blame_list[i++].ent = e;1233}1234*num_ents_p = num_ents;1235return blame_list;1236}12371238/*1239 * For lines target is suspected for, see if we can find code movement1240 * across file boundary from the parent commit. porigin is the path1241 * in the parent we already tried.1242 */1243static voidfind_copy_in_parent(struct blame_scoreboard *sb,1244struct blame_entry ***blamed,1245struct blame_entry **toosmall,1246struct blame_origin *target,1247struct commit *parent,1248struct blame_origin *porigin,1249int opt)1250{1251struct diff_options diff_opts;1252int i, j;1253struct blame_list *blame_list;1254int num_ents;1255struct blame_entry *unblamed = target->suspects;1256struct blame_entry *leftover = NULL;12571258if(!unblamed)1259return;/* nothing remains for this target */12601261diff_setup(&diff_opts);1262 diff_opts.flags.recursive =1;1263 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12641265diff_setup_done(&diff_opts);12661267/* Try "find copies harder" on new path if requested;1268 * we do not want to use diffcore_rename() actually to1269 * match things up; find_copies_harder is set only to1270 * force diff_tree_oid() to feed all filepairs to diff_queue,1271 * and this code needs to be after diff_setup_done(), which1272 * usually makes find-copies-harder imply copy detection.1273 */1274if((opt & PICKAXE_BLAME_COPY_HARDEST)1275|| ((opt & PICKAXE_BLAME_COPY_HARDER)1276&& (!porigin ||strcmp(target->path, porigin->path))))1277 diff_opts.flags.find_copies_harder =1;12781279if(is_null_oid(&target->commit->object.oid))1280do_diff_cache(get_commit_tree_oid(parent), &diff_opts);1281else1282diff_tree_oid(get_commit_tree_oid(parent),1283get_commit_tree_oid(target->commit),1284"", &diff_opts);12851286if(!diff_opts.flags.find_copies_harder)1287diffcore_std(&diff_opts);12881289do{1290struct blame_entry **unblamedtail = &unblamed;1291 blame_list =setup_blame_list(unblamed, &num_ents);12921293for(i =0; i < diff_queued_diff.nr; i++) {1294struct diff_filepair *p = diff_queued_diff.queue[i];1295struct blame_origin *norigin;1296 mmfile_t file_p;1297struct blame_entry potential[3];12981299if(!DIFF_FILE_VALID(p->one))1300continue;/* does not exist in parent */1301if(S_ISGITLINK(p->one->mode))1302continue;/* ignore git links */1303if(porigin && !strcmp(p->one->path, porigin->path))1304/* find_move already dealt with this path */1305continue;13061307 norigin =get_origin(parent, p->one->path);1308oidcpy(&norigin->blob_oid, &p->one->oid);1309 norigin->mode = p->one->mode;1310fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1311if(!file_p.ptr)1312continue;13131314for(j =0; j < num_ents; j++) {1315find_copy_in_blob(sb, blame_list[j].ent,1316 norigin, potential, &file_p);1317copy_split_if_better(sb, blame_list[j].split,1318 potential);1319decref_split(potential);1320}1321blame_origin_decref(norigin);1322}13231324for(j =0; j < num_ents; j++) {1325struct blame_entry *split = blame_list[j].split;1326if(split[1].suspect &&1327 sb->copy_score <blame_entry_score(sb, &split[1])) {1328split_blame(blamed, &unblamedtail, split,1329 blame_list[j].ent);1330}else{1331 blame_list[j].ent->next = leftover;1332 leftover = blame_list[j].ent;1333}1334decref_split(split);1335}1336free(blame_list);1337*unblamedtail = NULL;1338 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1339}while(unblamed);1340 target->suspects =reverse_blame(leftover, NULL);1341diff_flush(&diff_opts);1342clear_pathspec(&diff_opts.pathspec);1343}13441345/*1346 * The blobs of origin and porigin exactly match, so everything1347 * origin is suspected for can be blamed on the parent.1348 */1349static voidpass_whole_blame(struct blame_scoreboard *sb,1350struct blame_origin *origin,struct blame_origin *porigin)1351{1352struct blame_entry *e, *suspects;13531354if(!porigin->file.ptr && origin->file.ptr) {1355/* Steal its file */1356 porigin->file = origin->file;1357 origin->file.ptr = NULL;1358}1359 suspects = origin->suspects;1360 origin->suspects = NULL;1361for(e = suspects; e; e = e->next) {1362blame_origin_incref(porigin);1363blame_origin_decref(e->suspect);1364 e->suspect = porigin;1365}1366queue_blames(sb, porigin, suspects);1367}13681369/*1370 * We pass blame from the current commit to its parents. We keep saying1371 * "parent" (and "porigin"), but what we mean is to find scapegoat to1372 * exonerate ourselves.1373 */1374static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1375int reverse)1376{1377if(!reverse) {1378if(revs->first_parent_only &&1379 commit->parents &&1380 commit->parents->next) {1381free_commit_list(commit->parents->next);1382 commit->parents->next = NULL;1383}1384return commit->parents;1385}1386returnlookup_decoration(&revs->children, &commit->object);1387}13881389static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1390{1391struct commit_list *l =first_scapegoat(revs, commit, reverse);1392returncommit_list_count(l);1393}13941395/* Distribute collected unsorted blames to the respected sorted lists1396 * in the various origins.1397 */1398static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1399{1400 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1401 compare_blame_suspect);1402while(blamed)1403{1404struct blame_origin *porigin = blamed->suspect;1405struct blame_entry *suspects = NULL;1406do{1407struct blame_entry *next = blamed->next;1408 blamed->next = suspects;1409 suspects = blamed;1410 blamed = next;1411}while(blamed && blamed->suspect == porigin);1412 suspects =reverse_blame(suspects, NULL);1413queue_blames(sb, porigin, suspects);1414}1415}14161417#define MAXSG 1614181419static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1420{1421struct rev_info *revs = sb->revs;1422int i, pass, num_sg;1423struct commit *commit = origin->commit;1424struct commit_list *sg;1425struct blame_origin *sg_buf[MAXSG];1426struct blame_origin *porigin, **sg_origin = sg_buf;1427struct blame_entry *toosmall = NULL;1428struct blame_entry *blames, **blametail = &blames;14291430 num_sg =num_scapegoats(revs, commit, sb->reverse);1431if(!num_sg)1432goto finish;1433else if(num_sg <ARRAY_SIZE(sg_buf))1434memset(sg_buf,0,sizeof(sg_buf));1435else1436 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14371438/*1439 * The first pass looks for unrenamed path to optimize for1440 * common cases, then we look for renames in the second pass.1441 */1442for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1443struct blame_origin *(*find)(struct commit *,struct blame_origin *);1444 find = pass ? find_rename : find_origin;14451446for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1447 i < num_sg && sg;1448 sg = sg->next, i++) {1449struct commit *p = sg->item;1450int j, same;14511452if(sg_origin[i])1453continue;1454if(parse_commit(p))1455continue;1456 porigin =find(p, origin);1457if(!porigin)1458continue;1459if(!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1460pass_whole_blame(sb, origin, porigin);1461blame_origin_decref(porigin);1462goto finish;1463}1464for(j = same =0; j < i; j++)1465if(sg_origin[j] &&1466!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1467 same =1;1468break;1469}1470if(!same)1471 sg_origin[i] = porigin;1472else1473blame_origin_decref(porigin);1474}1475}14761477 sb->num_commits++;1478for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1479 i < num_sg && sg;1480 sg = sg->next, i++) {1481struct blame_origin *porigin = sg_origin[i];1482if(!porigin)1483continue;1484if(!origin->previous) {1485blame_origin_incref(porigin);1486 origin->previous = porigin;1487}1488pass_blame_to_parent(sb, origin, porigin);1489if(!origin->suspects)1490goto finish;1491}14921493/*1494 * Optionally find moves in parents' files.1495 */1496if(opt & PICKAXE_BLAME_MOVE) {1497filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1498if(origin->suspects) {1499for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1500 i < num_sg && sg;1501 sg = sg->next, i++) {1502struct blame_origin *porigin = sg_origin[i];1503if(!porigin)1504continue;1505find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1506if(!origin->suspects)1507break;1508}1509}1510}15111512/*1513 * Optionally find copies from parents' files.1514 */1515if(opt & PICKAXE_BLAME_COPY) {1516if(sb->copy_score > sb->move_score)1517filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1518else if(sb->copy_score < sb->move_score) {1519 origin->suspects =blame_merge(origin->suspects, toosmall);1520 toosmall = NULL;1521filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1522}1523if(!origin->suspects)1524goto finish;15251526for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1527 i < num_sg && sg;1528 sg = sg->next, i++) {1529struct blame_origin *porigin = sg_origin[i];1530find_copy_in_parent(sb, &blametail, &toosmall,1531 origin, sg->item, porigin, opt);1532if(!origin->suspects)1533goto finish;1534}1535}15361537finish:1538*blametail = NULL;1539distribute_blame(sb, blames);1540/*1541 * prepend toosmall to origin->suspects1542 *1543 * There is no point in sorting: this ends up on a big1544 * unsorted list in the caller anyway.1545 */1546if(toosmall) {1547struct blame_entry **tail = &toosmall;1548while(*tail)1549 tail = &(*tail)->next;1550*tail = origin->suspects;1551 origin->suspects = toosmall;1552}1553for(i =0; i < num_sg; i++) {1554if(sg_origin[i]) {1555drop_origin_blob(sg_origin[i]);1556blame_origin_decref(sg_origin[i]);1557}1558}1559drop_origin_blob(origin);1560if(sg_buf != sg_origin)1561free(sg_origin);1562}15631564/*1565 * The main loop -- while we have blobs with lines whose true origin1566 * is still unknown, pick one blob, and allow its lines to pass blames1567 * to its parents. */1568voidassign_blame(struct blame_scoreboard *sb,int opt)1569{1570struct rev_info *revs = sb->revs;1571struct commit *commit =prio_queue_get(&sb->commits);15721573while(commit) {1574struct blame_entry *ent;1575struct blame_origin *suspect =get_blame_suspects(commit);15761577/* find one suspect to break down */1578while(suspect && !suspect->suspects)1579 suspect = suspect->next;15801581if(!suspect) {1582 commit =prio_queue_get(&sb->commits);1583continue;1584}15851586assert(commit == suspect->commit);15871588/*1589 * We will use this suspect later in the loop,1590 * so hold onto it in the meantime.1591 */1592blame_origin_incref(suspect);1593parse_commit(commit);1594if(sb->reverse ||1595(!(commit->object.flags & UNINTERESTING) &&1596!(revs->max_age != -1&& commit->date < revs->max_age)))1597pass_blame(sb, suspect, opt);1598else{1599 commit->object.flags |= UNINTERESTING;1600if(commit->object.parsed)1601mark_parents_uninteresting(commit);1602}1603/* treat root commit as boundary */1604if(!commit->parents && !sb->show_root)1605 commit->object.flags |= UNINTERESTING;16061607/* Take responsibility for the remaining entries */1608 ent = suspect->suspects;1609if(ent) {1610 suspect->guilty =1;1611for(;;) {1612struct blame_entry *next = ent->next;1613if(sb->found_guilty_entry)1614 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1615if(next) {1616 ent = next;1617continue;1618}1619 ent->next = sb->ent;1620 sb->ent = suspect->suspects;1621 suspect->suspects = NULL;1622break;1623}1624}1625blame_origin_decref(suspect);16261627if(sb->debug)/* sanity */1628sanity_check_refcnt(sb);1629}1630}16311632static const char*get_next_line(const char*start,const char*end)1633{1634const char*nl =memchr(start,'\n', end - start);1635return nl ? nl +1: end;1636}16371638/*1639 * To allow quick access to the contents of nth line in the1640 * final image, prepare an index in the scoreboard.1641 */1642static intprepare_lines(struct blame_scoreboard *sb)1643{1644const char*buf = sb->final_buf;1645unsigned long len = sb->final_buf_size;1646const char*end = buf + len;1647const char*p;1648int*lineno;1649int num =0;16501651for(p = buf; p < end; p =get_next_line(p, end))1652 num++;16531654ALLOC_ARRAY(sb->lineno, num +1);1655 lineno = sb->lineno;16561657for(p = buf; p < end; p =get_next_line(p, end))1658*lineno++ = p - buf;16591660*lineno = len;16611662 sb->num_lines = num;1663return sb->num_lines;1664}16651666static struct commit *find_single_final(struct rev_info *revs,1667const char**name_p)1668{1669int i;1670struct commit *found = NULL;1671const char*name = NULL;16721673for(i =0; i < revs->pending.nr; i++) {1674struct object *obj = revs->pending.objects[i].item;1675if(obj->flags & UNINTERESTING)1676continue;1677 obj =deref_tag(obj, NULL,0);1678if(obj->type != OBJ_COMMIT)1679die("Non commit%s?", revs->pending.objects[i].name);1680if(found)1681die("More than one commit to dig from%sand%s?",1682 revs->pending.objects[i].name, name);1683 found = (struct commit *)obj;1684 name = revs->pending.objects[i].name;1685}1686if(name_p)1687*name_p =xstrdup_or_null(name);1688return found;1689}16901691static struct commit *dwim_reverse_initial(struct rev_info *revs,1692const char**name_p)1693{1694/*1695 * DWIM "git blame --reverse ONE -- PATH" as1696 * "git blame --reverse ONE..HEAD -- PATH" but only do so1697 * when it makes sense.1698 */1699struct object *obj;1700struct commit *head_commit;1701struct object_id head_oid;17021703if(revs->pending.nr !=1)1704return NULL;17051706/* Is that sole rev a committish? */1707 obj = revs->pending.objects[0].item;1708 obj =deref_tag(obj, NULL,0);1709if(obj->type != OBJ_COMMIT)1710return NULL;17111712/* Do we have HEAD? */1713if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1714return NULL;1715 head_commit =lookup_commit_reference_gently(&head_oid,1);1716if(!head_commit)1717return NULL;17181719/* Turn "ONE" into "ONE..HEAD" then */1720 obj->flags |= UNINTERESTING;1721add_pending_object(revs, &head_commit->object,"HEAD");17221723if(name_p)1724*name_p = revs->pending.objects[0].name;1725return(struct commit *)obj;1726}17271728static struct commit *find_single_initial(struct rev_info *revs,1729const char**name_p)1730{1731int i;1732struct commit *found = NULL;1733const char*name = NULL;17341735/*1736 * There must be one and only one negative commit, and it must be1737 * the boundary.1738 */1739for(i =0; i < revs->pending.nr; i++) {1740struct object *obj = revs->pending.objects[i].item;1741if(!(obj->flags & UNINTERESTING))1742continue;1743 obj =deref_tag(obj, NULL,0);1744if(obj->type != OBJ_COMMIT)1745die("Non commit%s?", revs->pending.objects[i].name);1746if(found)1747die("More than one commit to dig up from,%sand%s?",1748 revs->pending.objects[i].name, name);1749 found = (struct commit *) obj;1750 name = revs->pending.objects[i].name;1751}17521753if(!name)1754 found =dwim_reverse_initial(revs, &name);1755if(!name)1756die("No commit to dig up from?");17571758if(name_p)1759*name_p =xstrdup(name);1760return found;1761}17621763voidinit_scoreboard(struct blame_scoreboard *sb)1764{1765memset(sb,0,sizeof(struct blame_scoreboard));1766 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;1767 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;1768}17691770voidsetup_scoreboard(struct blame_scoreboard *sb,const char*path,struct blame_origin **orig)1771{1772const char*final_commit_name = NULL;1773struct blame_origin *o;1774struct commit *final_commit = NULL;1775enum object_type type;17761777init_blame_suspects(&blame_suspects);17781779if(sb->reverse && sb->contents_from)1780die(_("--contents and --reverse do not blend well."));17811782if(!sb->reverse) {1783 sb->final =find_single_final(sb->revs, &final_commit_name);1784 sb->commits.compare = compare_commits_by_commit_date;1785}else{1786 sb->final =find_single_initial(sb->revs, &final_commit_name);1787 sb->commits.compare = compare_commits_by_reverse_commit_date;1788}17891790if(sb->final && sb->contents_from)1791die(_("cannot use --contents with final commit object name"));17921793if(sb->reverse && sb->revs->first_parent_only)1794 sb->revs->children.name = NULL;17951796if(!sb->final) {1797/*1798 * "--not A B -- path" without anything positive;1799 * do not default to HEAD, but use the working tree1800 * or "--contents".1801 */1802setup_work_tree();1803 sb->final =fake_working_tree_commit(&sb->revs->diffopt,1804 path, sb->contents_from);1805add_pending_object(sb->revs, &(sb->final->object),":");1806}18071808if(sb->reverse && sb->revs->first_parent_only) {1809 final_commit =find_single_final(sb->revs, NULL);1810if(!final_commit)1811die(_("--reverse and --first-parent together require specified latest commit"));1812}18131814/*1815 * If we have bottom, this will mark the ancestors of the1816 * bottom commits we would reach while traversing as1817 * uninteresting.1818 */1819if(prepare_revision_walk(sb->revs))1820die(_("revision walk setup failed"));18211822if(sb->reverse && sb->revs->first_parent_only) {1823struct commit *c = final_commit;18241825 sb->revs->children.name ="children";1826while(c->parents &&1827oidcmp(&c->object.oid, &sb->final->object.oid)) {1828struct commit_list *l =xcalloc(1,sizeof(*l));18291830 l->item = c;1831if(add_decoration(&sb->revs->children,1832&c->parents->item->object, l))1833BUG("not unique item in first-parent chain");1834 c = c->parents->item;1835}18361837if(oidcmp(&c->object.oid, &sb->final->object.oid))1838die(_("--reverse --first-parent together require range along first-parent chain"));1839}18401841if(is_null_oid(&sb->final->object.oid)) {1842 o =get_blame_suspects(sb->final);1843 sb->final_buf =xmemdupz(o->file.ptr, o->file.size);1844 sb->final_buf_size = o->file.size;1845}1846else{1847 o =get_origin(sb->final, path);1848if(fill_blob_sha1_and_mode(o))1849die(_("no such path%sin%s"), path, final_commit_name);18501851if(sb->revs->diffopt.flags.allow_textconv &&1852textconv_object(path, o->mode, &o->blob_oid,1, (char**) &sb->final_buf,1853&sb->final_buf_size))1854;1855else1856 sb->final_buf =read_object_file(&o->blob_oid, &type,1857&sb->final_buf_size);18581859if(!sb->final_buf)1860die(_("cannot read blob%sfor path%s"),1861oid_to_hex(&o->blob_oid),1862 path);1863}1864 sb->num_read_blob++;1865prepare_lines(sb);18661867if(orig)1868*orig = o;18691870free((char*)final_commit_name);1871}1872187318741875struct blame_entry *blame_entry_prepend(struct blame_entry *head,1876long start,long end,1877struct blame_origin *o)1878{1879struct blame_entry *new_head =xcalloc(1,sizeof(struct blame_entry));1880 new_head->lno = start;1881 new_head->num_lines = end - start;1882 new_head->suspect = o;1883 new_head->s_lno = start;1884 new_head->next = head;1885blame_origin_incref(o);1886return new_head;1887}