1/* 2 * Pickaxe 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 23static char blame_usage[] ="git blame [options] [rev-opts] [rev] [--] file"; 24 25static const char*blame_opt_usage[] = { 26 blame_usage, 27"", 28"[rev-opts] are documented in git-rev-list(1)", 29 NULL 30}; 31 32static int longest_file; 33static int longest_author; 34static int max_orig_digits; 35static int max_digits; 36static int max_score_digits; 37static int show_root; 38static int reverse; 39static int blank_boundary; 40static int incremental; 41static int xdl_opts = XDF_NEED_MINIMAL; 42static struct string_list mailmap; 43 44#ifndef DEBUG 45#define DEBUG 0 46#endif 47 48/* stats */ 49static int num_read_blob; 50static int num_get_patch; 51static int num_commits; 52 53#define PICKAXE_BLAME_MOVE 01 54#define PICKAXE_BLAME_COPY 02 55#define PICKAXE_BLAME_COPY_HARDER 04 56#define PICKAXE_BLAME_COPY_HARDEST 010 57 58/* 59 * blame for a blame_entry with score lower than these thresholds 60 * is not passed to the parent using move/copy logic. 61 */ 62static unsigned blame_move_score; 63static unsigned blame_copy_score; 64#define BLAME_DEFAULT_MOVE_SCORE 20 65#define BLAME_DEFAULT_COPY_SCORE 40 66 67/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ 68#define METAINFO_SHOWN (1u<<12) 69#define MORE_THAN_ONE_PATH (1u<<13) 70 71/* 72 * One blob in a commit that is being suspected 73 */ 74struct origin { 75int refcnt; 76struct commit *commit; 77 mmfile_t file; 78unsigned char blob_sha1[20]; 79char path[FLEX_ARRAY]; 80}; 81 82/* 83 * Given an origin, prepare mmfile_t structure to be used by the 84 * diff machinery 85 */ 86static voidfill_origin_blob(struct origin *o, mmfile_t *file) 87{ 88if(!o->file.ptr) { 89enum object_type type; 90 num_read_blob++; 91 file->ptr =read_sha1_file(o->blob_sha1, &type, 92(unsigned long*)(&(file->size))); 93if(!file->ptr) 94die("Cannot read blob%sfor path%s", 95sha1_to_hex(o->blob_sha1), 96 o->path); 97 o->file = *file; 98} 99else 100*file = o->file; 101} 102 103/* 104 * Origin is refcounted and usually we keep the blob contents to be 105 * reused. 106 */ 107staticinlinestruct origin *origin_incref(struct origin *o) 108{ 109if(o) 110 o->refcnt++; 111return o; 112} 113 114static voidorigin_decref(struct origin *o) 115{ 116if(o && --o->refcnt <=0) { 117free(o->file.ptr); 118free(o); 119} 120} 121 122static voiddrop_origin_blob(struct origin *o) 123{ 124if(o->file.ptr) { 125free(o->file.ptr); 126 o->file.ptr = NULL; 127} 128} 129 130/* 131 * Each group of lines is described by a blame_entry; it can be split 132 * as we pass blame to the parents. They form a linked list in the 133 * scoreboard structure, sorted by the target line number. 134 */ 135struct blame_entry { 136struct blame_entry *prev; 137struct blame_entry *next; 138 139/* the first line of this group in the final image; 140 * internally all line numbers are 0 based. 141 */ 142int lno; 143 144/* how many lines this group has */ 145int num_lines; 146 147/* the commit that introduced this group into the final image */ 148struct origin *suspect; 149 150/* true if the suspect is truly guilty; false while we have not 151 * checked if the group came from one of its parents. 152 */ 153char guilty; 154 155/* true if the entry has been scanned for copies in the current parent 156 */ 157char scanned; 158 159/* the line number of the first line of this group in the 160 * suspect's file; internally all line numbers are 0 based. 161 */ 162int s_lno; 163 164/* how significant this entry is -- cached to avoid 165 * scanning the lines over and over. 166 */ 167unsigned score; 168}; 169 170/* 171 * The current state of the blame assignment. 172 */ 173struct scoreboard { 174/* the final commit (i.e. where we started digging from) */ 175struct commit *final; 176struct rev_info *revs; 177const char*path; 178 179/* 180 * The contents in the final image. 181 * Used by many functions to obtain contents of the nth line, 182 * indexed with scoreboard.lineno[blame_entry.lno]. 183 */ 184const char*final_buf; 185unsigned long final_buf_size; 186 187/* linked list of blames */ 188struct blame_entry *ent; 189 190/* look-up a line in the final buffer */ 191int num_lines; 192int*lineno; 193}; 194 195staticinlineintsame_suspect(struct origin *a,struct origin *b) 196{ 197if(a == b) 198return1; 199if(a->commit != b->commit) 200return0; 201return!strcmp(a->path, b->path); 202} 203 204static voidsanity_check_refcnt(struct scoreboard *); 205 206/* 207 * If two blame entries that are next to each other came from 208 * contiguous lines in the same origin (i.e. <commit, path> pair), 209 * merge them together. 210 */ 211static voidcoalesce(struct scoreboard *sb) 212{ 213struct blame_entry *ent, *next; 214 215for(ent = sb->ent; ent && (next = ent->next); ent = next) { 216if(same_suspect(ent->suspect, next->suspect) && 217 ent->guilty == next->guilty && 218 ent->s_lno + ent->num_lines == next->s_lno) { 219 ent->num_lines += next->num_lines; 220 ent->next = next->next; 221if(ent->next) 222 ent->next->prev = ent; 223origin_decref(next->suspect); 224free(next); 225 ent->score =0; 226 next = ent;/* again */ 227} 228} 229 230if(DEBUG)/* sanity */ 231sanity_check_refcnt(sb); 232} 233 234/* 235 * Given a commit and a path in it, create a new origin structure. 236 * The callers that add blame to the scoreboard should use 237 * get_origin() to obtain shared, refcounted copy instead of calling 238 * this function directly. 239 */ 240static struct origin *make_origin(struct commit *commit,const char*path) 241{ 242struct origin *o; 243 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 244 o->commit = commit; 245 o->refcnt =1; 246strcpy(o->path, path); 247return o; 248} 249 250/* 251 * Locate an existing origin or create a new one. 252 */ 253static struct origin *get_origin(struct scoreboard *sb, 254struct commit *commit, 255const char*path) 256{ 257struct blame_entry *e; 258 259for(e = sb->ent; e; e = e->next) { 260if(e->suspect->commit == commit && 261!strcmp(e->suspect->path, path)) 262returnorigin_incref(e->suspect); 263} 264returnmake_origin(commit, path); 265} 266 267/* 268 * Fill the blob_sha1 field of an origin if it hasn't, so that later 269 * call to fill_origin_blob() can use it to locate the data. blob_sha1 270 * for an origin is also used to pass the blame for the entire file to 271 * the parent to detect the case where a child's blob is identical to 272 * that of its parent's. 273 */ 274static intfill_blob_sha1(struct origin *origin) 275{ 276unsigned mode; 277 278if(!is_null_sha1(origin->blob_sha1)) 279return0; 280if(get_tree_entry(origin->commit->object.sha1, 281 origin->path, 282 origin->blob_sha1, &mode)) 283goto error_out; 284if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 285goto error_out; 286return0; 287 error_out: 288hashclr(origin->blob_sha1); 289return-1; 290} 291 292/* 293 * We have an origin -- check if the same path exists in the 294 * parent and return an origin structure to represent it. 295 */ 296static struct origin *find_origin(struct scoreboard *sb, 297struct commit *parent, 298struct origin *origin) 299{ 300struct origin *porigin = NULL; 301struct diff_options diff_opts; 302const char*paths[2]; 303 304if(parent->util) { 305/* 306 * Each commit object can cache one origin in that 307 * commit. This is a freestanding copy of origin and 308 * not refcounted. 309 */ 310struct origin *cached = parent->util; 311if(!strcmp(cached->path, origin->path)) { 312/* 313 * The same path between origin and its parent 314 * without renaming -- the most common case. 315 */ 316 porigin =get_origin(sb, parent, cached->path); 317 318/* 319 * If the origin was newly created (i.e. get_origin 320 * would call make_origin if none is found in the 321 * scoreboard), it does not know the blob_sha1, 322 * so copy it. Otherwise porigin was in the 323 * scoreboard and already knows blob_sha1. 324 */ 325if(porigin->refcnt ==1) 326hashcpy(porigin->blob_sha1, cached->blob_sha1); 327return porigin; 328} 329/* otherwise it was not very useful; free it */ 330free(parent->util); 331 parent->util = NULL; 332} 333 334/* See if the origin->path is different between parent 335 * and origin first. Most of the time they are the 336 * same and diff-tree is fairly efficient about this. 337 */ 338diff_setup(&diff_opts); 339DIFF_OPT_SET(&diff_opts, RECURSIVE); 340 diff_opts.detect_rename =0; 341 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 342 paths[0] = origin->path; 343 paths[1] = NULL; 344 345diff_tree_setup_paths(paths, &diff_opts); 346if(diff_setup_done(&diff_opts) <0) 347die("diff-setup"); 348 349if(is_null_sha1(origin->commit->object.sha1)) 350do_diff_cache(parent->tree->object.sha1, &diff_opts); 351else 352diff_tree_sha1(parent->tree->object.sha1, 353 origin->commit->tree->object.sha1, 354"", &diff_opts); 355diffcore_std(&diff_opts); 356 357/* It is either one entry that says "modified", or "created", 358 * or nothing. 359 */ 360if(!diff_queued_diff.nr) { 361/* The path is the same as parent */ 362 porigin =get_origin(sb, parent, origin->path); 363hashcpy(porigin->blob_sha1, origin->blob_sha1); 364} 365else if(diff_queued_diff.nr !=1) 366die("internal error in blame::find_origin"); 367else{ 368struct diff_filepair *p = diff_queued_diff.queue[0]; 369switch(p->status) { 370default: 371die("internal error in blame::find_origin (%c)", 372 p->status); 373case'M': 374 porigin =get_origin(sb, parent, origin->path); 375hashcpy(porigin->blob_sha1, p->one->sha1); 376break; 377case'A': 378case'T': 379/* Did not exist in parent, or type changed */ 380break; 381} 382} 383diff_flush(&diff_opts); 384diff_tree_release_paths(&diff_opts); 385if(porigin) { 386/* 387 * Create a freestanding copy that is not part of 388 * the refcounted origin found in the scoreboard, and 389 * cache it in the commit. 390 */ 391struct origin *cached; 392 393 cached =make_origin(porigin->commit, porigin->path); 394hashcpy(cached->blob_sha1, porigin->blob_sha1); 395 parent->util = cached; 396} 397return porigin; 398} 399 400/* 401 * We have an origin -- find the path that corresponds to it in its 402 * parent and return an origin structure to represent it. 403 */ 404static struct origin *find_rename(struct scoreboard *sb, 405struct commit *parent, 406struct origin *origin) 407{ 408struct origin *porigin = NULL; 409struct diff_options diff_opts; 410int i; 411const char*paths[2]; 412 413diff_setup(&diff_opts); 414DIFF_OPT_SET(&diff_opts, RECURSIVE); 415 diff_opts.detect_rename = DIFF_DETECT_RENAME; 416 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 417 diff_opts.single_follow = origin->path; 418 paths[0] = NULL; 419diff_tree_setup_paths(paths, &diff_opts); 420if(diff_setup_done(&diff_opts) <0) 421die("diff-setup"); 422 423if(is_null_sha1(origin->commit->object.sha1)) 424do_diff_cache(parent->tree->object.sha1, &diff_opts); 425else 426diff_tree_sha1(parent->tree->object.sha1, 427 origin->commit->tree->object.sha1, 428"", &diff_opts); 429diffcore_std(&diff_opts); 430 431for(i =0; i < diff_queued_diff.nr; i++) { 432struct diff_filepair *p = diff_queued_diff.queue[i]; 433if((p->status =='R'|| p->status =='C') && 434!strcmp(p->two->path, origin->path)) { 435 porigin =get_origin(sb, parent, p->one->path); 436hashcpy(porigin->blob_sha1, p->one->sha1); 437break; 438} 439} 440diff_flush(&diff_opts); 441diff_tree_release_paths(&diff_opts); 442return porigin; 443} 444 445/* 446 * Parsing of patch chunks... 447 */ 448struct chunk { 449/* line number in postimage; up to but not including this 450 * line is the same as preimage 451 */ 452int same; 453 454/* preimage line number after this chunk */ 455int p_next; 456 457/* postimage line number after this chunk */ 458int t_next; 459}; 460 461struct patch { 462struct chunk *chunks; 463int num; 464}; 465 466struct blame_diff_state { 467struct patch *ret; 468unsigned hunk_post_context; 469unsigned hunk_in_pre_context :1; 470}; 471 472static voidprocess_u_diff(void*state_,char*line,unsigned long len) 473{ 474struct blame_diff_state *state = state_; 475struct chunk *chunk; 476int off1, off2, len1, len2, num; 477 478 num = state->ret->num; 479if(len <4|| line[0] !='@'|| line[1] !='@') { 480if(state->hunk_in_pre_context && line[0] ==' ') 481 state->ret->chunks[num -1].same++; 482else{ 483 state->hunk_in_pre_context =0; 484if(line[0] ==' ') 485 state->hunk_post_context++; 486else 487 state->hunk_post_context =0; 488} 489return; 490} 491 492if(num && state->hunk_post_context) { 493 chunk = &state->ret->chunks[num -1]; 494 chunk->p_next -= state->hunk_post_context; 495 chunk->t_next -= state->hunk_post_context; 496} 497 state->ret->num = ++num; 498 state->ret->chunks =xrealloc(state->ret->chunks, 499sizeof(struct chunk) * num); 500 chunk = &state->ret->chunks[num -1]; 501if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 502 state->ret->num--; 503return; 504} 505 506/* Line numbers in patch output are one based. */ 507 off1--; 508 off2--; 509 510 chunk->same = len2 ? off2 : (off2 +1); 511 512 chunk->p_next = off1 + (len1 ? len1 :1); 513 chunk->t_next = chunk->same + len2; 514 state->hunk_in_pre_context =1; 515 state->hunk_post_context =0; 516} 517 518static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 519int context) 520{ 521struct blame_diff_state state; 522 xpparam_t xpp; 523 xdemitconf_t xecfg; 524 xdemitcb_t ecb; 525 526 xpp.flags = xdl_opts; 527memset(&xecfg,0,sizeof(xecfg)); 528 xecfg.ctxlen = context; 529memset(&state,0,sizeof(state)); 530 state.ret =xmalloc(sizeof(struct patch)); 531 state.ret->chunks = NULL; 532 state.ret->num =0; 533 534xdi_diff_outf(file_p, file_o, process_u_diff, &state, &xpp, &xecfg, &ecb); 535 536if(state.ret->num) { 537struct chunk *chunk; 538 chunk = &state.ret->chunks[state.ret->num -1]; 539 chunk->p_next -= state.hunk_post_context; 540 chunk->t_next -= state.hunk_post_context; 541} 542return state.ret; 543} 544 545/* 546 * Run diff between two origins and grab the patch output, so that 547 * we can pass blame for lines origin is currently suspected for 548 * to its parent. 549 */ 550static struct patch *get_patch(struct origin *parent,struct origin *origin) 551{ 552 mmfile_t file_p, file_o; 553struct patch *patch; 554 555fill_origin_blob(parent, &file_p); 556fill_origin_blob(origin, &file_o); 557if(!file_p.ptr || !file_o.ptr) 558return NULL; 559 patch =compare_buffer(&file_p, &file_o,0); 560 num_get_patch++; 561return patch; 562} 563 564static voidfree_patch(struct patch *p) 565{ 566free(p->chunks); 567free(p); 568} 569 570/* 571 * Link in a new blame entry to the scoreboard. Entries that cover the 572 * same line range have been removed from the scoreboard previously. 573 */ 574static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 575{ 576struct blame_entry *ent, *prev = NULL; 577 578origin_incref(e->suspect); 579 580for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 581 prev = ent; 582 583/* prev, if not NULL, is the last one that is below e */ 584 e->prev = prev; 585if(prev) { 586 e->next = prev->next; 587 prev->next = e; 588} 589else{ 590 e->next = sb->ent; 591 sb->ent = e; 592} 593if(e->next) 594 e->next->prev = e; 595} 596 597/* 598 * src typically is on-stack; we want to copy the information in it to 599 * a malloced blame_entry that is already on the linked list of the 600 * scoreboard. The origin of dst loses a refcnt while the origin of src 601 * gains one. 602 */ 603static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 604{ 605struct blame_entry *p, *n; 606 607 p = dst->prev; 608 n = dst->next; 609origin_incref(src->suspect); 610origin_decref(dst->suspect); 611memcpy(dst, src,sizeof(*src)); 612 dst->prev = p; 613 dst->next = n; 614 dst->score =0; 615} 616 617static const char*nth_line(struct scoreboard *sb,int lno) 618{ 619return sb->final_buf + sb->lineno[lno]; 620} 621 622/* 623 * It is known that lines between tlno to same came from parent, and e 624 * has an overlap with that range. it also is known that parent's 625 * line plno corresponds to e's line tlno. 626 * 627 * <---- e -----> 628 * <------> 629 * <------------> 630 * <------------> 631 * <------------------> 632 * 633 * Split e into potentially three parts; before this chunk, the chunk 634 * to be blamed for the parent, and after that portion. 635 */ 636static voidsplit_overlap(struct blame_entry *split, 637struct blame_entry *e, 638int tlno,int plno,int same, 639struct origin *parent) 640{ 641int chunk_end_lno; 642memset(split,0,sizeof(struct blame_entry [3])); 643 644if(e->s_lno < tlno) { 645/* there is a pre-chunk part not blamed on parent */ 646 split[0].suspect =origin_incref(e->suspect); 647 split[0].lno = e->lno; 648 split[0].s_lno = e->s_lno; 649 split[0].num_lines = tlno - e->s_lno; 650 split[1].lno = e->lno + tlno - e->s_lno; 651 split[1].s_lno = plno; 652} 653else{ 654 split[1].lno = e->lno; 655 split[1].s_lno = plno + (e->s_lno - tlno); 656} 657 658if(same < e->s_lno + e->num_lines) { 659/* there is a post-chunk part not blamed on parent */ 660 split[2].suspect =origin_incref(e->suspect); 661 split[2].lno = e->lno + (same - e->s_lno); 662 split[2].s_lno = e->s_lno + (same - e->s_lno); 663 split[2].num_lines = e->s_lno + e->num_lines - same; 664 chunk_end_lno = split[2].lno; 665} 666else 667 chunk_end_lno = e->lno + e->num_lines; 668 split[1].num_lines = chunk_end_lno - split[1].lno; 669 670/* 671 * if it turns out there is nothing to blame the parent for, 672 * forget about the splitting. !split[1].suspect signals this. 673 */ 674if(split[1].num_lines <1) 675return; 676 split[1].suspect =origin_incref(parent); 677} 678 679/* 680 * split_overlap() divided an existing blame e into up to three parts 681 * in split. Adjust the linked list of blames in the scoreboard to 682 * reflect the split. 683 */ 684static voidsplit_blame(struct scoreboard *sb, 685struct blame_entry *split, 686struct blame_entry *e) 687{ 688struct blame_entry *new_entry; 689 690if(split[0].suspect && split[2].suspect) { 691/* The first part (reuse storage for the existing entry e) */ 692dup_entry(e, &split[0]); 693 694/* The last part -- me */ 695 new_entry =xmalloc(sizeof(*new_entry)); 696memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 697add_blame_entry(sb, new_entry); 698 699/* ... and the middle part -- parent */ 700 new_entry =xmalloc(sizeof(*new_entry)); 701memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 702add_blame_entry(sb, new_entry); 703} 704else if(!split[0].suspect && !split[2].suspect) 705/* 706 * The parent covers the entire area; reuse storage for 707 * e and replace it with the parent. 708 */ 709dup_entry(e, &split[1]); 710else if(split[0].suspect) { 711/* me and then parent */ 712dup_entry(e, &split[0]); 713 714 new_entry =xmalloc(sizeof(*new_entry)); 715memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 716add_blame_entry(sb, new_entry); 717} 718else{ 719/* parent and then me */ 720dup_entry(e, &split[1]); 721 722 new_entry =xmalloc(sizeof(*new_entry)); 723memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 724add_blame_entry(sb, new_entry); 725} 726 727if(DEBUG) {/* sanity */ 728struct blame_entry *ent; 729int lno = sb->ent->lno, corrupt =0; 730 731for(ent = sb->ent; ent; ent = ent->next) { 732if(lno != ent->lno) 733 corrupt =1; 734if(ent->s_lno <0) 735 corrupt =1; 736 lno += ent->num_lines; 737} 738if(corrupt) { 739 lno = sb->ent->lno; 740for(ent = sb->ent; ent; ent = ent->next) { 741printf("L%8d l%8d n%8d\n", 742 lno, ent->lno, ent->num_lines); 743 lno = ent->lno + ent->num_lines; 744} 745die("oops"); 746} 747} 748} 749 750/* 751 * After splitting the blame, the origins used by the 752 * on-stack blame_entry should lose one refcnt each. 753 */ 754static voiddecref_split(struct blame_entry *split) 755{ 756int i; 757 758for(i =0; i <3; i++) 759origin_decref(split[i].suspect); 760} 761 762/* 763 * Helper for blame_chunk(). blame_entry e is known to overlap with 764 * the patch hunk; split it and pass blame to the parent. 765 */ 766static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 767int tlno,int plno,int same, 768struct origin *parent) 769{ 770struct blame_entry split[3]; 771 772split_overlap(split, e, tlno, plno, same, parent); 773if(split[1].suspect) 774split_blame(sb, split, e); 775decref_split(split); 776} 777 778/* 779 * Find the line number of the last line the target is suspected for. 780 */ 781static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 782{ 783struct blame_entry *e; 784int last_in_target = -1; 785 786for(e = sb->ent; e; e = e->next) { 787if(e->guilty || !same_suspect(e->suspect, target)) 788continue; 789if(last_in_target < e->s_lno + e->num_lines) 790 last_in_target = e->s_lno + e->num_lines; 791} 792return last_in_target; 793} 794 795/* 796 * Process one hunk from the patch between the current suspect for 797 * blame_entry e and its parent. Find and split the overlap, and 798 * pass blame to the overlapping part to the parent. 799 */ 800static voidblame_chunk(struct scoreboard *sb, 801int tlno,int plno,int same, 802struct origin *target,struct origin *parent) 803{ 804struct blame_entry *e; 805 806for(e = sb->ent; e; e = e->next) { 807if(e->guilty || !same_suspect(e->suspect, target)) 808continue; 809if(same <= e->s_lno) 810continue; 811if(tlno < e->s_lno + e->num_lines) 812blame_overlap(sb, e, tlno, plno, same, parent); 813} 814} 815 816/* 817 * We are looking at the origin 'target' and aiming to pass blame 818 * for the lines it is suspected to its parent. Run diff to find 819 * which lines came from parent and pass blame for them. 820 */ 821static intpass_blame_to_parent(struct scoreboard *sb, 822struct origin *target, 823struct origin *parent) 824{ 825int i, last_in_target, plno, tlno; 826struct patch *patch; 827 828 last_in_target =find_last_in_target(sb, target); 829if(last_in_target <0) 830return1;/* nothing remains for this target */ 831 832 patch =get_patch(parent, target); 833 plno = tlno =0; 834for(i =0; i < patch->num; i++) { 835struct chunk *chunk = &patch->chunks[i]; 836 837blame_chunk(sb, tlno, plno, chunk->same, target, parent); 838 plno = chunk->p_next; 839 tlno = chunk->t_next; 840} 841/* The rest (i.e. anything after tlno) are the same as the parent */ 842blame_chunk(sb, tlno, plno, last_in_target, target, parent); 843 844free_patch(patch); 845return0; 846} 847 848/* 849 * The lines in blame_entry after splitting blames many times can become 850 * very small and trivial, and at some point it becomes pointless to 851 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 852 * ordinary C program, and it is not worth to say it was copied from 853 * totally unrelated file in the parent. 854 * 855 * Compute how trivial the lines in the blame_entry are. 856 */ 857static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 858{ 859unsigned score; 860const char*cp, *ep; 861 862if(e->score) 863return e->score; 864 865 score =1; 866 cp =nth_line(sb, e->lno); 867 ep =nth_line(sb, e->lno + e->num_lines); 868while(cp < ep) { 869unsigned ch = *((unsigned char*)cp); 870if(isalnum(ch)) 871 score++; 872 cp++; 873} 874 e->score = score; 875return score; 876} 877 878/* 879 * best_so_far[] and this[] are both a split of an existing blame_entry 880 * that passes blame to the parent. Maintain best_so_far the best split 881 * so far, by comparing this and best_so_far and copying this into 882 * bst_so_far as needed. 883 */ 884static voidcopy_split_if_better(struct scoreboard *sb, 885struct blame_entry *best_so_far, 886struct blame_entry *this) 887{ 888int i; 889 890if(!this[1].suspect) 891return; 892if(best_so_far[1].suspect) { 893if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 894return; 895} 896 897for(i =0; i <3; i++) 898origin_incref(this[i].suspect); 899decref_split(best_so_far); 900memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 901} 902 903/* 904 * We are looking at a part of the final image represented by 905 * ent (tlno and same are offset by ent->s_lno). 906 * tlno is where we are looking at in the final image. 907 * up to (but not including) same match preimage. 908 * plno is where we are looking at in the preimage. 909 * 910 * <-------------- final image ----------------------> 911 * <------ent------> 912 * ^tlno ^same 913 * <---------preimage-----> 914 * ^plno 915 * 916 * All line numbers are 0-based. 917 */ 918static voidhandle_split(struct scoreboard *sb, 919struct blame_entry *ent, 920int tlno,int plno,int same, 921struct origin *parent, 922struct blame_entry *split) 923{ 924if(ent->num_lines <= tlno) 925return; 926if(tlno < same) { 927struct blame_entry this[3]; 928 tlno += ent->s_lno; 929 same += ent->s_lno; 930split_overlap(this, ent, tlno, plno, same, parent); 931copy_split_if_better(sb, split,this); 932decref_split(this); 933} 934} 935 936/* 937 * Find the lines from parent that are the same as ent so that 938 * we can pass blames to it. file_p has the blob contents for 939 * the parent. 940 */ 941static voidfind_copy_in_blob(struct scoreboard *sb, 942struct blame_entry *ent, 943struct origin *parent, 944struct blame_entry *split, 945 mmfile_t *file_p) 946{ 947const char*cp; 948int cnt; 949 mmfile_t file_o; 950struct patch *patch; 951int i, plno, tlno; 952 953/* 954 * Prepare mmfile that contains only the lines in ent. 955 */ 956 cp =nth_line(sb, ent->lno); 957 file_o.ptr = (char*) cp; 958 cnt = ent->num_lines; 959 960while(cnt && cp < sb->final_buf + sb->final_buf_size) { 961if(*cp++ =='\n') 962 cnt--; 963} 964 file_o.size = cp - file_o.ptr; 965 966 patch =compare_buffer(file_p, &file_o,1); 967 968/* 969 * file_o is a part of final image we are annotating. 970 * file_p partially may match that image. 971 */ 972memset(split,0,sizeof(struct blame_entry [3])); 973 plno = tlno =0; 974for(i =0; i < patch->num; i++) { 975struct chunk *chunk = &patch->chunks[i]; 976 977handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 978 plno = chunk->p_next; 979 tlno = chunk->t_next; 980} 981/* remainder, if any, all match the preimage */ 982handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 983free_patch(patch); 984} 985 986/* 987 * See if lines currently target is suspected for can be attributed to 988 * parent. 989 */ 990static intfind_move_in_parent(struct scoreboard *sb, 991struct origin *target, 992struct origin *parent) 993{ 994int last_in_target, made_progress; 995struct blame_entry *e, split[3]; 996 mmfile_t file_p; 997 998 last_in_target =find_last_in_target(sb, target); 999if(last_in_target <0)1000return1;/* nothing remains for this target */10011002fill_origin_blob(parent, &file_p);1003if(!file_p.ptr)1004return0;10051006 made_progress =1;1007while(made_progress) {1008 made_progress =0;1009for(e = sb->ent; e; e = e->next) {1010if(e->guilty || !same_suspect(e->suspect, target) ||1011ent_score(sb, e) < blame_move_score)1012continue;1013find_copy_in_blob(sb, e, parent, split, &file_p);1014if(split[1].suspect &&1015 blame_move_score <ent_score(sb, &split[1])) {1016split_blame(sb, split, e);1017 made_progress =1;1018}1019decref_split(split);1020}1021}1022return0;1023}10241025struct blame_list {1026struct blame_entry *ent;1027struct blame_entry split[3];1028};10291030/*1031 * Count the number of entries the target is suspected for,1032 * and prepare a list of entry and the best split.1033 */1034static struct blame_list *setup_blame_list(struct scoreboard *sb,1035struct origin *target,1036int min_score,1037int*num_ents_p)1038{1039struct blame_entry *e;1040int num_ents, i;1041struct blame_list *blame_list = NULL;10421043for(e = sb->ent, num_ents =0; e; e = e->next)1044if(!e->scanned && !e->guilty &&1045same_suspect(e->suspect, target) &&1046 min_score <ent_score(sb, e))1047 num_ents++;1048if(num_ents) {1049 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1050for(e = sb->ent, i =0; e; e = e->next)1051if(!e->scanned && !e->guilty &&1052same_suspect(e->suspect, target) &&1053 min_score <ent_score(sb, e))1054 blame_list[i++].ent = e;1055}1056*num_ents_p = num_ents;1057return blame_list;1058}10591060/*1061 * Reset the scanned status on all entries.1062 */1063static voidreset_scanned_flag(struct scoreboard *sb)1064{1065struct blame_entry *e;1066for(e = sb->ent; e; e = e->next)1067 e->scanned =0;1068}10691070/*1071 * For lines target is suspected for, see if we can find code movement1072 * across file boundary from the parent commit. porigin is the path1073 * in the parent we already tried.1074 */1075static intfind_copy_in_parent(struct scoreboard *sb,1076struct origin *target,1077struct commit *parent,1078struct origin *porigin,1079int opt)1080{1081struct diff_options diff_opts;1082const char*paths[1];1083int i, j;1084int retval;1085struct blame_list *blame_list;1086int num_ents;10871088 blame_list =setup_blame_list(sb, target, blame_copy_score, &num_ents);1089if(!blame_list)1090return1;/* nothing remains for this target */10911092diff_setup(&diff_opts);1093DIFF_OPT_SET(&diff_opts, RECURSIVE);1094 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10951096 paths[0] = NULL;1097diff_tree_setup_paths(paths, &diff_opts);1098if(diff_setup_done(&diff_opts) <0)1099die("diff-setup");11001101/* Try "find copies harder" on new path if requested;1102 * we do not want to use diffcore_rename() actually to1103 * match things up; find_copies_harder is set only to1104 * force diff_tree_sha1() to feed all filepairs to diff_queue,1105 * and this code needs to be after diff_setup_done(), which1106 * usually makes find-copies-harder imply copy detection.1107 */1108if((opt & PICKAXE_BLAME_COPY_HARDEST)1109|| ((opt & PICKAXE_BLAME_COPY_HARDER)1110&& (!porigin ||strcmp(target->path, porigin->path))))1111DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);11121113if(is_null_sha1(target->commit->object.sha1))1114do_diff_cache(parent->tree->object.sha1, &diff_opts);1115else1116diff_tree_sha1(parent->tree->object.sha1,1117 target->commit->tree->object.sha1,1118"", &diff_opts);11191120if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1121diffcore_std(&diff_opts);11221123 retval =0;1124while(1) {1125int made_progress =0;11261127for(i =0; i < diff_queued_diff.nr; i++) {1128struct diff_filepair *p = diff_queued_diff.queue[i];1129struct origin *norigin;1130 mmfile_t file_p;1131struct blame_entry this[3];11321133if(!DIFF_FILE_VALID(p->one))1134continue;/* does not exist in parent */1135if(S_ISGITLINK(p->one->mode))1136continue;/* ignore git links */1137if(porigin && !strcmp(p->one->path, porigin->path))1138/* find_move already dealt with this path */1139continue;11401141 norigin =get_origin(sb, parent, p->one->path);1142hashcpy(norigin->blob_sha1, p->one->sha1);1143fill_origin_blob(norigin, &file_p);1144if(!file_p.ptr)1145continue;11461147for(j =0; j < num_ents; j++) {1148find_copy_in_blob(sb, blame_list[j].ent,1149 norigin,this, &file_p);1150copy_split_if_better(sb, blame_list[j].split,1151this);1152decref_split(this);1153}1154origin_decref(norigin);1155}11561157for(j =0; j < num_ents; j++) {1158struct blame_entry *split = blame_list[j].split;1159if(split[1].suspect &&1160 blame_copy_score <ent_score(sb, &split[1])) {1161split_blame(sb, split, blame_list[j].ent);1162 made_progress =1;1163}1164else1165 blame_list[j].ent->scanned =1;1166decref_split(split);1167}1168free(blame_list);11691170if(!made_progress)1171break;1172 blame_list =setup_blame_list(sb, target, blame_copy_score, &num_ents);1173if(!blame_list) {1174 retval =1;1175break;1176}1177}1178reset_scanned_flag(sb);1179diff_flush(&diff_opts);1180diff_tree_release_paths(&diff_opts);1181return retval;1182}11831184/*1185 * The blobs of origin and porigin exactly match, so everything1186 * origin is suspected for can be blamed on the parent.1187 */1188static voidpass_whole_blame(struct scoreboard *sb,1189struct origin *origin,struct origin *porigin)1190{1191struct blame_entry *e;11921193if(!porigin->file.ptr && origin->file.ptr) {1194/* Steal its file */1195 porigin->file = origin->file;1196 origin->file.ptr = NULL;1197}1198for(e = sb->ent; e; e = e->next) {1199if(!same_suspect(e->suspect, origin))1200continue;1201origin_incref(porigin);1202origin_decref(e->suspect);1203 e->suspect = porigin;1204}1205}12061207/*1208 * We pass blame from the current commit to its parents. We keep saying1209 * "parent" (and "porigin"), but what we mean is to find scapegoat to1210 * exonerate ourselves.1211 */1212static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit)1213{1214if(!reverse)1215return commit->parents;1216returnlookup_decoration(&revs->children, &commit->object);1217}12181219static intnum_scapegoats(struct rev_info *revs,struct commit *commit)1220{1221int cnt;1222struct commit_list *l =first_scapegoat(revs, commit);1223for(cnt =0; l; l = l->next)1224 cnt++;1225return cnt;1226}12271228#define MAXSG 1612291230static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1231{1232struct rev_info *revs = sb->revs;1233int i, pass, num_sg;1234struct commit *commit = origin->commit;1235struct commit_list *sg;1236struct origin *sg_buf[MAXSG];1237struct origin *porigin, **sg_origin = sg_buf;12381239 num_sg =num_scapegoats(revs, commit);1240if(!num_sg)1241goto finish;1242else if(num_sg <ARRAY_SIZE(sg_buf))1243memset(sg_buf,0,sizeof(sg_buf));1244else1245 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));12461247/*1248 * The first pass looks for unrenamed path to optimize for1249 * common cases, then we look for renames in the second pass.1250 */1251for(pass =0; pass <2; pass++) {1252struct origin *(*find)(struct scoreboard *,1253struct commit *,struct origin *);1254 find = pass ? find_rename : find_origin;12551256for(i =0, sg =first_scapegoat(revs, commit);1257 i < num_sg && sg;1258 sg = sg->next, i++) {1259struct commit *p = sg->item;1260int j, same;12611262if(sg_origin[i])1263continue;1264if(parse_commit(p))1265continue;1266 porigin =find(sb, p, origin);1267if(!porigin)1268continue;1269if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1270pass_whole_blame(sb, origin, porigin);1271origin_decref(porigin);1272goto finish;1273}1274for(j = same =0; j < i; j++)1275if(sg_origin[j] &&1276!hashcmp(sg_origin[j]->blob_sha1,1277 porigin->blob_sha1)) {1278 same =1;1279break;1280}1281if(!same)1282 sg_origin[i] = porigin;1283else1284origin_decref(porigin);1285}1286}12871288 num_commits++;1289for(i =0, sg =first_scapegoat(revs, commit);1290 i < num_sg && sg;1291 sg = sg->next, i++) {1292struct origin *porigin = sg_origin[i];1293if(!porigin)1294continue;1295if(pass_blame_to_parent(sb, origin, porigin))1296goto finish;1297}12981299/*1300 * Optionally find moves in parents' files.1301 */1302if(opt & PICKAXE_BLAME_MOVE)1303for(i =0, sg =first_scapegoat(revs, commit);1304 i < num_sg && sg;1305 sg = sg->next, i++) {1306struct origin *porigin = sg_origin[i];1307if(!porigin)1308continue;1309if(find_move_in_parent(sb, origin, porigin))1310goto finish;1311}13121313/*1314 * Optionally find copies from parents' files.1315 */1316if(opt & PICKAXE_BLAME_COPY)1317for(i =0, sg =first_scapegoat(revs, commit);1318 i < num_sg && sg;1319 sg = sg->next, i++) {1320struct origin *porigin = sg_origin[i];1321if(find_copy_in_parent(sb, origin, sg->item,1322 porigin, opt))1323goto finish;1324}13251326 finish:1327for(i =0; i < num_sg; i++) {1328if(sg_origin[i]) {1329drop_origin_blob(sg_origin[i]);1330origin_decref(sg_origin[i]);1331}1332}1333drop_origin_blob(origin);1334if(sg_buf != sg_origin)1335free(sg_origin);1336}13371338/*1339 * Information on commits, used for output.1340 */1341struct commit_info1342{1343const char*author;1344const char*author_mail;1345unsigned long author_time;1346const char*author_tz;13471348/* filled only when asked for details */1349const char*committer;1350const char*committer_mail;1351unsigned long committer_time;1352const char*committer_tz;13531354const char*summary;1355};13561357/*1358 * Parse author/committer line in the commit object buffer1359 */1360static voidget_ac_line(const char*inbuf,const char*what,1361int bufsz,char*person,const char**mail,1362unsigned long*time,const char**tz)1363{1364int len, tzlen, maillen;1365char*tmp, *endp, *timepos;13661367 tmp =strstr(inbuf, what);1368if(!tmp)1369goto error_out;1370 tmp +=strlen(what);1371 endp =strchr(tmp,'\n');1372if(!endp)1373 len =strlen(tmp);1374else1375 len = endp - tmp;1376if(bufsz <= len) {1377 error_out:1378/* Ugh */1379*mail = *tz ="(unknown)";1380*time =0;1381return;1382}1383memcpy(person, tmp, len);13841385 tmp = person;1386 tmp += len;1387*tmp =0;1388while(*tmp !=' ')1389 tmp--;1390*tz = tmp+1;1391 tzlen = (person+len)-(tmp+1);13921393*tmp =0;1394while(*tmp !=' ')1395 tmp--;1396*time =strtoul(tmp, NULL,10);1397 timepos = tmp;13981399*tmp =0;1400while(*tmp !=' ')1401 tmp--;1402*mail = tmp +1;1403*tmp =0;1404 maillen = timepos - tmp;14051406if(!mailmap.nr)1407return;14081409/*1410 * mailmap expansion may make the name longer.1411 * make room by pushing stuff down.1412 */1413 tmp = person + bufsz - (tzlen +1);1414memmove(tmp, *tz, tzlen);1415 tmp[tzlen] =0;1416*tz = tmp;14171418 tmp = tmp - (maillen +1);1419memmove(tmp, *mail, maillen);1420 tmp[maillen] =0;1421*mail = tmp;14221423/*1424 * Now, convert e-mail using mailmap1425 */1426map_email(&mailmap, tmp +1, person, tmp-person-1);1427}14281429static voidget_commit_info(struct commit *commit,1430struct commit_info *ret,1431int detailed)1432{1433int len;1434char*tmp, *endp;1435static char author_buf[1024];1436static char committer_buf[1024];1437static char summary_buf[1024];14381439/*1440 * We've operated without save_commit_buffer, so1441 * we now need to populate them for output.1442 */1443if(!commit->buffer) {1444enum object_type type;1445unsigned long size;1446 commit->buffer =1447read_sha1_file(commit->object.sha1, &type, &size);1448if(!commit->buffer)1449die("Cannot read commit%s",1450sha1_to_hex(commit->object.sha1));1451}1452 ret->author = author_buf;1453get_ac_line(commit->buffer,"\nauthor ",1454sizeof(author_buf), author_buf, &ret->author_mail,1455&ret->author_time, &ret->author_tz);14561457if(!detailed)1458return;14591460 ret->committer = committer_buf;1461get_ac_line(commit->buffer,"\ncommitter ",1462sizeof(committer_buf), committer_buf, &ret->committer_mail,1463&ret->committer_time, &ret->committer_tz);14641465 ret->summary = summary_buf;1466 tmp =strstr(commit->buffer,"\n\n");1467if(!tmp) {1468 error_out:1469sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1470return;1471}1472 tmp +=2;1473 endp =strchr(tmp,'\n');1474if(!endp)1475 endp = tmp +strlen(tmp);1476 len = endp - tmp;1477if(len >=sizeof(summary_buf) || len ==0)1478goto error_out;1479memcpy(summary_buf, tmp, len);1480 summary_buf[len] =0;1481}14821483/*1484 * To allow LF and other nonportable characters in pathnames,1485 * they are c-style quoted as needed.1486 */1487static voidwrite_filename_info(const char*path)1488{1489printf("filename ");1490write_name_quoted(path, stdout,'\n');1491}14921493/*1494 * Porcelain/Incremental format wants to show a lot of details per1495 * commit. Instead of repeating this every line, emit it only once,1496 * the first time each commit appears in the output.1497 */1498static intemit_one_suspect_detail(struct origin *suspect)1499{1500struct commit_info ci;15011502if(suspect->commit->object.flags & METAINFO_SHOWN)1503return0;15041505 suspect->commit->object.flags |= METAINFO_SHOWN;1506get_commit_info(suspect->commit, &ci,1);1507printf("author%s\n", ci.author);1508printf("author-mail%s\n", ci.author_mail);1509printf("author-time%lu\n", ci.author_time);1510printf("author-tz%s\n", ci.author_tz);1511printf("committer%s\n", ci.committer);1512printf("committer-mail%s\n", ci.committer_mail);1513printf("committer-time%lu\n", ci.committer_time);1514printf("committer-tz%s\n", ci.committer_tz);1515printf("summary%s\n", ci.summary);1516if(suspect->commit->object.flags & UNINTERESTING)1517printf("boundary\n");1518return1;1519}15201521/*1522 * The blame_entry is found to be guilty for the range. Mark it1523 * as such, and show it in incremental output.1524 */1525static voidfound_guilty_entry(struct blame_entry *ent)1526{1527if(ent->guilty)1528return;1529 ent->guilty =1;1530if(incremental) {1531struct origin *suspect = ent->suspect;15321533printf("%s %d %d %d\n",1534sha1_to_hex(suspect->commit->object.sha1),1535 ent->s_lno +1, ent->lno +1, ent->num_lines);1536emit_one_suspect_detail(suspect);1537write_filename_info(suspect->path);1538maybe_flush_or_die(stdout,"stdout");1539}1540}15411542/*1543 * The main loop -- while the scoreboard has lines whose true origin1544 * is still unknown, pick one blame_entry, and allow its current1545 * suspect to pass blames to its parents.1546 */1547static voidassign_blame(struct scoreboard *sb,int opt)1548{1549struct rev_info *revs = sb->revs;15501551while(1) {1552struct blame_entry *ent;1553struct commit *commit;1554struct origin *suspect = NULL;15551556/* find one suspect to break down */1557for(ent = sb->ent; !suspect && ent; ent = ent->next)1558if(!ent->guilty)1559 suspect = ent->suspect;1560if(!suspect)1561return;/* all done */15621563/*1564 * We will use this suspect later in the loop,1565 * so hold onto it in the meantime.1566 */1567origin_incref(suspect);1568 commit = suspect->commit;1569if(!commit->object.parsed)1570parse_commit(commit);1571if(reverse ||1572(!(commit->object.flags & UNINTERESTING) &&1573!(revs->max_age != -1&& commit->date < revs->max_age)))1574pass_blame(sb, suspect, opt);1575else{1576 commit->object.flags |= UNINTERESTING;1577if(commit->object.parsed)1578mark_parents_uninteresting(commit);1579}1580/* treat root commit as boundary */1581if(!commit->parents && !show_root)1582 commit->object.flags |= UNINTERESTING;15831584/* Take responsibility for the remaining entries */1585for(ent = sb->ent; ent; ent = ent->next)1586if(same_suspect(ent->suspect, suspect))1587found_guilty_entry(ent);1588origin_decref(suspect);15891590if(DEBUG)/* sanity */1591sanity_check_refcnt(sb);1592}1593}15941595static const char*format_time(unsigned long time,const char*tz_str,1596int show_raw_time)1597{1598static char time_buf[128];1599time_t t = time;1600int minutes, tz;1601struct tm *tm;16021603if(show_raw_time) {1604sprintf(time_buf,"%lu%s", time, tz_str);1605return time_buf;1606}16071608 tz =atoi(tz_str);1609 minutes = tz <0? -tz : tz;1610 minutes = (minutes /100)*60+ (minutes %100);1611 minutes = tz <0? -minutes : minutes;1612 t = time + minutes *60;1613 tm =gmtime(&t);16141615strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1616strcat(time_buf, tz_str);1617return time_buf;1618}16191620#define OUTPUT_ANNOTATE_COMPAT 0011621#define OUTPUT_LONG_OBJECT_NAME 0021622#define OUTPUT_RAW_TIMESTAMP 0041623#define OUTPUT_PORCELAIN 0101624#define OUTPUT_SHOW_NAME 0201625#define OUTPUT_SHOW_NUMBER 0401626#define OUTPUT_SHOW_SCORE 01001627#define OUTPUT_NO_AUTHOR 020016281629static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1630{1631int cnt;1632const char*cp;1633struct origin *suspect = ent->suspect;1634char hex[41];16351636strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1637printf("%s%c%d %d %d\n",1638 hex,1639 ent->guilty ?' ':'*',// purely for debugging1640 ent->s_lno +1,1641 ent->lno +1,1642 ent->num_lines);1643if(emit_one_suspect_detail(suspect) ||1644(suspect->commit->object.flags & MORE_THAN_ONE_PATH))1645write_filename_info(suspect->path);16461647 cp =nth_line(sb, ent->lno);1648for(cnt =0; cnt < ent->num_lines; cnt++) {1649char ch;1650if(cnt)1651printf("%s %d %d\n", hex,1652 ent->s_lno +1+ cnt,1653 ent->lno +1+ cnt);1654putchar('\t');1655do{1656 ch = *cp++;1657putchar(ch);1658}while(ch !='\n'&&1659 cp < sb->final_buf + sb->final_buf_size);1660}1661}16621663static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1664{1665int cnt;1666const char*cp;1667struct origin *suspect = ent->suspect;1668struct commit_info ci;1669char hex[41];1670int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16711672get_commit_info(suspect->commit, &ci,1);1673strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16741675 cp =nth_line(sb, ent->lno);1676for(cnt =0; cnt < ent->num_lines; cnt++) {1677char ch;1678int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16791680if(suspect->commit->object.flags & UNINTERESTING) {1681if(blank_boundary)1682memset(hex,' ', length);1683else if(!(opt & OUTPUT_ANNOTATE_COMPAT)) {1684 length--;1685putchar('^');1686}1687}16881689printf("%.*s", length, hex);1690if(opt & OUTPUT_ANNOTATE_COMPAT)1691printf("\t(%10s\t%10s\t%d)", ci.author,1692format_time(ci.author_time, ci.author_tz,1693 show_raw_time),1694 ent->lno +1+ cnt);1695else{1696if(opt & OUTPUT_SHOW_SCORE)1697printf(" %*d%02d",1698 max_score_digits, ent->score,1699 ent->suspect->refcnt);1700if(opt & OUTPUT_SHOW_NAME)1701printf(" %-*.*s", longest_file, longest_file,1702 suspect->path);1703if(opt & OUTPUT_SHOW_NUMBER)1704printf(" %*d", max_orig_digits,1705 ent->s_lno +1+ cnt);17061707if(!(opt & OUTPUT_NO_AUTHOR))1708printf(" (%-*.*s%10s",1709 longest_author, longest_author,1710 ci.author,1711format_time(ci.author_time,1712 ci.author_tz,1713 show_raw_time));1714printf(" %*d) ",1715 max_digits, ent->lno +1+ cnt);1716}1717do{1718 ch = *cp++;1719putchar(ch);1720}while(ch !='\n'&&1721 cp < sb->final_buf + sb->final_buf_size);1722}1723}17241725static voidoutput(struct scoreboard *sb,int option)1726{1727struct blame_entry *ent;17281729if(option & OUTPUT_PORCELAIN) {1730for(ent = sb->ent; ent; ent = ent->next) {1731struct blame_entry *oth;1732struct origin *suspect = ent->suspect;1733struct commit *commit = suspect->commit;1734if(commit->object.flags & MORE_THAN_ONE_PATH)1735continue;1736for(oth = ent->next; oth; oth = oth->next) {1737if((oth->suspect->commit != commit) ||1738!strcmp(oth->suspect->path, suspect->path))1739continue;1740 commit->object.flags |= MORE_THAN_ONE_PATH;1741break;1742}1743}1744}17451746for(ent = sb->ent; ent; ent = ent->next) {1747if(option & OUTPUT_PORCELAIN)1748emit_porcelain(sb, ent);1749else{1750emit_other(sb, ent, option);1751}1752}1753}17541755/*1756 * To allow quick access to the contents of nth line in the1757 * final image, prepare an index in the scoreboard.1758 */1759static intprepare_lines(struct scoreboard *sb)1760{1761const char*buf = sb->final_buf;1762unsigned long len = sb->final_buf_size;1763int num =0, incomplete =0, bol =1;17641765if(len && buf[len-1] !='\n')1766 incomplete++;/* incomplete line at the end */1767while(len--) {1768if(bol) {1769 sb->lineno =xrealloc(sb->lineno,1770sizeof(int* ) * (num +1));1771 sb->lineno[num] = buf - sb->final_buf;1772 bol =0;1773}1774if(*buf++ =='\n') {1775 num++;1776 bol =1;1777}1778}1779 sb->lineno =xrealloc(sb->lineno,1780sizeof(int* ) * (num + incomplete +1));1781 sb->lineno[num + incomplete] = buf - sb->final_buf;1782 sb->num_lines = num + incomplete;1783return sb->num_lines;1784}17851786/*1787 * Add phony grafts for use with -S; this is primarily to1788 * support git's cvsserver that wants to give a linear history1789 * to its clients.1790 */1791static intread_ancestry(const char*graft_file)1792{1793FILE*fp =fopen(graft_file,"r");1794char buf[1024];1795if(!fp)1796return-1;1797while(fgets(buf,sizeof(buf), fp)) {1798/* The format is just "Commit Parent1 Parent2 ...\n" */1799int len =strlen(buf);1800struct commit_graft *graft =read_graft_line(buf, len);1801if(graft)1802register_commit_graft(graft,0);1803}1804fclose(fp);1805return0;1806}18071808/*1809 * How many columns do we need to show line numbers in decimal?1810 */1811static intlineno_width(int lines)1812{1813int i, width;18141815for(width =1, i =10; i <= lines +1; width++)1816 i *=10;1817return width;1818}18191820/*1821 * How many columns do we need to show line numbers, authors,1822 * and filenames?1823 */1824static voidfind_alignment(struct scoreboard *sb,int*option)1825{1826int longest_src_lines =0;1827int longest_dst_lines =0;1828unsigned largest_score =0;1829struct blame_entry *e;18301831for(e = sb->ent; e; e = e->next) {1832struct origin *suspect = e->suspect;1833struct commit_info ci;1834int num;18351836if(strcmp(suspect->path, sb->path))1837*option |= OUTPUT_SHOW_NAME;1838 num =strlen(suspect->path);1839if(longest_file < num)1840 longest_file = num;1841if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1842 suspect->commit->object.flags |= METAINFO_SHOWN;1843get_commit_info(suspect->commit, &ci,1);1844 num =strlen(ci.author);1845if(longest_author < num)1846 longest_author = num;1847}1848 num = e->s_lno + e->num_lines;1849if(longest_src_lines < num)1850 longest_src_lines = num;1851 num = e->lno + e->num_lines;1852if(longest_dst_lines < num)1853 longest_dst_lines = num;1854if(largest_score <ent_score(sb, e))1855 largest_score =ent_score(sb, e);1856}1857 max_orig_digits =lineno_width(longest_src_lines);1858 max_digits =lineno_width(longest_dst_lines);1859 max_score_digits =lineno_width(largest_score);1860}18611862/*1863 * For debugging -- origin is refcounted, and this asserts that1864 * we do not underflow.1865 */1866static voidsanity_check_refcnt(struct scoreboard *sb)1867{1868int baa =0;1869struct blame_entry *ent;18701871for(ent = sb->ent; ent; ent = ent->next) {1872/* Nobody should have zero or negative refcnt */1873if(ent->suspect->refcnt <=0) {1874fprintf(stderr,"%sin%shas negative refcnt%d\n",1875 ent->suspect->path,1876sha1_to_hex(ent->suspect->commit->object.sha1),1877 ent->suspect->refcnt);1878 baa =1;1879}1880}1881for(ent = sb->ent; ent; ent = ent->next) {1882/* Mark the ones that haven't been checked */1883if(0< ent->suspect->refcnt)1884 ent->suspect->refcnt = -ent->suspect->refcnt;1885}1886for(ent = sb->ent; ent; ent = ent->next) {1887/*1888 * ... then pick each and see if they have the the1889 * correct refcnt.1890 */1891int found;1892struct blame_entry *e;1893struct origin *suspect = ent->suspect;18941895if(0< suspect->refcnt)1896continue;1897 suspect->refcnt = -suspect->refcnt;/* Unmark */1898for(found =0, e = sb->ent; e; e = e->next) {1899if(e->suspect != suspect)1900continue;1901 found++;1902}1903if(suspect->refcnt != found) {1904fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1905 ent->suspect->path,1906sha1_to_hex(ent->suspect->commit->object.sha1),1907 ent->suspect->refcnt, found);1908 baa =2;1909}1910}1911if(baa) {1912int opt =0160;1913find_alignment(sb, &opt);1914output(sb, opt);1915die("Baa%d!", baa);1916}1917}19181919/*1920 * Used for the command line parsing; check if the path exists1921 * in the working tree.1922 */1923static inthas_string_in_work_tree(const char*path)1924{1925struct stat st;1926return!lstat(path, &st);1927}19281929static unsignedparse_score(const char*arg)1930{1931char*end;1932unsigned long score =strtoul(arg, &end,10);1933if(*end)1934return0;1935return score;1936}19371938static const char*add_prefix(const char*prefix,const char*path)1939{1940returnprefix_path(prefix, prefix ?strlen(prefix) :0, path);1941}19421943/*1944 * Parsing of (comma separated) one item in the -L option1945 */1946static const char*parse_loc(const char*spec,1947struct scoreboard *sb,long lno,1948long begin,long*ret)1949{1950char*term;1951const char*line;1952long num;1953int reg_error;1954 regex_t regexp;1955 regmatch_t match[1];19561957/* Allow "-L <something>,+20" to mean starting at <something>1958 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1959 * <something>.1960 */1961if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1962 num =strtol(spec +1, &term,10);1963if(term != spec +1) {1964if(spec[0] =='-')1965 num =0- num;1966if(0< num)1967*ret = begin + num -2;1968else if(!num)1969*ret = begin;1970else1971*ret = begin + num;1972return term;1973}1974return spec;1975}1976 num =strtol(spec, &term,10);1977if(term != spec) {1978*ret = num;1979return term;1980}1981if(spec[0] !='/')1982return spec;19831984/* it could be a regexp of form /.../ */1985for(term = (char*) spec +1; *term && *term !='/'; term++) {1986if(*term =='\\')1987 term++;1988}1989if(*term !='/')1990return spec;19911992/* try [spec+1 .. term-1] as regexp */1993*term =0;1994 begin--;/* input is in human terms */1995 line =nth_line(sb, begin);19961997if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1998!(reg_error =regexec(®exp, line,1, match,0))) {1999const char*cp = line + match[0].rm_so;2000const char*nline;20012002while(begin++ < lno) {2003 nline =nth_line(sb, begin);2004if(line <= cp && cp < nline)2005break;2006 line = nline;2007}2008*ret = begin;2009regfree(®exp);2010*term++ ='/';2011return term;2012}2013else{2014char errbuf[1024];2015regerror(reg_error, ®exp, errbuf,1024);2016die("-L parameter '%s':%s", spec +1, errbuf);2017}2018}20192020/*2021 * Parsing of -L option2022 */2023static voidprepare_blame_range(struct scoreboard *sb,2024const char*bottomtop,2025long lno,2026long*bottom,long*top)2027{2028const char*term;20292030 term =parse_loc(bottomtop, sb, lno,1, bottom);2031if(*term ==',') {2032 term =parse_loc(term +1, sb, lno, *bottom +1, top);2033if(*term)2034usage(blame_usage);2035}2036if(*term)2037usage(blame_usage);2038}20392040static intgit_blame_config(const char*var,const char*value,void*cb)2041{2042if(!strcmp(var,"blame.showroot")) {2043 show_root =git_config_bool(var, value);2044return0;2045}2046if(!strcmp(var,"blame.blankboundary")) {2047 blank_boundary =git_config_bool(var, value);2048return0;2049}2050returngit_default_config(var, value, cb);2051}20522053/*2054 * Prepare a dummy commit that represents the work tree (or staged) item.2055 * Note that annotating work tree item never works in the reverse.2056 */2057static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2058{2059struct commit *commit;2060struct origin *origin;2061unsigned char head_sha1[20];2062struct strbuf buf = STRBUF_INIT;2063const char*ident;2064time_t now;2065int size, len;2066struct cache_entry *ce;2067unsigned mode;20682069if(get_sha1("HEAD", head_sha1))2070die("No such ref: HEAD");20712072time(&now);2073 commit =xcalloc(1,sizeof(*commit));2074 commit->parents =xcalloc(1,sizeof(*commit->parents));2075 commit->parents->item =lookup_commit_reference(head_sha1);2076 commit->object.parsed =1;2077 commit->date = now;2078 commit->object.type = OBJ_COMMIT;20792080 origin =make_origin(commit, path);20812082if(!contents_from ||strcmp("-", contents_from)) {2083struct stat st;2084const char*read_from;2085unsigned long fin_size;20862087if(contents_from) {2088if(stat(contents_from, &st) <0)2089die("Cannot stat%s", contents_from);2090 read_from = contents_from;2091}2092else{2093if(lstat(path, &st) <0)2094die("Cannot lstat%s", path);2095 read_from = path;2096}2097 fin_size =xsize_t(st.st_size);2098 mode =canon_mode(st.st_mode);2099switch(st.st_mode & S_IFMT) {2100case S_IFREG:2101if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2102die("cannot open or read%s", read_from);2103break;2104case S_IFLNK:2105if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2106die("cannot readlink%s", read_from);2107 buf.len = fin_size;2108break;2109default:2110die("unsupported file type%s", read_from);2111}2112}2113else{2114/* Reading from stdin */2115 contents_from ="standard input";2116 mode =0;2117if(strbuf_read(&buf,0,0) <0)2118die("read error%sfrom stdin",strerror(errno));2119}2120convert_to_git(path, buf.buf, buf.len, &buf,0);2121 origin->file.ptr = buf.buf;2122 origin->file.size = buf.len;2123pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2124 commit->util = origin;21252126/*2127 * Read the current index, replace the path entry with2128 * origin->blob_sha1 without mucking with its mode or type2129 * bits; we are not going to write this index out -- we just2130 * want to run "diff-index --cached".2131 */2132discard_cache();2133read_cache();21342135 len =strlen(path);2136if(!mode) {2137int pos =cache_name_pos(path, len);2138if(0<= pos)2139 mode = active_cache[pos]->ce_mode;2140else2141/* Let's not bother reading from HEAD tree */2142 mode = S_IFREG |0644;2143}2144 size =cache_entry_size(len);2145 ce =xcalloc(1, size);2146hashcpy(ce->sha1, origin->blob_sha1);2147memcpy(ce->name, path, len);2148 ce->ce_flags =create_ce_flags(len,0);2149 ce->ce_mode =create_ce_mode(mode);2150add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);21512152/*2153 * We are not going to write this out, so this does not matter2154 * right now, but someday we might optimize diff-index --cached2155 * with cache-tree information.2156 */2157cache_tree_invalidate_path(active_cache_tree, path);21582159 commit->buffer =xmalloc(400);2160 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2161snprintf(commit->buffer,400,2162"tree 0000000000000000000000000000000000000000\n"2163"parent%s\n"2164"author%s\n"2165"committer%s\n\n"2166"Version of%sfrom%s\n",2167sha1_to_hex(head_sha1),2168 ident, ident, path, contents_from ? contents_from : path);2169return commit;2170}21712172static const char*prepare_final(struct scoreboard *sb)2173{2174int i;2175const char*final_commit_name = NULL;2176struct rev_info *revs = sb->revs;21772178/*2179 * There must be one and only one positive commit in the2180 * revs->pending array.2181 */2182for(i =0; i < revs->pending.nr; i++) {2183struct object *obj = revs->pending.objects[i].item;2184if(obj->flags & UNINTERESTING)2185continue;2186while(obj->type == OBJ_TAG)2187 obj =deref_tag(obj, NULL,0);2188if(obj->type != OBJ_COMMIT)2189die("Non commit%s?", revs->pending.objects[i].name);2190if(sb->final)2191die("More than one commit to dig from%sand%s?",2192 revs->pending.objects[i].name,2193 final_commit_name);2194 sb->final = (struct commit *) obj;2195 final_commit_name = revs->pending.objects[i].name;2196}2197return final_commit_name;2198}21992200static const char*prepare_initial(struct scoreboard *sb)2201{2202int i;2203const char*final_commit_name = NULL;2204struct rev_info *revs = sb->revs;22052206/*2207 * There must be one and only one negative commit, and it must be2208 * the boundary.2209 */2210for(i =0; i < revs->pending.nr; i++) {2211struct object *obj = revs->pending.objects[i].item;2212if(!(obj->flags & UNINTERESTING))2213continue;2214while(obj->type == OBJ_TAG)2215 obj =deref_tag(obj, NULL,0);2216if(obj->type != OBJ_COMMIT)2217die("Non commit%s?", revs->pending.objects[i].name);2218if(sb->final)2219die("More than one commit to dig down to%sand%s?",2220 revs->pending.objects[i].name,2221 final_commit_name);2222 sb->final = (struct commit *) obj;2223 final_commit_name = revs->pending.objects[i].name;2224}2225if(!final_commit_name)2226die("No commit to dig down to?");2227return final_commit_name;2228}22292230static intblame_copy_callback(const struct option *option,const char*arg,int unset)2231{2232int*opt = option->value;22332234/*2235 * -C enables copy from removed files;2236 * -C -C enables copy from existing files, but only2237 * when blaming a new file;2238 * -C -C -C enables copy from existing files for2239 * everybody2240 */2241if(*opt & PICKAXE_BLAME_COPY_HARDER)2242*opt |= PICKAXE_BLAME_COPY_HARDEST;2243if(*opt & PICKAXE_BLAME_COPY)2244*opt |= PICKAXE_BLAME_COPY_HARDER;2245*opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;22462247if(arg)2248 blame_copy_score =parse_score(arg);2249return0;2250}22512252static intblame_move_callback(const struct option *option,const char*arg,int unset)2253{2254int*opt = option->value;22552256*opt |= PICKAXE_BLAME_MOVE;22572258if(arg)2259 blame_move_score =parse_score(arg);2260return0;2261}22622263static intblame_bottomtop_callback(const struct option *option,const char*arg,int unset)2264{2265const char**bottomtop = option->value;2266if(!arg)2267return-1;2268if(*bottomtop)2269die("More than one '-L n,m' option given");2270*bottomtop = arg;2271return0;2272}22732274intcmd_blame(int argc,const char**argv,const char*prefix)2275{2276struct rev_info revs;2277const char*path;2278struct scoreboard sb;2279struct origin *o;2280struct blame_entry *ent;2281long dashdash_pos, bottom, top, lno;2282const char*final_commit_name = NULL;2283enum object_type type;22842285static const char*bottomtop = NULL;2286static int output_option =0, opt =0;2287static int show_stats =0;2288static const char*revs_file = NULL;2289static const char*contents_from = NULL;2290static const struct option options[] = {2291OPT_BOOLEAN(0,"incremental", &incremental,"Show blame entries as we find them, incrementally"),2292OPT_BOOLEAN('b', NULL, &blank_boundary,"Show blank SHA-1 for boundary commits (Default: off)"),2293OPT_BOOLEAN(0,"root", &show_root,"Do not treat root commits as boundaries (Default: off)"),2294OPT_BOOLEAN(0,"show-stats", &show_stats,"Show work cost statistics"),2295OPT_BIT(0,"score-debug", &output_option,"Show output score for blame entries", OUTPUT_SHOW_SCORE),2296OPT_BIT('f',"show-name", &output_option,"Show original filename (Default: auto)", OUTPUT_SHOW_NAME),2297OPT_BIT('n',"show-number", &output_option,"Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER),2298OPT_BIT('p',"porcelain", &output_option,"Show in a format designed for machine consumption", OUTPUT_PORCELAIN),2299OPT_BIT('c', NULL, &output_option,"Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT),2300OPT_BIT('t', NULL, &output_option,"Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP),2301OPT_BIT('l', NULL, &output_option,"Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME),2302OPT_BIT('s', NULL, &output_option,"Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR),2303OPT_BIT('w', NULL, &xdl_opts,"Ignore whitespace differences", XDF_IGNORE_WHITESPACE),2304OPT_STRING('S', NULL, &revs_file,"file","Use revisions from <file> instead of calling git-rev-list"),2305OPT_STRING(0,"contents", &contents_from,"file","Use <file>'s contents as the final image"),2306{ OPTION_CALLBACK,'C', NULL, &opt,"score","Find line copies within and across files", PARSE_OPT_OPTARG, blame_copy_callback },2307{ OPTION_CALLBACK,'M', NULL, &opt,"score","Find line movements within and across files", PARSE_OPT_OPTARG, blame_move_callback },2308OPT_CALLBACK('L', NULL, &bottomtop,"n,m","Process only line range n,m, counting from 1", blame_bottomtop_callback),2309OPT_END()2310};23112312struct parse_opt_ctx_t ctx;2313int cmd_is_annotate = !strcmp(argv[0],"annotate");23142315git_config(git_blame_config, NULL);2316init_revisions(&revs, NULL);2317 save_commit_buffer =0;2318 dashdash_pos =0;23192320parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |2321 PARSE_OPT_KEEP_ARGV0);2322for(;;) {2323switch(parse_options_step(&ctx, options, blame_opt_usage)) {2324case PARSE_OPT_HELP:2325exit(129);2326case PARSE_OPT_DONE:2327if(ctx.argv[0])2328 dashdash_pos = ctx.cpidx;2329goto parse_done;2330}23312332if(!strcmp(ctx.argv[0],"--reverse")) {2333 ctx.argv[0] ="--children";2334 reverse =1;2335}2336parse_revision_opt(&revs, &ctx, options, blame_opt_usage);2337}2338parse_done:2339 argc =parse_options_end(&ctx);23402341if(cmd_is_annotate)2342 output_option |= OUTPUT_ANNOTATE_COMPAT;23432344if(DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))2345 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |2346 PICKAXE_BLAME_COPY_HARDER);23472348if(!blame_move_score)2349 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2350if(!blame_copy_score)2351 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;23522353/*2354 * We have collected options unknown to us in argv[1..unk]2355 * which are to be passed to revision machinery if we are2356 * going to do the "bottom" processing.2357 *2358 * The remaining are:2359 *2360 * (1) if dashdash_pos != 0, its either2361 * "blame [revisions] -- <path>" or2362 * "blame -- <path> <rev>"2363 *2364 * (2) otherwise, its one of the two:2365 * "blame [revisions] <path>"2366 * "blame <path> <rev>"2367 *2368 * Note that we must strip out <path> from the arguments: we do not2369 * want the path pruning but we may want "bottom" processing.2370 */2371if(dashdash_pos) {2372switch(argc - dashdash_pos -1) {2373case2:/* (1b) */2374if(argc !=4)2375usage_with_options(blame_opt_usage, options);2376/* reorder for the new way: <rev> -- <path> */2377 argv[1] = argv[3];2378 argv[3] = argv[2];2379 argv[2] ="--";2380/* FALLTHROUGH */2381case1:/* (1a) */2382 path =add_prefix(prefix, argv[--argc]);2383 argv[argc] = NULL;2384break;2385default:2386usage_with_options(blame_opt_usage, options);2387}2388}else{2389if(argc <2)2390usage_with_options(blame_opt_usage, options);2391 path =add_prefix(prefix, argv[argc -1]);2392if(argc ==3&& !has_string_in_work_tree(path)) {/* (2b) */2393 path =add_prefix(prefix, argv[1]);2394 argv[1] = argv[2];2395}2396 argv[argc -1] ="--";23972398setup_work_tree();2399if(!has_string_in_work_tree(path))2400die("cannot stat path%s:%s", path,strerror(errno));2401}24022403setup_revisions(argc, argv, &revs, NULL);2404memset(&sb,0,sizeof(sb));24052406 sb.revs = &revs;2407if(!reverse)2408 final_commit_name =prepare_final(&sb);2409else if(contents_from)2410die("--contents and --children do not blend well.");2411else2412 final_commit_name =prepare_initial(&sb);24132414if(!sb.final) {2415/*2416 * "--not A B -- path" without anything positive;2417 * do not default to HEAD, but use the working tree2418 * or "--contents".2419 */2420setup_work_tree();2421 sb.final =fake_working_tree_commit(path, contents_from);2422add_pending_object(&revs, &(sb.final->object),":");2423}2424else if(contents_from)2425die("Cannot use --contents with final commit object name");24262427/*2428 * If we have bottom, this will mark the ancestors of the2429 * bottom commits we would reach while traversing as2430 * uninteresting.2431 */2432if(prepare_revision_walk(&revs))2433die("revision walk setup failed");24342435if(is_null_sha1(sb.final->object.sha1)) {2436char*buf;2437 o = sb.final->util;2438 buf =xmalloc(o->file.size +1);2439memcpy(buf, o->file.ptr, o->file.size +1);2440 sb.final_buf = buf;2441 sb.final_buf_size = o->file.size;2442}2443else{2444 o =get_origin(&sb, sb.final, path);2445if(fill_blob_sha1(o))2446die("no such path%sin%s", path, final_commit_name);24472448 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2449&sb.final_buf_size);2450if(!sb.final_buf)2451die("Cannot read blob%sfor path%s",2452sha1_to_hex(o->blob_sha1),2453 path);2454}2455 num_read_blob++;2456 lno =prepare_lines(&sb);24572458 bottom = top =0;2459if(bottomtop)2460prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2461if(bottom && top && top < bottom) {2462long tmp;2463 tmp = top; top = bottom; bottom = tmp;2464}2465if(bottom <1)2466 bottom =1;2467if(top <1)2468 top = lno;2469 bottom--;2470if(lno < top)2471die("file%shas only%lu lines", path, lno);24722473 ent =xcalloc(1,sizeof(*ent));2474 ent->lno = bottom;2475 ent->num_lines = top - bottom;2476 ent->suspect = o;2477 ent->s_lno = bottom;24782479 sb.ent = ent;2480 sb.path = path;24812482if(revs_file &&read_ancestry(revs_file))2483die("reading graft file%sfailed:%s",2484 revs_file,strerror(errno));24852486read_mailmap(&mailmap,".mailmap", NULL);24872488if(!incremental)2489setup_pager();24902491assign_blame(&sb, opt);24922493if(incremental)2494return0;24952496coalesce(&sb);24972498if(!(output_option & OUTPUT_PORCELAIN))2499find_alignment(&sb, &output_option);25002501output(&sb, output_option);2502free((void*)sb.final_buf);2503for(ent = sb.ent; ent; ) {2504struct blame_entry *e = ent->next;2505free(ent);2506 ent = e;2507}25082509if(show_stats) {2510printf("num read blob:%d\n", num_read_blob);2511printf("num get patch:%d\n", num_get_patch);2512printf("num commits:%d\n", num_commits);2513}2514return0;2515}