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