1#include"cache.h" 2#include"refs.h" 3#include"object-store.h" 4#include"cache-tree.h" 5#include"mergesort.h" 6#include"diff.h" 7#include"diffcore.h" 8#include"tag.h" 9#include"blame.h" 10#include"alloc.h" 11#include"commit-slab.h" 12 13define_commit_slab(blame_suspects,struct blame_origin *); 14static struct blame_suspects blame_suspects; 15 16struct blame_origin *get_blame_suspects(struct commit *commit) 17{ 18struct blame_origin **result; 19 20 result =blame_suspects_peek(&blame_suspects, commit); 21 22return result ? *result : NULL; 23} 24 25static voidset_blame_suspects(struct commit *commit,struct blame_origin *origin) 26{ 27*blame_suspects_at(&blame_suspects, commit) = origin; 28} 29 30voidblame_origin_decref(struct blame_origin *o) 31{ 32if(o && --o->refcnt <=0) { 33struct blame_origin *p, *l = NULL; 34if(o->previous) 35blame_origin_decref(o->previous); 36free(o->file.ptr); 37/* Should be present exactly once in commit chain */ 38for(p =get_blame_suspects(o->commit); p; l = p, p = p->next) { 39if(p == o) { 40if(l) 41 l->next = p->next; 42else 43set_blame_suspects(o->commit, p->next); 44free(o); 45return; 46} 47} 48die("internal error in blame_origin_decref"); 49} 50} 51 52/* 53 * Given a commit and a path in it, create a new origin structure. 54 * The callers that add blame to the scoreboard should use 55 * get_origin() to obtain shared, refcounted copy instead of calling 56 * this function directly. 57 */ 58static struct blame_origin *make_origin(struct commit *commit,const char*path) 59{ 60struct blame_origin *o; 61FLEX_ALLOC_STR(o, path, path); 62 o->commit = commit; 63 o->refcnt =1; 64 o->next =get_blame_suspects(commit); 65set_blame_suspects(commit, o); 66return o; 67} 68 69/* 70 * Locate an existing origin or create a new one. 71 * This moves the origin to front position in the commit util list. 72 */ 73static struct blame_origin *get_origin(struct commit *commit,const char*path) 74{ 75struct blame_origin *o, *l; 76 77for(o =get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) { 78if(!strcmp(o->path, path)) { 79/* bump to front */ 80if(l) { 81 l->next = o->next; 82 o->next =get_blame_suspects(commit); 83set_blame_suspects(commit, o); 84} 85returnblame_origin_incref(o); 86} 87} 88returnmake_origin(commit, path); 89} 90 91 92 93static voidverify_working_tree_path(struct repository *r, 94struct commit *work_tree,const char*path) 95{ 96struct commit_list *parents; 97int pos; 98 99for(parents = work_tree->parents; parents; parents = parents->next) { 100const struct object_id *commit_oid = &parents->item->object.oid; 101struct object_id blob_oid; 102unsigned short mode; 103 104if(!get_tree_entry(commit_oid, path, &blob_oid, &mode) && 105oid_object_info(r, &blob_oid, NULL) == OBJ_BLOB) 106return; 107} 108 109 pos =index_name_pos(r->index, path,strlen(path)); 110if(pos >=0) 111;/* path is in the index */ 112else if(-1- pos < r->index->cache_nr && 113!strcmp(r->index->cache[-1- pos]->name, path)) 114;/* path is in the index, unmerged */ 115else 116die("no such path '%s' in HEAD", path); 117} 118 119static struct commit_list **append_parent(struct repository *r, 120struct commit_list **tail, 121const struct object_id *oid) 122{ 123struct commit *parent; 124 125 parent =lookup_commit_reference(r, oid); 126if(!parent) 127die("no such commit%s",oid_to_hex(oid)); 128return&commit_list_insert(parent, tail)->next; 129} 130 131static voidappend_merge_parents(struct repository *r, 132struct commit_list **tail) 133{ 134int merge_head; 135struct strbuf line = STRBUF_INIT; 136 137 merge_head =open(git_path_merge_head(r), O_RDONLY); 138if(merge_head <0) { 139if(errno == ENOENT) 140return; 141die("cannot open '%s' for reading", 142git_path_merge_head(r)); 143} 144 145while(!strbuf_getwholeline_fd(&line, merge_head,'\n')) { 146struct object_id oid; 147if(line.len < GIT_SHA1_HEXSZ ||get_oid_hex(line.buf, &oid)) 148die("unknown line in '%s':%s", 149git_path_merge_head(r), line.buf); 150 tail =append_parent(r, tail, &oid); 151} 152close(merge_head); 153strbuf_release(&line); 154} 155 156/* 157 * This isn't as simple as passing sb->buf and sb->len, because we 158 * want to transfer ownership of the buffer to the commit (so we 159 * must use detach). 160 */ 161static voidset_commit_buffer_from_strbuf(struct repository *r, 162struct commit *c, 163struct strbuf *sb) 164{ 165size_t len; 166void*buf =strbuf_detach(sb, &len); 167set_commit_buffer(r, c, buf, len); 168} 169 170/* 171 * Prepare a dummy commit that represents the work tree (or staged) item. 172 * Note that annotating work tree item never works in the reverse. 173 */ 174static struct commit *fake_working_tree_commit(struct repository *r, 175struct diff_options *opt, 176const char*path, 177const char*contents_from) 178{ 179struct commit *commit; 180struct blame_origin *origin; 181struct commit_list **parent_tail, *parent; 182struct object_id head_oid; 183struct strbuf buf = STRBUF_INIT; 184const char*ident; 185time_t now; 186int len; 187struct cache_entry *ce; 188unsigned mode; 189struct strbuf msg = STRBUF_INIT; 190 191repo_read_index(r); 192time(&now); 193 commit =alloc_commit_node(r); 194 commit->object.parsed =1; 195 commit->date = now; 196 parent_tail = &commit->parents; 197 198if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) 199die("no such ref: HEAD"); 200 201 parent_tail =append_parent(r, parent_tail, &head_oid); 202append_merge_parents(r, parent_tail); 203verify_working_tree_path(r, commit, path); 204 205 origin =make_origin(commit, path); 206 207 ident =fmt_ident("Not Committed Yet","not.committed.yet", 208 WANT_BLANK_IDENT, NULL,0); 209strbuf_addstr(&msg,"tree 0000000000000000000000000000000000000000\n"); 210for(parent = commit->parents; parent; parent = parent->next) 211strbuf_addf(&msg,"parent%s\n", 212oid_to_hex(&parent->item->object.oid)); 213strbuf_addf(&msg, 214"author%s\n" 215"committer%s\n\n" 216"Version of%sfrom%s\n", 217 ident, ident, path, 218(!contents_from ? path : 219(!strcmp(contents_from,"-") ?"standard input": contents_from))); 220set_commit_buffer_from_strbuf(r, commit, &msg); 221 222if(!contents_from ||strcmp("-", contents_from)) { 223struct stat st; 224const char*read_from; 225char*buf_ptr; 226unsigned long buf_len; 227 228if(contents_from) { 229if(stat(contents_from, &st) <0) 230die_errno("Cannot stat '%s'", contents_from); 231 read_from = contents_from; 232} 233else{ 234if(lstat(path, &st) <0) 235die_errno("Cannot lstat '%s'", path); 236 read_from = path; 237} 238 mode =canon_mode(st.st_mode); 239 240switch(st.st_mode & S_IFMT) { 241case S_IFREG: 242if(opt->flags.allow_textconv && 243textconv_object(r, read_from, mode, &null_oid,0, &buf_ptr, &buf_len)) 244strbuf_attach(&buf, buf_ptr, buf_len, buf_len +1); 245else if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) 246die_errno("cannot open or read '%s'", read_from); 247break; 248case S_IFLNK: 249if(strbuf_readlink(&buf, read_from, st.st_size) <0) 250die_errno("cannot readlink '%s'", read_from); 251break; 252default: 253die("unsupported file type%s", read_from); 254} 255} 256else{ 257/* Reading from stdin */ 258 mode =0; 259if(strbuf_read(&buf,0,0) <0) 260die_errno("failed to read from stdin"); 261} 262convert_to_git(r->index, path, buf.buf, buf.len, &buf,0); 263 origin->file.ptr = buf.buf; 264 origin->file.size = buf.len; 265pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); 266 267/* 268 * Read the current index, replace the path entry with 269 * origin->blob_sha1 without mucking with its mode or type 270 * bits; we are not going to write this index out -- we just 271 * want to run "diff-index --cached". 272 */ 273discard_index(r->index); 274repo_read_index(r); 275 276 len =strlen(path); 277if(!mode) { 278int pos =index_name_pos(r->index, path, len); 279if(0<= pos) 280 mode = r->index->cache[pos]->ce_mode; 281else 282/* Let's not bother reading from HEAD tree */ 283 mode = S_IFREG |0644; 284} 285 ce =make_empty_cache_entry(r->index, len); 286oidcpy(&ce->oid, &origin->blob_oid); 287memcpy(ce->name, path, len); 288 ce->ce_flags =create_ce_flags(0); 289 ce->ce_namelen = len; 290 ce->ce_mode =create_ce_mode(mode); 291add_index_entry(r->index, ce, 292 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 293 294cache_tree_invalidate_path(r->index, path); 295 296return commit; 297} 298 299 300 301static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 302 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 303{ 304 xpparam_t xpp = {0}; 305 xdemitconf_t xecfg = {0}; 306 xdemitcb_t ecb = {NULL}; 307 308 xpp.flags = xdl_opts; 309 xecfg.hunk_func = hunk_func; 310 ecb.priv = cb_data; 311returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 312} 313 314/* 315 * Given an origin, prepare mmfile_t structure to be used by the 316 * diff machinery 317 */ 318static voidfill_origin_blob(struct diff_options *opt, 319struct blame_origin *o, mmfile_t *file,int*num_read_blob) 320{ 321if(!o->file.ptr) { 322enum object_type type; 323unsigned long file_size; 324 325(*num_read_blob)++; 326if(opt->flags.allow_textconv && 327textconv_object(opt->repo, o->path, o->mode, 328&o->blob_oid,1, &file->ptr, &file_size)) 329; 330else 331 file->ptr =read_object_file(&o->blob_oid, &type, 332&file_size); 333 file->size = file_size; 334 335if(!file->ptr) 336die("Cannot read blob%sfor path%s", 337oid_to_hex(&o->blob_oid), 338 o->path); 339 o->file = *file; 340} 341else 342*file = o->file; 343} 344 345static voiddrop_origin_blob(struct blame_origin *o) 346{ 347FREE_AND_NULL(o->file.ptr); 348} 349 350/* 351 * Any merge of blames happens on lists of blames that arrived via 352 * different parents in a single suspect. In this case, we want to 353 * sort according to the suspect line numbers as opposed to the final 354 * image line numbers. The function body is somewhat longish because 355 * it avoids unnecessary writes. 356 */ 357 358static struct blame_entry *blame_merge(struct blame_entry *list1, 359struct blame_entry *list2) 360{ 361struct blame_entry *p1 = list1, *p2 = list2, 362**tail = &list1; 363 364if(!p1) 365return p2; 366if(!p2) 367return p1; 368 369if(p1->s_lno <= p2->s_lno) { 370do{ 371 tail = &p1->next; 372if((p1 = *tail) == NULL) { 373*tail = p2; 374return list1; 375} 376}while(p1->s_lno <= p2->s_lno); 377} 378for(;;) { 379*tail = p2; 380do{ 381 tail = &p2->next; 382if((p2 = *tail) == NULL) { 383*tail = p1; 384return list1; 385} 386}while(p1->s_lno > p2->s_lno); 387*tail = p1; 388do{ 389 tail = &p1->next; 390if((p1 = *tail) == NULL) { 391*tail = p2; 392return list1; 393} 394}while(p1->s_lno <= p2->s_lno); 395} 396} 397 398static void*get_next_blame(const void*p) 399{ 400return((struct blame_entry *)p)->next; 401} 402 403static voidset_next_blame(void*p1,void*p2) 404{ 405((struct blame_entry *)p1)->next = p2; 406} 407 408/* 409 * Final image line numbers are all different, so we don't need a 410 * three-way comparison here. 411 */ 412 413static intcompare_blame_final(const void*p1,const void*p2) 414{ 415return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 416?1: -1; 417} 418 419static intcompare_blame_suspect(const void*p1,const void*p2) 420{ 421const struct blame_entry *s1 = p1, *s2 = p2; 422/* 423 * to allow for collating suspects, we sort according to the 424 * respective pointer value as the primary sorting criterion. 425 * The actual relation is pretty unimportant as long as it 426 * establishes a total order. Comparing as integers gives us 427 * that. 428 */ 429if(s1->suspect != s2->suspect) 430return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 431if(s1->s_lno == s2->s_lno) 432return0; 433return s1->s_lno > s2->s_lno ?1: -1; 434} 435 436voidblame_sort_final(struct blame_scoreboard *sb) 437{ 438 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 439 compare_blame_final); 440} 441 442static intcompare_commits_by_reverse_commit_date(const void*a, 443const void*b, 444void*c) 445{ 446return-compare_commits_by_commit_date(a, b, c); 447} 448 449/* 450 * For debugging -- origin is refcounted, and this asserts that 451 * we do not underflow. 452 */ 453static voidsanity_check_refcnt(struct blame_scoreboard *sb) 454{ 455int baa =0; 456struct blame_entry *ent; 457 458for(ent = sb->ent; ent; ent = ent->next) { 459/* Nobody should have zero or negative refcnt */ 460if(ent->suspect->refcnt <=0) { 461fprintf(stderr,"%sin%shas negative refcnt%d\n", 462 ent->suspect->path, 463oid_to_hex(&ent->suspect->commit->object.oid), 464 ent->suspect->refcnt); 465 baa =1; 466} 467} 468if(baa) 469 sb->on_sanity_fail(sb, baa); 470} 471 472/* 473 * If two blame entries that are next to each other came from 474 * contiguous lines in the same origin (i.e. <commit, path> pair), 475 * merge them together. 476 */ 477voidblame_coalesce(struct blame_scoreboard *sb) 478{ 479struct blame_entry *ent, *next; 480 481for(ent = sb->ent; ent && (next = ent->next); ent = next) { 482if(ent->suspect == next->suspect && 483 ent->s_lno + ent->num_lines == next->s_lno) { 484 ent->num_lines += next->num_lines; 485 ent->next = next->next; 486blame_origin_decref(next->suspect); 487free(next); 488 ent->score =0; 489 next = ent;/* again */ 490} 491} 492 493if(sb->debug)/* sanity */ 494sanity_check_refcnt(sb); 495} 496 497/* 498 * Merge the given sorted list of blames into a preexisting origin. 499 * If there were no previous blames to that commit, it is entered into 500 * the commit priority queue of the score board. 501 */ 502 503static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 504struct blame_entry *sorted) 505{ 506if(porigin->suspects) 507 porigin->suspects =blame_merge(porigin->suspects, sorted); 508else{ 509struct blame_origin *o; 510for(o =get_blame_suspects(porigin->commit); o; o = o->next) { 511if(o->suspects) { 512 porigin->suspects = sorted; 513return; 514} 515} 516 porigin->suspects = sorted; 517prio_queue_put(&sb->commits, porigin->commit); 518} 519} 520 521/* 522 * Fill the blob_sha1 field of an origin if it hasn't, so that later 523 * call to fill_origin_blob() can use it to locate the data. blob_sha1 524 * for an origin is also used to pass the blame for the entire file to 525 * the parent to detect the case where a child's blob is identical to 526 * that of its parent's. 527 * 528 * This also fills origin->mode for corresponding tree path. 529 */ 530static intfill_blob_sha1_and_mode(struct repository *r, 531struct blame_origin *origin) 532{ 533if(!is_null_oid(&origin->blob_oid)) 534return0; 535if(get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) 536goto error_out; 537if(oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB) 538goto error_out; 539return0; 540 error_out: 541oidclr(&origin->blob_oid); 542 origin->mode = S_IFINVALID; 543return-1; 544} 545 546/* 547 * We have an origin -- check if the same path exists in the 548 * parent and return an origin structure to represent it. 549 */ 550static struct blame_origin *find_origin(struct repository *r, 551struct commit *parent, 552struct blame_origin *origin) 553{ 554struct blame_origin *porigin; 555struct diff_options diff_opts; 556const char*paths[2]; 557 558/* First check any existing origins */ 559for(porigin =get_blame_suspects(parent); porigin; porigin = porigin->next) 560if(!strcmp(porigin->path, origin->path)) { 561/* 562 * The same path between origin and its parent 563 * without renaming -- the most common case. 564 */ 565returnblame_origin_incref(porigin); 566} 567 568/* See if the origin->path is different between parent 569 * and origin first. Most of the time they are the 570 * same and diff-tree is fairly efficient about this. 571 */ 572repo_diff_setup(r, &diff_opts); 573 diff_opts.flags.recursive =1; 574 diff_opts.detect_rename =0; 575 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 576 paths[0] = origin->path; 577 paths[1] = NULL; 578 579parse_pathspec(&diff_opts.pathspec, 580 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 581 PATHSPEC_LITERAL_PATH,"", paths); 582diff_setup_done(&diff_opts); 583 584if(is_null_oid(&origin->commit->object.oid)) 585do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 586else 587diff_tree_oid(get_commit_tree_oid(parent), 588get_commit_tree_oid(origin->commit), 589"", &diff_opts); 590diffcore_std(&diff_opts); 591 592if(!diff_queued_diff.nr) { 593/* The path is the same as parent */ 594 porigin =get_origin(parent, origin->path); 595oidcpy(&porigin->blob_oid, &origin->blob_oid); 596 porigin->mode = origin->mode; 597}else{ 598/* 599 * Since origin->path is a pathspec, if the parent 600 * commit had it as a directory, we will see a whole 601 * bunch of deletion of files in the directory that we 602 * do not care about. 603 */ 604int i; 605struct diff_filepair *p = NULL; 606for(i =0; i < diff_queued_diff.nr; i++) { 607const char*name; 608 p = diff_queued_diff.queue[i]; 609 name = p->one->path ? p->one->path : p->two->path; 610if(!strcmp(name, origin->path)) 611break; 612} 613if(!p) 614die("internal error in blame::find_origin"); 615switch(p->status) { 616default: 617die("internal error in blame::find_origin (%c)", 618 p->status); 619case'M': 620 porigin =get_origin(parent, origin->path); 621oidcpy(&porigin->blob_oid, &p->one->oid); 622 porigin->mode = p->one->mode; 623break; 624case'A': 625case'T': 626/* Did not exist in parent, or type changed */ 627break; 628} 629} 630diff_flush(&diff_opts); 631clear_pathspec(&diff_opts.pathspec); 632return porigin; 633} 634 635/* 636 * We have an origin -- find the path that corresponds to it in its 637 * parent and return an origin structure to represent it. 638 */ 639static struct blame_origin *find_rename(struct repository *r, 640struct commit *parent, 641struct blame_origin *origin) 642{ 643struct blame_origin *porigin = NULL; 644struct diff_options diff_opts; 645int i; 646 647repo_diff_setup(r, &diff_opts); 648 diff_opts.flags.recursive =1; 649 diff_opts.detect_rename = DIFF_DETECT_RENAME; 650 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 651 diff_opts.single_follow = origin->path; 652diff_setup_done(&diff_opts); 653 654if(is_null_oid(&origin->commit->object.oid)) 655do_diff_cache(get_commit_tree_oid(parent), &diff_opts); 656else 657diff_tree_oid(get_commit_tree_oid(parent), 658get_commit_tree_oid(origin->commit), 659"", &diff_opts); 660diffcore_std(&diff_opts); 661 662for(i =0; i < diff_queued_diff.nr; i++) { 663struct diff_filepair *p = diff_queued_diff.queue[i]; 664if((p->status =='R'|| p->status =='C') && 665!strcmp(p->two->path, origin->path)) { 666 porigin =get_origin(parent, p->one->path); 667oidcpy(&porigin->blob_oid, &p->one->oid); 668 porigin->mode = p->one->mode; 669break; 670} 671} 672diff_flush(&diff_opts); 673clear_pathspec(&diff_opts.pathspec); 674return porigin; 675} 676 677/* 678 * Append a new blame entry to a given output queue. 679 */ 680static voidadd_blame_entry(struct blame_entry ***queue, 681const struct blame_entry *src) 682{ 683struct blame_entry *e =xmalloc(sizeof(*e)); 684memcpy(e, src,sizeof(*e)); 685blame_origin_incref(e->suspect); 686 687 e->next = **queue; 688**queue = e; 689*queue = &e->next; 690} 691 692/* 693 * src typically is on-stack; we want to copy the information in it to 694 * a malloced blame_entry that gets added to the given queue. The 695 * origin of dst loses a refcnt. 696 */ 697static voiddup_entry(struct blame_entry ***queue, 698struct blame_entry *dst,struct blame_entry *src) 699{ 700blame_origin_incref(src->suspect); 701blame_origin_decref(dst->suspect); 702memcpy(dst, src,sizeof(*src)); 703 dst->next = **queue; 704**queue = dst; 705*queue = &dst->next; 706} 707 708const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 709{ 710return sb->final_buf + sb->lineno[lno]; 711} 712 713/* 714 * It is known that lines between tlno to same came from parent, and e 715 * has an overlap with that range. it also is known that parent's 716 * line plno corresponds to e's line tlno. 717 * 718 * <---- e -----> 719 * <------> 720 * <------------> 721 * <------------> 722 * <------------------> 723 * 724 * Split e into potentially three parts; before this chunk, the chunk 725 * to be blamed for the parent, and after that portion. 726 */ 727static voidsplit_overlap(struct blame_entry *split, 728struct blame_entry *e, 729int tlno,int plno,int same, 730struct blame_origin *parent) 731{ 732int chunk_end_lno; 733memset(split,0,sizeof(struct blame_entry [3])); 734 735if(e->s_lno < tlno) { 736/* there is a pre-chunk part not blamed on parent */ 737 split[0].suspect =blame_origin_incref(e->suspect); 738 split[0].lno = e->lno; 739 split[0].s_lno = e->s_lno; 740 split[0].num_lines = tlno - e->s_lno; 741 split[1].lno = e->lno + tlno - e->s_lno; 742 split[1].s_lno = plno; 743} 744else{ 745 split[1].lno = e->lno; 746 split[1].s_lno = plno + (e->s_lno - tlno); 747} 748 749if(same < e->s_lno + e->num_lines) { 750/* there is a post-chunk part not blamed on parent */ 751 split[2].suspect =blame_origin_incref(e->suspect); 752 split[2].lno = e->lno + (same - e->s_lno); 753 split[2].s_lno = e->s_lno + (same - e->s_lno); 754 split[2].num_lines = e->s_lno + e->num_lines - same; 755 chunk_end_lno = split[2].lno; 756} 757else 758 chunk_end_lno = e->lno + e->num_lines; 759 split[1].num_lines = chunk_end_lno - split[1].lno; 760 761/* 762 * if it turns out there is nothing to blame the parent for, 763 * forget about the splitting. !split[1].suspect signals this. 764 */ 765if(split[1].num_lines <1) 766return; 767 split[1].suspect =blame_origin_incref(parent); 768} 769 770/* 771 * split_overlap() divided an existing blame e into up to three parts 772 * in split. Any assigned blame is moved to queue to 773 * reflect the split. 774 */ 775static voidsplit_blame(struct blame_entry ***blamed, 776struct blame_entry ***unblamed, 777struct blame_entry *split, 778struct blame_entry *e) 779{ 780if(split[0].suspect && split[2].suspect) { 781/* The first part (reuse storage for the existing entry e) */ 782dup_entry(unblamed, e, &split[0]); 783 784/* The last part -- me */ 785add_blame_entry(unblamed, &split[2]); 786 787/* ... and the middle part -- parent */ 788add_blame_entry(blamed, &split[1]); 789} 790else if(!split[0].suspect && !split[2].suspect) 791/* 792 * The parent covers the entire area; reuse storage for 793 * e and replace it with the parent. 794 */ 795dup_entry(blamed, e, &split[1]); 796else if(split[0].suspect) { 797/* me and then parent */ 798dup_entry(unblamed, e, &split[0]); 799add_blame_entry(blamed, &split[1]); 800} 801else{ 802/* parent and then me */ 803dup_entry(blamed, e, &split[1]); 804add_blame_entry(unblamed, &split[2]); 805} 806} 807 808/* 809 * After splitting the blame, the origins used by the 810 * on-stack blame_entry should lose one refcnt each. 811 */ 812static voiddecref_split(struct blame_entry *split) 813{ 814int i; 815 816for(i =0; i <3; i++) 817blame_origin_decref(split[i].suspect); 818} 819 820/* 821 * reverse_blame reverses the list given in head, appending tail. 822 * That allows us to build lists in reverse order, then reverse them 823 * afterwards. This can be faster than building the list in proper 824 * order right away. The reason is that building in proper order 825 * requires writing a link in the _previous_ element, while building 826 * in reverse order just requires placing the list head into the 827 * _current_ element. 828 */ 829 830static struct blame_entry *reverse_blame(struct blame_entry *head, 831struct blame_entry *tail) 832{ 833while(head) { 834struct blame_entry *next = head->next; 835 head->next = tail; 836 tail = head; 837 head = next; 838} 839return tail; 840} 841 842/* 843 * Process one hunk from the patch between the current suspect for 844 * blame_entry e and its parent. This first blames any unfinished 845 * entries before the chunk (which is where target and parent start 846 * differing) on the parent, and then splits blame entries at the 847 * start and at the end of the difference region. Since use of -M and 848 * -C options may lead to overlapping/duplicate source line number 849 * ranges, all we can rely on from sorting/merging is the order of the 850 * first suspect line number. 851 */ 852static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 853int tlno,int offset,int same, 854struct blame_origin *parent) 855{ 856struct blame_entry *e = **srcq; 857struct blame_entry *samep = NULL, *diffp = NULL; 858 859while(e && e->s_lno < tlno) { 860struct blame_entry *next = e->next; 861/* 862 * current record starts before differing portion. If 863 * it reaches into it, we need to split it up and 864 * examine the second part separately. 865 */ 866if(e->s_lno + e->num_lines > tlno) { 867/* Move second half to a new record */ 868int len = tlno - e->s_lno; 869struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 870 n->suspect = e->suspect; 871 n->lno = e->lno + len; 872 n->s_lno = e->s_lno + len; 873 n->num_lines = e->num_lines - len; 874 e->num_lines = len; 875 e->score =0; 876/* Push new record to diffp */ 877 n->next = diffp; 878 diffp = n; 879}else 880blame_origin_decref(e->suspect); 881/* Pass blame for everything before the differing 882 * chunk to the parent */ 883 e->suspect =blame_origin_incref(parent); 884 e->s_lno += offset; 885 e->next = samep; 886 samep = e; 887 e = next; 888} 889/* 890 * As we don't know how much of a common stretch after this 891 * diff will occur, the currently blamed parts are all that we 892 * can assign to the parent for now. 893 */ 894 895if(samep) { 896**dstq =reverse_blame(samep, **dstq); 897*dstq = &samep->next; 898} 899/* 900 * Prepend the split off portions: everything after e starts 901 * after the blameable portion. 902 */ 903 e =reverse_blame(diffp, e); 904 905/* 906 * Now retain records on the target while parts are different 907 * from the parent. 908 */ 909 samep = NULL; 910 diffp = NULL; 911while(e && e->s_lno < same) { 912struct blame_entry *next = e->next; 913 914/* 915 * If current record extends into sameness, need to split. 916 */ 917if(e->s_lno + e->num_lines > same) { 918/* 919 * Move second half to a new record to be 920 * processed by later chunks 921 */ 922int len = same - e->s_lno; 923struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 924 n->suspect =blame_origin_incref(e->suspect); 925 n->lno = e->lno + len; 926 n->s_lno = e->s_lno + len; 927 n->num_lines = e->num_lines - len; 928 e->num_lines = len; 929 e->score =0; 930/* Push new record to samep */ 931 n->next = samep; 932 samep = n; 933} 934 e->next = diffp; 935 diffp = e; 936 e = next; 937} 938**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 939/* Move across elements that are in the unblamable portion */ 940if(diffp) 941*srcq = &diffp->next; 942} 943 944struct blame_chunk_cb_data { 945struct blame_origin *parent; 946long offset; 947struct blame_entry **dstq; 948struct blame_entry **srcq; 949}; 950 951/* diff chunks are from parent to target */ 952static intblame_chunk_cb(long start_a,long count_a, 953long start_b,long count_b,void*data) 954{ 955struct blame_chunk_cb_data *d = data; 956if(start_a - start_b != d->offset) 957die("internal error in blame::blame_chunk_cb"); 958blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 959 start_b + count_b, d->parent); 960 d->offset = start_a + count_a - (start_b + count_b); 961return0; 962} 963 964/* 965 * We are looking at the origin 'target' and aiming to pass blame 966 * for the lines it is suspected to its parent. Run diff to find 967 * which lines came from parent and pass blame for them. 968 */ 969static voidpass_blame_to_parent(struct blame_scoreboard *sb, 970struct blame_origin *target, 971struct blame_origin *parent) 972{ 973 mmfile_t file_p, file_o; 974struct blame_chunk_cb_data d; 975struct blame_entry *newdest = NULL; 976 977if(!target->suspects) 978return;/* nothing remains for this target */ 979 980 d.parent = parent; 981 d.offset =0; 982 d.dstq = &newdest; d.srcq = &target->suspects; 983 984fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 985fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 986 sb->num_get_patch++; 987 988if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 989die("unable to generate diff (%s->%s)", 990oid_to_hex(&parent->commit->object.oid), 991oid_to_hex(&target->commit->object.oid)); 992/* The rest are the same as the parent */ 993blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 994*d.dstq = NULL; 995queue_blames(sb, parent, newdest); 996 997return; 998} 9991000/*1001 * The lines in blame_entry after splitting blames many times can become1002 * very small and trivial, and at some point it becomes pointless to1003 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any1004 * ordinary C program, and it is not worth to say it was copied from1005 * totally unrelated file in the parent.1006 *1007 * Compute how trivial the lines in the blame_entry are.1008 */1009unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e)1010{1011unsigned score;1012const char*cp, *ep;10131014if(e->score)1015return e->score;10161017 score =1;1018 cp =blame_nth_line(sb, e->lno);1019 ep =blame_nth_line(sb, e->lno + e->num_lines);1020while(cp < ep) {1021unsigned ch = *((unsigned char*)cp);1022if(isalnum(ch))1023 score++;1024 cp++;1025}1026 e->score = score;1027return score;1028}10291030/*1031 * best_so_far[] and potential[] are both a split of an existing blame_entry1032 * that passes blame to the parent. Maintain best_so_far the best split so1033 * far, by comparing potential and best_so_far and copying potential into1034 * bst_so_far as needed.1035 */1036static voidcopy_split_if_better(struct blame_scoreboard *sb,1037struct blame_entry *best_so_far,1038struct blame_entry *potential)1039{1040int i;10411042if(!potential[1].suspect)1043return;1044if(best_so_far[1].suspect) {1045if(blame_entry_score(sb, &potential[1]) <1046blame_entry_score(sb, &best_so_far[1]))1047return;1048}10491050for(i =0; i <3; i++)1051blame_origin_incref(potential[i].suspect);1052decref_split(best_so_far);1053memcpy(best_so_far, potential,sizeof(struct blame_entry[3]));1054}10551056/*1057 * We are looking at a part of the final image represented by1058 * ent (tlno and same are offset by ent->s_lno).1059 * tlno is where we are looking at in the final image.1060 * up to (but not including) same match preimage.1061 * plno is where we are looking at in the preimage.1062 *1063 * <-------------- final image ---------------------->1064 * <------ent------>1065 * ^tlno ^same1066 * <---------preimage----->1067 * ^plno1068 *1069 * All line numbers are 0-based.1070 */1071static voidhandle_split(struct blame_scoreboard *sb,1072struct blame_entry *ent,1073int tlno,int plno,int same,1074struct blame_origin *parent,1075struct blame_entry *split)1076{1077if(ent->num_lines <= tlno)1078return;1079if(tlno < same) {1080struct blame_entry potential[3];1081 tlno += ent->s_lno;1082 same += ent->s_lno;1083split_overlap(potential, ent, tlno, plno, same, parent);1084copy_split_if_better(sb, split, potential);1085decref_split(potential);1086}1087}10881089struct handle_split_cb_data {1090struct blame_scoreboard *sb;1091struct blame_entry *ent;1092struct blame_origin *parent;1093struct blame_entry *split;1094long plno;1095long tlno;1096};10971098static inthandle_split_cb(long start_a,long count_a,1099long start_b,long count_b,void*data)1100{1101struct handle_split_cb_data *d = data;1102handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1103 d->split);1104 d->plno = start_a + count_a;1105 d->tlno = start_b + count_b;1106return0;1107}11081109/*1110 * Find the lines from parent that are the same as ent so that1111 * we can pass blames to it. file_p has the blob contents for1112 * the parent.1113 */1114static voidfind_copy_in_blob(struct blame_scoreboard *sb,1115struct blame_entry *ent,1116struct blame_origin *parent,1117struct blame_entry *split,1118 mmfile_t *file_p)1119{1120const char*cp;1121 mmfile_t file_o;1122struct handle_split_cb_data d;11231124memset(&d,0,sizeof(d));1125 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1126/*1127 * Prepare mmfile that contains only the lines in ent.1128 */1129 cp =blame_nth_line(sb, ent->lno);1130 file_o.ptr = (char*) cp;1131 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11321133/*1134 * file_o is a part of final image we are annotating.1135 * file_p partially may match that image.1136 */1137memset(split,0,sizeof(struct blame_entry [3]));1138if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1139die("unable to generate diff (%s)",1140oid_to_hex(&parent->commit->object.oid));1141/* remainder, if any, all match the preimage */1142handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1143}11441145/* Move all blame entries from list *source that have a score smaller1146 * than score_min to the front of list *small.1147 * Returns a pointer to the link pointing to the old head of the small list.1148 */11491150static struct blame_entry **filter_small(struct blame_scoreboard *sb,1151struct blame_entry **small,1152struct blame_entry **source,1153unsigned score_min)1154{1155struct blame_entry *p = *source;1156struct blame_entry *oldsmall = *small;1157while(p) {1158if(blame_entry_score(sb, p) <= score_min) {1159*small = p;1160 small = &p->next;1161 p = *small;1162}else{1163*source = p;1164 source = &p->next;1165 p = *source;1166}1167}1168*small = oldsmall;1169*source = NULL;1170return small;1171}11721173/*1174 * See if lines currently target is suspected for can be attributed to1175 * parent.1176 */1177static voidfind_move_in_parent(struct blame_scoreboard *sb,1178struct blame_entry ***blamed,1179struct blame_entry **toosmall,1180struct blame_origin *target,1181struct blame_origin *parent)1182{1183struct blame_entry *e, split[3];1184struct blame_entry *unblamed = target->suspects;1185struct blame_entry *leftover = NULL;1186 mmfile_t file_p;11871188if(!unblamed)1189return;/* nothing remains for this target */11901191fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1192if(!file_p.ptr)1193return;11941195/* At each iteration, unblamed has a NULL-terminated list of1196 * entries that have not yet been tested for blame. leftover1197 * contains the reversed list of entries that have been tested1198 * without being assignable to the parent.1199 */1200do{1201struct blame_entry **unblamedtail = &unblamed;1202struct blame_entry *next;1203for(e = unblamed; e; e = next) {1204 next = e->next;1205find_copy_in_blob(sb, e, parent, split, &file_p);1206if(split[1].suspect &&1207 sb->move_score <blame_entry_score(sb, &split[1])) {1208split_blame(blamed, &unblamedtail, split, e);1209}else{1210 e->next = leftover;1211 leftover = e;1212}1213decref_split(split);1214}1215*unblamedtail = NULL;1216 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1217}while(unblamed);1218 target->suspects =reverse_blame(leftover, NULL);1219}12201221struct blame_list {1222struct blame_entry *ent;1223struct blame_entry split[3];1224};12251226/*1227 * Count the number of entries the target is suspected for,1228 * and prepare a list of entry and the best split.1229 */1230static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1231int*num_ents_p)1232{1233struct blame_entry *e;1234int num_ents, i;1235struct blame_list *blame_list = NULL;12361237for(e = unblamed, num_ents =0; e; e = e->next)1238 num_ents++;1239if(num_ents) {1240 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1241for(e = unblamed, i =0; e; e = e->next)1242 blame_list[i++].ent = e;1243}1244*num_ents_p = num_ents;1245return blame_list;1246}12471248/*1249 * For lines target is suspected for, see if we can find code movement1250 * across file boundary from the parent commit. porigin is the path1251 * in the parent we already tried.1252 */1253static voidfind_copy_in_parent(struct blame_scoreboard *sb,1254struct blame_entry ***blamed,1255struct blame_entry **toosmall,1256struct blame_origin *target,1257struct commit *parent,1258struct blame_origin *porigin,1259int opt)1260{1261struct diff_options diff_opts;1262int i, j;1263struct blame_list *blame_list;1264int num_ents;1265struct blame_entry *unblamed = target->suspects;1266struct blame_entry *leftover = NULL;12671268if(!unblamed)1269return;/* nothing remains for this target */12701271repo_diff_setup(sb->repo, &diff_opts);1272 diff_opts.flags.recursive =1;1273 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12741275diff_setup_done(&diff_opts);12761277/* Try "find copies harder" on new path if requested;1278 * we do not want to use diffcore_rename() actually to1279 * match things up; find_copies_harder is set only to1280 * force diff_tree_oid() to feed all filepairs to diff_queue,1281 * and this code needs to be after diff_setup_done(), which1282 * usually makes find-copies-harder imply copy detection.1283 */1284if((opt & PICKAXE_BLAME_COPY_HARDEST)1285|| ((opt & PICKAXE_BLAME_COPY_HARDER)1286&& (!porigin ||strcmp(target->path, porigin->path))))1287 diff_opts.flags.find_copies_harder =1;12881289if(is_null_oid(&target->commit->object.oid))1290do_diff_cache(get_commit_tree_oid(parent), &diff_opts);1291else1292diff_tree_oid(get_commit_tree_oid(parent),1293get_commit_tree_oid(target->commit),1294"", &diff_opts);12951296if(!diff_opts.flags.find_copies_harder)1297diffcore_std(&diff_opts);12981299do{1300struct blame_entry **unblamedtail = &unblamed;1301 blame_list =setup_blame_list(unblamed, &num_ents);13021303for(i =0; i < diff_queued_diff.nr; i++) {1304struct diff_filepair *p = diff_queued_diff.queue[i];1305struct blame_origin *norigin;1306 mmfile_t file_p;1307struct blame_entry potential[3];13081309if(!DIFF_FILE_VALID(p->one))1310continue;/* does not exist in parent */1311if(S_ISGITLINK(p->one->mode))1312continue;/* ignore git links */1313if(porigin && !strcmp(p->one->path, porigin->path))1314/* find_move already dealt with this path */1315continue;13161317 norigin =get_origin(parent, p->one->path);1318oidcpy(&norigin->blob_oid, &p->one->oid);1319 norigin->mode = p->one->mode;1320fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1321if(!file_p.ptr)1322continue;13231324for(j =0; j < num_ents; j++) {1325find_copy_in_blob(sb, blame_list[j].ent,1326 norigin, potential, &file_p);1327copy_split_if_better(sb, blame_list[j].split,1328 potential);1329decref_split(potential);1330}1331blame_origin_decref(norigin);1332}13331334for(j =0; j < num_ents; j++) {1335struct blame_entry *split = blame_list[j].split;1336if(split[1].suspect &&1337 sb->copy_score <blame_entry_score(sb, &split[1])) {1338split_blame(blamed, &unblamedtail, split,1339 blame_list[j].ent);1340}else{1341 blame_list[j].ent->next = leftover;1342 leftover = blame_list[j].ent;1343}1344decref_split(split);1345}1346free(blame_list);1347*unblamedtail = NULL;1348 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1349}while(unblamed);1350 target->suspects =reverse_blame(leftover, NULL);1351diff_flush(&diff_opts);1352clear_pathspec(&diff_opts.pathspec);1353}13541355/*1356 * The blobs of origin and porigin exactly match, so everything1357 * origin is suspected for can be blamed on the parent.1358 */1359static voidpass_whole_blame(struct blame_scoreboard *sb,1360struct blame_origin *origin,struct blame_origin *porigin)1361{1362struct blame_entry *e, *suspects;13631364if(!porigin->file.ptr && origin->file.ptr) {1365/* Steal its file */1366 porigin->file = origin->file;1367 origin->file.ptr = NULL;1368}1369 suspects = origin->suspects;1370 origin->suspects = NULL;1371for(e = suspects; e; e = e->next) {1372blame_origin_incref(porigin);1373blame_origin_decref(e->suspect);1374 e->suspect = porigin;1375}1376queue_blames(sb, porigin, suspects);1377}13781379/*1380 * We pass blame from the current commit to its parents. We keep saying1381 * "parent" (and "porigin"), but what we mean is to find scapegoat to1382 * exonerate ourselves.1383 */1384static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1385int reverse)1386{1387if(!reverse) {1388if(revs->first_parent_only &&1389 commit->parents &&1390 commit->parents->next) {1391free_commit_list(commit->parents->next);1392 commit->parents->next = NULL;1393}1394return commit->parents;1395}1396returnlookup_decoration(&revs->children, &commit->object);1397}13981399static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1400{1401struct commit_list *l =first_scapegoat(revs, commit, reverse);1402returncommit_list_count(l);1403}14041405/* Distribute collected unsorted blames to the respected sorted lists1406 * in the various origins.1407 */1408static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1409{1410 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1411 compare_blame_suspect);1412while(blamed)1413{1414struct blame_origin *porigin = blamed->suspect;1415struct blame_entry *suspects = NULL;1416do{1417struct blame_entry *next = blamed->next;1418 blamed->next = suspects;1419 suspects = blamed;1420 blamed = next;1421}while(blamed && blamed->suspect == porigin);1422 suspects =reverse_blame(suspects, NULL);1423queue_blames(sb, porigin, suspects);1424}1425}14261427#define MAXSG 1614281429static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1430{1431struct rev_info *revs = sb->revs;1432int i, pass, num_sg;1433struct commit *commit = origin->commit;1434struct commit_list *sg;1435struct blame_origin *sg_buf[MAXSG];1436struct blame_origin *porigin, **sg_origin = sg_buf;1437struct blame_entry *toosmall = NULL;1438struct blame_entry *blames, **blametail = &blames;14391440 num_sg =num_scapegoats(revs, commit, sb->reverse);1441if(!num_sg)1442goto finish;1443else if(num_sg <ARRAY_SIZE(sg_buf))1444memset(sg_buf,0,sizeof(sg_buf));1445else1446 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14471448/*1449 * The first pass looks for unrenamed path to optimize for1450 * common cases, then we look for renames in the second pass.1451 */1452for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1453struct blame_origin *(*find)(struct repository *,struct commit *,struct blame_origin *);1454 find = pass ? find_rename : find_origin;14551456for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1457 i < num_sg && sg;1458 sg = sg->next, i++) {1459struct commit *p = sg->item;1460int j, same;14611462if(sg_origin[i])1463continue;1464if(parse_commit(p))1465continue;1466 porigin =find(sb->repo, p, origin);1467if(!porigin)1468continue;1469if(oideq(&porigin->blob_oid, &origin->blob_oid)) {1470pass_whole_blame(sb, origin, porigin);1471blame_origin_decref(porigin);1472goto finish;1473}1474for(j = same =0; j < i; j++)1475if(sg_origin[j] &&1476oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1477 same =1;1478break;1479}1480if(!same)1481 sg_origin[i] = porigin;1482else1483blame_origin_decref(porigin);1484}1485}14861487 sb->num_commits++;1488for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1489 i < num_sg && sg;1490 sg = sg->next, i++) {1491struct blame_origin *porigin = sg_origin[i];1492if(!porigin)1493continue;1494if(!origin->previous) {1495blame_origin_incref(porigin);1496 origin->previous = porigin;1497}1498pass_blame_to_parent(sb, origin, porigin);1499if(!origin->suspects)1500goto finish;1501}15021503/*1504 * Optionally find moves in parents' files.1505 */1506if(opt & PICKAXE_BLAME_MOVE) {1507filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1508if(origin->suspects) {1509for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1510 i < num_sg && sg;1511 sg = sg->next, i++) {1512struct blame_origin *porigin = sg_origin[i];1513if(!porigin)1514continue;1515find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1516if(!origin->suspects)1517break;1518}1519}1520}15211522/*1523 * Optionally find copies from parents' files.1524 */1525if(opt & PICKAXE_BLAME_COPY) {1526if(sb->copy_score > sb->move_score)1527filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1528else if(sb->copy_score < sb->move_score) {1529 origin->suspects =blame_merge(origin->suspects, toosmall);1530 toosmall = NULL;1531filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1532}1533if(!origin->suspects)1534goto finish;15351536for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1537 i < num_sg && sg;1538 sg = sg->next, i++) {1539struct blame_origin *porigin = sg_origin[i];1540find_copy_in_parent(sb, &blametail, &toosmall,1541 origin, sg->item, porigin, opt);1542if(!origin->suspects)1543goto finish;1544}1545}15461547finish:1548*blametail = NULL;1549distribute_blame(sb, blames);1550/*1551 * prepend toosmall to origin->suspects1552 *1553 * There is no point in sorting: this ends up on a big1554 * unsorted list in the caller anyway.1555 */1556if(toosmall) {1557struct blame_entry **tail = &toosmall;1558while(*tail)1559 tail = &(*tail)->next;1560*tail = origin->suspects;1561 origin->suspects = toosmall;1562}1563for(i =0; i < num_sg; i++) {1564if(sg_origin[i]) {1565drop_origin_blob(sg_origin[i]);1566blame_origin_decref(sg_origin[i]);1567}1568}1569drop_origin_blob(origin);1570if(sg_buf != sg_origin)1571free(sg_origin);1572}15731574/*1575 * The main loop -- while we have blobs with lines whose true origin1576 * is still unknown, pick one blob, and allow its lines to pass blames1577 * to its parents. */1578voidassign_blame(struct blame_scoreboard *sb,int opt)1579{1580struct rev_info *revs = sb->revs;1581struct commit *commit =prio_queue_get(&sb->commits);15821583while(commit) {1584struct blame_entry *ent;1585struct blame_origin *suspect =get_blame_suspects(commit);15861587/* find one suspect to break down */1588while(suspect && !suspect->suspects)1589 suspect = suspect->next;15901591if(!suspect) {1592 commit =prio_queue_get(&sb->commits);1593continue;1594}15951596assert(commit == suspect->commit);15971598/*1599 * We will use this suspect later in the loop,1600 * so hold onto it in the meantime.1601 */1602blame_origin_incref(suspect);1603parse_commit(commit);1604if(sb->reverse ||1605(!(commit->object.flags & UNINTERESTING) &&1606!(revs->max_age != -1&& commit->date < revs->max_age)))1607pass_blame(sb, suspect, opt);1608else{1609 commit->object.flags |= UNINTERESTING;1610if(commit->object.parsed)1611mark_parents_uninteresting(commit);1612}1613/* treat root commit as boundary */1614if(!commit->parents && !sb->show_root)1615 commit->object.flags |= UNINTERESTING;16161617/* Take responsibility for the remaining entries */1618 ent = suspect->suspects;1619if(ent) {1620 suspect->guilty =1;1621for(;;) {1622struct blame_entry *next = ent->next;1623if(sb->found_guilty_entry)1624 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1625if(next) {1626 ent = next;1627continue;1628}1629 ent->next = sb->ent;1630 sb->ent = suspect->suspects;1631 suspect->suspects = NULL;1632break;1633}1634}1635blame_origin_decref(suspect);16361637if(sb->debug)/* sanity */1638sanity_check_refcnt(sb);1639}1640}16411642static const char*get_next_line(const char*start,const char*end)1643{1644const char*nl =memchr(start,'\n', end - start);1645return nl ? nl +1: end;1646}16471648/*1649 * To allow quick access to the contents of nth line in the1650 * final image, prepare an index in the scoreboard.1651 */1652static intprepare_lines(struct blame_scoreboard *sb)1653{1654const char*buf = sb->final_buf;1655unsigned long len = sb->final_buf_size;1656const char*end = buf + len;1657const char*p;1658int*lineno;1659int num =0;16601661for(p = buf; p < end; p =get_next_line(p, end))1662 num++;16631664ALLOC_ARRAY(sb->lineno, num +1);1665 lineno = sb->lineno;16661667for(p = buf; p < end; p =get_next_line(p, end))1668*lineno++ = p - buf;16691670*lineno = len;16711672 sb->num_lines = num;1673return sb->num_lines;1674}16751676static struct commit *find_single_final(struct rev_info *revs,1677const char**name_p)1678{1679int i;1680struct commit *found = NULL;1681const char*name = NULL;16821683for(i =0; i < revs->pending.nr; i++) {1684struct object *obj = revs->pending.objects[i].item;1685if(obj->flags & UNINTERESTING)1686continue;1687 obj =deref_tag(revs->repo, obj, NULL,0);1688if(obj->type != OBJ_COMMIT)1689die("Non commit%s?", revs->pending.objects[i].name);1690if(found)1691die("More than one commit to dig from%sand%s?",1692 revs->pending.objects[i].name, name);1693 found = (struct commit *)obj;1694 name = revs->pending.objects[i].name;1695}1696if(name_p)1697*name_p =xstrdup_or_null(name);1698return found;1699}17001701static struct commit *dwim_reverse_initial(struct rev_info *revs,1702const char**name_p)1703{1704/*1705 * DWIM "git blame --reverse ONE -- PATH" as1706 * "git blame --reverse ONE..HEAD -- PATH" but only do so1707 * when it makes sense.1708 */1709struct object *obj;1710struct commit *head_commit;1711struct object_id head_oid;17121713if(revs->pending.nr !=1)1714return NULL;17151716/* Is that sole rev a committish? */1717 obj = revs->pending.objects[0].item;1718 obj =deref_tag(revs->repo, obj, NULL,0);1719if(obj->type != OBJ_COMMIT)1720return NULL;17211722/* Do we have HEAD? */1723if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))1724return NULL;1725 head_commit =lookup_commit_reference_gently(revs->repo,1726&head_oid,1);1727if(!head_commit)1728return NULL;17291730/* Turn "ONE" into "ONE..HEAD" then */1731 obj->flags |= UNINTERESTING;1732add_pending_object(revs, &head_commit->object,"HEAD");17331734if(name_p)1735*name_p = revs->pending.objects[0].name;1736return(struct commit *)obj;1737}17381739static struct commit *find_single_initial(struct rev_info *revs,1740const char**name_p)1741{1742int i;1743struct commit *found = NULL;1744const char*name = NULL;17451746/*1747 * There must be one and only one negative commit, and it must be1748 * the boundary.1749 */1750for(i =0; i < revs->pending.nr; i++) {1751struct object *obj = revs->pending.objects[i].item;1752if(!(obj->flags & UNINTERESTING))1753continue;1754 obj =deref_tag(revs->repo, obj, NULL,0);1755if(obj->type != OBJ_COMMIT)1756die("Non commit%s?", revs->pending.objects[i].name);1757if(found)1758die("More than one commit to dig up from,%sand%s?",1759 revs->pending.objects[i].name, name);1760 found = (struct commit *) obj;1761 name = revs->pending.objects[i].name;1762}17631764if(!name)1765 found =dwim_reverse_initial(revs, &name);1766if(!name)1767die("No commit to dig up from?");17681769if(name_p)1770*name_p =xstrdup(name);1771return found;1772}17731774voidinit_scoreboard(struct blame_scoreboard *sb)1775{1776memset(sb,0,sizeof(struct blame_scoreboard));1777 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;1778 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;1779}17801781voidsetup_scoreboard(struct blame_scoreboard *sb,1782const char*path,1783struct blame_origin **orig)1784{1785const char*final_commit_name = NULL;1786struct blame_origin *o;1787struct commit *final_commit = NULL;1788enum object_type type;17891790init_blame_suspects(&blame_suspects);17911792if(sb->reverse && sb->contents_from)1793die(_("--contents and --reverse do not blend well."));17941795if(!sb->repo)1796BUG("repo is NULL");17971798if(!sb->reverse) {1799 sb->final =find_single_final(sb->revs, &final_commit_name);1800 sb->commits.compare = compare_commits_by_commit_date;1801}else{1802 sb->final =find_single_initial(sb->revs, &final_commit_name);1803 sb->commits.compare = compare_commits_by_reverse_commit_date;1804}18051806if(sb->final && sb->contents_from)1807die(_("cannot use --contents with final commit object name"));18081809if(sb->reverse && sb->revs->first_parent_only)1810 sb->revs->children.name = NULL;18111812if(!sb->final) {1813/*1814 * "--not A B -- path" without anything positive;1815 * do not default to HEAD, but use the working tree1816 * or "--contents".1817 */1818setup_work_tree();1819 sb->final =fake_working_tree_commit(sb->repo,1820&sb->revs->diffopt,1821 path, sb->contents_from);1822add_pending_object(sb->revs, &(sb->final->object),":");1823}18241825if(sb->reverse && sb->revs->first_parent_only) {1826 final_commit =find_single_final(sb->revs, NULL);1827if(!final_commit)1828die(_("--reverse and --first-parent together require specified latest commit"));1829}18301831/*1832 * If we have bottom, this will mark the ancestors of the1833 * bottom commits we would reach while traversing as1834 * uninteresting.1835 */1836if(prepare_revision_walk(sb->revs))1837die(_("revision walk setup failed"));18381839if(sb->reverse && sb->revs->first_parent_only) {1840struct commit *c = final_commit;18411842 sb->revs->children.name ="children";1843while(c->parents &&1844!oideq(&c->object.oid, &sb->final->object.oid)) {1845struct commit_list *l =xcalloc(1,sizeof(*l));18461847 l->item = c;1848if(add_decoration(&sb->revs->children,1849&c->parents->item->object, l))1850BUG("not unique item in first-parent chain");1851 c = c->parents->item;1852}18531854if(!oideq(&c->object.oid, &sb->final->object.oid))1855die(_("--reverse --first-parent together require range along first-parent chain"));1856}18571858if(is_null_oid(&sb->final->object.oid)) {1859 o =get_blame_suspects(sb->final);1860 sb->final_buf =xmemdupz(o->file.ptr, o->file.size);1861 sb->final_buf_size = o->file.size;1862}1863else{1864 o =get_origin(sb->final, path);1865if(fill_blob_sha1_and_mode(sb->repo, o))1866die(_("no such path%sin%s"), path, final_commit_name);18671868if(sb->revs->diffopt.flags.allow_textconv &&1869textconv_object(sb->repo, path, o->mode, &o->blob_oid,1, (char**) &sb->final_buf,1870&sb->final_buf_size))1871;1872else1873 sb->final_buf =read_object_file(&o->blob_oid, &type,1874&sb->final_buf_size);18751876if(!sb->final_buf)1877die(_("cannot read blob%sfor path%s"),1878oid_to_hex(&o->blob_oid),1879 path);1880}1881 sb->num_read_blob++;1882prepare_lines(sb);18831884if(orig)1885*orig = o;18861887free((char*)final_commit_name);1888}1889189018911892struct blame_entry *blame_entry_prepend(struct blame_entry *head,1893long start,long end,1894struct blame_origin *o)1895{1896struct blame_entry *new_head =xcalloc(1,sizeof(struct blame_entry));1897 new_head->lno = start;1898 new_head->num_lines = end - start;1899 new_head->suspect = o;1900 new_head->s_lno = start;1901 new_head->next = head;1902blame_origin_incref(o);1903return new_head;1904}