1/* 2 * Blame 3 * 4 * Copyright (c) 2006, Junio C Hamano 5 */ 6 7#include"cache.h" 8#include"builtin.h" 9#include"blob.h" 10#include"commit.h" 11#include"tag.h" 12#include"tree-walk.h" 13#include"diff.h" 14#include"diffcore.h" 15#include"revision.h" 16#include"quote.h" 17#include"xdiff-interface.h" 18#include"cache-tree.h" 19#include"string-list.h" 20#include"mailmap.h" 21#include"parse-options.h" 22#include"utf8.h" 23 24static char blame_usage[] ="git blame [options] [rev-opts] [rev] [--] file"; 25 26static const char*blame_opt_usage[] = { 27 blame_usage, 28"", 29"[rev-opts] are documented in git-rev-list(1)", 30 NULL 31}; 32 33static int longest_file; 34static int longest_author; 35static int max_orig_digits; 36static int max_digits; 37static int max_score_digits; 38static int show_root; 39static int reverse; 40static int blank_boundary; 41static int incremental; 42static int xdl_opts = XDF_NEED_MINIMAL; 43 44static enum date_mode blame_date_mode = DATE_ISO8601; 45static size_t blame_date_width; 46 47static struct string_list mailmap; 48 49#ifndef DEBUG 50#define DEBUG 0 51#endif 52 53/* stats */ 54static int num_read_blob; 55static int num_get_patch; 56static int num_commits; 57 58#define PICKAXE_BLAME_MOVE 01 59#define PICKAXE_BLAME_COPY 02 60#define PICKAXE_BLAME_COPY_HARDER 04 61#define PICKAXE_BLAME_COPY_HARDEST 010 62 63/* 64 * blame for a blame_entry with score lower than these thresholds 65 * is not passed to the parent using move/copy logic. 66 */ 67static unsigned blame_move_score; 68static unsigned blame_copy_score; 69#define BLAME_DEFAULT_MOVE_SCORE 20 70#define BLAME_DEFAULT_COPY_SCORE 40 71 72/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ 73#define METAINFO_SHOWN (1u<<12) 74#define MORE_THAN_ONE_PATH (1u<<13) 75 76/* 77 * One blob in a commit that is being suspected 78 */ 79struct origin { 80int refcnt; 81struct origin *previous; 82struct commit *commit; 83 mmfile_t file; 84unsigned char blob_sha1[20]; 85char path[FLEX_ARRAY]; 86}; 87 88/* 89 * Given an origin, prepare mmfile_t structure to be used by the 90 * diff machinery 91 */ 92static voidfill_origin_blob(struct origin *o, mmfile_t *file) 93{ 94if(!o->file.ptr) { 95enum object_type type; 96 num_read_blob++; 97 file->ptr =read_sha1_file(o->blob_sha1, &type, 98(unsigned long*)(&(file->size))); 99if(!file->ptr) 100die("Cannot read blob%sfor path%s", 101sha1_to_hex(o->blob_sha1), 102 o->path); 103 o->file = *file; 104} 105else 106*file = o->file; 107} 108 109/* 110 * Origin is refcounted and usually we keep the blob contents to be 111 * reused. 112 */ 113staticinlinestruct origin *origin_incref(struct origin *o) 114{ 115if(o) 116 o->refcnt++; 117return o; 118} 119 120static voidorigin_decref(struct origin *o) 121{ 122if(o && --o->refcnt <=0) { 123if(o->previous) 124origin_decref(o->previous); 125free(o->file.ptr); 126free(o); 127} 128} 129 130static voiddrop_origin_blob(struct origin *o) 131{ 132if(o->file.ptr) { 133free(o->file.ptr); 134 o->file.ptr = NULL; 135} 136} 137 138/* 139 * Each group of lines is described by a blame_entry; it can be split 140 * as we pass blame to the parents. They form a linked list in the 141 * scoreboard structure, sorted by the target line number. 142 */ 143struct blame_entry { 144struct blame_entry *prev; 145struct blame_entry *next; 146 147/* the first line of this group in the final image; 148 * internally all line numbers are 0 based. 149 */ 150int lno; 151 152/* how many lines this group has */ 153int num_lines; 154 155/* the commit that introduced this group into the final image */ 156struct origin *suspect; 157 158/* true if the suspect is truly guilty; false while we have not 159 * checked if the group came from one of its parents. 160 */ 161char guilty; 162 163/* true if the entry has been scanned for copies in the current parent 164 */ 165char scanned; 166 167/* the line number of the first line of this group in the 168 * suspect's file; internally all line numbers are 0 based. 169 */ 170int s_lno; 171 172/* how significant this entry is -- cached to avoid 173 * scanning the lines over and over. 174 */ 175unsigned score; 176}; 177 178/* 179 * The current state of the blame assignment. 180 */ 181struct scoreboard { 182/* the final commit (i.e. where we started digging from) */ 183struct commit *final; 184struct rev_info *revs; 185const char*path; 186 187/* 188 * The contents in the final image. 189 * Used by many functions to obtain contents of the nth line, 190 * indexed with scoreboard.lineno[blame_entry.lno]. 191 */ 192const char*final_buf; 193unsigned long final_buf_size; 194 195/* linked list of blames */ 196struct blame_entry *ent; 197 198/* look-up a line in the final buffer */ 199int num_lines; 200int*lineno; 201}; 202 203staticinlineintsame_suspect(struct origin *a,struct origin *b) 204{ 205if(a == b) 206return1; 207if(a->commit != b->commit) 208return0; 209return!strcmp(a->path, b->path); 210} 211 212static voidsanity_check_refcnt(struct scoreboard *); 213 214/* 215 * If two blame entries that are next to each other came from 216 * contiguous lines in the same origin (i.e. <commit, path> pair), 217 * merge them together. 218 */ 219static voidcoalesce(struct scoreboard *sb) 220{ 221struct blame_entry *ent, *next; 222 223for(ent = sb->ent; ent && (next = ent->next); ent = next) { 224if(same_suspect(ent->suspect, next->suspect) && 225 ent->guilty == next->guilty && 226 ent->s_lno + ent->num_lines == next->s_lno) { 227 ent->num_lines += next->num_lines; 228 ent->next = next->next; 229if(ent->next) 230 ent->next->prev = ent; 231origin_decref(next->suspect); 232free(next); 233 ent->score =0; 234 next = ent;/* again */ 235} 236} 237 238if(DEBUG)/* sanity */ 239sanity_check_refcnt(sb); 240} 241 242/* 243 * Given a commit and a path in it, create a new origin structure. 244 * The callers that add blame to the scoreboard should use 245 * get_origin() to obtain shared, refcounted copy instead of calling 246 * this function directly. 247 */ 248static struct origin *make_origin(struct commit *commit,const char*path) 249{ 250struct origin *o; 251 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 252 o->commit = commit; 253 o->refcnt =1; 254strcpy(o->path, path); 255return o; 256} 257 258/* 259 * Locate an existing origin or create a new one. 260 */ 261static struct origin *get_origin(struct scoreboard *sb, 262struct commit *commit, 263const char*path) 264{ 265struct blame_entry *e; 266 267for(e = sb->ent; e; e = e->next) { 268if(e->suspect->commit == commit && 269!strcmp(e->suspect->path, path)) 270returnorigin_incref(e->suspect); 271} 272returnmake_origin(commit, path); 273} 274 275/* 276 * Fill the blob_sha1 field of an origin if it hasn't, so that later 277 * call to fill_origin_blob() can use it to locate the data. blob_sha1 278 * for an origin is also used to pass the blame for the entire file to 279 * the parent to detect the case where a child's blob is identical to 280 * that of its parent's. 281 */ 282static intfill_blob_sha1(struct origin *origin) 283{ 284unsigned mode; 285 286if(!is_null_sha1(origin->blob_sha1)) 287return0; 288if(get_tree_entry(origin->commit->object.sha1, 289 origin->path, 290 origin->blob_sha1, &mode)) 291goto error_out; 292if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 293goto error_out; 294return0; 295 error_out: 296hashclr(origin->blob_sha1); 297return-1; 298} 299 300/* 301 * We have an origin -- check if the same path exists in the 302 * parent and return an origin structure to represent it. 303 */ 304static struct origin *find_origin(struct scoreboard *sb, 305struct commit *parent, 306struct origin *origin) 307{ 308struct origin *porigin = NULL; 309struct diff_options diff_opts; 310const char*paths[2]; 311 312if(parent->util) { 313/* 314 * Each commit object can cache one origin in that 315 * commit. This is a freestanding copy of origin and 316 * not refcounted. 317 */ 318struct origin *cached = parent->util; 319if(!strcmp(cached->path, origin->path)) { 320/* 321 * The same path between origin and its parent 322 * without renaming -- the most common case. 323 */ 324 porigin =get_origin(sb, parent, cached->path); 325 326/* 327 * If the origin was newly created (i.e. get_origin 328 * would call make_origin if none is found in the 329 * scoreboard), it does not know the blob_sha1, 330 * so copy it. Otherwise porigin was in the 331 * scoreboard and already knows blob_sha1. 332 */ 333if(porigin->refcnt ==1) 334hashcpy(porigin->blob_sha1, cached->blob_sha1); 335return porigin; 336} 337/* otherwise it was not very useful; free it */ 338free(parent->util); 339 parent->util = NULL; 340} 341 342/* See if the origin->path is different between parent 343 * and origin first. Most of the time they are the 344 * same and diff-tree is fairly efficient about this. 345 */ 346diff_setup(&diff_opts); 347DIFF_OPT_SET(&diff_opts, RECURSIVE); 348 diff_opts.detect_rename =0; 349 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 350 paths[0] = origin->path; 351 paths[1] = NULL; 352 353diff_tree_setup_paths(paths, &diff_opts); 354if(diff_setup_done(&diff_opts) <0) 355die("diff-setup"); 356 357if(is_null_sha1(origin->commit->object.sha1)) 358do_diff_cache(parent->tree->object.sha1, &diff_opts); 359else 360diff_tree_sha1(parent->tree->object.sha1, 361 origin->commit->tree->object.sha1, 362"", &diff_opts); 363diffcore_std(&diff_opts); 364 365/* It is either one entry that says "modified", or "created", 366 * or nothing. 367 */ 368if(!diff_queued_diff.nr) { 369/* The path is the same as parent */ 370 porigin =get_origin(sb, parent, origin->path); 371hashcpy(porigin->blob_sha1, origin->blob_sha1); 372} 373else if(diff_queued_diff.nr !=1) 374die("internal error in blame::find_origin"); 375else{ 376struct diff_filepair *p = diff_queued_diff.queue[0]; 377switch(p->status) { 378default: 379die("internal error in blame::find_origin (%c)", 380 p->status); 381case'M': 382 porigin =get_origin(sb, parent, origin->path); 383hashcpy(porigin->blob_sha1, p->one->sha1); 384break; 385case'A': 386case'T': 387/* Did not exist in parent, or type changed */ 388break; 389} 390} 391diff_flush(&diff_opts); 392diff_tree_release_paths(&diff_opts); 393if(porigin) { 394/* 395 * Create a freestanding copy that is not part of 396 * the refcounted origin found in the scoreboard, and 397 * cache it in the commit. 398 */ 399struct origin *cached; 400 401 cached =make_origin(porigin->commit, porigin->path); 402hashcpy(cached->blob_sha1, porigin->blob_sha1); 403 parent->util = cached; 404} 405return porigin; 406} 407 408/* 409 * We have an origin -- find the path that corresponds to it in its 410 * parent and return an origin structure to represent it. 411 */ 412static struct origin *find_rename(struct scoreboard *sb, 413struct commit *parent, 414struct origin *origin) 415{ 416struct origin *porigin = NULL; 417struct diff_options diff_opts; 418int i; 419const char*paths[2]; 420 421diff_setup(&diff_opts); 422DIFF_OPT_SET(&diff_opts, RECURSIVE); 423 diff_opts.detect_rename = DIFF_DETECT_RENAME; 424 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 425 diff_opts.single_follow = origin->path; 426 paths[0] = NULL; 427diff_tree_setup_paths(paths, &diff_opts); 428if(diff_setup_done(&diff_opts) <0) 429die("diff-setup"); 430 431if(is_null_sha1(origin->commit->object.sha1)) 432do_diff_cache(parent->tree->object.sha1, &diff_opts); 433else 434diff_tree_sha1(parent->tree->object.sha1, 435 origin->commit->tree->object.sha1, 436"", &diff_opts); 437diffcore_std(&diff_opts); 438 439for(i =0; i < diff_queued_diff.nr; i++) { 440struct diff_filepair *p = diff_queued_diff.queue[i]; 441if((p->status =='R'|| p->status =='C') && 442!strcmp(p->two->path, origin->path)) { 443 porigin =get_origin(sb, parent, p->one->path); 444hashcpy(porigin->blob_sha1, p->one->sha1); 445break; 446} 447} 448diff_flush(&diff_opts); 449diff_tree_release_paths(&diff_opts); 450return porigin; 451} 452 453/* 454 * Link in a new blame entry to the scoreboard. Entries that cover the 455 * same line range have been removed from the scoreboard previously. 456 */ 457static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 458{ 459struct blame_entry *ent, *prev = NULL; 460 461origin_incref(e->suspect); 462 463for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 464 prev = ent; 465 466/* prev, if not NULL, is the last one that is below e */ 467 e->prev = prev; 468if(prev) { 469 e->next = prev->next; 470 prev->next = e; 471} 472else{ 473 e->next = sb->ent; 474 sb->ent = e; 475} 476if(e->next) 477 e->next->prev = e; 478} 479 480/* 481 * src typically is on-stack; we want to copy the information in it to 482 * a malloced blame_entry that is already on the linked list of the 483 * scoreboard. The origin of dst loses a refcnt while the origin of src 484 * gains one. 485 */ 486static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 487{ 488struct blame_entry *p, *n; 489 490 p = dst->prev; 491 n = dst->next; 492origin_incref(src->suspect); 493origin_decref(dst->suspect); 494memcpy(dst, src,sizeof(*src)); 495 dst->prev = p; 496 dst->next = n; 497 dst->score =0; 498} 499 500static const char*nth_line(struct scoreboard *sb,int lno) 501{ 502return sb->final_buf + sb->lineno[lno]; 503} 504 505/* 506 * It is known that lines between tlno to same came from parent, and e 507 * has an overlap with that range. it also is known that parent's 508 * line plno corresponds to e's line tlno. 509 * 510 * <---- e -----> 511 * <------> 512 * <------------> 513 * <------------> 514 * <------------------> 515 * 516 * Split e into potentially three parts; before this chunk, the chunk 517 * to be blamed for the parent, and after that portion. 518 */ 519static voidsplit_overlap(struct blame_entry *split, 520struct blame_entry *e, 521int tlno,int plno,int same, 522struct origin *parent) 523{ 524int chunk_end_lno; 525memset(split,0,sizeof(struct blame_entry [3])); 526 527if(e->s_lno < tlno) { 528/* there is a pre-chunk part not blamed on parent */ 529 split[0].suspect =origin_incref(e->suspect); 530 split[0].lno = e->lno; 531 split[0].s_lno = e->s_lno; 532 split[0].num_lines = tlno - e->s_lno; 533 split[1].lno = e->lno + tlno - e->s_lno; 534 split[1].s_lno = plno; 535} 536else{ 537 split[1].lno = e->lno; 538 split[1].s_lno = plno + (e->s_lno - tlno); 539} 540 541if(same < e->s_lno + e->num_lines) { 542/* there is a post-chunk part not blamed on parent */ 543 split[2].suspect =origin_incref(e->suspect); 544 split[2].lno = e->lno + (same - e->s_lno); 545 split[2].s_lno = e->s_lno + (same - e->s_lno); 546 split[2].num_lines = e->s_lno + e->num_lines - same; 547 chunk_end_lno = split[2].lno; 548} 549else 550 chunk_end_lno = e->lno + e->num_lines; 551 split[1].num_lines = chunk_end_lno - split[1].lno; 552 553/* 554 * if it turns out there is nothing to blame the parent for, 555 * forget about the splitting. !split[1].suspect signals this. 556 */ 557if(split[1].num_lines <1) 558return; 559 split[1].suspect =origin_incref(parent); 560} 561 562/* 563 * split_overlap() divided an existing blame e into up to three parts 564 * in split. Adjust the linked list of blames in the scoreboard to 565 * reflect the split. 566 */ 567static voidsplit_blame(struct scoreboard *sb, 568struct blame_entry *split, 569struct blame_entry *e) 570{ 571struct blame_entry *new_entry; 572 573if(split[0].suspect && split[2].suspect) { 574/* The first part (reuse storage for the existing entry e) */ 575dup_entry(e, &split[0]); 576 577/* The last part -- me */ 578 new_entry =xmalloc(sizeof(*new_entry)); 579memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 580add_blame_entry(sb, new_entry); 581 582/* ... and the middle part -- parent */ 583 new_entry =xmalloc(sizeof(*new_entry)); 584memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 585add_blame_entry(sb, new_entry); 586} 587else if(!split[0].suspect && !split[2].suspect) 588/* 589 * The parent covers the entire area; reuse storage for 590 * e and replace it with the parent. 591 */ 592dup_entry(e, &split[1]); 593else if(split[0].suspect) { 594/* me and then parent */ 595dup_entry(e, &split[0]); 596 597 new_entry =xmalloc(sizeof(*new_entry)); 598memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 599add_blame_entry(sb, new_entry); 600} 601else{ 602/* parent and then me */ 603dup_entry(e, &split[1]); 604 605 new_entry =xmalloc(sizeof(*new_entry)); 606memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 607add_blame_entry(sb, new_entry); 608} 609 610if(DEBUG) {/* sanity */ 611struct blame_entry *ent; 612int lno = sb->ent->lno, corrupt =0; 613 614for(ent = sb->ent; ent; ent = ent->next) { 615if(lno != ent->lno) 616 corrupt =1; 617if(ent->s_lno <0) 618 corrupt =1; 619 lno += ent->num_lines; 620} 621if(corrupt) { 622 lno = sb->ent->lno; 623for(ent = sb->ent; ent; ent = ent->next) { 624printf("L%8d l%8d n%8d\n", 625 lno, ent->lno, ent->num_lines); 626 lno = ent->lno + ent->num_lines; 627} 628die("oops"); 629} 630} 631} 632 633/* 634 * After splitting the blame, the origins used by the 635 * on-stack blame_entry should lose one refcnt each. 636 */ 637static voiddecref_split(struct blame_entry *split) 638{ 639int i; 640 641for(i =0; i <3; i++) 642origin_decref(split[i].suspect); 643} 644 645/* 646 * Helper for blame_chunk(). blame_entry e is known to overlap with 647 * the patch hunk; split it and pass blame to the parent. 648 */ 649static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 650int tlno,int plno,int same, 651struct origin *parent) 652{ 653struct blame_entry split[3]; 654 655split_overlap(split, e, tlno, plno, same, parent); 656if(split[1].suspect) 657split_blame(sb, split, e); 658decref_split(split); 659} 660 661/* 662 * Find the line number of the last line the target is suspected for. 663 */ 664static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 665{ 666struct blame_entry *e; 667int last_in_target = -1; 668 669for(e = sb->ent; e; e = e->next) { 670if(e->guilty || !same_suspect(e->suspect, target)) 671continue; 672if(last_in_target < e->s_lno + e->num_lines) 673 last_in_target = e->s_lno + e->num_lines; 674} 675return last_in_target; 676} 677 678/* 679 * Process one hunk from the patch between the current suspect for 680 * blame_entry e and its parent. Find and split the overlap, and 681 * pass blame to the overlapping part to the parent. 682 */ 683static voidblame_chunk(struct scoreboard *sb, 684int tlno,int plno,int same, 685struct origin *target,struct origin *parent) 686{ 687struct blame_entry *e; 688 689for(e = sb->ent; e; e = e->next) { 690if(e->guilty || !same_suspect(e->suspect, target)) 691continue; 692if(same <= e->s_lno) 693continue; 694if(tlno < e->s_lno + e->num_lines) 695blame_overlap(sb, e, tlno, plno, same, parent); 696} 697} 698 699struct blame_chunk_cb_data { 700struct scoreboard *sb; 701struct origin *target; 702struct origin *parent; 703long plno; 704long tlno; 705}; 706 707static voidblame_chunk_cb(void*data,long same,long p_next,long t_next) 708{ 709struct blame_chunk_cb_data *d = data; 710blame_chunk(d->sb, d->tlno, d->plno, same, d->target, d->parent); 711 d->plno = p_next; 712 d->tlno = t_next; 713} 714 715/* 716 * We are looking at the origin 'target' and aiming to pass blame 717 * for the lines it is suspected to its parent. Run diff to find 718 * which lines came from parent and pass blame for them. 719 */ 720static intpass_blame_to_parent(struct scoreboard *sb, 721struct origin *target, 722struct origin *parent) 723{ 724int last_in_target; 725 mmfile_t file_p, file_o; 726struct blame_chunk_cb_data d = { sb, target, parent,0,0}; 727 xpparam_t xpp; 728 xdemitconf_t xecfg; 729 730 last_in_target =find_last_in_target(sb, target); 731if(last_in_target <0) 732return1;/* nothing remains for this target */ 733 734fill_origin_blob(parent, &file_p); 735fill_origin_blob(target, &file_o); 736 num_get_patch++; 737 738memset(&xpp,0,sizeof(xpp)); 739 xpp.flags = xdl_opts; 740memset(&xecfg,0,sizeof(xecfg)); 741 xecfg.ctxlen =0; 742xdi_diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, &xpp, &xecfg); 743/* The rest (i.e. anything after tlno) are the same as the parent */ 744blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent); 745 746return0; 747} 748 749/* 750 * The lines in blame_entry after splitting blames many times can become 751 * very small and trivial, and at some point it becomes pointless to 752 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 753 * ordinary C program, and it is not worth to say it was copied from 754 * totally unrelated file in the parent. 755 * 756 * Compute how trivial the lines in the blame_entry are. 757 */ 758static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 759{ 760unsigned score; 761const char*cp, *ep; 762 763if(e->score) 764return e->score; 765 766 score =1; 767 cp =nth_line(sb, e->lno); 768 ep =nth_line(sb, e->lno + e->num_lines); 769while(cp < ep) { 770unsigned ch = *((unsigned char*)cp); 771if(isalnum(ch)) 772 score++; 773 cp++; 774} 775 e->score = score; 776return score; 777} 778 779/* 780 * best_so_far[] and this[] are both a split of an existing blame_entry 781 * that passes blame to the parent. Maintain best_so_far the best split 782 * so far, by comparing this and best_so_far and copying this into 783 * bst_so_far as needed. 784 */ 785static voidcopy_split_if_better(struct scoreboard *sb, 786struct blame_entry *best_so_far, 787struct blame_entry *this) 788{ 789int i; 790 791if(!this[1].suspect) 792return; 793if(best_so_far[1].suspect) { 794if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 795return; 796} 797 798for(i =0; i <3; i++) 799origin_incref(this[i].suspect); 800decref_split(best_so_far); 801memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 802} 803 804/* 805 * We are looking at a part of the final image represented by 806 * ent (tlno and same are offset by ent->s_lno). 807 * tlno is where we are looking at in the final image. 808 * up to (but not including) same match preimage. 809 * plno is where we are looking at in the preimage. 810 * 811 * <-------------- final image ----------------------> 812 * <------ent------> 813 * ^tlno ^same 814 * <---------preimage-----> 815 * ^plno 816 * 817 * All line numbers are 0-based. 818 */ 819static voidhandle_split(struct scoreboard *sb, 820struct blame_entry *ent, 821int tlno,int plno,int same, 822struct origin *parent, 823struct blame_entry *split) 824{ 825if(ent->num_lines <= tlno) 826return; 827if(tlno < same) { 828struct blame_entry this[3]; 829 tlno += ent->s_lno; 830 same += ent->s_lno; 831split_overlap(this, ent, tlno, plno, same, parent); 832copy_split_if_better(sb, split,this); 833decref_split(this); 834} 835} 836 837struct handle_split_cb_data { 838struct scoreboard *sb; 839struct blame_entry *ent; 840struct origin *parent; 841struct blame_entry *split; 842long plno; 843long tlno; 844}; 845 846static voidhandle_split_cb(void*data,long same,long p_next,long t_next) 847{ 848struct handle_split_cb_data *d = data; 849handle_split(d->sb, d->ent, d->tlno, d->plno, same, d->parent, d->split); 850 d->plno = p_next; 851 d->tlno = t_next; 852} 853 854/* 855 * Find the lines from parent that are the same as ent so that 856 * we can pass blames to it. file_p has the blob contents for 857 * the parent. 858 */ 859static voidfind_copy_in_blob(struct scoreboard *sb, 860struct blame_entry *ent, 861struct origin *parent, 862struct blame_entry *split, 863 mmfile_t *file_p) 864{ 865const char*cp; 866int cnt; 867 mmfile_t file_o; 868struct handle_split_cb_data d = { sb, ent, parent, split,0,0}; 869 xpparam_t xpp; 870 xdemitconf_t xecfg; 871 872/* 873 * Prepare mmfile that contains only the lines in ent. 874 */ 875 cp =nth_line(sb, ent->lno); 876 file_o.ptr = (char*) cp; 877 cnt = ent->num_lines; 878 879while(cnt && cp < sb->final_buf + sb->final_buf_size) { 880if(*cp++ =='\n') 881 cnt--; 882} 883 file_o.size = cp - file_o.ptr; 884 885/* 886 * file_o is a part of final image we are annotating. 887 * file_p partially may match that image. 888 */ 889memset(&xpp,0,sizeof(xpp)); 890 xpp.flags = xdl_opts; 891memset(&xecfg,0,sizeof(xecfg)); 892 xecfg.ctxlen =1; 893memset(split,0,sizeof(struct blame_entry [3])); 894xdi_diff_hunks(file_p, &file_o, handle_split_cb, &d, &xpp, &xecfg); 895/* remainder, if any, all match the preimage */ 896handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); 897} 898 899/* 900 * See if lines currently target is suspected for can be attributed to 901 * parent. 902 */ 903static intfind_move_in_parent(struct scoreboard *sb, 904struct origin *target, 905struct origin *parent) 906{ 907int last_in_target, made_progress; 908struct blame_entry *e, split[3]; 909 mmfile_t file_p; 910 911 last_in_target =find_last_in_target(sb, target); 912if(last_in_target <0) 913return1;/* nothing remains for this target */ 914 915fill_origin_blob(parent, &file_p); 916if(!file_p.ptr) 917return0; 918 919 made_progress =1; 920while(made_progress) { 921 made_progress =0; 922for(e = sb->ent; e; e = e->next) { 923if(e->guilty || !same_suspect(e->suspect, target) || 924ent_score(sb, e) < blame_move_score) 925continue; 926find_copy_in_blob(sb, e, parent, split, &file_p); 927if(split[1].suspect && 928 blame_move_score <ent_score(sb, &split[1])) { 929split_blame(sb, split, e); 930 made_progress =1; 931} 932decref_split(split); 933} 934} 935return0; 936} 937 938struct blame_list { 939struct blame_entry *ent; 940struct blame_entry split[3]; 941}; 942 943/* 944 * Count the number of entries the target is suspected for, 945 * and prepare a list of entry and the best split. 946 */ 947static struct blame_list *setup_blame_list(struct scoreboard *sb, 948struct origin *target, 949int min_score, 950int*num_ents_p) 951{ 952struct blame_entry *e; 953int num_ents, i; 954struct blame_list *blame_list = NULL; 955 956for(e = sb->ent, num_ents =0; e; e = e->next) 957if(!e->scanned && !e->guilty && 958same_suspect(e->suspect, target) && 959 min_score <ent_score(sb, e)) 960 num_ents++; 961if(num_ents) { 962 blame_list =xcalloc(num_ents,sizeof(struct blame_list)); 963for(e = sb->ent, i =0; e; e = e->next) 964if(!e->scanned && !e->guilty && 965same_suspect(e->suspect, target) && 966 min_score <ent_score(sb, e)) 967 blame_list[i++].ent = e; 968} 969*num_ents_p = num_ents; 970return blame_list; 971} 972 973/* 974 * Reset the scanned status on all entries. 975 */ 976static voidreset_scanned_flag(struct scoreboard *sb) 977{ 978struct blame_entry *e; 979for(e = sb->ent; e; e = e->next) 980 e->scanned =0; 981} 982 983/* 984 * For lines target is suspected for, see if we can find code movement 985 * across file boundary from the parent commit. porigin is the path 986 * in the parent we already tried. 987 */ 988static intfind_copy_in_parent(struct scoreboard *sb, 989struct origin *target, 990struct commit *parent, 991struct origin *porigin, 992int opt) 993{ 994struct diff_options diff_opts; 995const char*paths[1]; 996int i, j; 997int retval; 998struct blame_list *blame_list; 999int num_ents;10001001 blame_list =setup_blame_list(sb, target, blame_copy_score, &num_ents);1002if(!blame_list)1003return1;/* nothing remains for this target */10041005diff_setup(&diff_opts);1006DIFF_OPT_SET(&diff_opts, RECURSIVE);1007 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10081009 paths[0] = NULL;1010diff_tree_setup_paths(paths, &diff_opts);1011if(diff_setup_done(&diff_opts) <0)1012die("diff-setup");10131014/* Try "find copies harder" on new path if requested;1015 * we do not want to use diffcore_rename() actually to1016 * match things up; find_copies_harder is set only to1017 * force diff_tree_sha1() to feed all filepairs to diff_queue,1018 * and this code needs to be after diff_setup_done(), which1019 * usually makes find-copies-harder imply copy detection.1020 */1021if((opt & PICKAXE_BLAME_COPY_HARDEST)1022|| ((opt & PICKAXE_BLAME_COPY_HARDER)1023&& (!porigin ||strcmp(target->path, porigin->path))))1024DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);10251026if(is_null_sha1(target->commit->object.sha1))1027do_diff_cache(parent->tree->object.sha1, &diff_opts);1028else1029diff_tree_sha1(parent->tree->object.sha1,1030 target->commit->tree->object.sha1,1031"", &diff_opts);10321033if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1034diffcore_std(&diff_opts);10351036 retval =0;1037while(1) {1038int made_progress =0;10391040for(i =0; i < diff_queued_diff.nr; i++) {1041struct diff_filepair *p = diff_queued_diff.queue[i];1042struct origin *norigin;1043 mmfile_t file_p;1044struct blame_entry this[3];10451046if(!DIFF_FILE_VALID(p->one))1047continue;/* does not exist in parent */1048if(S_ISGITLINK(p->one->mode))1049continue;/* ignore git links */1050if(porigin && !strcmp(p->one->path, porigin->path))1051/* find_move already dealt with this path */1052continue;10531054 norigin =get_origin(sb, parent, p->one->path);1055hashcpy(norigin->blob_sha1, p->one->sha1);1056fill_origin_blob(norigin, &file_p);1057if(!file_p.ptr)1058continue;10591060for(j =0; j < num_ents; j++) {1061find_copy_in_blob(sb, blame_list[j].ent,1062 norigin,this, &file_p);1063copy_split_if_better(sb, blame_list[j].split,1064this);1065decref_split(this);1066}1067origin_decref(norigin);1068}10691070for(j =0; j < num_ents; j++) {1071struct blame_entry *split = blame_list[j].split;1072if(split[1].suspect &&1073 blame_copy_score <ent_score(sb, &split[1])) {1074split_blame(sb, split, blame_list[j].ent);1075 made_progress =1;1076}1077else1078 blame_list[j].ent->scanned =1;1079decref_split(split);1080}1081free(blame_list);10821083if(!made_progress)1084break;1085 blame_list =setup_blame_list(sb, target, blame_copy_score, &num_ents);1086if(!blame_list) {1087 retval =1;1088break;1089}1090}1091reset_scanned_flag(sb);1092diff_flush(&diff_opts);1093diff_tree_release_paths(&diff_opts);1094return retval;1095}10961097/*1098 * The blobs of origin and porigin exactly match, so everything1099 * origin is suspected for can be blamed on the parent.1100 */1101static voidpass_whole_blame(struct scoreboard *sb,1102struct origin *origin,struct origin *porigin)1103{1104struct blame_entry *e;11051106if(!porigin->file.ptr && origin->file.ptr) {1107/* Steal its file */1108 porigin->file = origin->file;1109 origin->file.ptr = NULL;1110}1111for(e = sb->ent; e; e = e->next) {1112if(!same_suspect(e->suspect, origin))1113continue;1114origin_incref(porigin);1115origin_decref(e->suspect);1116 e->suspect = porigin;1117}1118}11191120/*1121 * We pass blame from the current commit to its parents. We keep saying1122 * "parent" (and "porigin"), but what we mean is to find scapegoat to1123 * exonerate ourselves.1124 */1125static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit)1126{1127if(!reverse)1128return commit->parents;1129returnlookup_decoration(&revs->children, &commit->object);1130}11311132static intnum_scapegoats(struct rev_info *revs,struct commit *commit)1133{1134int cnt;1135struct commit_list *l =first_scapegoat(revs, commit);1136for(cnt =0; l; l = l->next)1137 cnt++;1138return cnt;1139}11401141#define MAXSG 1611421143static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1144{1145struct rev_info *revs = sb->revs;1146int i, pass, num_sg;1147struct commit *commit = origin->commit;1148struct commit_list *sg;1149struct origin *sg_buf[MAXSG];1150struct origin *porigin, **sg_origin = sg_buf;11511152 num_sg =num_scapegoats(revs, commit);1153if(!num_sg)1154goto finish;1155else if(num_sg <ARRAY_SIZE(sg_buf))1156memset(sg_buf,0,sizeof(sg_buf));1157else1158 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));11591160/*1161 * The first pass looks for unrenamed path to optimize for1162 * common cases, then we look for renames in the second pass.1163 */1164for(pass =0; pass <2; pass++) {1165struct origin *(*find)(struct scoreboard *,1166struct commit *,struct origin *);1167 find = pass ? find_rename : find_origin;11681169for(i =0, sg =first_scapegoat(revs, commit);1170 i < num_sg && sg;1171 sg = sg->next, i++) {1172struct commit *p = sg->item;1173int j, same;11741175if(sg_origin[i])1176continue;1177if(parse_commit(p))1178continue;1179 porigin =find(sb, p, origin);1180if(!porigin)1181continue;1182if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1183pass_whole_blame(sb, origin, porigin);1184origin_decref(porigin);1185goto finish;1186}1187for(j = same =0; j < i; j++)1188if(sg_origin[j] &&1189!hashcmp(sg_origin[j]->blob_sha1,1190 porigin->blob_sha1)) {1191 same =1;1192break;1193}1194if(!same)1195 sg_origin[i] = porigin;1196else1197origin_decref(porigin);1198}1199}12001201 num_commits++;1202for(i =0, sg =first_scapegoat(revs, commit);1203 i < num_sg && sg;1204 sg = sg->next, i++) {1205struct origin *porigin = sg_origin[i];1206if(!porigin)1207continue;1208if(!origin->previous) {1209origin_incref(porigin);1210 origin->previous = porigin;1211}1212if(pass_blame_to_parent(sb, origin, porigin))1213goto finish;1214}12151216/*1217 * Optionally find moves in parents' files.1218 */1219if(opt & PICKAXE_BLAME_MOVE)1220for(i =0, sg =first_scapegoat(revs, commit);1221 i < num_sg && sg;1222 sg = sg->next, i++) {1223struct origin *porigin = sg_origin[i];1224if(!porigin)1225continue;1226if(find_move_in_parent(sb, origin, porigin))1227goto finish;1228}12291230/*1231 * Optionally find copies from parents' files.1232 */1233if(opt & PICKAXE_BLAME_COPY)1234for(i =0, sg =first_scapegoat(revs, commit);1235 i < num_sg && sg;1236 sg = sg->next, i++) {1237struct origin *porigin = sg_origin[i];1238if(find_copy_in_parent(sb, origin, sg->item,1239 porigin, opt))1240goto finish;1241}12421243 finish:1244for(i =0; i < num_sg; i++) {1245if(sg_origin[i]) {1246drop_origin_blob(sg_origin[i]);1247origin_decref(sg_origin[i]);1248}1249}1250drop_origin_blob(origin);1251if(sg_buf != sg_origin)1252free(sg_origin);1253}12541255/*1256 * Information on commits, used for output.1257 */1258struct commit_info1259{1260const char*author;1261const char*author_mail;1262unsigned long author_time;1263const char*author_tz;12641265/* filled only when asked for details */1266const char*committer;1267const char*committer_mail;1268unsigned long committer_time;1269const char*committer_tz;12701271const char*summary;1272};12731274/*1275 * Parse author/committer line in the commit object buffer1276 */1277static voidget_ac_line(const char*inbuf,const char*what,1278int person_len,char*person,1279int mail_len,char*mail,1280unsigned long*time,const char**tz)1281{1282int len, tzlen, maillen;1283char*tmp, *endp, *timepos, *mailpos;12841285 tmp =strstr(inbuf, what);1286if(!tmp)1287goto error_out;1288 tmp +=strlen(what);1289 endp =strchr(tmp,'\n');1290if(!endp)1291 len =strlen(tmp);1292else1293 len = endp - tmp;1294if(person_len <= len) {1295 error_out:1296/* Ugh */1297*tz ="(unknown)";1298strcpy(mail, *tz);1299*time =0;1300return;1301}1302memcpy(person, tmp, len);13031304 tmp = person;1305 tmp += len;1306*tmp =0;1307while(*tmp !=' ')1308 tmp--;1309*tz = tmp+1;1310 tzlen = (person+len)-(tmp+1);13111312*tmp =0;1313while(*tmp !=' ')1314 tmp--;1315*time =strtoul(tmp, NULL,10);1316 timepos = tmp;13171318*tmp =0;1319while(*tmp !=' ')1320 tmp--;1321 mailpos = tmp +1;1322*tmp =0;1323 maillen = timepos - tmp;1324memcpy(mail, mailpos, maillen);13251326if(!mailmap.nr)1327return;13281329/*1330 * mailmap expansion may make the name longer.1331 * make room by pushing stuff down.1332 */1333 tmp = person + person_len - (tzlen +1);1334memmove(tmp, *tz, tzlen);1335 tmp[tzlen] =0;1336*tz = tmp;13371338/*1339 * Now, convert both name and e-mail using mailmap1340 */1341if(map_user(&mailmap, mail+1, mail_len-1, person, tmp-person-1)) {1342/* Add a trailing '>' to email, since map_user returns plain emails1343 Note: It already has '<', since we replace from mail+1 */1344 mailpos =memchr(mail,'\0', mail_len);1345if(mailpos && mailpos-mail < mail_len -1) {1346*mailpos ='>';1347*(mailpos+1) ='\0';1348}1349}1350}13511352static voidget_commit_info(struct commit *commit,1353struct commit_info *ret,1354int detailed)1355{1356int len;1357char*tmp, *endp, *reencoded, *message;1358static char author_name[1024];1359static char author_mail[1024];1360static char committer_name[1024];1361static char committer_mail[1024];1362static char summary_buf[1024];13631364/*1365 * We've operated without save_commit_buffer, so1366 * we now need to populate them for output.1367 */1368if(!commit->buffer) {1369enum object_type type;1370unsigned long size;1371 commit->buffer =1372read_sha1_file(commit->object.sha1, &type, &size);1373if(!commit->buffer)1374die("Cannot read commit%s",1375sha1_to_hex(commit->object.sha1));1376}1377 reencoded =reencode_commit_message(commit, NULL);1378 message = reencoded ? reencoded : commit->buffer;1379 ret->author = author_name;1380 ret->author_mail = author_mail;1381get_ac_line(message,"\nauthor ",1382sizeof(author_name), author_name,1383sizeof(author_mail), author_mail,1384&ret->author_time, &ret->author_tz);13851386if(!detailed) {1387free(reencoded);1388return;1389}13901391 ret->committer = committer_name;1392 ret->committer_mail = committer_mail;1393get_ac_line(message,"\ncommitter ",1394sizeof(committer_name), committer_name,1395sizeof(committer_mail), committer_mail,1396&ret->committer_time, &ret->committer_tz);13971398 ret->summary = summary_buf;1399 tmp =strstr(message,"\n\n");1400if(!tmp) {1401 error_out:1402sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1403free(reencoded);1404return;1405}1406 tmp +=2;1407 endp =strchr(tmp,'\n');1408if(!endp)1409 endp = tmp +strlen(tmp);1410 len = endp - tmp;1411if(len >=sizeof(summary_buf) || len ==0)1412goto error_out;1413memcpy(summary_buf, tmp, len);1414 summary_buf[len] =0;1415free(reencoded);1416}14171418/*1419 * To allow LF and other nonportable characters in pathnames,1420 * they are c-style quoted as needed.1421 */1422static voidwrite_filename_info(const char*path)1423{1424printf("filename ");1425write_name_quoted(path, stdout,'\n');1426}14271428/*1429 * Porcelain/Incremental format wants to show a lot of details per1430 * commit. Instead of repeating this every line, emit it only once,1431 * the first time each commit appears in the output.1432 */1433static intemit_one_suspect_detail(struct origin *suspect)1434{1435struct commit_info ci;14361437if(suspect->commit->object.flags & METAINFO_SHOWN)1438return0;14391440 suspect->commit->object.flags |= METAINFO_SHOWN;1441get_commit_info(suspect->commit, &ci,1);1442printf("author%s\n", ci.author);1443printf("author-mail%s\n", ci.author_mail);1444printf("author-time%lu\n", ci.author_time);1445printf("author-tz%s\n", ci.author_tz);1446printf("committer%s\n", ci.committer);1447printf("committer-mail%s\n", ci.committer_mail);1448printf("committer-time%lu\n", ci.committer_time);1449printf("committer-tz%s\n", ci.committer_tz);1450printf("summary%s\n", ci.summary);1451if(suspect->commit->object.flags & UNINTERESTING)1452printf("boundary\n");1453if(suspect->previous) {1454struct origin *prev = suspect->previous;1455printf("previous%s",sha1_to_hex(prev->commit->object.sha1));1456write_name_quoted(prev->path, stdout,'\n');1457}1458return1;1459}14601461/*1462 * The blame_entry is found to be guilty for the range. Mark it1463 * as such, and show it in incremental output.1464 */1465static voidfound_guilty_entry(struct blame_entry *ent)1466{1467if(ent->guilty)1468return;1469 ent->guilty =1;1470if(incremental) {1471struct origin *suspect = ent->suspect;14721473printf("%s %d %d %d\n",1474sha1_to_hex(suspect->commit->object.sha1),1475 ent->s_lno +1, ent->lno +1, ent->num_lines);1476emit_one_suspect_detail(suspect);1477write_filename_info(suspect->path);1478maybe_flush_or_die(stdout,"stdout");1479}1480}14811482/*1483 * The main loop -- while the scoreboard has lines whose true origin1484 * is still unknown, pick one blame_entry, and allow its current1485 * suspect to pass blames to its parents.1486 */1487static voidassign_blame(struct scoreboard *sb,int opt)1488{1489struct rev_info *revs = sb->revs;14901491while(1) {1492struct blame_entry *ent;1493struct commit *commit;1494struct origin *suspect = NULL;14951496/* find one suspect to break down */1497for(ent = sb->ent; !suspect && ent; ent = ent->next)1498if(!ent->guilty)1499 suspect = ent->suspect;1500if(!suspect)1501return;/* all done */15021503/*1504 * We will use this suspect later in the loop,1505 * so hold onto it in the meantime.1506 */1507origin_incref(suspect);1508 commit = suspect->commit;1509if(!commit->object.parsed)1510parse_commit(commit);1511if(reverse ||1512(!(commit->object.flags & UNINTERESTING) &&1513!(revs->max_age != -1&& commit->date < revs->max_age)))1514pass_blame(sb, suspect, opt);1515else{1516 commit->object.flags |= UNINTERESTING;1517if(commit->object.parsed)1518mark_parents_uninteresting(commit);1519}1520/* treat root commit as boundary */1521if(!commit->parents && !show_root)1522 commit->object.flags |= UNINTERESTING;15231524/* Take responsibility for the remaining entries */1525for(ent = sb->ent; ent; ent = ent->next)1526if(same_suspect(ent->suspect, suspect))1527found_guilty_entry(ent);1528origin_decref(suspect);15291530if(DEBUG)/* sanity */1531sanity_check_refcnt(sb);1532}1533}15341535static const char*format_time(unsigned long time,const char*tz_str,1536int show_raw_time)1537{1538static char time_buf[128];1539const char*time_str;1540int time_len;1541int tz;15421543if(show_raw_time) {1544sprintf(time_buf,"%lu%s", time, tz_str);1545}1546else{1547 tz =atoi(tz_str);1548 time_str =show_date(time, tz, blame_date_mode);1549 time_len =strlen(time_str);1550memcpy(time_buf, time_str, time_len);1551memset(time_buf + time_len,' ', blame_date_width - time_len);1552}1553return time_buf;1554}15551556#define OUTPUT_ANNOTATE_COMPAT 0011557#define OUTPUT_LONG_OBJECT_NAME 0021558#define OUTPUT_RAW_TIMESTAMP 0041559#define OUTPUT_PORCELAIN 0101560#define OUTPUT_SHOW_NAME 0201561#define OUTPUT_SHOW_NUMBER 0401562#define OUTPUT_SHOW_SCORE 01001563#define OUTPUT_NO_AUTHOR 020015641565static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1566{1567int cnt;1568const char*cp;1569struct origin *suspect = ent->suspect;1570char hex[41];15711572strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1573printf("%s%c%d %d %d\n",1574 hex,1575 ent->guilty ?' ':'*',// purely for debugging1576 ent->s_lno +1,1577 ent->lno +1,1578 ent->num_lines);1579if(emit_one_suspect_detail(suspect) ||1580(suspect->commit->object.flags & MORE_THAN_ONE_PATH))1581write_filename_info(suspect->path);15821583 cp =nth_line(sb, ent->lno);1584for(cnt =0; cnt < ent->num_lines; cnt++) {1585char ch;1586if(cnt)1587printf("%s %d %d\n", hex,1588 ent->s_lno +1+ cnt,1589 ent->lno +1+ cnt);1590putchar('\t');1591do{1592 ch = *cp++;1593putchar(ch);1594}while(ch !='\n'&&1595 cp < sb->final_buf + sb->final_buf_size);1596}1597}15981599static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1600{1601int cnt;1602const char*cp;1603struct origin *suspect = ent->suspect;1604struct commit_info ci;1605char hex[41];1606int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16071608get_commit_info(suspect->commit, &ci,1);1609strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16101611 cp =nth_line(sb, ent->lno);1612for(cnt =0; cnt < ent->num_lines; cnt++) {1613char ch;1614int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16151616if(suspect->commit->object.flags & UNINTERESTING) {1617if(blank_boundary)1618memset(hex,' ', length);1619else if(!(opt & OUTPUT_ANNOTATE_COMPAT)) {1620 length--;1621putchar('^');1622}1623}16241625printf("%.*s", length, hex);1626if(opt & OUTPUT_ANNOTATE_COMPAT)1627printf("\t(%10s\t%10s\t%d)", ci.author,1628format_time(ci.author_time, ci.author_tz,1629 show_raw_time),1630 ent->lno +1+ cnt);1631else{1632if(opt & OUTPUT_SHOW_SCORE)1633printf(" %*d%02d",1634 max_score_digits, ent->score,1635 ent->suspect->refcnt);1636if(opt & OUTPUT_SHOW_NAME)1637printf(" %-*.*s", longest_file, longest_file,1638 suspect->path);1639if(opt & OUTPUT_SHOW_NUMBER)1640printf(" %*d", max_orig_digits,1641 ent->s_lno +1+ cnt);16421643if(!(opt & OUTPUT_NO_AUTHOR)) {1644int pad = longest_author -utf8_strwidth(ci.author);1645printf(" (%s%*s%10s",1646 ci.author, pad,"",1647format_time(ci.author_time,1648 ci.author_tz,1649 show_raw_time));1650}1651printf(" %*d) ",1652 max_digits, ent->lno +1+ cnt);1653}1654do{1655 ch = *cp++;1656putchar(ch);1657}while(ch !='\n'&&1658 cp < sb->final_buf + sb->final_buf_size);1659}1660}16611662static voidoutput(struct scoreboard *sb,int option)1663{1664struct blame_entry *ent;16651666if(option & OUTPUT_PORCELAIN) {1667for(ent = sb->ent; ent; ent = ent->next) {1668struct blame_entry *oth;1669struct origin *suspect = ent->suspect;1670struct commit *commit = suspect->commit;1671if(commit->object.flags & MORE_THAN_ONE_PATH)1672continue;1673for(oth = ent->next; oth; oth = oth->next) {1674if((oth->suspect->commit != commit) ||1675!strcmp(oth->suspect->path, suspect->path))1676continue;1677 commit->object.flags |= MORE_THAN_ONE_PATH;1678break;1679}1680}1681}16821683for(ent = sb->ent; ent; ent = ent->next) {1684if(option & OUTPUT_PORCELAIN)1685emit_porcelain(sb, ent);1686else{1687emit_other(sb, ent, option);1688}1689}1690}16911692/*1693 * To allow quick access to the contents of nth line in the1694 * final image, prepare an index in the scoreboard.1695 */1696static intprepare_lines(struct scoreboard *sb)1697{1698const char*buf = sb->final_buf;1699unsigned long len = sb->final_buf_size;1700int num =0, incomplete =0, bol =1;17011702if(len && buf[len-1] !='\n')1703 incomplete++;/* incomplete line at the end */1704while(len--) {1705if(bol) {1706 sb->lineno =xrealloc(sb->lineno,1707sizeof(int*) * (num +1));1708 sb->lineno[num] = buf - sb->final_buf;1709 bol =0;1710}1711if(*buf++ =='\n') {1712 num++;1713 bol =1;1714}1715}1716 sb->lineno =xrealloc(sb->lineno,1717sizeof(int*) * (num + incomplete +1));1718 sb->lineno[num + incomplete] = buf - sb->final_buf;1719 sb->num_lines = num + incomplete;1720return sb->num_lines;1721}17221723/*1724 * Add phony grafts for use with -S; this is primarily to1725 * support git's cvsserver that wants to give a linear history1726 * to its clients.1727 */1728static intread_ancestry(const char*graft_file)1729{1730FILE*fp =fopen(graft_file,"r");1731char buf[1024];1732if(!fp)1733return-1;1734while(fgets(buf,sizeof(buf), fp)) {1735/* The format is just "Commit Parent1 Parent2 ...\n" */1736int len =strlen(buf);1737struct commit_graft *graft =read_graft_line(buf, len);1738if(graft)1739register_commit_graft(graft,0);1740}1741fclose(fp);1742return0;1743}17441745/*1746 * How many columns do we need to show line numbers in decimal?1747 */1748static intlineno_width(int lines)1749{1750int i, width;17511752for(width =1, i =10; i <= lines +1; width++)1753 i *=10;1754return width;1755}17561757/*1758 * How many columns do we need to show line numbers, authors,1759 * and filenames?1760 */1761static voidfind_alignment(struct scoreboard *sb,int*option)1762{1763int longest_src_lines =0;1764int longest_dst_lines =0;1765unsigned largest_score =0;1766struct blame_entry *e;17671768for(e = sb->ent; e; e = e->next) {1769struct origin *suspect = e->suspect;1770struct commit_info ci;1771int num;17721773if(strcmp(suspect->path, sb->path))1774*option |= OUTPUT_SHOW_NAME;1775 num =strlen(suspect->path);1776if(longest_file < num)1777 longest_file = num;1778if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1779 suspect->commit->object.flags |= METAINFO_SHOWN;1780get_commit_info(suspect->commit, &ci,1);1781 num =utf8_strwidth(ci.author);1782if(longest_author < num)1783 longest_author = num;1784}1785 num = e->s_lno + e->num_lines;1786if(longest_src_lines < num)1787 longest_src_lines = num;1788 num = e->lno + e->num_lines;1789if(longest_dst_lines < num)1790 longest_dst_lines = num;1791if(largest_score <ent_score(sb, e))1792 largest_score =ent_score(sb, e);1793}1794 max_orig_digits =lineno_width(longest_src_lines);1795 max_digits =lineno_width(longest_dst_lines);1796 max_score_digits =lineno_width(largest_score);1797}17981799/*1800 * For debugging -- origin is refcounted, and this asserts that1801 * we do not underflow.1802 */1803static voidsanity_check_refcnt(struct scoreboard *sb)1804{1805int baa =0;1806struct blame_entry *ent;18071808for(ent = sb->ent; ent; ent = ent->next) {1809/* Nobody should have zero or negative refcnt */1810if(ent->suspect->refcnt <=0) {1811fprintf(stderr,"%sin%shas negative refcnt%d\n",1812 ent->suspect->path,1813sha1_to_hex(ent->suspect->commit->object.sha1),1814 ent->suspect->refcnt);1815 baa =1;1816}1817}1818if(baa) {1819int opt =0160;1820find_alignment(sb, &opt);1821output(sb, opt);1822die("Baa%d!", baa);1823}1824}18251826/*1827 * Used for the command line parsing; check if the path exists1828 * in the working tree.1829 */1830static inthas_string_in_work_tree(const char*path)1831{1832struct stat st;1833return!lstat(path, &st);1834}18351836static unsignedparse_score(const char*arg)1837{1838char*end;1839unsigned long score =strtoul(arg, &end,10);1840if(*end)1841return0;1842return score;1843}18441845static const char*add_prefix(const char*prefix,const char*path)1846{1847returnprefix_path(prefix, prefix ?strlen(prefix) :0, path);1848}18491850/*1851 * Parsing of (comma separated) one item in the -L option1852 */1853static const char*parse_loc(const char*spec,1854struct scoreboard *sb,long lno,1855long begin,long*ret)1856{1857char*term;1858const char*line;1859long num;1860int reg_error;1861 regex_t regexp;1862 regmatch_t match[1];18631864/* Allow "-L <something>,+20" to mean starting at <something>1865 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1866 * <something>.1867 */1868if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1869 num =strtol(spec +1, &term,10);1870if(term != spec +1) {1871if(spec[0] =='-')1872 num =0- num;1873if(0< num)1874*ret = begin + num -2;1875else if(!num)1876*ret = begin;1877else1878*ret = begin + num;1879return term;1880}1881return spec;1882}1883 num =strtol(spec, &term,10);1884if(term != spec) {1885*ret = num;1886return term;1887}1888if(spec[0] !='/')1889return spec;18901891/* it could be a regexp of form /.../ */1892for(term = (char*) spec +1; *term && *term !='/'; term++) {1893if(*term =='\\')1894 term++;1895}1896if(*term !='/')1897return spec;18981899/* try [spec+1 .. term-1] as regexp */1900*term =0;1901 begin--;/* input is in human terms */1902 line =nth_line(sb, begin);19031904if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1905!(reg_error =regexec(®exp, line,1, match,0))) {1906const char*cp = line + match[0].rm_so;1907const char*nline;19081909while(begin++ < lno) {1910 nline =nth_line(sb, begin);1911if(line <= cp && cp < nline)1912break;1913 line = nline;1914}1915*ret = begin;1916regfree(®exp);1917*term++ ='/';1918return term;1919}1920else{1921char errbuf[1024];1922regerror(reg_error, ®exp, errbuf,1024);1923die("-L parameter '%s':%s", spec +1, errbuf);1924}1925}19261927/*1928 * Parsing of -L option1929 */1930static voidprepare_blame_range(struct scoreboard *sb,1931const char*bottomtop,1932long lno,1933long*bottom,long*top)1934{1935const char*term;19361937 term =parse_loc(bottomtop, sb, lno,1, bottom);1938if(*term ==',') {1939 term =parse_loc(term +1, sb, lno, *bottom +1, top);1940if(*term)1941usage(blame_usage);1942}1943if(*term)1944usage(blame_usage);1945}19461947static intgit_blame_config(const char*var,const char*value,void*cb)1948{1949if(!strcmp(var,"blame.showroot")) {1950 show_root =git_config_bool(var, value);1951return0;1952}1953if(!strcmp(var,"blame.blankboundary")) {1954 blank_boundary =git_config_bool(var, value);1955return0;1956}1957if(!strcmp(var,"blame.date")) {1958if(!value)1959returnconfig_error_nonbool(var);1960 blame_date_mode =parse_date_format(value);1961return0;1962}1963returngit_default_config(var, value, cb);1964}19651966/*1967 * Prepare a dummy commit that represents the work tree (or staged) item.1968 * Note that annotating work tree item never works in the reverse.1969 */1970static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)1971{1972struct commit *commit;1973struct origin *origin;1974unsigned char head_sha1[20];1975struct strbuf buf = STRBUF_INIT;1976const char*ident;1977time_t now;1978int size, len;1979struct cache_entry *ce;1980unsigned mode;19811982if(get_sha1("HEAD", head_sha1))1983die("No such ref: HEAD");19841985time(&now);1986 commit =xcalloc(1,sizeof(*commit));1987 commit->parents =xcalloc(1,sizeof(*commit->parents));1988 commit->parents->item =lookup_commit_reference(head_sha1);1989 commit->object.parsed =1;1990 commit->date = now;1991 commit->object.type = OBJ_COMMIT;19921993 origin =make_origin(commit, path);19941995if(!contents_from ||strcmp("-", contents_from)) {1996struct stat st;1997const char*read_from;19981999if(contents_from) {2000if(stat(contents_from, &st) <0)2001die("Cannot stat%s", contents_from);2002 read_from = contents_from;2003}2004else{2005if(lstat(path, &st) <0)2006die("Cannot lstat%s", path);2007 read_from = path;2008}2009 mode =canon_mode(st.st_mode);2010switch(st.st_mode & S_IFMT) {2011case S_IFREG:2012if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2013die("cannot open or read%s", read_from);2014break;2015case S_IFLNK:2016if(strbuf_readlink(&buf, read_from, st.st_size) <0)2017die("cannot readlink%s", read_from);2018break;2019default:2020die("unsupported file type%s", read_from);2021}2022}2023else{2024/* Reading from stdin */2025 contents_from ="standard input";2026 mode =0;2027if(strbuf_read(&buf,0,0) <0)2028die("read error%sfrom stdin",strerror(errno));2029}2030convert_to_git(path, buf.buf, buf.len, &buf,0);2031 origin->file.ptr = buf.buf;2032 origin->file.size = buf.len;2033pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2034 commit->util = origin;20352036/*2037 * Read the current index, replace the path entry with2038 * origin->blob_sha1 without mucking with its mode or type2039 * bits; we are not going to write this index out -- we just2040 * want to run "diff-index --cached".2041 */2042discard_cache();2043read_cache();20442045 len =strlen(path);2046if(!mode) {2047int pos =cache_name_pos(path, len);2048if(0<= pos)2049 mode = active_cache[pos]->ce_mode;2050else2051/* Let's not bother reading from HEAD tree */2052 mode = S_IFREG |0644;2053}2054 size =cache_entry_size(len);2055 ce =xcalloc(1, size);2056hashcpy(ce->sha1, origin->blob_sha1);2057memcpy(ce->name, path, len);2058 ce->ce_flags =create_ce_flags(len,0);2059 ce->ce_mode =create_ce_mode(mode);2060add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);20612062/*2063 * We are not going to write this out, so this does not matter2064 * right now, but someday we might optimize diff-index --cached2065 * with cache-tree information.2066 */2067cache_tree_invalidate_path(active_cache_tree, path);20682069 commit->buffer =xmalloc(400);2070 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2071snprintf(commit->buffer,400,2072"tree 0000000000000000000000000000000000000000\n"2073"parent%s\n"2074"author%s\n"2075"committer%s\n\n"2076"Version of%sfrom%s\n",2077sha1_to_hex(head_sha1),2078 ident, ident, path, contents_from ? contents_from : path);2079return commit;2080}20812082static const char*prepare_final(struct scoreboard *sb)2083{2084int i;2085const char*final_commit_name = NULL;2086struct rev_info *revs = sb->revs;20872088/*2089 * There must be one and only one positive commit in the2090 * revs->pending array.2091 */2092for(i =0; i < revs->pending.nr; i++) {2093struct object *obj = revs->pending.objects[i].item;2094if(obj->flags & UNINTERESTING)2095continue;2096while(obj->type == OBJ_TAG)2097 obj =deref_tag(obj, NULL,0);2098if(obj->type != OBJ_COMMIT)2099die("Non commit%s?", revs->pending.objects[i].name);2100if(sb->final)2101die("More than one commit to dig from%sand%s?",2102 revs->pending.objects[i].name,2103 final_commit_name);2104 sb->final= (struct commit *) obj;2105 final_commit_name = revs->pending.objects[i].name;2106}2107return final_commit_name;2108}21092110static const char*prepare_initial(struct scoreboard *sb)2111{2112int i;2113const char*final_commit_name = NULL;2114struct rev_info *revs = sb->revs;21152116/*2117 * There must be one and only one negative commit, and it must be2118 * the boundary.2119 */2120for(i =0; i < revs->pending.nr; i++) {2121struct object *obj = revs->pending.objects[i].item;2122if(!(obj->flags & UNINTERESTING))2123continue;2124while(obj->type == OBJ_TAG)2125 obj =deref_tag(obj, NULL,0);2126if(obj->type != OBJ_COMMIT)2127die("Non commit%s?", revs->pending.objects[i].name);2128if(sb->final)2129die("More than one commit to dig down to%sand%s?",2130 revs->pending.objects[i].name,2131 final_commit_name);2132 sb->final= (struct commit *) obj;2133 final_commit_name = revs->pending.objects[i].name;2134}2135if(!final_commit_name)2136die("No commit to dig down to?");2137return final_commit_name;2138}21392140static intblame_copy_callback(const struct option *option,const char*arg,int unset)2141{2142int*opt = option->value;21432144/*2145 * -C enables copy from removed files;2146 * -C -C enables copy from existing files, but only2147 * when blaming a new file;2148 * -C -C -C enables copy from existing files for2149 * everybody2150 */2151if(*opt & PICKAXE_BLAME_COPY_HARDER)2152*opt |= PICKAXE_BLAME_COPY_HARDEST;2153if(*opt & PICKAXE_BLAME_COPY)2154*opt |= PICKAXE_BLAME_COPY_HARDER;2155*opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;21562157if(arg)2158 blame_copy_score =parse_score(arg);2159return0;2160}21612162static intblame_move_callback(const struct option *option,const char*arg,int unset)2163{2164int*opt = option->value;21652166*opt |= PICKAXE_BLAME_MOVE;21672168if(arg)2169 blame_move_score =parse_score(arg);2170return0;2171}21722173static intblame_bottomtop_callback(const struct option *option,const char*arg,int unset)2174{2175const char**bottomtop = option->value;2176if(!arg)2177return-1;2178if(*bottomtop)2179die("More than one '-L n,m' option given");2180*bottomtop = arg;2181return0;2182}21832184intcmd_blame(int argc,const char**argv,const char*prefix)2185{2186struct rev_info revs;2187const char*path;2188struct scoreboard sb;2189struct origin *o;2190struct blame_entry *ent;2191long dashdash_pos, bottom, top, lno;2192const char*final_commit_name = NULL;2193enum object_type type;21942195static const char*bottomtop = NULL;2196static int output_option =0, opt =0;2197static int show_stats =0;2198static const char*revs_file = NULL;2199static const char*contents_from = NULL;2200static const struct option options[] = {2201OPT_BOOLEAN(0,"incremental", &incremental,"Show blame entries as we find them, incrementally"),2202OPT_BOOLEAN('b', NULL, &blank_boundary,"Show blank SHA-1 for boundary commits (Default: off)"),2203OPT_BOOLEAN(0,"root", &show_root,"Do not treat root commits as boundaries (Default: off)"),2204OPT_BOOLEAN(0,"show-stats", &show_stats,"Show work cost statistics"),2205OPT_BIT(0,"score-debug", &output_option,"Show output score for blame entries", OUTPUT_SHOW_SCORE),2206OPT_BIT('f',"show-name", &output_option,"Show original filename (Default: auto)", OUTPUT_SHOW_NAME),2207OPT_BIT('n',"show-number", &output_option,"Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER),2208OPT_BIT('p',"porcelain", &output_option,"Show in a format designed for machine consumption", OUTPUT_PORCELAIN),2209OPT_BIT('c', NULL, &output_option,"Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT),2210OPT_BIT('t', NULL, &output_option,"Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP),2211OPT_BIT('l', NULL, &output_option,"Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME),2212OPT_BIT('s', NULL, &output_option,"Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR),2213OPT_BIT('w', NULL, &xdl_opts,"Ignore whitespace differences", XDF_IGNORE_WHITESPACE),2214OPT_STRING('S', NULL, &revs_file,"file","Use revisions from <file> instead of calling git-rev-list"),2215OPT_STRING(0,"contents", &contents_from,"file","Use <file>'s contents as the final image"),2216{ OPTION_CALLBACK,'C', NULL, &opt,"score","Find line copies within and across files", PARSE_OPT_OPTARG, blame_copy_callback },2217{ OPTION_CALLBACK,'M', NULL, &opt,"score","Find line movements within and across files", PARSE_OPT_OPTARG, blame_move_callback },2218OPT_CALLBACK('L', NULL, &bottomtop,"n,m","Process only line range n,m, counting from 1", blame_bottomtop_callback),2219OPT_END()2220};22212222struct parse_opt_ctx_t ctx;2223int cmd_is_annotate = !strcmp(argv[0],"annotate");22242225git_config(git_blame_config, NULL);2226init_revisions(&revs, NULL);2227 revs.date_mode = blame_date_mode;22282229 save_commit_buffer =0;2230 dashdash_pos =0;22312232parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |2233 PARSE_OPT_KEEP_ARGV0);2234for(;;) {2235switch(parse_options_step(&ctx, options, blame_opt_usage)) {2236case PARSE_OPT_HELP:2237exit(129);2238case PARSE_OPT_DONE:2239if(ctx.argv[0])2240 dashdash_pos = ctx.cpidx;2241goto parse_done;2242}22432244if(!strcmp(ctx.argv[0],"--reverse")) {2245 ctx.argv[0] ="--children";2246 reverse =1;2247}2248parse_revision_opt(&revs, &ctx, options, blame_opt_usage);2249}2250parse_done:2251 argc =parse_options_end(&ctx);22522253if(revs_file &&read_ancestry(revs_file))2254die("reading graft file%sfailed:%s",2255 revs_file,strerror(errno));22562257if(cmd_is_annotate) {2258 output_option |= OUTPUT_ANNOTATE_COMPAT;2259 blame_date_mode = DATE_ISO8601;2260}else{2261 blame_date_mode = revs.date_mode;2262}22632264/* The maximum width used to show the dates */2265switch(blame_date_mode) {2266case DATE_RFC2822:2267 blame_date_width =sizeof("Thu, 19 Oct 2006 16:00:04 -0700");2268break;2269case DATE_ISO8601:2270 blame_date_width =sizeof("2006-10-19 16:00:04 -0700");2271break;2272case DATE_RAW:2273 blame_date_width =sizeof("1161298804 -0700");2274break;2275case DATE_SHORT:2276 blame_date_width =sizeof("2006-10-19");2277break;2278case DATE_RELATIVE:2279/* "normal" is used as the fallback for "relative" */2280case DATE_LOCAL:2281case DATE_NORMAL:2282 blame_date_width =sizeof("Thu Oct 19 16:00:04 2006 -0700");2283break;2284}2285 blame_date_width -=1;/* strip the null */22862287if(DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))2288 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |2289 PICKAXE_BLAME_COPY_HARDER);22902291if(!blame_move_score)2292 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2293if(!blame_copy_score)2294 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22952296/*2297 * We have collected options unknown to us in argv[1..unk]2298 * which are to be passed to revision machinery if we are2299 * going to do the "bottom" processing.2300 *2301 * The remaining are:2302 *2303 * (1) if dashdash_pos != 0, its either2304 * "blame [revisions] -- <path>" or2305 * "blame -- <path> <rev>"2306 *2307 * (2) otherwise, its one of the two:2308 * "blame [revisions] <path>"2309 * "blame <path> <rev>"2310 *2311 * Note that we must strip out <path> from the arguments: we do not2312 * want the path pruning but we may want "bottom" processing.2313 */2314if(dashdash_pos) {2315switch(argc - dashdash_pos -1) {2316case2:/* (1b) */2317if(argc !=4)2318usage_with_options(blame_opt_usage, options);2319/* reorder for the new way: <rev> -- <path> */2320 argv[1] = argv[3];2321 argv[3] = argv[2];2322 argv[2] ="--";2323/* FALLTHROUGH */2324case1:/* (1a) */2325 path =add_prefix(prefix, argv[--argc]);2326 argv[argc] = NULL;2327break;2328default:2329usage_with_options(blame_opt_usage, options);2330}2331}else{2332if(argc <2)2333usage_with_options(blame_opt_usage, options);2334 path =add_prefix(prefix, argv[argc -1]);2335if(argc ==3&& !has_string_in_work_tree(path)) {/* (2b) */2336 path =add_prefix(prefix, argv[1]);2337 argv[1] = argv[2];2338}2339 argv[argc -1] ="--";23402341setup_work_tree();2342if(!has_string_in_work_tree(path))2343die("cannot stat path%s:%s", path,strerror(errno));2344}23452346setup_revisions(argc, argv, &revs, NULL);2347memset(&sb,0,sizeof(sb));23482349 sb.revs = &revs;2350if(!reverse)2351 final_commit_name =prepare_final(&sb);2352else if(contents_from)2353die("--contents and --children do not blend well.");2354else2355 final_commit_name =prepare_initial(&sb);23562357if(!sb.final) {2358/*2359 * "--not A B -- path" without anything positive;2360 * do not default to HEAD, but use the working tree2361 * or "--contents".2362 */2363setup_work_tree();2364 sb.final=fake_working_tree_commit(path, contents_from);2365add_pending_object(&revs, &(sb.final->object),":");2366}2367else if(contents_from)2368die("Cannot use --contents with final commit object name");23692370/*2371 * If we have bottom, this will mark the ancestors of the2372 * bottom commits we would reach while traversing as2373 * uninteresting.2374 */2375if(prepare_revision_walk(&revs))2376die("revision walk setup failed");23772378if(is_null_sha1(sb.final->object.sha1)) {2379char*buf;2380 o = sb.final->util;2381 buf =xmalloc(o->file.size +1);2382memcpy(buf, o->file.ptr, o->file.size +1);2383 sb.final_buf = buf;2384 sb.final_buf_size = o->file.size;2385}2386else{2387 o =get_origin(&sb, sb.final, path);2388if(fill_blob_sha1(o))2389die("no such path%sin%s", path, final_commit_name);23902391 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2392&sb.final_buf_size);2393if(!sb.final_buf)2394die("Cannot read blob%sfor path%s",2395sha1_to_hex(o->blob_sha1),2396 path);2397}2398 num_read_blob++;2399 lno =prepare_lines(&sb);24002401 bottom = top =0;2402if(bottomtop)2403prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2404if(bottom && top && top < bottom) {2405long tmp;2406 tmp = top; top = bottom; bottom = tmp;2407}2408if(bottom <1)2409 bottom =1;2410if(top <1)2411 top = lno;2412 bottom--;2413if(lno < top)2414die("file%shas only%lu lines", path, lno);24152416 ent =xcalloc(1,sizeof(*ent));2417 ent->lno = bottom;2418 ent->num_lines = top - bottom;2419 ent->suspect = o;2420 ent->s_lno = bottom;24212422 sb.ent = ent;2423 sb.path = path;24242425read_mailmap(&mailmap, NULL);24262427if(!incremental)2428setup_pager();24292430assign_blame(&sb, opt);24312432if(incremental)2433return0;24342435coalesce(&sb);24362437if(!(output_option & OUTPUT_PORCELAIN))2438find_alignment(&sb, &output_option);24392440output(&sb, output_option);2441free((void*)sb.final_buf);2442for(ent = sb.ent; ent; ) {2443struct blame_entry *e = ent->next;2444free(ent);2445 ent = e;2446}24472448if(show_stats) {2449printf("num read blob:%d\n", num_read_blob);2450printf("num get patch:%d\n", num_get_patch);2451printf("num commits:%d\n", num_commits);2452}2453return0;2454}