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"path-list.h" 20#include"mailmap.h" 21 22static char blame_usage[] = 23"git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n" 24" -c Use the same output mode as git-annotate (Default: off)\n" 25" -b Show blank SHA-1 for boundary commits (Default: off)\n" 26" -l Show long commit SHA1 (Default: off)\n" 27" --root Do not treat root commits as boundaries (Default: off)\n" 28" -t Show raw timestamp (Default: off)\n" 29" -f, --show-name Show original filename (Default: auto)\n" 30" -n, --show-number Show original linenumber (Default: off)\n" 31" -s Suppress author name and timestamp (Default: off)\n" 32" -p, --porcelain Show in a format designed for machine consumption\n" 33" -w Ignore whitespace differences\n" 34" -L n,m Process only line range n,m, counting from 1\n" 35" -M, -C Find line movements within and across files\n" 36" --incremental Show blame entries as we find them, incrementally\n" 37" --contents file Use <file>'s contents as the final image\n" 38" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"; 39 40static int longest_file; 41static int longest_author; 42static int max_orig_digits; 43static int max_digits; 44static int max_score_digits; 45static int show_root; 46static int blank_boundary; 47static int incremental; 48static int cmd_is_annotate; 49static int xdl_opts = XDF_NEED_MINIMAL; 50static struct path_list mailmap; 51 52#ifndef DEBUG 53#define DEBUG 0 54#endif 55 56/* stats */ 57static int num_read_blob; 58static int num_get_patch; 59static int num_commits; 60 61#define PICKAXE_BLAME_MOVE 01 62#define PICKAXE_BLAME_COPY 02 63#define PICKAXE_BLAME_COPY_HARDER 04 64#define PICKAXE_BLAME_COPY_HARDEST 010 65 66/* 67 * blame for a blame_entry with score lower than these thresholds 68 * is not passed to the parent using move/copy logic. 69 */ 70static unsigned blame_move_score; 71static unsigned blame_copy_score; 72#define BLAME_DEFAULT_MOVE_SCORE 20 73#define BLAME_DEFAULT_COPY_SCORE 40 74 75/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ 76#define METAINFO_SHOWN (1u<<12) 77#define MORE_THAN_ONE_PATH (1u<<13) 78 79/* 80 * One blob in a commit that is being suspected 81 */ 82struct origin { 83int refcnt; 84struct commit *commit; 85 mmfile_t file; 86unsigned char blob_sha1[20]; 87char path[FLEX_ARRAY]; 88}; 89 90/* 91 * Given an origin, prepare mmfile_t structure to be used by the 92 * diff machinery 93 */ 94static char*fill_origin_blob(struct origin *o, mmfile_t *file) 95{ 96if(!o->file.ptr) { 97enum object_type type; 98 num_read_blob++; 99 file->ptr =read_sha1_file(o->blob_sha1, &type, 100(unsigned long*)(&(file->size))); 101if(!file->ptr) 102die("Cannot read blob%sfor path%s", 103sha1_to_hex(o->blob_sha1), 104 o->path); 105 o->file = *file; 106} 107else 108*file = o->file; 109return file->ptr; 110} 111 112/* 113 * Origin is refcounted and usually we keep the blob contents to be 114 * reused. 115 */ 116staticinlinestruct origin *origin_incref(struct origin *o) 117{ 118if(o) 119 o->refcnt++; 120return o; 121} 122 123static voidorigin_decref(struct origin *o) 124{ 125if(o && --o->refcnt <=0) { 126if(o->file.ptr) 127free(o->file.ptr); 128memset(o,0,sizeof(*o)); 129free(o); 130} 131} 132 133static voiddrop_origin_blob(struct origin *o) 134{ 135if(o->file.ptr) { 136free(o->file.ptr); 137 o->file.ptr = NULL; 138} 139} 140 141/* 142 * Each group of lines is described by a blame_entry; it can be split 143 * as we pass blame to the parents. They form a linked list in the 144 * scoreboard structure, sorted by the target line number. 145 */ 146struct blame_entry { 147struct blame_entry *prev; 148struct blame_entry *next; 149 150/* the first line of this group in the final image; 151 * internally all line numbers are 0 based. 152 */ 153int lno; 154 155/* how many lines this group has */ 156int num_lines; 157 158/* the commit that introduced this group into the final image */ 159struct origin *suspect; 160 161/* true if the suspect is truly guilty; false while we have not 162 * checked if the group came from one of its parents. 163 */ 164char guilty; 165 166/* the line number of the first line of this group in the 167 * suspect's file; internally all line numbers are 0 based. 168 */ 169int s_lno; 170 171/* how significant this entry is -- cached to avoid 172 * scanning the lines over and over. 173 */ 174unsigned score; 175}; 176 177/* 178 * The current state of the blame assignment. 179 */ 180struct scoreboard { 181/* the final commit (i.e. where we started digging from) */ 182struct commit *final; 183 184const char*path; 185 186/* 187 * The contents in the final image. 188 * Used by many functions to obtain contents of the nth line, 189 * indexed with scoreboard.lineno[blame_entry.lno]. 190 */ 191const char*final_buf; 192unsigned long final_buf_size; 193 194/* linked list of blames */ 195struct blame_entry *ent; 196 197/* look-up a line in the final buffer */ 198int num_lines; 199int*lineno; 200}; 201 202staticinlineintsame_suspect(struct origin *a,struct origin *b) 203{ 204if(a == b) 205return1; 206if(a->commit != b->commit) 207return0; 208return!strcmp(a->path, b->path); 209} 210 211static voidsanity_check_refcnt(struct scoreboard *); 212 213/* 214 * If two blame entries that are next to each other came from 215 * contiguous lines in the same origin (i.e. <commit, path> pair), 216 * merge them together. 217 */ 218static voidcoalesce(struct scoreboard *sb) 219{ 220struct blame_entry *ent, *next; 221 222for(ent = sb->ent; ent && (next = ent->next); ent = next) { 223if(same_suspect(ent->suspect, next->suspect) && 224 ent->guilty == next->guilty && 225 ent->s_lno + ent->num_lines == next->s_lno) { 226 ent->num_lines += next->num_lines; 227 ent->next = next->next; 228if(ent->next) 229 ent->next->prev = ent; 230origin_decref(next->suspect); 231free(next); 232 ent->score =0; 233 next = ent;/* again */ 234} 235} 236 237if(DEBUG)/* sanity */ 238sanity_check_refcnt(sb); 239} 240 241/* 242 * Given a commit and a path in it, create a new origin structure. 243 * The callers that add blame to the scoreboard should use 244 * get_origin() to obtain shared, refcounted copy instead of calling 245 * this function directly. 246 */ 247static struct origin *make_origin(struct commit *commit,const char*path) 248{ 249struct origin *o; 250 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 251 o->commit = commit; 252 o->refcnt =1; 253strcpy(o->path, path); 254return o; 255} 256 257/* 258 * Locate an existing origin or create a new one. 259 */ 260static struct origin *get_origin(struct scoreboard *sb, 261struct commit *commit, 262const char*path) 263{ 264struct blame_entry *e; 265 266for(e = sb->ent; e; e = e->next) { 267if(e->suspect->commit == commit && 268!strcmp(e->suspect->path, path)) 269returnorigin_incref(e->suspect); 270} 271returnmake_origin(commit, path); 272} 273 274/* 275 * Fill the blob_sha1 field of an origin if it hasn't, so that later 276 * call to fill_origin_blob() can use it to locate the data. blob_sha1 277 * for an origin is also used to pass the blame for the entire file to 278 * the parent to detect the case where a child's blob is identical to 279 * that of its parent's. 280 */ 281static intfill_blob_sha1(struct origin *origin) 282{ 283unsigned mode; 284 285if(!is_null_sha1(origin->blob_sha1)) 286return0; 287if(get_tree_entry(origin->commit->object.sha1, 288 origin->path, 289 origin->blob_sha1, &mode)) 290goto error_out; 291if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 292goto error_out; 293return0; 294 error_out: 295hashclr(origin->blob_sha1); 296return-1; 297} 298 299/* 300 * We have an origin -- check if the same path exists in the 301 * parent and return an origin structure to represent it. 302 */ 303static struct origin *find_origin(struct scoreboard *sb, 304struct commit *parent, 305struct origin *origin) 306{ 307struct origin *porigin = NULL; 308struct diff_options diff_opts; 309const char*paths[2]; 310 311if(parent->util) { 312/* 313 * Each commit object can cache one origin in that 314 * commit. This is a freestanding copy of origin and 315 * not refcounted. 316 */ 317struct origin *cached = parent->util; 318if(!strcmp(cached->path, origin->path)) { 319/* 320 * The same path between origin and its parent 321 * without renaming -- the most common case. 322 */ 323 porigin =get_origin(sb, parent, cached->path); 324 325/* 326 * If the origin was newly created (i.e. get_origin 327 * would call make_origin if none is found in the 328 * scoreboard), it does not know the blob_sha1, 329 * so copy it. Otherwise porigin was in the 330 * scoreboard and already knows blob_sha1. 331 */ 332if(porigin->refcnt ==1) 333hashcpy(porigin->blob_sha1, cached->blob_sha1); 334return porigin; 335} 336/* otherwise it was not very useful; free it */ 337free(parent->util); 338 parent->util = NULL; 339} 340 341/* See if the origin->path is different between parent 342 * and origin first. Most of the time they are the 343 * same and diff-tree is fairly efficient about this. 344 */ 345diff_setup(&diff_opts); 346DIFF_OPT_SET(&diff_opts, RECURSIVE); 347 diff_opts.detect_rename =0; 348 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 349 paths[0] = origin->path; 350 paths[1] = NULL; 351 352diff_tree_setup_paths(paths, &diff_opts); 353if(diff_setup_done(&diff_opts) <0) 354die("diff-setup"); 355 356if(is_null_sha1(origin->commit->object.sha1)) 357do_diff_cache(parent->tree->object.sha1, &diff_opts); 358else 359diff_tree_sha1(parent->tree->object.sha1, 360 origin->commit->tree->object.sha1, 361"", &diff_opts); 362diffcore_std(&diff_opts); 363 364/* It is either one entry that says "modified", or "created", 365 * or nothing. 366 */ 367if(!diff_queued_diff.nr) { 368/* The path is the same as parent */ 369 porigin =get_origin(sb, parent, origin->path); 370hashcpy(porigin->blob_sha1, origin->blob_sha1); 371} 372else if(diff_queued_diff.nr !=1) 373die("internal error in blame::find_origin"); 374else{ 375struct diff_filepair *p = diff_queued_diff.queue[0]; 376switch(p->status) { 377default: 378die("internal error in blame::find_origin (%c)", 379 p->status); 380case'M': 381 porigin =get_origin(sb, parent, origin->path); 382hashcpy(porigin->blob_sha1, p->one->sha1); 383break; 384case'A': 385case'T': 386/* Did not exist in parent, or type changed */ 387break; 388} 389} 390diff_flush(&diff_opts); 391diff_tree_release_paths(&diff_opts); 392if(porigin) { 393/* 394 * Create a freestanding copy that is not part of 395 * the refcounted origin found in the scoreboard, and 396 * cache it in the commit. 397 */ 398struct origin *cached; 399 400 cached =make_origin(porigin->commit, porigin->path); 401hashcpy(cached->blob_sha1, porigin->blob_sha1); 402 parent->util = cached; 403} 404return porigin; 405} 406 407/* 408 * We have an origin -- find the path that corresponds to it in its 409 * parent and return an origin structure to represent it. 410 */ 411static struct origin *find_rename(struct scoreboard *sb, 412struct commit *parent, 413struct origin *origin) 414{ 415struct origin *porigin = NULL; 416struct diff_options diff_opts; 417int i; 418const char*paths[2]; 419 420diff_setup(&diff_opts); 421DIFF_OPT_SET(&diff_opts, RECURSIVE); 422 diff_opts.detect_rename = DIFF_DETECT_RENAME; 423 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 424 diff_opts.single_follow = origin->path; 425 paths[0] = NULL; 426diff_tree_setup_paths(paths, &diff_opts); 427if(diff_setup_done(&diff_opts) <0) 428die("diff-setup"); 429 430if(is_null_sha1(origin->commit->object.sha1)) 431do_diff_cache(parent->tree->object.sha1, &diff_opts); 432else 433diff_tree_sha1(parent->tree->object.sha1, 434 origin->commit->tree->object.sha1, 435"", &diff_opts); 436diffcore_std(&diff_opts); 437 438for(i =0; i < diff_queued_diff.nr; i++) { 439struct diff_filepair *p = diff_queued_diff.queue[i]; 440if((p->status =='R'|| p->status =='C') && 441!strcmp(p->two->path, origin->path)) { 442 porigin =get_origin(sb, parent, p->one->path); 443hashcpy(porigin->blob_sha1, p->one->sha1); 444break; 445} 446} 447diff_flush(&diff_opts); 448diff_tree_release_paths(&diff_opts); 449return porigin; 450} 451 452/* 453 * Parsing of patch chunks... 454 */ 455struct chunk { 456/* line number in postimage; up to but not including this 457 * line is the same as preimage 458 */ 459int same; 460 461/* preimage line number after this chunk */ 462int p_next; 463 464/* postimage line number after this chunk */ 465int t_next; 466}; 467 468struct patch { 469struct chunk *chunks; 470int num; 471}; 472 473struct blame_diff_state { 474struct xdiff_emit_state xm; 475struct patch *ret; 476unsigned hunk_post_context; 477unsigned hunk_in_pre_context :1; 478}; 479 480static voidprocess_u_diff(void*state_,char*line,unsigned long len) 481{ 482struct blame_diff_state *state = state_; 483struct chunk *chunk; 484int off1, off2, len1, len2, num; 485 486 num = state->ret->num; 487if(len <4|| line[0] !='@'|| line[1] !='@') { 488if(state->hunk_in_pre_context && line[0] ==' ') 489 state->ret->chunks[num -1].same++; 490else{ 491 state->hunk_in_pre_context =0; 492if(line[0] ==' ') 493 state->hunk_post_context++; 494else 495 state->hunk_post_context =0; 496} 497return; 498} 499 500if(num && state->hunk_post_context) { 501 chunk = &state->ret->chunks[num -1]; 502 chunk->p_next -= state->hunk_post_context; 503 chunk->t_next -= state->hunk_post_context; 504} 505 state->ret->num = ++num; 506 state->ret->chunks =xrealloc(state->ret->chunks, 507sizeof(struct chunk) * num); 508 chunk = &state->ret->chunks[num -1]; 509if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 510 state->ret->num--; 511return; 512} 513 514/* Line numbers in patch output are one based. */ 515 off1--; 516 off2--; 517 518 chunk->same = len2 ? off2 : (off2 +1); 519 520 chunk->p_next = off1 + (len1 ? len1 :1); 521 chunk->t_next = chunk->same + len2; 522 state->hunk_in_pre_context =1; 523 state->hunk_post_context =0; 524} 525 526static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 527int context) 528{ 529struct blame_diff_state state; 530 xpparam_t xpp; 531 xdemitconf_t xecfg; 532 xdemitcb_t ecb; 533 534 xpp.flags = xdl_opts; 535memset(&xecfg,0,sizeof(xecfg)); 536 xecfg.ctxlen = context; 537 ecb.outf = xdiff_outf; 538 ecb.priv = &state; 539memset(&state,0,sizeof(state)); 540 state.xm.consume = process_u_diff; 541 state.ret =xmalloc(sizeof(struct patch)); 542 state.ret->chunks = NULL; 543 state.ret->num =0; 544 545xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb); 546 547if(state.ret->num) { 548struct chunk *chunk; 549 chunk = &state.ret->chunks[state.ret->num -1]; 550 chunk->p_next -= state.hunk_post_context; 551 chunk->t_next -= state.hunk_post_context; 552} 553return state.ret; 554} 555 556/* 557 * Run diff between two origins and grab the patch output, so that 558 * we can pass blame for lines origin is currently suspected for 559 * to its parent. 560 */ 561static struct patch *get_patch(struct origin *parent,struct origin *origin) 562{ 563 mmfile_t file_p, file_o; 564struct patch *patch; 565 566fill_origin_blob(parent, &file_p); 567fill_origin_blob(origin, &file_o); 568if(!file_p.ptr || !file_o.ptr) 569return NULL; 570 patch =compare_buffer(&file_p, &file_o,0); 571 num_get_patch++; 572return patch; 573} 574 575static voidfree_patch(struct patch *p) 576{ 577free(p->chunks); 578free(p); 579} 580 581/* 582 * Link in a new blame entry to the scoreboard. Entries that cover the 583 * same line range have been removed from the scoreboard previously. 584 */ 585static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 586{ 587struct blame_entry *ent, *prev = NULL; 588 589origin_incref(e->suspect); 590 591for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 592 prev = ent; 593 594/* prev, if not NULL, is the last one that is below e */ 595 e->prev = prev; 596if(prev) { 597 e->next = prev->next; 598 prev->next = e; 599} 600else{ 601 e->next = sb->ent; 602 sb->ent = e; 603} 604if(e->next) 605 e->next->prev = e; 606} 607 608/* 609 * src typically is on-stack; we want to copy the information in it to 610 * an malloced blame_entry that is already on the linked list of the 611 * scoreboard. The origin of dst loses a refcnt while the origin of src 612 * gains one. 613 */ 614static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 615{ 616struct blame_entry *p, *n; 617 618 p = dst->prev; 619 n = dst->next; 620origin_incref(src->suspect); 621origin_decref(dst->suspect); 622memcpy(dst, src,sizeof(*src)); 623 dst->prev = p; 624 dst->next = n; 625 dst->score =0; 626} 627 628static const char*nth_line(struct scoreboard *sb,int lno) 629{ 630return sb->final_buf + sb->lineno[lno]; 631} 632 633/* 634 * It is known that lines between tlno to same came from parent, and e 635 * has an overlap with that range. it also is known that parent's 636 * line plno corresponds to e's line tlno. 637 * 638 * <---- e -----> 639 * <------> 640 * <------------> 641 * <------------> 642 * <------------------> 643 * 644 * Split e into potentially three parts; before this chunk, the chunk 645 * to be blamed for the parent, and after that portion. 646 */ 647static voidsplit_overlap(struct blame_entry *split, 648struct blame_entry *e, 649int tlno,int plno,int same, 650struct origin *parent) 651{ 652int chunk_end_lno; 653memset(split,0,sizeof(struct blame_entry [3])); 654 655if(e->s_lno < tlno) { 656/* there is a pre-chunk part not blamed on parent */ 657 split[0].suspect =origin_incref(e->suspect); 658 split[0].lno = e->lno; 659 split[0].s_lno = e->s_lno; 660 split[0].num_lines = tlno - e->s_lno; 661 split[1].lno = e->lno + tlno - e->s_lno; 662 split[1].s_lno = plno; 663} 664else{ 665 split[1].lno = e->lno; 666 split[1].s_lno = plno + (e->s_lno - tlno); 667} 668 669if(same < e->s_lno + e->num_lines) { 670/* there is a post-chunk part not blamed on parent */ 671 split[2].suspect =origin_incref(e->suspect); 672 split[2].lno = e->lno + (same - e->s_lno); 673 split[2].s_lno = e->s_lno + (same - e->s_lno); 674 split[2].num_lines = e->s_lno + e->num_lines - same; 675 chunk_end_lno = split[2].lno; 676} 677else 678 chunk_end_lno = e->lno + e->num_lines; 679 split[1].num_lines = chunk_end_lno - split[1].lno; 680 681/* 682 * if it turns out there is nothing to blame the parent for, 683 * forget about the splitting. !split[1].suspect signals this. 684 */ 685if(split[1].num_lines <1) 686return; 687 split[1].suspect =origin_incref(parent); 688} 689 690/* 691 * split_overlap() divided an existing blame e into up to three parts 692 * in split. Adjust the linked list of blames in the scoreboard to 693 * reflect the split. 694 */ 695static voidsplit_blame(struct scoreboard *sb, 696struct blame_entry *split, 697struct blame_entry *e) 698{ 699struct blame_entry *new_entry; 700 701if(split[0].suspect && split[2].suspect) { 702/* The first part (reuse storage for the existing entry e) */ 703dup_entry(e, &split[0]); 704 705/* The last part -- me */ 706 new_entry =xmalloc(sizeof(*new_entry)); 707memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 708add_blame_entry(sb, new_entry); 709 710/* ... and the middle part -- parent */ 711 new_entry =xmalloc(sizeof(*new_entry)); 712memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 713add_blame_entry(sb, new_entry); 714} 715else if(!split[0].suspect && !split[2].suspect) 716/* 717 * The parent covers the entire area; reuse storage for 718 * e and replace it with the parent. 719 */ 720dup_entry(e, &split[1]); 721else if(split[0].suspect) { 722/* me and then parent */ 723dup_entry(e, &split[0]); 724 725 new_entry =xmalloc(sizeof(*new_entry)); 726memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 727add_blame_entry(sb, new_entry); 728} 729else{ 730/* parent and then me */ 731dup_entry(e, &split[1]); 732 733 new_entry =xmalloc(sizeof(*new_entry)); 734memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 735add_blame_entry(sb, new_entry); 736} 737 738if(DEBUG) {/* sanity */ 739struct blame_entry *ent; 740int lno = sb->ent->lno, corrupt =0; 741 742for(ent = sb->ent; ent; ent = ent->next) { 743if(lno != ent->lno) 744 corrupt =1; 745if(ent->s_lno <0) 746 corrupt =1; 747 lno += ent->num_lines; 748} 749if(corrupt) { 750 lno = sb->ent->lno; 751for(ent = sb->ent; ent; ent = ent->next) { 752printf("L%8d l%8d n%8d\n", 753 lno, ent->lno, ent->num_lines); 754 lno = ent->lno + ent->num_lines; 755} 756die("oops"); 757} 758} 759} 760 761/* 762 * After splitting the blame, the origins used by the 763 * on-stack blame_entry should lose one refcnt each. 764 */ 765static voiddecref_split(struct blame_entry *split) 766{ 767int i; 768 769for(i =0; i <3; i++) 770origin_decref(split[i].suspect); 771} 772 773/* 774 * Helper for blame_chunk(). blame_entry e is known to overlap with 775 * the patch hunk; split it and pass blame to the parent. 776 */ 777static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 778int tlno,int plno,int same, 779struct origin *parent) 780{ 781struct blame_entry split[3]; 782 783split_overlap(split, e, tlno, plno, same, parent); 784if(split[1].suspect) 785split_blame(sb, split, e); 786decref_split(split); 787} 788 789/* 790 * Find the line number of the last line the target is suspected for. 791 */ 792static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 793{ 794struct blame_entry *e; 795int last_in_target = -1; 796 797for(e = sb->ent; e; e = e->next) { 798if(e->guilty || !same_suspect(e->suspect, target)) 799continue; 800if(last_in_target < e->s_lno + e->num_lines) 801 last_in_target = e->s_lno + e->num_lines; 802} 803return last_in_target; 804} 805 806/* 807 * Process one hunk from the patch between the current suspect for 808 * blame_entry e and its parent. Find and split the overlap, and 809 * pass blame to the overlapping part to the parent. 810 */ 811static voidblame_chunk(struct scoreboard *sb, 812int tlno,int plno,int same, 813struct origin *target,struct origin *parent) 814{ 815struct blame_entry *e; 816 817for(e = sb->ent; e; e = e->next) { 818if(e->guilty || !same_suspect(e->suspect, target)) 819continue; 820if(same <= e->s_lno) 821continue; 822if(tlno < e->s_lno + e->num_lines) 823blame_overlap(sb, e, tlno, plno, same, parent); 824} 825} 826 827/* 828 * We are looking at the origin 'target' and aiming to pass blame 829 * for the lines it is suspected to its parent. Run diff to find 830 * which lines came from parent and pass blame for them. 831 */ 832static intpass_blame_to_parent(struct scoreboard *sb, 833struct origin *target, 834struct origin *parent) 835{ 836int i, last_in_target, plno, tlno; 837struct patch *patch; 838 839 last_in_target =find_last_in_target(sb, target); 840if(last_in_target <0) 841return1;/* nothing remains for this target */ 842 843 patch =get_patch(parent, target); 844 plno = tlno =0; 845for(i =0; i < patch->num; i++) { 846struct chunk *chunk = &patch->chunks[i]; 847 848blame_chunk(sb, tlno, plno, chunk->same, target, parent); 849 plno = chunk->p_next; 850 tlno = chunk->t_next; 851} 852/* The rest (i.e. anything after tlno) are the same as the parent */ 853blame_chunk(sb, tlno, plno, last_in_target, target, parent); 854 855free_patch(patch); 856return0; 857} 858 859/* 860 * The lines in blame_entry after splitting blames many times can become 861 * very small and trivial, and at some point it becomes pointless to 862 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 863 * ordinary C program, and it is not worth to say it was copied from 864 * totally unrelated file in the parent. 865 * 866 * Compute how trivial the lines in the blame_entry are. 867 */ 868static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 869{ 870unsigned score; 871const char*cp, *ep; 872 873if(e->score) 874return e->score; 875 876 score =1; 877 cp =nth_line(sb, e->lno); 878 ep =nth_line(sb, e->lno + e->num_lines); 879while(cp < ep) { 880unsigned ch = *((unsigned char*)cp); 881if(isalnum(ch)) 882 score++; 883 cp++; 884} 885 e->score = score; 886return score; 887} 888 889/* 890 * best_so_far[] and this[] are both a split of an existing blame_entry 891 * that passes blame to the parent. Maintain best_so_far the best split 892 * so far, by comparing this and best_so_far and copying this into 893 * bst_so_far as needed. 894 */ 895static voidcopy_split_if_better(struct scoreboard *sb, 896struct blame_entry *best_so_far, 897struct blame_entry *this) 898{ 899int i; 900 901if(!this[1].suspect) 902return; 903if(best_so_far[1].suspect) { 904if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 905return; 906} 907 908for(i =0; i <3; i++) 909origin_incref(this[i].suspect); 910decref_split(best_so_far); 911memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 912} 913 914/* 915 * We are looking at a part of the final image represented by 916 * ent (tlno and same are offset by ent->s_lno). 917 * tlno is where we are looking at in the final image. 918 * up to (but not including) same match preimage. 919 * plno is where we are looking at in the preimage. 920 * 921 * <-------------- final image ----------------------> 922 * <------ent------> 923 * ^tlno ^same 924 * <---------preimage-----> 925 * ^plno 926 * 927 * All line numbers are 0-based. 928 */ 929static voidhandle_split(struct scoreboard *sb, 930struct blame_entry *ent, 931int tlno,int plno,int same, 932struct origin *parent, 933struct blame_entry *split) 934{ 935if(ent->num_lines <= tlno) 936return; 937if(tlno < same) { 938struct blame_entry this[3]; 939 tlno += ent->s_lno; 940 same += ent->s_lno; 941split_overlap(this, ent, tlno, plno, same, parent); 942copy_split_if_better(sb, split,this); 943decref_split(this); 944} 945} 946 947/* 948 * Find the lines from parent that are the same as ent so that 949 * we can pass blames to it. file_p has the blob contents for 950 * the parent. 951 */ 952static voidfind_copy_in_blob(struct scoreboard *sb, 953struct blame_entry *ent, 954struct origin *parent, 955struct blame_entry *split, 956 mmfile_t *file_p) 957{ 958const char*cp; 959int cnt; 960 mmfile_t file_o; 961struct patch *patch; 962int i, plno, tlno; 963 964/* 965 * Prepare mmfile that contains only the lines in ent. 966 */ 967 cp =nth_line(sb, ent->lno); 968 file_o.ptr = (char*) cp; 969 cnt = ent->num_lines; 970 971while(cnt && cp < sb->final_buf + sb->final_buf_size) { 972if(*cp++ =='\n') 973 cnt--; 974} 975 file_o.size = cp - file_o.ptr; 976 977 patch =compare_buffer(file_p, &file_o,1); 978 979/* 980 * file_o is a part of final image we are annotating. 981 * file_p partially may match that image. 982 */ 983memset(split,0,sizeof(struct blame_entry [3])); 984 plno = tlno =0; 985for(i =0; i < patch->num; i++) { 986struct chunk *chunk = &patch->chunks[i]; 987 988handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 989 plno = chunk->p_next; 990 tlno = chunk->t_next; 991} 992/* remainder, if any, all match the preimage */ 993handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 994free_patch(patch); 995} 996 997/* 998 * See if lines currently target is suspected for can be attributed to 999 * parent.1000 */1001static intfind_move_in_parent(struct scoreboard *sb,1002struct origin *target,1003struct origin *parent)1004{1005int last_in_target, made_progress;1006struct blame_entry *e, split[3];1007 mmfile_t file_p;10081009 last_in_target =find_last_in_target(sb, target);1010if(last_in_target <0)1011return1;/* nothing remains for this target */10121013fill_origin_blob(parent, &file_p);1014if(!file_p.ptr)1015return0;10161017 made_progress =1;1018while(made_progress) {1019 made_progress =0;1020for(e = sb->ent; e; e = e->next) {1021if(e->guilty || !same_suspect(e->suspect, target))1022continue;1023find_copy_in_blob(sb, e, parent, split, &file_p);1024if(split[1].suspect &&1025 blame_move_score <ent_score(sb, &split[1])) {1026split_blame(sb, split, e);1027 made_progress =1;1028}1029decref_split(split);1030}1031}1032return0;1033}10341035struct blame_list {1036struct blame_entry *ent;1037struct blame_entry split[3];1038};10391040/*1041 * Count the number of entries the target is suspected for,1042 * and prepare a list of entry and the best split.1043 */1044static struct blame_list *setup_blame_list(struct scoreboard *sb,1045struct origin *target,1046int*num_ents_p)1047{1048struct blame_entry *e;1049int num_ents, i;1050struct blame_list *blame_list = NULL;10511052for(e = sb->ent, num_ents =0; e; e = e->next)1053if(!e->guilty &&same_suspect(e->suspect, target))1054 num_ents++;1055if(num_ents) {1056 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1057for(e = sb->ent, i =0; e; e = e->next)1058if(!e->guilty &&same_suspect(e->suspect, target))1059 blame_list[i++].ent = e;1060}1061*num_ents_p = num_ents;1062return blame_list;1063}10641065/*1066 * For lines target is suspected for, see if we can find code movement1067 * across file boundary from the parent commit. porigin is the path1068 * in the parent we already tried.1069 */1070static intfind_copy_in_parent(struct scoreboard *sb,1071struct origin *target,1072struct commit *parent,1073struct origin *porigin,1074int opt)1075{1076struct diff_options diff_opts;1077const char*paths[1];1078int i, j;1079int retval;1080struct blame_list *blame_list;1081int num_ents;10821083 blame_list =setup_blame_list(sb, target, &num_ents);1084if(!blame_list)1085return1;/* nothing remains for this target */10861087diff_setup(&diff_opts);1088DIFF_OPT_SET(&diff_opts, RECURSIVE);1089 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10901091 paths[0] = NULL;1092diff_tree_setup_paths(paths, &diff_opts);1093if(diff_setup_done(&diff_opts) <0)1094die("diff-setup");10951096/* Try "find copies harder" on new path if requested;1097 * we do not want to use diffcore_rename() actually to1098 * match things up; find_copies_harder is set only to1099 * force diff_tree_sha1() to feed all filepairs to diff_queue,1100 * and this code needs to be after diff_setup_done(), which1101 * usually makes find-copies-harder imply copy detection.1102 */1103if((opt & PICKAXE_BLAME_COPY_HARDEST)1104|| ((opt & PICKAXE_BLAME_COPY_HARDER)1105&& (!porigin ||strcmp(target->path, porigin->path))))1106DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);11071108if(is_null_sha1(target->commit->object.sha1))1109do_diff_cache(parent->tree->object.sha1, &diff_opts);1110else1111diff_tree_sha1(parent->tree->object.sha1,1112 target->commit->tree->object.sha1,1113"", &diff_opts);11141115if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1116diffcore_std(&diff_opts);11171118 retval =0;1119while(1) {1120int made_progress =0;11211122for(i =0; i < diff_queued_diff.nr; i++) {1123struct diff_filepair *p = diff_queued_diff.queue[i];1124struct origin *norigin;1125 mmfile_t file_p;1126struct blame_entry this[3];11271128if(!DIFF_FILE_VALID(p->one))1129continue;/* does not exist in parent */1130if(porigin && !strcmp(p->one->path, porigin->path))1131/* find_move already dealt with this path */1132continue;11331134 norigin =get_origin(sb, parent, p->one->path);1135hashcpy(norigin->blob_sha1, p->one->sha1);1136fill_origin_blob(norigin, &file_p);1137if(!file_p.ptr)1138continue;11391140for(j =0; j < num_ents; j++) {1141find_copy_in_blob(sb, blame_list[j].ent,1142 norigin,this, &file_p);1143copy_split_if_better(sb, blame_list[j].split,1144this);1145decref_split(this);1146}1147origin_decref(norigin);1148}11491150for(j =0; j < num_ents; j++) {1151struct blame_entry *split = blame_list[j].split;1152if(split[1].suspect &&1153 blame_copy_score <ent_score(sb, &split[1])) {1154split_blame(sb, split, blame_list[j].ent);1155 made_progress =1;1156}1157decref_split(split);1158}1159free(blame_list);11601161if(!made_progress)1162break;1163 blame_list =setup_blame_list(sb, target, &num_ents);1164if(!blame_list) {1165 retval =1;1166break;1167}1168}1169diff_flush(&diff_opts);1170diff_tree_release_paths(&diff_opts);1171return retval;1172}11731174/*1175 * The blobs of origin and porigin exactly match, so everything1176 * origin is suspected for can be blamed on the parent.1177 */1178static voidpass_whole_blame(struct scoreboard *sb,1179struct origin *origin,struct origin *porigin)1180{1181struct blame_entry *e;11821183if(!porigin->file.ptr && origin->file.ptr) {1184/* Steal its file */1185 porigin->file = origin->file;1186 origin->file.ptr = NULL;1187}1188for(e = sb->ent; e; e = e->next) {1189if(!same_suspect(e->suspect, origin))1190continue;1191origin_incref(porigin);1192origin_decref(e->suspect);1193 e->suspect = porigin;1194}1195}11961197#define MAXPARENT 1611981199static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1200{1201int i, pass;1202struct commit *commit = origin->commit;1203struct commit_list *parent;1204struct origin *parent_origin[MAXPARENT], *porigin;12051206memset(parent_origin,0,sizeof(parent_origin));12071208/* The first pass looks for unrenamed path to optimize for1209 * common cases, then we look for renames in the second pass.1210 */1211for(pass =0; pass <2; pass++) {1212struct origin *(*find)(struct scoreboard *,1213struct commit *,struct origin *);1214 find = pass ? find_rename : find_origin;12151216for(i =0, parent = commit->parents;1217 i < MAXPARENT && parent;1218 parent = parent->next, i++) {1219struct commit *p = parent->item;1220int j, same;12211222if(parent_origin[i])1223continue;1224if(parse_commit(p))1225continue;1226 porigin =find(sb, p, origin);1227if(!porigin)1228continue;1229if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1230pass_whole_blame(sb, origin, porigin);1231origin_decref(porigin);1232goto finish;1233}1234for(j = same =0; j < i; j++)1235if(parent_origin[j] &&1236!hashcmp(parent_origin[j]->blob_sha1,1237 porigin->blob_sha1)) {1238 same =1;1239break;1240}1241if(!same)1242 parent_origin[i] = porigin;1243else1244origin_decref(porigin);1245}1246}12471248 num_commits++;1249for(i =0, parent = commit->parents;1250 i < MAXPARENT && parent;1251 parent = parent->next, i++) {1252struct origin *porigin = parent_origin[i];1253if(!porigin)1254continue;1255if(pass_blame_to_parent(sb, origin, porigin))1256goto finish;1257}12581259/*1260 * Optionally find moves in parents' files.1261 */1262if(opt & PICKAXE_BLAME_MOVE)1263for(i =0, parent = commit->parents;1264 i < MAXPARENT && parent;1265 parent = parent->next, i++) {1266struct origin *porigin = parent_origin[i];1267if(!porigin)1268continue;1269if(find_move_in_parent(sb, origin, porigin))1270goto finish;1271}12721273/*1274 * Optionally find copies from parents' files.1275 */1276if(opt & PICKAXE_BLAME_COPY)1277for(i =0, parent = commit->parents;1278 i < MAXPARENT && parent;1279 parent = parent->next, i++) {1280struct origin *porigin = parent_origin[i];1281if(find_copy_in_parent(sb, origin, parent->item,1282 porigin, opt))1283goto finish;1284}12851286 finish:1287for(i =0; i < MAXPARENT; i++) {1288if(parent_origin[i]) {1289drop_origin_blob(parent_origin[i]);1290origin_decref(parent_origin[i]);1291}1292}1293drop_origin_blob(origin);1294}12951296/*1297 * Information on commits, used for output.1298 */1299struct commit_info1300{1301const char*author;1302const char*author_mail;1303unsigned long author_time;1304const char*author_tz;13051306/* filled only when asked for details */1307const char*committer;1308const char*committer_mail;1309unsigned long committer_time;1310const char*committer_tz;13111312const char*summary;1313};13141315/*1316 * Parse author/committer line in the commit object buffer1317 */1318static voidget_ac_line(const char*inbuf,const char*what,1319int bufsz,char*person,const char**mail,1320unsigned long*time,const char**tz)1321{1322int len, tzlen, maillen;1323char*tmp, *endp, *timepos;13241325 tmp =strstr(inbuf, what);1326if(!tmp)1327goto error_out;1328 tmp +=strlen(what);1329 endp =strchr(tmp,'\n');1330if(!endp)1331 len =strlen(tmp);1332else1333 len = endp - tmp;1334if(bufsz <= len) {1335 error_out:1336/* Ugh */1337*mail = *tz ="(unknown)";1338*time =0;1339return;1340}1341memcpy(person, tmp, len);13421343 tmp = person;1344 tmp += len;1345*tmp =0;1346while(*tmp !=' ')1347 tmp--;1348*tz = tmp+1;1349 tzlen = (person+len)-(tmp+1);13501351*tmp =0;1352while(*tmp !=' ')1353 tmp--;1354*time =strtoul(tmp, NULL,10);1355 timepos = tmp;13561357*tmp =0;1358while(*tmp !=' ')1359 tmp--;1360*mail = tmp +1;1361*tmp =0;1362 maillen = timepos - tmp;13631364if(!mailmap.nr)1365return;13661367/*1368 * mailmap expansion may make the name longer.1369 * make room by pushing stuff down.1370 */1371 tmp = person + bufsz - (tzlen +1);1372memmove(tmp, *tz, tzlen);1373 tmp[tzlen] =0;1374*tz = tmp;13751376 tmp = tmp - (maillen +1);1377memmove(tmp, *mail, maillen);1378 tmp[maillen] =0;1379*mail = tmp;13801381/*1382 * Now, convert e-mail using mailmap1383 */1384map_email(&mailmap, tmp +1, person, tmp-person-1);1385}13861387static voidget_commit_info(struct commit *commit,1388struct commit_info *ret,1389int detailed)1390{1391int len;1392char*tmp, *endp;1393static char author_buf[1024];1394static char committer_buf[1024];1395static char summary_buf[1024];13961397/*1398 * We've operated without save_commit_buffer, so1399 * we now need to populate them for output.1400 */1401if(!commit->buffer) {1402enum object_type type;1403unsigned long size;1404 commit->buffer =1405read_sha1_file(commit->object.sha1, &type, &size);1406if(!commit->buffer)1407die("Cannot read commit%s",1408sha1_to_hex(commit->object.sha1));1409}1410 ret->author = author_buf;1411get_ac_line(commit->buffer,"\nauthor ",1412sizeof(author_buf), author_buf, &ret->author_mail,1413&ret->author_time, &ret->author_tz);14141415if(!detailed)1416return;14171418 ret->committer = committer_buf;1419get_ac_line(commit->buffer,"\ncommitter ",1420sizeof(committer_buf), committer_buf, &ret->committer_mail,1421&ret->committer_time, &ret->committer_tz);14221423 ret->summary = summary_buf;1424 tmp =strstr(commit->buffer,"\n\n");1425if(!tmp) {1426 error_out:1427sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1428return;1429}1430 tmp +=2;1431 endp =strchr(tmp,'\n');1432if(!endp)1433 endp = tmp +strlen(tmp);1434 len = endp - tmp;1435if(len >=sizeof(summary_buf) || len ==0)1436goto error_out;1437memcpy(summary_buf, tmp, len);1438 summary_buf[len] =0;1439}14401441/*1442 * To allow LF and other nonportable characters in pathnames,1443 * they are c-style quoted as needed.1444 */1445static voidwrite_filename_info(const char*path)1446{1447printf("filename ");1448write_name_quoted(path, stdout,'\n');1449}14501451/*1452 * The blame_entry is found to be guilty for the range. Mark it1453 * as such, and show it in incremental output.1454 */1455static voidfound_guilty_entry(struct blame_entry *ent)1456{1457if(ent->guilty)1458return;1459 ent->guilty =1;1460if(incremental) {1461struct origin *suspect = ent->suspect;14621463printf("%s %d %d %d\n",1464sha1_to_hex(suspect->commit->object.sha1),1465 ent->s_lno +1, ent->lno +1, ent->num_lines);1466if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1467struct commit_info ci;1468 suspect->commit->object.flags |= METAINFO_SHOWN;1469get_commit_info(suspect->commit, &ci,1);1470printf("author%s\n", ci.author);1471printf("author-mail%s\n", ci.author_mail);1472printf("author-time%lu\n", ci.author_time);1473printf("author-tz%s\n", ci.author_tz);1474printf("committer%s\n", ci.committer);1475printf("committer-mail%s\n", ci.committer_mail);1476printf("committer-time%lu\n", ci.committer_time);1477printf("committer-tz%s\n", ci.committer_tz);1478printf("summary%s\n", ci.summary);1479if(suspect->commit->object.flags & UNINTERESTING)1480printf("boundary\n");1481}1482write_filename_info(suspect->path);1483maybe_flush_or_die(stdout,"stdout");1484}1485}14861487/*1488 * The main loop -- while the scoreboard has lines whose true origin1489 * is still unknown, pick one blame_entry, and allow its current1490 * suspect to pass blames to its parents.1491 */1492static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1493{1494while(1) {1495struct blame_entry *ent;1496struct commit *commit;1497struct origin *suspect = NULL;14981499/* find one suspect to break down */1500for(ent = sb->ent; !suspect && ent; ent = ent->next)1501if(!ent->guilty)1502 suspect = ent->suspect;1503if(!suspect)1504return;/* all done */15051506/*1507 * We will use this suspect later in the loop,1508 * so hold onto it in the meantime.1509 */1510origin_incref(suspect);1511 commit = suspect->commit;1512if(!commit->object.parsed)1513parse_commit(commit);1514if(!(commit->object.flags & UNINTERESTING) &&1515!(revs->max_age != -1&& commit->date < revs->max_age))1516pass_blame(sb, suspect, opt);1517else{1518 commit->object.flags |= UNINTERESTING;1519if(commit->object.parsed)1520mark_parents_uninteresting(commit);1521}1522/* treat root commit as boundary */1523if(!commit->parents && !show_root)1524 commit->object.flags |= UNINTERESTING;15251526/* Take responsibility for the remaining entries */1527for(ent = sb->ent; ent; ent = ent->next)1528if(same_suspect(ent->suspect, suspect))1529found_guilty_entry(ent);1530origin_decref(suspect);15311532if(DEBUG)/* sanity */1533sanity_check_refcnt(sb);1534}1535}15361537static const char*format_time(unsigned long time,const char*tz_str,1538int show_raw_time)1539{1540static char time_buf[128];1541time_t t = time;1542int minutes, tz;1543struct tm *tm;15441545if(show_raw_time) {1546sprintf(time_buf,"%lu%s", time, tz_str);1547return time_buf;1548}15491550 tz =atoi(tz_str);1551 minutes = tz <0? -tz : tz;1552 minutes = (minutes /100)*60+ (minutes %100);1553 minutes = tz <0? -minutes : minutes;1554 t = time + minutes *60;1555 tm =gmtime(&t);15561557strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1558strcat(time_buf, tz_str);1559return time_buf;1560}15611562#define OUTPUT_ANNOTATE_COMPAT 0011563#define OUTPUT_LONG_OBJECT_NAME 0021564#define OUTPUT_RAW_TIMESTAMP 0041565#define OUTPUT_PORCELAIN 0101566#define OUTPUT_SHOW_NAME 0201567#define OUTPUT_SHOW_NUMBER 0401568#define OUTPUT_SHOW_SCORE 01001569#define OUTPUT_NO_AUTHOR 020015701571static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1572{1573int cnt;1574const char*cp;1575struct origin *suspect = ent->suspect;1576char hex[41];15771578strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1579printf("%s%c%d %d %d\n",1580 hex,1581 ent->guilty ?' ':'*',// purely for debugging1582 ent->s_lno +1,1583 ent->lno +1,1584 ent->num_lines);1585if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1586struct commit_info ci;1587 suspect->commit->object.flags |= METAINFO_SHOWN;1588get_commit_info(suspect->commit, &ci,1);1589printf("author%s\n", ci.author);1590printf("author-mail%s\n", ci.author_mail);1591printf("author-time%lu\n", ci.author_time);1592printf("author-tz%s\n", ci.author_tz);1593printf("committer%s\n", ci.committer);1594printf("committer-mail%s\n", ci.committer_mail);1595printf("committer-time%lu\n", ci.committer_time);1596printf("committer-tz%s\n", ci.committer_tz);1597write_filename_info(suspect->path);1598printf("summary%s\n", ci.summary);1599if(suspect->commit->object.flags & UNINTERESTING)1600printf("boundary\n");1601}1602else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1603write_filename_info(suspect->path);16041605 cp =nth_line(sb, ent->lno);1606for(cnt =0; cnt < ent->num_lines; cnt++) {1607char ch;1608if(cnt)1609printf("%s %d %d\n", hex,1610 ent->s_lno +1+ cnt,1611 ent->lno +1+ cnt);1612putchar('\t');1613do{1614 ch = *cp++;1615putchar(ch);1616}while(ch !='\n'&&1617 cp < sb->final_buf + sb->final_buf_size);1618}1619}16201621static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1622{1623int cnt;1624const char*cp;1625struct origin *suspect = ent->suspect;1626struct commit_info ci;1627char hex[41];1628int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16291630get_commit_info(suspect->commit, &ci,1);1631strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16321633 cp =nth_line(sb, ent->lno);1634for(cnt =0; cnt < ent->num_lines; cnt++) {1635char ch;1636int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16371638if(suspect->commit->object.flags & UNINTERESTING) {1639if(blank_boundary)1640memset(hex,' ', length);1641else if(!cmd_is_annotate) {1642 length--;1643putchar('^');1644}1645}16461647printf("%.*s", length, hex);1648if(opt & OUTPUT_ANNOTATE_COMPAT)1649printf("\t(%10s\t%10s\t%d)", ci.author,1650format_time(ci.author_time, ci.author_tz,1651 show_raw_time),1652 ent->lno +1+ cnt);1653else{1654if(opt & OUTPUT_SHOW_SCORE)1655printf(" %*d%02d",1656 max_score_digits, ent->score,1657 ent->suspect->refcnt);1658if(opt & OUTPUT_SHOW_NAME)1659printf(" %-*.*s", longest_file, longest_file,1660 suspect->path);1661if(opt & OUTPUT_SHOW_NUMBER)1662printf(" %*d", max_orig_digits,1663 ent->s_lno +1+ cnt);16641665if(!(opt & OUTPUT_NO_AUTHOR))1666printf(" (%-*.*s%10s",1667 longest_author, longest_author,1668 ci.author,1669format_time(ci.author_time,1670 ci.author_tz,1671 show_raw_time));1672printf(" %*d) ",1673 max_digits, ent->lno +1+ cnt);1674}1675do{1676 ch = *cp++;1677putchar(ch);1678}while(ch !='\n'&&1679 cp < sb->final_buf + sb->final_buf_size);1680}1681}16821683static voidoutput(struct scoreboard *sb,int option)1684{1685struct blame_entry *ent;16861687if(option & OUTPUT_PORCELAIN) {1688for(ent = sb->ent; ent; ent = ent->next) {1689struct blame_entry *oth;1690struct origin *suspect = ent->suspect;1691struct commit *commit = suspect->commit;1692if(commit->object.flags & MORE_THAN_ONE_PATH)1693continue;1694for(oth = ent->next; oth; oth = oth->next) {1695if((oth->suspect->commit != commit) ||1696!strcmp(oth->suspect->path, suspect->path))1697continue;1698 commit->object.flags |= MORE_THAN_ONE_PATH;1699break;1700}1701}1702}17031704for(ent = sb->ent; ent; ent = ent->next) {1705if(option & OUTPUT_PORCELAIN)1706emit_porcelain(sb, ent);1707else{1708emit_other(sb, ent, option);1709}1710}1711}17121713/*1714 * To allow quick access to the contents of nth line in the1715 * final image, prepare an index in the scoreboard.1716 */1717static intprepare_lines(struct scoreboard *sb)1718{1719const char*buf = sb->final_buf;1720unsigned long len = sb->final_buf_size;1721int num =0, incomplete =0, bol =1;17221723if(len && buf[len-1] !='\n')1724 incomplete++;/* incomplete line at the end */1725while(len--) {1726if(bol) {1727 sb->lineno =xrealloc(sb->lineno,1728sizeof(int* ) * (num +1));1729 sb->lineno[num] = buf - sb->final_buf;1730 bol =0;1731}1732if(*buf++ =='\n') {1733 num++;1734 bol =1;1735}1736}1737 sb->lineno =xrealloc(sb->lineno,1738sizeof(int* ) * (num + incomplete +1));1739 sb->lineno[num + incomplete] = buf - sb->final_buf;1740 sb->num_lines = num + incomplete;1741return sb->num_lines;1742}17431744/*1745 * Add phony grafts for use with -S; this is primarily to1746 * support git-cvsserver that wants to give a linear history1747 * to its clients.1748 */1749static intread_ancestry(const char*graft_file)1750{1751FILE*fp =fopen(graft_file,"r");1752char buf[1024];1753if(!fp)1754return-1;1755while(fgets(buf,sizeof(buf), fp)) {1756/* The format is just "Commit Parent1 Parent2 ...\n" */1757int len =strlen(buf);1758struct commit_graft *graft =read_graft_line(buf, len);1759if(graft)1760register_commit_graft(graft,0);1761}1762fclose(fp);1763return0;1764}17651766/*1767 * How many columns do we need to show line numbers in decimal?1768 */1769static intlineno_width(int lines)1770{1771int i, width;17721773for(width =1, i =10; i <= lines +1; width++)1774 i *=10;1775return width;1776}17771778/*1779 * How many columns do we need to show line numbers, authors,1780 * and filenames?1781 */1782static voidfind_alignment(struct scoreboard *sb,int*option)1783{1784int longest_src_lines =0;1785int longest_dst_lines =0;1786unsigned largest_score =0;1787struct blame_entry *e;17881789for(e = sb->ent; e; e = e->next) {1790struct origin *suspect = e->suspect;1791struct commit_info ci;1792int num;17931794if(strcmp(suspect->path, sb->path))1795*option |= OUTPUT_SHOW_NAME;1796 num =strlen(suspect->path);1797if(longest_file < num)1798 longest_file = num;1799if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1800 suspect->commit->object.flags |= METAINFO_SHOWN;1801get_commit_info(suspect->commit, &ci,1);1802 num =strlen(ci.author);1803if(longest_author < num)1804 longest_author = num;1805}1806 num = e->s_lno + e->num_lines;1807if(longest_src_lines < num)1808 longest_src_lines = num;1809 num = e->lno + e->num_lines;1810if(longest_dst_lines < num)1811 longest_dst_lines = num;1812if(largest_score <ent_score(sb, e))1813 largest_score =ent_score(sb, e);1814}1815 max_orig_digits =lineno_width(longest_src_lines);1816 max_digits =lineno_width(longest_dst_lines);1817 max_score_digits =lineno_width(largest_score);1818}18191820/*1821 * For debugging -- origin is refcounted, and this asserts that1822 * we do not underflow.1823 */1824static voidsanity_check_refcnt(struct scoreboard *sb)1825{1826int baa =0;1827struct blame_entry *ent;18281829for(ent = sb->ent; ent; ent = ent->next) {1830/* Nobody should have zero or negative refcnt */1831if(ent->suspect->refcnt <=0) {1832fprintf(stderr,"%sin%shas negative refcnt%d\n",1833 ent->suspect->path,1834sha1_to_hex(ent->suspect->commit->object.sha1),1835 ent->suspect->refcnt);1836 baa =1;1837}1838}1839for(ent = sb->ent; ent; ent = ent->next) {1840/* Mark the ones that haven't been checked */1841if(0< ent->suspect->refcnt)1842 ent->suspect->refcnt = -ent->suspect->refcnt;1843}1844for(ent = sb->ent; ent; ent = ent->next) {1845/*1846 * ... then pick each and see if they have the the1847 * correct refcnt.1848 */1849int found;1850struct blame_entry *e;1851struct origin *suspect = ent->suspect;18521853if(0< suspect->refcnt)1854continue;1855 suspect->refcnt = -suspect->refcnt;/* Unmark */1856for(found =0, e = sb->ent; e; e = e->next) {1857if(e->suspect != suspect)1858continue;1859 found++;1860}1861if(suspect->refcnt != found) {1862fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1863 ent->suspect->path,1864sha1_to_hex(ent->suspect->commit->object.sha1),1865 ent->suspect->refcnt, found);1866 baa =2;1867}1868}1869if(baa) {1870int opt =0160;1871find_alignment(sb, &opt);1872output(sb, opt);1873die("Baa%d!", baa);1874}1875}18761877/*1878 * Used for the command line parsing; check if the path exists1879 * in the working tree.1880 */1881static inthas_path_in_work_tree(const char*path)1882{1883struct stat st;1884return!lstat(path, &st);1885}18861887static unsignedparse_score(const char*arg)1888{1889char*end;1890unsigned long score =strtoul(arg, &end,10);1891if(*end)1892return0;1893return score;1894}18951896static const char*add_prefix(const char*prefix,const char*path)1897{1898if(!prefix || !prefix[0])1899return path;1900returnprefix_path(prefix,strlen(prefix), path);1901}19021903/*1904 * Parsing of (comma separated) one item in the -L option1905 */1906static const char*parse_loc(const char*spec,1907struct scoreboard *sb,long lno,1908long begin,long*ret)1909{1910char*term;1911const char*line;1912long num;1913int reg_error;1914 regex_t regexp;1915 regmatch_t match[1];19161917/* Allow "-L <something>,+20" to mean starting at <something>1918 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1919 * <something>.1920 */1921if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1922 num =strtol(spec +1, &term,10);1923if(term != spec +1) {1924if(spec[0] =='-')1925 num =0- num;1926if(0< num)1927*ret = begin + num -2;1928else if(!num)1929*ret = begin;1930else1931*ret = begin + num;1932return term;1933}1934return spec;1935}1936 num =strtol(spec, &term,10);1937if(term != spec) {1938*ret = num;1939return term;1940}1941if(spec[0] !='/')1942return spec;19431944/* it could be a regexp of form /.../ */1945for(term = (char*) spec +1; *term && *term !='/'; term++) {1946if(*term =='\\')1947 term++;1948}1949if(*term !='/')1950return spec;19511952/* try [spec+1 .. term-1] as regexp */1953*term =0;1954 begin--;/* input is in human terms */1955 line =nth_line(sb, begin);19561957if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1958!(reg_error =regexec(®exp, line,1, match,0))) {1959const char*cp = line + match[0].rm_so;1960const char*nline;19611962while(begin++ < lno) {1963 nline =nth_line(sb, begin);1964if(line <= cp && cp < nline)1965break;1966 line = nline;1967}1968*ret = begin;1969regfree(®exp);1970*term++ ='/';1971return term;1972}1973else{1974char errbuf[1024];1975regerror(reg_error, ®exp, errbuf,1024);1976die("-L parameter '%s':%s", spec +1, errbuf);1977}1978}19791980/*1981 * Parsing of -L option1982 */1983static voidprepare_blame_range(struct scoreboard *sb,1984const char*bottomtop,1985long lno,1986long*bottom,long*top)1987{1988const char*term;19891990 term =parse_loc(bottomtop, sb, lno,1, bottom);1991if(*term ==',') {1992 term =parse_loc(term +1, sb, lno, *bottom +1, top);1993if(*term)1994usage(blame_usage);1995}1996if(*term)1997usage(blame_usage);1998}19992000static intgit_blame_config(const char*var,const char*value)2001{2002if(!strcmp(var,"blame.showroot")) {2003 show_root =git_config_bool(var, value);2004return0;2005}2006if(!strcmp(var,"blame.blankboundary")) {2007 blank_boundary =git_config_bool(var, value);2008return0;2009}2010returngit_default_config(var, value);2011}20122013static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2014{2015struct commit *commit;2016struct origin *origin;2017unsigned char head_sha1[20];2018struct strbuf buf;2019const char*ident;2020time_t now;2021int size, len;2022struct cache_entry *ce;2023unsigned mode;20242025if(get_sha1("HEAD", head_sha1))2026die("No such ref: HEAD");20272028time(&now);2029 commit =xcalloc(1,sizeof(*commit));2030 commit->parents =xcalloc(1,sizeof(*commit->parents));2031 commit->parents->item =lookup_commit_reference(head_sha1);2032 commit->object.parsed =1;2033 commit->date = now;2034 commit->object.type = OBJ_COMMIT;20352036 origin =make_origin(commit, path);20372038strbuf_init(&buf,0);2039if(!contents_from ||strcmp("-", contents_from)) {2040struct stat st;2041const char*read_from;2042unsigned long fin_size;20432044if(contents_from) {2045if(stat(contents_from, &st) <0)2046die("Cannot stat%s", contents_from);2047 read_from = contents_from;2048}2049else{2050if(lstat(path, &st) <0)2051die("Cannot lstat%s", path);2052 read_from = path;2053}2054 fin_size =xsize_t(st.st_size);2055 mode =canon_mode(st.st_mode);2056switch(st.st_mode & S_IFMT) {2057case S_IFREG:2058if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2059die("cannot open or read%s", read_from);2060break;2061case S_IFLNK:2062if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2063die("cannot readlink%s", read_from);2064 buf.len = fin_size;2065break;2066default:2067die("unsupported file type%s", read_from);2068}2069}2070else{2071/* Reading from stdin */2072 contents_from ="standard input";2073 mode =0;2074if(strbuf_read(&buf,0,0) <0)2075die("read error%sfrom stdin",strerror(errno));2076}2077convert_to_git(path, buf.buf, buf.len, &buf);2078 origin->file.ptr = buf.buf;2079 origin->file.size = buf.len;2080pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2081 commit->util = origin;20822083/*2084 * Read the current index, replace the path entry with2085 * origin->blob_sha1 without mucking with its mode or type2086 * bits; we are not going to write this index out -- we just2087 * want to run "diff-index --cached".2088 */2089discard_cache();2090read_cache();20912092 len =strlen(path);2093if(!mode) {2094int pos =cache_name_pos(path, len);2095if(0<= pos)2096 mode =ntohl(active_cache[pos]->ce_mode);2097else2098/* Let's not bother reading from HEAD tree */2099 mode = S_IFREG |0644;2100}2101 size =cache_entry_size(len);2102 ce =xcalloc(1, size);2103hashcpy(ce->sha1, origin->blob_sha1);2104memcpy(ce->name, path, len);2105 ce->ce_flags =create_ce_flags(len,0);2106 ce->ce_mode =create_ce_mode(mode);2107add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);21082109/*2110 * We are not going to write this out, so this does not matter2111 * right now, but someday we might optimize diff-index --cached2112 * with cache-tree information.2113 */2114cache_tree_invalidate_path(active_cache_tree, path);21152116 commit->buffer =xmalloc(400);2117 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2118snprintf(commit->buffer,400,2119"tree 0000000000000000000000000000000000000000\n"2120"parent%s\n"2121"author%s\n"2122"committer%s\n\n"2123"Version of%sfrom%s\n",2124sha1_to_hex(head_sha1),2125 ident, ident, path, contents_from ? contents_from : path);2126return commit;2127}21282129intcmd_blame(int argc,const char**argv,const char*prefix)2130{2131struct rev_info revs;2132const char*path;2133struct scoreboard sb;2134struct origin *o;2135struct blame_entry *ent;2136int i, seen_dashdash, unk, opt;2137long bottom, top, lno;2138int output_option =0;2139int show_stats =0;2140const char*revs_file = NULL;2141const char*final_commit_name = NULL;2142enum object_type type;2143const char*bottomtop = NULL;2144const char*contents_from = NULL;21452146 cmd_is_annotate = !strcmp(argv[0],"annotate");21472148git_config(git_blame_config);2149 save_commit_buffer =0;21502151 opt =0;2152 seen_dashdash =0;2153for(unk = i =1; i < argc; i++) {2154const char*arg = argv[i];2155if(*arg !='-')2156break;2157else if(!strcmp("-b", arg))2158 blank_boundary =1;2159else if(!strcmp("--root", arg))2160 show_root =1;2161else if(!strcmp(arg,"--show-stats"))2162 show_stats =1;2163else if(!strcmp("-c", arg))2164 output_option |= OUTPUT_ANNOTATE_COMPAT;2165else if(!strcmp("-t", arg))2166 output_option |= OUTPUT_RAW_TIMESTAMP;2167else if(!strcmp("-l", arg))2168 output_option |= OUTPUT_LONG_OBJECT_NAME;2169else if(!strcmp("-s", arg))2170 output_option |= OUTPUT_NO_AUTHOR;2171else if(!strcmp("-w", arg))2172 xdl_opts |= XDF_IGNORE_WHITESPACE;2173else if(!strcmp("-S", arg) && ++i < argc)2174 revs_file = argv[i];2175else if(!prefixcmp(arg,"-M")) {2176 opt |= PICKAXE_BLAME_MOVE;2177 blame_move_score =parse_score(arg+2);2178}2179else if(!prefixcmp(arg,"-C")) {2180/*2181 * -C enables copy from removed files;2182 * -C -C enables copy from existing files, but only2183 * when blaming a new file;2184 * -C -C -C enables copy from existing files for2185 * everybody2186 */2187if(opt & PICKAXE_BLAME_COPY_HARDER)2188 opt |= PICKAXE_BLAME_COPY_HARDEST;2189if(opt & PICKAXE_BLAME_COPY)2190 opt |= PICKAXE_BLAME_COPY_HARDER;2191 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2192 blame_copy_score =parse_score(arg+2);2193}2194else if(!prefixcmp(arg,"-L")) {2195if(!arg[2]) {2196if(++i >= argc)2197usage(blame_usage);2198 arg = argv[i];2199}2200else2201 arg +=2;2202if(bottomtop)2203die("More than one '-L n,m' option given");2204 bottomtop = arg;2205}2206else if(!strcmp("--contents", arg)) {2207if(++i >= argc)2208usage(blame_usage);2209 contents_from = argv[i];2210}2211else if(!strcmp("--incremental", arg))2212 incremental =1;2213else if(!strcmp("--score-debug", arg))2214 output_option |= OUTPUT_SHOW_SCORE;2215else if(!strcmp("-f", arg) ||2216!strcmp("--show-name", arg))2217 output_option |= OUTPUT_SHOW_NAME;2218else if(!strcmp("-n", arg) ||2219!strcmp("--show-number", arg))2220 output_option |= OUTPUT_SHOW_NUMBER;2221else if(!strcmp("-p", arg) ||2222!strcmp("--porcelain", arg))2223 output_option |= OUTPUT_PORCELAIN;2224else if(!strcmp("--", arg)) {2225 seen_dashdash =1;2226 i++;2227break;2228}2229else2230 argv[unk++] = arg;2231}22322233if(!blame_move_score)2234 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2235if(!blame_copy_score)2236 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22372238/*2239 * We have collected options unknown to us in argv[1..unk]2240 * which are to be passed to revision machinery if we are2241 * going to do the "bottom" processing.2242 *2243 * The remaining are:2244 *2245 * (1) if seen_dashdash, its either2246 * "-options -- <path>" or2247 * "-options -- <path> <rev>".2248 * but the latter is allowed only if there is no2249 * options that we passed to revision machinery.2250 *2251 * (2) otherwise, we may have "--" somewhere later and2252 * might be looking at the first one of multiple 'rev'2253 * parameters (e.g. " master ^next ^maint -- path").2254 * See if there is a dashdash first, and give the2255 * arguments before that to revision machinery.2256 * After that there must be one 'path'.2257 *2258 * (3) otherwise, its one of the three:2259 * "-options <path> <rev>"2260 * "-options <rev> <path>"2261 * "-options <path>"2262 * but again the first one is allowed only if2263 * there is no options that we passed to revision2264 * machinery.2265 */22662267if(seen_dashdash) {2268/* (1) */2269if(argc <= i)2270usage(blame_usage);2271 path =add_prefix(prefix, argv[i]);2272if(i +1== argc -1) {2273if(unk !=1)2274usage(blame_usage);2275 argv[unk++] = argv[i +1];2276}2277else if(i +1!= argc)2278/* garbage at end */2279usage(blame_usage);2280}2281else{2282int j;2283for(j = i; !seen_dashdash && j < argc; j++)2284if(!strcmp(argv[j],"--"))2285 seen_dashdash = j;2286if(seen_dashdash) {2287/* (2) */2288if(seen_dashdash +1!= argc -1)2289usage(blame_usage);2290 path =add_prefix(prefix, argv[seen_dashdash +1]);2291for(j = i; j < seen_dashdash; j++)2292 argv[unk++] = argv[j];2293}2294else{2295/* (3) */2296if(argc <= i)2297usage(blame_usage);2298 path =add_prefix(prefix, argv[i]);2299if(i +1== argc -1) {2300 final_commit_name = argv[i +1];23012302/* if (unk == 1) we could be getting2303 * old-style2304 */2305if(unk ==1&& !has_path_in_work_tree(path)) {2306 path =add_prefix(prefix, argv[i +1]);2307 final_commit_name = argv[i];2308}2309}2310else if(i != argc -1)2311usage(blame_usage);/* garbage at end */23122313setup_work_tree();2314if(!has_path_in_work_tree(path))2315die("cannot stat path%s:%s",2316 path,strerror(errno));2317}2318}23192320if(final_commit_name)2321 argv[unk++] = final_commit_name;23222323/*2324 * Now we got rev and path. We do not want the path pruning2325 * but we may want "bottom" processing.2326 */2327 argv[unk++] ="--";/* terminate the rev name */2328 argv[unk] = NULL;23292330init_revisions(&revs, NULL);2331setup_revisions(unk, argv, &revs, NULL);2332memset(&sb,0,sizeof(sb));23332334/*2335 * There must be one and only one positive commit in the2336 * revs->pending array.2337 */2338for(i =0; i < revs.pending.nr; i++) {2339struct object *obj = revs.pending.objects[i].item;2340if(obj->flags & UNINTERESTING)2341continue;2342while(obj->type == OBJ_TAG)2343 obj =deref_tag(obj, NULL,0);2344if(obj->type != OBJ_COMMIT)2345die("Non commit%s?",2346 revs.pending.objects[i].name);2347if(sb.final)2348die("More than one commit to dig from%sand%s?",2349 revs.pending.objects[i].name,2350 final_commit_name);2351 sb.final = (struct commit *) obj;2352 final_commit_name = revs.pending.objects[i].name;2353}23542355if(!sb.final) {2356/*2357 * "--not A B -- path" without anything positive;2358 * do not default to HEAD, but use the working tree2359 * or "--contents".2360 */2361setup_work_tree();2362 sb.final =fake_working_tree_commit(path, contents_from);2363add_pending_object(&revs, &(sb.final->object),":");2364}2365else if(contents_from)2366die("Cannot use --contents with final commit object name");23672368/*2369 * If we have bottom, this will mark the ancestors of the2370 * bottom commits we would reach while traversing as2371 * uninteresting.2372 */2373prepare_revision_walk(&revs);23742375if(is_null_sha1(sb.final->object.sha1)) {2376char*buf;2377 o = sb.final->util;2378 buf =xmalloc(o->file.size +1);2379memcpy(buf, o->file.ptr, o->file.size +1);2380 sb.final_buf = buf;2381 sb.final_buf_size = o->file.size;2382}2383else{2384 o =get_origin(&sb, sb.final, path);2385if(fill_blob_sha1(o))2386die("no such path%sin%s", path, final_commit_name);23872388 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2389&sb.final_buf_size);2390if(!sb.final_buf)2391die("Cannot read blob%sfor path%s",2392sha1_to_hex(o->blob_sha1),2393 path);2394}2395 num_read_blob++;2396 lno =prepare_lines(&sb);23972398 bottom = top =0;2399if(bottomtop)2400prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2401if(bottom && top && top < bottom) {2402long tmp;2403 tmp = top; top = bottom; bottom = tmp;2404}2405if(bottom <1)2406 bottom =1;2407if(top <1)2408 top = lno;2409 bottom--;2410if(lno < top)2411die("file%shas only%lu lines", path, lno);24122413 ent =xcalloc(1,sizeof(*ent));2414 ent->lno = bottom;2415 ent->num_lines = top - bottom;2416 ent->suspect = o;2417 ent->s_lno = bottom;24182419 sb.ent = ent;2420 sb.path = path;24212422if(revs_file &&read_ancestry(revs_file))2423die("reading graft file%sfailed:%s",2424 revs_file,strerror(errno));24252426read_mailmap(&mailmap,".mailmap", NULL);24272428if(!incremental)2429setup_pager();24302431assign_blame(&sb, &revs, opt);24322433if(incremental)2434return0;24352436coalesce(&sb);24372438if(!(output_option & OUTPUT_PORCELAIN))2439find_alignment(&sb, &output_option);24402441output(&sb, output_option);2442free((void*)sb.final_buf);2443for(ent = sb.ent; ent; ) {2444struct blame_entry *e = ent->next;2445free(ent);2446 ent = e;2447}24482449if(show_stats) {2450printf("num read blob:%d\n", num_read_blob);2451printf("num get patch:%d\n", num_get_patch);2452printf("num commits:%d\n", num_commits);2453}2454return0;2455}