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