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(the_repository, 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(the_repository, 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 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 ce =make_empty_cache_entry(&the_index, len); 278oidcpy(&ce->oid, &origin->blob_oid); 279memcpy(ce->name, path, len); 280 ce->ce_flags =create_ce_flags(0); 281 ce->ce_namelen = len; 282 ce->ce_mode =create_ce_mode(mode); 283add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 284 285cache_tree_invalidate_path(&the_index, path); 286 287return commit; 288} 289 290 291 292static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 293 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 294{ 295 xpparam_t xpp = {0}; 296 xdemitconf_t xecfg = {0}; 297 xdemitcb_t ecb = {NULL}; 298 299 xpp.flags = xdl_opts; 300 xecfg.hunk_func = hunk_func; 301 ecb.priv = cb_data; 302returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 303} 304 305/* 306 * Given an origin, prepare mmfile_t structure to be used by the 307 * diff machinery 308 */ 309static voidfill_origin_blob(struct diff_options *opt, 310struct blame_origin *o, mmfile_t *file,int*num_read_blob) 311{ 312if(!o->file.ptr) { 313enum object_type type; 314unsigned long file_size; 315 316(*num_read_blob)++; 317if(opt->flags.allow_textconv && 318textconv_object(o->path, o->mode, &o->blob_oid,1, &file->ptr, &file_size)) 319; 320else 321 file->ptr =read_object_file(&o->blob_oid, &type, 322&file_size); 323 file->size = file_size; 324 325if(!file->ptr) 326die("Cannot read blob%sfor path%s", 327oid_to_hex(&o->blob_oid), 328 o->path); 329 o->file = *file; 330} 331else 332*file = o->file; 333} 334 335static voiddrop_origin_blob(struct blame_origin *o) 336{ 337if(o->file.ptr) { 338FREE_AND_NULL(o->file.ptr); 339} 340} 341 342/* 343 * Any merge of blames happens on lists of blames that arrived via 344 * different parents in a single suspect. In this case, we want to 345 * sort according to the suspect line numbers as opposed to the final 346 * image line numbers. The function body is somewhat longish because 347 * it avoids unnecessary writes. 348 */ 349 350static struct blame_entry *blame_merge(struct blame_entry *list1, 351struct blame_entry *list2) 352{ 353struct blame_entry *p1 = list1, *p2 = list2, 354**tail = &list1; 355 356if(!p1) 357return p2; 358if(!p2) 359return p1; 360 361if(p1->s_lno <= p2->s_lno) { 362do{ 363 tail = &p1->next; 364if((p1 = *tail) == NULL) { 365*tail = p2; 366return list1; 367} 368}while(p1->s_lno <= p2->s_lno); 369} 370for(;;) { 371*tail = p2; 372do{ 373 tail = &p2->next; 374if((p2 = *tail) == NULL) { 375*tail = p1; 376return list1; 377} 378}while(p1->s_lno > p2->s_lno); 379*tail = p1; 380do{ 381 tail = &p1->next; 382if((p1 = *tail) == NULL) { 383*tail = p2; 384return list1; 385} 386}while(p1->s_lno <= p2->s_lno); 387} 388} 389 390static void*get_next_blame(const void*p) 391{ 392return((struct blame_entry *)p)->next; 393} 394 395static voidset_next_blame(void*p1,void*p2) 396{ 397((struct blame_entry *)p1)->next = p2; 398} 399 400/* 401 * Final image line numbers are all different, so we don't need a 402 * three-way comparison here. 403 */ 404 405static intcompare_blame_final(const void*p1,const void*p2) 406{ 407return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 408?1: -1; 409} 410 411static intcompare_blame_suspect(const void*p1,const void*p2) 412{ 413const struct blame_entry *s1 = p1, *s2 = p2; 414/* 415 * to allow for collating suspects, we sort according to the 416 * respective pointer value as the primary sorting criterion. 417 * The actual relation is pretty unimportant as long as it 418 * establishes a total order. Comparing as integers gives us 419 * that. 420 */ 421if(s1->suspect != s2->suspect) 422return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 423if(s1->s_lno == s2->s_lno) 424return0; 425return s1->s_lno > s2->s_lno ?1: -1; 426} 427 428voidblame_sort_final(struct blame_scoreboard *sb) 429{ 430 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 431 compare_blame_final); 432} 433 434static intcompare_commits_by_reverse_commit_date(const void*a, 435const void*b, 436void*c) 437{ 438return-compare_commits_by_commit_date(a, b, c); 439} 440 441/* 442 * For debugging -- origin is refcounted, and this asserts that 443 * we do not underflow. 444 */ 445static voidsanity_check_refcnt(struct blame_scoreboard *sb) 446{ 447int baa =0; 448struct blame_entry *ent; 449 450for(ent = sb->ent; ent; ent = ent->next) { 451/* Nobody should have zero or negative refcnt */ 452if(ent->suspect->refcnt <=0) { 453fprintf(stderr,"%sin%shas negative refcnt%d\n", 454 ent->suspect->path, 455oid_to_hex(&ent->suspect->commit->object.oid), 456 ent->suspect->refcnt); 457 baa =1; 458} 459} 460if(baa) 461 sb->on_sanity_fail(sb, baa); 462} 463 464/* 465 * If two blame entries that are next to each other came from 466 * contiguous lines in the same origin (i.e. <commit, path> pair), 467 * merge them together. 468 */ 469voidblame_coalesce(struct blame_scoreboard *sb) 470{ 471struct blame_entry *ent, *next; 472 473for(ent = sb->ent; ent && (next = ent->next); ent = next) { 474if(ent->suspect == next->suspect && 475 ent->s_lno + ent->num_lines == next->s_lno) { 476 ent->num_lines += next->num_lines; 477 ent->next = next->next; 478blame_origin_decref(next->suspect); 479free(next); 480 ent->score =0; 481 next = ent;/* again */ 482} 483} 484 485if(sb->debug)/* sanity */ 486sanity_check_refcnt(sb); 487} 488 489/* 490 * Merge the given sorted list of blames into a preexisting origin. 491 * If there were no previous blames to that commit, it is entered into 492 * the commit priority queue of the score board. 493 */ 494 495static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 496struct blame_entry *sorted) 497{ 498if(porigin->suspects) 499 porigin->suspects =blame_merge(porigin->suspects, sorted); 500else{ 501struct blame_origin *o; 502for(o =get_blame_suspects(porigin->commit); o; o = o->next) { 503if(o->suspects) { 504 porigin->suspects = sorted; 505return; 506} 507} 508 porigin->suspects = sorted; 509prio_queue_put(&sb->commits, porigin->commit); 510} 511} 512 513/* 514 * Fill the blob_sha1 field of an origin if it hasn't, so that later 515 * call to fill_origin_blob() can use it to locate the data. blob_sha1 516 * for an origin is also used to pass the blame for the entire file to 517 * the parent to detect the case where a child's blob is identical to 518 * that of its parent's. 519 * 520 * This also fills origin->mode for corresponding tree path. 521 */ 522static intfill_blob_sha1_and_mode(struct blame_origin *origin) 523{ 524if(!is_null_oid(&origin->blob_oid)) 525return0; 526if(get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 527goto error_out; 528if(oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB) 529goto error_out; 530return0; 531 error_out: 532oidclr(&origin->blob_oid); 533 origin->mode = S_IFINVALID; 534return-1; 535} 536 537/* 538 * We have an origin -- check if the same path exists in the 539 * parent and return an origin structure to represent it. 540 */ 541static struct blame_origin *find_origin(struct commit *parent, 542struct blame_origin *origin) 543{ 544struct blame_origin *porigin; 545struct diff_options diff_opts; 546const char*paths[2]; 547 548/* First check any existing origins */ 549for(porigin =get_blame_suspects(parent); porigin; porigin = porigin->next) 550if(!strcmp(porigin->path, origin->path)) { 551/* 552 * The same path between origin and its parent 553 * without renaming -- the most common case. 554 */ 555returnblame_origin_incref(porigin); 556} 557 558/* See if the origin->path is different between parent 559 * and origin first. Most of the time they are the 560 * same and diff-tree is fairly efficient about this. 561 */ 562diff_setup(&diff_opts); 563 diff_opts.flags.recursive =1; 564 diff_opts.detect_rename =0; 565 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 566 paths[0] = origin->path; 567 paths[1] = NULL; 568 569parse_pathspec(&diff_opts.pathspec, 570 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 571 PATHSPEC_LITERAL_PATH,"", paths); 572diff_setup_done(&diff_opts); 573 574if(is_null_oid(&origin->commit->object.oid)) 575do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 576else 577diff_tree_oid(get_commit_tree_oid(parent), 578get_commit_tree_oid(origin->commit), 579"", &diff_opts); 580diffcore_std(&diff_opts); 581 582if(!diff_queued_diff.nr) { 583/* The path is the same as parent */ 584 porigin =get_origin(parent, origin->path); 585oidcpy(&porigin->blob_oid, &origin->blob_oid); 586 porigin->mode = origin->mode; 587}else{ 588/* 589 * Since origin->path is a pathspec, if the parent 590 * commit had it as a directory, we will see a whole 591 * bunch of deletion of files in the directory that we 592 * do not care about. 593 */ 594int i; 595struct diff_filepair *p = NULL; 596for(i =0; i < diff_queued_diff.nr; i++) { 597const char*name; 598 p = diff_queued_diff.queue[i]; 599 name = p->one->path ? p->one->path : p->two->path; 600if(!strcmp(name, origin->path)) 601break; 602} 603if(!p) 604die("internal error in blame::find_origin"); 605switch(p->status) { 606default: 607die("internal error in blame::find_origin (%c)", 608 p->status); 609case'M': 610 porigin =get_origin(parent, origin->path); 611oidcpy(&porigin->blob_oid, &p->one->oid); 612 porigin->mode = p->one->mode; 613break; 614case'A': 615case'T': 616/* Did not exist in parent, or type changed */ 617break; 618} 619} 620diff_flush(&diff_opts); 621clear_pathspec(&diff_opts.pathspec); 622return porigin; 623} 624 625/* 626 * We have an origin -- find the path that corresponds to it in its 627 * parent and return an origin structure to represent it. 628 */ 629static struct blame_origin *find_rename(struct commit *parent, 630struct blame_origin *origin) 631{ 632struct blame_origin *porigin = NULL; 633struct diff_options diff_opts; 634int i; 635 636diff_setup(&diff_opts); 637 diff_opts.flags.recursive =1; 638 diff_opts.detect_rename = DIFF_DETECT_RENAME; 639 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 640 diff_opts.single_follow = origin->path; 641diff_setup_done(&diff_opts); 642 643if(is_null_oid(&origin->commit->object.oid)) 644do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 645else 646diff_tree_oid(get_commit_tree_oid(parent), 647get_commit_tree_oid(origin->commit), 648"", &diff_opts); 649diffcore_std(&diff_opts); 650 651for(i =0; i < diff_queued_diff.nr; i++) { 652struct diff_filepair *p = diff_queued_diff.queue[i]; 653if((p->status =='R'|| p->status =='C') && 654!strcmp(p->two->path, origin->path)) { 655 porigin =get_origin(parent, p->one->path); 656oidcpy(&porigin->blob_oid, &p->one->oid); 657 porigin->mode = p->one->mode; 658break; 659} 660} 661diff_flush(&diff_opts); 662clear_pathspec(&diff_opts.pathspec); 663return porigin; 664} 665 666/* 667 * Append a new blame entry to a given output queue. 668 */ 669static voidadd_blame_entry(struct blame_entry ***queue, 670const struct blame_entry *src) 671{ 672struct blame_entry *e =xmalloc(sizeof(*e)); 673memcpy(e, src,sizeof(*e)); 674blame_origin_incref(e->suspect); 675 676 e->next = **queue; 677**queue = e; 678*queue = &e->next; 679} 680 681/* 682 * src typically is on-stack; we want to copy the information in it to 683 * a malloced blame_entry that gets added to the given queue. The 684 * origin of dst loses a refcnt. 685 */ 686static voiddup_entry(struct blame_entry ***queue, 687struct blame_entry *dst,struct blame_entry *src) 688{ 689blame_origin_incref(src->suspect); 690blame_origin_decref(dst->suspect); 691memcpy(dst, src,sizeof(*src)); 692 dst->next = **queue; 693**queue = dst; 694*queue = &dst->next; 695} 696 697const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 698{ 699return sb->final_buf + sb->lineno[lno]; 700} 701 702/* 703 * It is known that lines between tlno to same came from parent, and e 704 * has an overlap with that range. it also is known that parent's 705 * line plno corresponds to e's line tlno. 706 * 707 * <---- e -----> 708 * <------> 709 * <------------> 710 * <------------> 711 * <------------------> 712 * 713 * Split e into potentially three parts; before this chunk, the chunk 714 * to be blamed for the parent, and after that portion. 715 */ 716static voidsplit_overlap(struct blame_entry *split, 717struct blame_entry *e, 718int tlno,int plno,int same, 719struct blame_origin *parent) 720{ 721int chunk_end_lno; 722memset(split,0,sizeof(struct blame_entry [3])); 723 724if(e->s_lno < tlno) { 725/* there is a pre-chunk part not blamed on parent */ 726 split[0].suspect =blame_origin_incref(e->suspect); 727 split[0].lno = e->lno; 728 split[0].s_lno = e->s_lno; 729 split[0].num_lines = tlno - e->s_lno; 730 split[1].lno = e->lno + tlno - e->s_lno; 731 split[1].s_lno = plno; 732} 733else{ 734 split[1].lno = e->lno; 735 split[1].s_lno = plno + (e->s_lno - tlno); 736} 737 738if(same < e->s_lno + e->num_lines) { 739/* there is a post-chunk part not blamed on parent */ 740 split[2].suspect =blame_origin_incref(e->suspect); 741 split[2].lno = e->lno + (same - e->s_lno); 742 split[2].s_lno = e->s_lno + (same - e->s_lno); 743 split[2].num_lines = e->s_lno + e->num_lines - same; 744 chunk_end_lno = split[2].lno; 745} 746else 747 chunk_end_lno = e->lno + e->num_lines; 748 split[1].num_lines = chunk_end_lno - split[1].lno; 749 750/* 751 * if it turns out there is nothing to blame the parent for, 752 * forget about the splitting. !split[1].suspect signals this. 753 */ 754if(split[1].num_lines <1) 755return; 756 split[1].suspect =blame_origin_incref(parent); 757} 758 759/* 760 * split_overlap() divided an existing blame e into up to three parts 761 * in split. Any assigned blame is moved to queue to 762 * reflect the split. 763 */ 764static voidsplit_blame(struct blame_entry ***blamed, 765struct blame_entry ***unblamed, 766struct blame_entry *split, 767struct blame_entry *e) 768{ 769if(split[0].suspect && split[2].suspect) { 770/* The first part (reuse storage for the existing entry e) */ 771dup_entry(unblamed, e, &split[0]); 772 773/* The last part -- me */ 774add_blame_entry(unblamed, &split[2]); 775 776/* ... and the middle part -- parent */ 777add_blame_entry(blamed, &split[1]); 778} 779else if(!split[0].suspect && !split[2].suspect) 780/* 781 * The parent covers the entire area; reuse storage for 782 * e and replace it with the parent. 783 */ 784dup_entry(blamed, e, &split[1]); 785else if(split[0].suspect) { 786/* me and then parent */ 787dup_entry(unblamed, e, &split[0]); 788add_blame_entry(blamed, &split[1]); 789} 790else{ 791/* parent and then me */ 792dup_entry(blamed, e, &split[1]); 793add_blame_entry(unblamed, &split[2]); 794} 795} 796 797/* 798 * After splitting the blame, the origins used by the 799 * on-stack blame_entry should lose one refcnt each. 800 */ 801static voiddecref_split(struct blame_entry *split) 802{ 803int i; 804 805for(i =0; i <3; i++) 806blame_origin_decref(split[i].suspect); 807} 808 809/* 810 * reverse_blame reverses the list given in head, appending tail. 811 * That allows us to build lists in reverse order, then reverse them 812 * afterwards. This can be faster than building the list in proper 813 * order right away. The reason is that building in proper order 814 * requires writing a link in the _previous_ element, while building 815 * in reverse order just requires placing the list head into the 816 * _current_ element. 817 */ 818 819static struct blame_entry *reverse_blame(struct blame_entry *head, 820struct blame_entry *tail) 821{ 822while(head) { 823struct blame_entry *next = head->next; 824 head->next = tail; 825 tail = head; 826 head = next; 827} 828return tail; 829} 830 831/* 832 * Process one hunk from the patch between the current suspect for 833 * blame_entry e and its parent. This first blames any unfinished 834 * entries before the chunk (which is where target and parent start 835 * differing) on the parent, and then splits blame entries at the 836 * start and at the end of the difference region. Since use of -M and 837 * -C options may lead to overlapping/duplicate source line number 838 * ranges, all we can rely on from sorting/merging is the order of the 839 * first suspect line number. 840 */ 841static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 842int tlno,int offset,int same, 843struct blame_origin *parent) 844{ 845struct blame_entry *e = **srcq; 846struct blame_entry *samep = NULL, *diffp = NULL; 847 848while(e && e->s_lno < tlno) { 849struct blame_entry *next = e->next; 850/* 851 * current record starts before differing portion. If 852 * it reaches into it, we need to split it up and 853 * examine the second part separately. 854 */ 855if(e->s_lno + e->num_lines > tlno) { 856/* Move second half to a new record */ 857int len = tlno - e->s_lno; 858struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 859 n->suspect = e->suspect; 860 n->lno = e->lno + len; 861 n->s_lno = e->s_lno + len; 862 n->num_lines = e->num_lines - len; 863 e->num_lines = len; 864 e->score =0; 865/* Push new record to diffp */ 866 n->next = diffp; 867 diffp = n; 868}else 869blame_origin_decref(e->suspect); 870/* Pass blame for everything before the differing 871 * chunk to the parent */ 872 e->suspect =blame_origin_incref(parent); 873 e->s_lno += offset; 874 e->next = samep; 875 samep = e; 876 e = next; 877} 878/* 879 * As we don't know how much of a common stretch after this 880 * diff will occur, the currently blamed parts are all that we 881 * can assign to the parent for now. 882 */ 883 884if(samep) { 885**dstq =reverse_blame(samep, **dstq); 886*dstq = &samep->next; 887} 888/* 889 * Prepend the split off portions: everything after e starts 890 * after the blameable portion. 891 */ 892 e =reverse_blame(diffp, e); 893 894/* 895 * Now retain records on the target while parts are different 896 * from the parent. 897 */ 898 samep = NULL; 899 diffp = NULL; 900while(e && e->s_lno < same) { 901struct blame_entry *next = e->next; 902 903/* 904 * If current record extends into sameness, need to split. 905 */ 906if(e->s_lno + e->num_lines > same) { 907/* 908 * Move second half to a new record to be 909 * processed by later chunks 910 */ 911int len = same - e->s_lno; 912struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 913 n->suspect =blame_origin_incref(e->suspect); 914 n->lno = e->lno + len; 915 n->s_lno = e->s_lno + len; 916 n->num_lines = e->num_lines - len; 917 e->num_lines = len; 918 e->score =0; 919/* Push new record to samep */ 920 n->next = samep; 921 samep = n; 922} 923 e->next = diffp; 924 diffp = e; 925 e = next; 926} 927**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 928/* Move across elements that are in the unblamable portion */ 929if(diffp) 930*srcq = &diffp->next; 931} 932 933struct blame_chunk_cb_data { 934struct blame_origin *parent; 935long offset; 936struct blame_entry **dstq; 937struct blame_entry **srcq; 938}; 939 940/* diff chunks are from parent to target */ 941static intblame_chunk_cb(long start_a,long count_a, 942long start_b,long count_b,void*data) 943{ 944struct blame_chunk_cb_data *d = data; 945if(start_a - start_b != d->offset) 946die("internal error in blame::blame_chunk_cb"); 947blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 948 start_b + count_b, d->parent); 949 d->offset = start_a + count_a - (start_b + count_b); 950return0; 951} 952 953/* 954 * We are looking at the origin 'target' and aiming to pass blame 955 * for the lines it is suspected to its parent. Run diff to find 956 * which lines came from parent and pass blame for them. 957 */ 958static voidpass_blame_to_parent(struct blame_scoreboard *sb, 959struct blame_origin *target, 960struct blame_origin *parent) 961{ 962 mmfile_t file_p, file_o; 963struct blame_chunk_cb_data d; 964struct blame_entry *newdest = NULL; 965 966if(!target->suspects) 967return;/* nothing remains for this target */ 968 969 d.parent = parent; 970 d.offset =0; 971 d.dstq = &newdest; d.srcq = &target->suspects; 972 973fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 974fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 975 sb->num_get_patch++; 976 977if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 978die("unable to generate diff (%s->%s)", 979oid_to_hex(&parent->commit->object.oid), 980oid_to_hex(&target->commit->object.oid)); 981/* The rest are the same as the parent */ 982blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 983*d.dstq = NULL; 984queue_blames(sb, parent, newdest); 985 986return; 987} 988 989/* 990 * The lines in blame_entry after splitting blames many times can become 991 * very small and trivial, and at some point it becomes pointless to 992 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 993 * ordinary C program, and it is not worth to say it was copied from 994 * totally unrelated file in the parent. 995 * 996 * Compute how trivial the lines in the blame_entry are. 997 */ 998unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e) 999{1000unsigned score;1001const char*cp, *ep;10021003if(e->score)1004return e->score;10051006 score =1;1007 cp =blame_nth_line(sb, e->lno);1008 ep =blame_nth_line(sb, e->lno + e->num_lines);1009while(cp < ep) {1010unsigned ch = *((unsigned char*)cp);1011if(isalnum(ch))1012 score++;1013 cp++;1014}1015 e->score = score;1016return score;1017}10181019/*1020 * best_so_far[] and potential[] are both a split of an existing blame_entry1021 * that passes blame to the parent. Maintain best_so_far the best split so1022 * far, by comparing potential and best_so_far and copying potential into1023 * bst_so_far as needed.1024 */1025static voidcopy_split_if_better(struct blame_scoreboard *sb,1026struct blame_entry *best_so_far,1027struct blame_entry *potential)1028{1029int i;10301031if(!potential[1].suspect)1032return;1033if(best_so_far[1].suspect) {1034if(blame_entry_score(sb, &potential[1]) <1035blame_entry_score(sb, &best_so_far[1]))1036return;1037}10381039for(i =0; i <3; i++)1040blame_origin_incref(potential[i].suspect);1041decref_split(best_so_far);1042memcpy(best_so_far, potential,sizeof(struct blame_entry[3]));1043}10441045/*1046 * We are looking at a part of the final image represented by1047 * ent (tlno and same are offset by ent->s_lno).1048 * tlno is where we are looking at in the final image.1049 * up to (but not including) same match preimage.1050 * plno is where we are looking at in the preimage.1051 *1052 * <-------------- final image ---------------------->1053 * <------ent------>1054 * ^tlno ^same1055 * <---------preimage----->1056 * ^plno1057 *1058 * All line numbers are 0-based.1059 */1060static voidhandle_split(struct blame_scoreboard *sb,1061struct blame_entry *ent,1062int tlno,int plno,int same,1063struct blame_origin *parent,1064struct blame_entry *split)1065{1066if(ent->num_lines <= tlno)1067return;1068if(tlno < same) {1069struct blame_entry potential[3];1070 tlno += ent->s_lno;1071 same += ent->s_lno;1072split_overlap(potential, ent, tlno, plno, same, parent);1073copy_split_if_better(sb, split, potential);1074decref_split(potential);1075}1076}10771078struct handle_split_cb_data {1079struct blame_scoreboard *sb;1080struct blame_entry *ent;1081struct blame_origin *parent;1082struct blame_entry *split;1083long plno;1084long tlno;1085};10861087static inthandle_split_cb(long start_a,long count_a,1088long start_b,long count_b,void*data)1089{1090struct handle_split_cb_data *d = data;1091handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1092 d->split);1093 d->plno = start_a + count_a;1094 d->tlno = start_b + count_b;1095return0;1096}10971098/*1099 * Find the lines from parent that are the same as ent so that1100 * we can pass blames to it. file_p has the blob contents for1101 * the parent.1102 */1103static voidfind_copy_in_blob(struct blame_scoreboard *sb,1104struct blame_entry *ent,1105struct blame_origin *parent,1106struct blame_entry *split,1107 mmfile_t *file_p)1108{1109const char*cp;1110 mmfile_t file_o;1111struct handle_split_cb_data d;11121113memset(&d,0,sizeof(d));1114 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1115/*1116 * Prepare mmfile that contains only the lines in ent.1117 */1118 cp =blame_nth_line(sb, ent->lno);1119 file_o.ptr = (char*) cp;1120 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11211122/*1123 * file_o is a part of final image we are annotating.1124 * file_p partially may match that image.1125 */1126memset(split,0,sizeof(struct blame_entry [3]));1127if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1128die("unable to generate diff (%s)",1129oid_to_hex(&parent->commit->object.oid));1130/* remainder, if any, all match the preimage */1131handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1132}11331134/* Move all blame entries from list *source that have a score smaller1135 * than score_min to the front of list *small.1136 * Returns a pointer to the link pointing to the old head of the small list.1137 */11381139static struct blame_entry **filter_small(struct blame_scoreboard *sb,1140struct blame_entry **small,1141struct blame_entry **source,1142unsigned score_min)1143{1144struct blame_entry *p = *source;1145struct blame_entry *oldsmall = *small;1146while(p) {1147if(blame_entry_score(sb, p) <= score_min) {1148*small = p;1149 small = &p->next;1150 p = *small;1151}else{1152*source = p;1153 source = &p->next;1154 p = *source;1155}1156}1157*small = oldsmall;1158*source = NULL;1159return small;1160}11611162/*1163 * See if lines currently target is suspected for can be attributed to1164 * parent.1165 */1166static voidfind_move_in_parent(struct blame_scoreboard *sb,1167struct blame_entry ***blamed,1168struct blame_entry **toosmall,1169struct blame_origin *target,1170struct blame_origin *parent)1171{1172struct blame_entry *e, split[3];1173struct blame_entry *unblamed = target->suspects;1174struct blame_entry *leftover = NULL;1175 mmfile_t file_p;11761177if(!unblamed)1178return;/* nothing remains for this target */11791180fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1181if(!file_p.ptr)1182return;11831184/* At each iteration, unblamed has a NULL-terminated list of1185 * entries that have not yet been tested for blame. leftover1186 * contains the reversed list of entries that have been tested1187 * without being assignable to the parent.1188 */1189do{1190struct blame_entry **unblamedtail = &unblamed;1191struct blame_entry *next;1192for(e = unblamed; e; e = next) {1193 next = e->next;1194find_copy_in_blob(sb, e, parent, split, &file_p);1195if(split[1].suspect &&1196 sb->move_score <blame_entry_score(sb, &split[1])) {1197split_blame(blamed, &unblamedtail, split, e);1198}else{1199 e->next = leftover;1200 leftover = e;1201}1202decref_split(split);1203}1204*unblamedtail = NULL;1205 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1206}while(unblamed);1207 target->suspects =reverse_blame(leftover, NULL);1208}12091210struct blame_list {1211struct blame_entry *ent;1212struct blame_entry split[3];1213};12141215/*1216 * Count the number of entries the target is suspected for,1217 * and prepare a list of entry and the best split.1218 */1219static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1220int*num_ents_p)1221{1222struct blame_entry *e;1223int num_ents, i;1224struct blame_list *blame_list = NULL;12251226for(e = unblamed, num_ents =0; e; e = e->next)1227 num_ents++;1228if(num_ents) {1229 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1230for(e = unblamed, i =0; e; e = e->next)1231 blame_list[i++].ent = e;1232}1233*num_ents_p = num_ents;1234return blame_list;1235}12361237/*1238 * For lines target is suspected for, see if we can find code movement1239 * across file boundary from the parent commit. porigin is the path1240 * in the parent we already tried.1241 */1242static voidfind_copy_in_parent(struct blame_scoreboard *sb,1243struct blame_entry ***blamed,1244struct blame_entry **toosmall,1245struct blame_origin *target,1246struct commit *parent,1247struct blame_origin *porigin,1248int opt)1249{1250struct diff_options diff_opts;1251int i, j;1252struct blame_list *blame_list;1253int num_ents;1254struct blame_entry *unblamed = target->suspects;1255struct blame_entry *leftover = NULL;12561257if(!unblamed)1258return;/* nothing remains for this target */12591260diff_setup(&diff_opts);1261 diff_opts.flags.recursive =1;1262 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12631264diff_setup_done(&diff_opts);12651266/* Try "find copies harder" on new path if requested;1267 * we do not want to use diffcore_rename() actually to1268 * match things up; find_copies_harder is set only to1269 * force diff_tree_oid() to feed all filepairs to diff_queue,1270 * and this code needs to be after diff_setup_done(), which1271 * usually makes find-copies-harder imply copy detection.1272 */1273if((opt & PICKAXE_BLAME_COPY_HARDEST)1274|| ((opt & PICKAXE_BLAME_COPY_HARDER)1275&& (!porigin ||strcmp(target->path, porigin->path))))1276 diff_opts.flags.find_copies_harder =1;12771278if(is_null_oid(&target->commit->object.oid))1279do_diff_cache(get_commit_tree_oid(parent), &diff_opts);1280else1281diff_tree_oid(get_commit_tree_oid(parent),1282get_commit_tree_oid(target->commit),1283"", &diff_opts);12841285if(!diff_opts.flags.find_copies_harder)1286diffcore_std(&diff_opts);12871288do{1289struct blame_entry **unblamedtail = &unblamed;1290 blame_list =setup_blame_list(unblamed, &num_ents);12911292for(i =0; i < diff_queued_diff.nr; i++) {1293struct diff_filepair *p = diff_queued_diff.queue[i];1294struct blame_origin *norigin;1295 mmfile_t file_p;1296struct blame_entry potential[3];12971298if(!DIFF_FILE_VALID(p->one))1299continue;/* does not exist in parent */1300if(S_ISGITLINK(p->one->mode))1301continue;/* ignore git links */1302if(porigin && !strcmp(p->one->path, porigin->path))1303/* find_move already dealt with this path */1304continue;13051306 norigin =get_origin(parent, p->one->path);1307oidcpy(&norigin->blob_oid, &p->one->oid);1308 norigin->mode = p->one->mode;1309fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1310if(!file_p.ptr)1311continue;13121313for(j =0; j < num_ents; j++) {1314find_copy_in_blob(sb, blame_list[j].ent,1315 norigin, potential, &file_p);1316copy_split_if_better(sb, blame_list[j].split,1317 potential);1318decref_split(potential);1319}1320blame_origin_decref(norigin);1321}13221323for(j =0; j < num_ents; j++) {1324struct blame_entry *split = blame_list[j].split;1325if(split[1].suspect &&1326 sb->copy_score <blame_entry_score(sb, &split[1])) {1327split_blame(blamed, &unblamedtail, split,1328 blame_list[j].ent);1329}else{1330 blame_list[j].ent->next = leftover;1331 leftover = blame_list[j].ent;1332}1333decref_split(split);1334}1335free(blame_list);1336*unblamedtail = NULL;1337 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1338}while(unblamed);1339 target->suspects =reverse_blame(leftover, NULL);1340diff_flush(&diff_opts);1341clear_pathspec(&diff_opts.pathspec);1342}13431344/*1345 * The blobs of origin and porigin exactly match, so everything1346 * origin is suspected for can be blamed on the parent.1347 */1348static voidpass_whole_blame(struct blame_scoreboard *sb,1349struct blame_origin *origin,struct blame_origin *porigin)1350{1351struct blame_entry *e, *suspects;13521353if(!porigin->file.ptr && origin->file.ptr) {1354/* Steal its file */1355 porigin->file = origin->file;1356 origin->file.ptr = NULL;1357}1358 suspects = origin->suspects;1359 origin->suspects = NULL;1360for(e = suspects; e; e = e->next) {1361blame_origin_incref(porigin);1362blame_origin_decref(e->suspect);1363 e->suspect = porigin;1364}1365queue_blames(sb, porigin, suspects);1366}13671368/*1369 * We pass blame from the current commit to its parents. We keep saying1370 * "parent" (and "porigin"), but what we mean is to find scapegoat to1371 * exonerate ourselves.1372 */1373static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1374int reverse)1375{1376if(!reverse) {1377if(revs->first_parent_only &&1378 commit->parents &&1379 commit->parents->next) {1380free_commit_list(commit->parents->next);1381 commit->parents->next = NULL;1382}1383return commit->parents;1384}1385returnlookup_decoration(&revs->children, &commit->object);1386}13871388static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1389{1390struct commit_list *l =first_scapegoat(revs, commit, reverse);1391returncommit_list_count(l);1392}13931394/* Distribute collected unsorted blames to the respected sorted lists1395 * in the various origins.1396 */1397static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1398{1399 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1400 compare_blame_suspect);1401while(blamed)1402{1403struct blame_origin *porigin = blamed->suspect;1404struct blame_entry *suspects = NULL;1405do{1406struct blame_entry *next = blamed->next;1407 blamed->next = suspects;1408 suspects = blamed;1409 blamed = next;1410}while(blamed && blamed->suspect == porigin);1411 suspects =reverse_blame(suspects, NULL);1412queue_blames(sb, porigin, suspects);1413}1414}14151416#define MAXSG 1614171418static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1419{1420struct rev_info *revs = sb->revs;1421int i, pass, num_sg;1422struct commit *commit = origin->commit;1423struct commit_list *sg;1424struct blame_origin *sg_buf[MAXSG];1425struct blame_origin *porigin, **sg_origin = sg_buf;1426struct blame_entry *toosmall = NULL;1427struct blame_entry *blames, **blametail = &blames;14281429 num_sg =num_scapegoats(revs, commit, sb->reverse);1430if(!num_sg)1431goto finish;1432else if(num_sg <ARRAY_SIZE(sg_buf))1433memset(sg_buf,0,sizeof(sg_buf));1434else1435 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14361437/*1438 * The first pass looks for unrenamed path to optimize for1439 * common cases, then we look for renames in the second pass.1440 */1441for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1442struct blame_origin *(*find)(struct commit *,struct blame_origin *);1443 find = pass ? find_rename : find_origin;14441445for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1446 i < num_sg && sg;1447 sg = sg->next, i++) {1448struct commit *p = sg->item;1449int j, same;14501451if(sg_origin[i])1452continue;1453if(parse_commit(p))1454continue;1455 porigin =find(p, origin);1456if(!porigin)1457continue;1458if(!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1459pass_whole_blame(sb, origin, porigin);1460blame_origin_decref(porigin);1461goto finish;1462}1463for(j = same =0; j < i; j++)1464if(sg_origin[j] &&1465!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1466 same =1;1467break;1468}1469if(!same)1470 sg_origin[i] = porigin;1471else1472blame_origin_decref(porigin);1473}1474}14751476 sb->num_commits++;1477for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1478 i < num_sg && sg;1479 sg = sg->next, i++) {1480struct blame_origin *porigin = sg_origin[i];1481if(!porigin)1482continue;1483if(!origin->previous) {1484blame_origin_incref(porigin);1485 origin->previous = porigin;1486}1487pass_blame_to_parent(sb, origin, porigin);1488if(!origin->suspects)1489goto finish;1490}14911492/*1493 * Optionally find moves in parents' files.1494 */1495if(opt & PICKAXE_BLAME_MOVE) {1496filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1497if(origin->suspects) {1498for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1499 i < num_sg && sg;1500 sg = sg->next, i++) {1501struct blame_origin *porigin = sg_origin[i];1502if(!porigin)1503continue;1504find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1505if(!origin->suspects)1506break;1507}1508}1509}15101511/*1512 * Optionally find copies from parents' files.1513 */1514if(opt & PICKAXE_BLAME_COPY) {1515if(sb->copy_score > sb->move_score)1516filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1517else if(sb->copy_score < sb->move_score) {1518 origin->suspects =blame_merge(origin->suspects, toosmall);1519 toosmall = NULL;1520filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1521}1522if(!origin->suspects)1523goto finish;15241525for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1526 i < num_sg && sg;1527 sg = sg->next, i++) {1528struct blame_origin *porigin = sg_origin[i];1529find_copy_in_parent(sb, &blametail, &toosmall,1530 origin, sg->item, porigin, opt);1531if(!origin->suspects)1532goto finish;1533}1534}15351536finish:1537*blametail = NULL;1538distribute_blame(sb, blames);1539/*1540 * prepend toosmall to origin->suspects1541 *1542 * There is no point in sorting: this ends up on a big1543 * unsorted list in the caller anyway.1544 */1545if(toosmall) {1546struct blame_entry **tail = &toosmall;1547while(*tail)1548 tail = &(*tail)->next;1549*tail = origin->suspects;1550 origin->suspects = toosmall;1551}1552for(i =0; i < num_sg; i++) {1553if(sg_origin[i]) {1554drop_origin_blob(sg_origin[i]);1555blame_origin_decref(sg_origin[i]);1556}1557}1558drop_origin_blob(origin);1559if(sg_buf != sg_origin)1560free(sg_origin);1561}15621563/*1564 * The main loop -- while we have blobs with lines whose true origin1565 * is still unknown, pick one blob, and allow its lines to pass blames1566 * to its parents. */1567voidassign_blame(struct blame_scoreboard *sb,int opt)1568{1569struct rev_info *revs = sb->revs;1570struct commit *commit =prio_queue_get(&sb->commits);15711572while(commit) {1573struct blame_entry *ent;1574struct blame_origin *suspect =get_blame_suspects(commit);15751576/* find one suspect to break down */1577while(suspect && !suspect->suspects)1578 suspect = suspect->next;15791580if(!suspect) {1581 commit =prio_queue_get(&sb->commits);1582continue;1583}15841585assert(commit == suspect->commit);15861587/*1588 * We will use this suspect later in the loop,1589 * so hold onto it in the meantime.1590 */1591blame_origin_incref(suspect);1592parse_commit(commit);1593if(sb->reverse ||1594(!(commit->object.flags & UNINTERESTING) &&1595!(revs->max_age != -1&& commit->date < revs->max_age)))1596pass_blame(sb, suspect, opt);1597else{1598 commit->object.flags |= UNINTERESTING;1599if(commit->object.parsed)1600mark_parents_uninteresting(commit);1601}1602/* treat root commit as boundary */1603if(!commit->parents && !sb->show_root)1604 commit->object.flags |= UNINTERESTING;16051606/* Take responsibility for the remaining entries */1607 ent = suspect->suspects;1608if(ent) {1609 suspect->guilty =1;1610for(;;) {1611struct blame_entry *next = ent->next;1612if(sb->found_guilty_entry)1613 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1614if(next) {1615 ent = next;1616continue;1617}1618 ent->next = sb->ent;1619 sb->ent = suspect->suspects;1620 suspect->suspects = NULL;1621break;1622}1623}1624blame_origin_decref(suspect);16251626if(sb->debug)/* sanity */1627sanity_check_refcnt(sb);1628}1629}16301631static const char*get_next_line(const char*start,const char*end)1632{1633const char*nl =memchr(start,'\n', end - start);1634return nl ? nl +1: end;1635}16361637/*1638 * To allow quick access to the contents of nth line in the1639 * final image, prepare an index in the scoreboard.1640 */1641static intprepare_lines(struct blame_scoreboard *sb)1642{1643const char*buf = sb->final_buf;1644unsigned long len = sb->final_buf_size;1645const char*end = buf + len;1646const char*p;1647int*lineno;1648int num =0;16491650for(p = buf; p < end; p =get_next_line(p, end))1651 num++;16521653ALLOC_ARRAY(sb->lineno, num +1);1654 lineno = sb->lineno;16551656for(p = buf; p < end; p =get_next_line(p, end))1657*lineno++ = p - buf;16581659*lineno = len;16601661 sb->num_lines = num;1662return sb->num_lines;1663}16641665static struct commit *find_single_final(struct rev_info *revs,1666const char**name_p)1667{1668int i;1669struct commit *found = NULL;1670const char*name = NULL;16711672for(i =0; i < revs->pending.nr; i++) {1673struct object *obj = revs->pending.objects[i].item;1674if(obj->flags & UNINTERESTING)1675continue;1676 obj =deref_tag(the_repository, obj, NULL,0);1677if(obj->type != OBJ_COMMIT)1678die("Non commit%s?", revs->pending.objects[i].name);1679if(found)1680die("More than one commit to dig from%sand%s?",1681 revs->pending.objects[i].name, name);1682 found = (struct commit *)obj;1683 name = revs->pending.objects[i].name;1684}1685if(name_p)1686*name_p =xstrdup_or_null(name);1687return found;1688}16891690static struct commit *dwim_reverse_initial(struct rev_info *revs,1691const char**name_p)1692{1693/*1694 * DWIM "git blame --reverse ONE -- PATH" as1695 * "git blame --reverse ONE..HEAD -- PATH" but only do so1696 * when it makes sense.1697 */1698struct object *obj;1699struct commit *head_commit;1700struct object_id head_oid;17011702if(revs->pending.nr !=1)1703return NULL;17041705/* Is that sole rev a committish? */1706 obj = revs->pending.objects[0].item;1707 obj =deref_tag(the_repository, obj, NULL,0);1708if(obj->type != OBJ_COMMIT)1709return NULL;17101711/* Do we have HEAD? */1712if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1713return NULL;1714 head_commit =lookup_commit_reference_gently(the_repository,1715&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(the_repository, 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}