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); 128free(o); 129} 130} 131 132static voiddrop_origin_blob(struct origin *o) 133{ 134if(o->file.ptr) { 135free(o->file.ptr); 136 o->file.ptr = NULL; 137} 138} 139 140/* 141 * Each group of lines is described by a blame_entry; it can be split 142 * as we pass blame to the parents. They form a linked list in the 143 * scoreboard structure, sorted by the target line number. 144 */ 145struct blame_entry { 146struct blame_entry *prev; 147struct blame_entry *next; 148 149/* the first line of this group in the final image; 150 * internally all line numbers are 0 based. 151 */ 152int lno; 153 154/* how many lines this group has */ 155int num_lines; 156 157/* the commit that introduced this group into the final image */ 158struct origin *suspect; 159 160/* true if the suspect is truly guilty; false while we have not 161 * checked if the group came from one of its parents. 162 */ 163char guilty; 164 165/* the line number of the first line of this group in the 166 * suspect's file; internally all line numbers are 0 based. 167 */ 168int s_lno; 169 170/* how significant this entry is -- cached to avoid 171 * scanning the lines over and over. 172 */ 173unsigned score; 174}; 175 176/* 177 * The current state of the blame assignment. 178 */ 179struct scoreboard { 180/* the final commit (i.e. where we started digging from) */ 181struct commit *final; 182 183const char*path; 184 185/* 186 * The contents in the final image. 187 * Used by many functions to obtain contents of the nth line, 188 * indexed with scoreboard.lineno[blame_entry.lno]. 189 */ 190const char*final_buf; 191unsigned long final_buf_size; 192 193/* linked list of blames */ 194struct blame_entry *ent; 195 196/* look-up a line in the final buffer */ 197int num_lines; 198int*lineno; 199}; 200 201staticinlineintsame_suspect(struct origin *a,struct origin *b) 202{ 203if(a == b) 204return1; 205if(a->commit != b->commit) 206return0; 207return!strcmp(a->path, b->path); 208} 209 210static voidsanity_check_refcnt(struct scoreboard *); 211 212/* 213 * If two blame entries that are next to each other came from 214 * contiguous lines in the same origin (i.e. <commit, path> pair), 215 * merge them together. 216 */ 217static voidcoalesce(struct scoreboard *sb) 218{ 219struct blame_entry *ent, *next; 220 221for(ent = sb->ent; ent && (next = ent->next); ent = next) { 222if(same_suspect(ent->suspect, next->suspect) && 223 ent->guilty == next->guilty && 224 ent->s_lno + ent->num_lines == next->s_lno) { 225 ent->num_lines += next->num_lines; 226 ent->next = next->next; 227if(ent->next) 228 ent->next->prev = ent; 229origin_decref(next->suspect); 230free(next); 231 ent->score =0; 232 next = ent;/* again */ 233} 234} 235 236if(DEBUG)/* sanity */ 237sanity_check_refcnt(sb); 238} 239 240/* 241 * Given a commit and a path in it, create a new origin structure. 242 * The callers that add blame to the scoreboard should use 243 * get_origin() to obtain shared, refcounted copy instead of calling 244 * this function directly. 245 */ 246static struct origin *make_origin(struct commit *commit,const char*path) 247{ 248struct origin *o; 249 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 250 o->commit = commit; 251 o->refcnt =1; 252strcpy(o->path, path); 253return o; 254} 255 256/* 257 * Locate an existing origin or create a new one. 258 */ 259static struct origin *get_origin(struct scoreboard *sb, 260struct commit *commit, 261const char*path) 262{ 263struct blame_entry *e; 264 265for(e = sb->ent; e; e = e->next) { 266if(e->suspect->commit == commit && 267!strcmp(e->suspect->path, path)) 268returnorigin_incref(e->suspect); 269} 270returnmake_origin(commit, path); 271} 272 273/* 274 * Fill the blob_sha1 field of an origin if it hasn't, so that later 275 * call to fill_origin_blob() can use it to locate the data. blob_sha1 276 * for an origin is also used to pass the blame for the entire file to 277 * the parent to detect the case where a child's blob is identical to 278 * that of its parent's. 279 */ 280static intfill_blob_sha1(struct origin *origin) 281{ 282unsigned mode; 283 284if(!is_null_sha1(origin->blob_sha1)) 285return0; 286if(get_tree_entry(origin->commit->object.sha1, 287 origin->path, 288 origin->blob_sha1, &mode)) 289goto error_out; 290if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 291goto error_out; 292return0; 293 error_out: 294hashclr(origin->blob_sha1); 295return-1; 296} 297 298/* 299 * We have an origin -- check if the same path exists in the 300 * parent and return an origin structure to represent it. 301 */ 302static struct origin *find_origin(struct scoreboard *sb, 303struct commit *parent, 304struct origin *origin) 305{ 306struct origin *porigin = NULL; 307struct diff_options diff_opts; 308const char*paths[2]; 309 310if(parent->util) { 311/* 312 * Each commit object can cache one origin in that 313 * commit. This is a freestanding copy of origin and 314 * not refcounted. 315 */ 316struct origin *cached = parent->util; 317if(!strcmp(cached->path, origin->path)) { 318/* 319 * The same path between origin and its parent 320 * without renaming -- the most common case. 321 */ 322 porigin =get_origin(sb, parent, cached->path); 323 324/* 325 * If the origin was newly created (i.e. get_origin 326 * would call make_origin if none is found in the 327 * scoreboard), it does not know the blob_sha1, 328 * so copy it. Otherwise porigin was in the 329 * scoreboard and already knows blob_sha1. 330 */ 331if(porigin->refcnt ==1) 332hashcpy(porigin->blob_sha1, cached->blob_sha1); 333return porigin; 334} 335/* otherwise it was not very useful; free it */ 336free(parent->util); 337 parent->util = NULL; 338} 339 340/* See if the origin->path is different between parent 341 * and origin first. Most of the time they are the 342 * same and diff-tree is fairly efficient about this. 343 */ 344diff_setup(&diff_opts); 345DIFF_OPT_SET(&diff_opts, RECURSIVE); 346 diff_opts.detect_rename =0; 347 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 348 paths[0] = origin->path; 349 paths[1] = NULL; 350 351diff_tree_setup_paths(paths, &diff_opts); 352if(diff_setup_done(&diff_opts) <0) 353die("diff-setup"); 354 355if(is_null_sha1(origin->commit->object.sha1)) 356do_diff_cache(parent->tree->object.sha1, &diff_opts); 357else 358diff_tree_sha1(parent->tree->object.sha1, 359 origin->commit->tree->object.sha1, 360"", &diff_opts); 361diffcore_std(&diff_opts); 362 363/* It is either one entry that says "modified", or "created", 364 * or nothing. 365 */ 366if(!diff_queued_diff.nr) { 367/* The path is the same as parent */ 368 porigin =get_origin(sb, parent, origin->path); 369hashcpy(porigin->blob_sha1, origin->blob_sha1); 370} 371else if(diff_queued_diff.nr !=1) 372die("internal error in blame::find_origin"); 373else{ 374struct diff_filepair *p = diff_queued_diff.queue[0]; 375switch(p->status) { 376default: 377die("internal error in blame::find_origin (%c)", 378 p->status); 379case'M': 380 porigin =get_origin(sb, parent, origin->path); 381hashcpy(porigin->blob_sha1, p->one->sha1); 382break; 383case'A': 384case'T': 385/* Did not exist in parent, or type changed */ 386break; 387} 388} 389diff_flush(&diff_opts); 390diff_tree_release_paths(&diff_opts); 391if(porigin) { 392/* 393 * Create a freestanding copy that is not part of 394 * the refcounted origin found in the scoreboard, and 395 * cache it in the commit. 396 */ 397struct origin *cached; 398 399 cached =make_origin(porigin->commit, porigin->path); 400hashcpy(cached->blob_sha1, porigin->blob_sha1); 401 parent->util = cached; 402} 403return porigin; 404} 405 406/* 407 * We have an origin -- find the path that corresponds to it in its 408 * parent and return an origin structure to represent it. 409 */ 410static struct origin *find_rename(struct scoreboard *sb, 411struct commit *parent, 412struct origin *origin) 413{ 414struct origin *porigin = NULL; 415struct diff_options diff_opts; 416int i; 417const char*paths[2]; 418 419diff_setup(&diff_opts); 420DIFF_OPT_SET(&diff_opts, RECURSIVE); 421 diff_opts.detect_rename = DIFF_DETECT_RENAME; 422 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 423 diff_opts.single_follow = origin->path; 424 paths[0] = NULL; 425diff_tree_setup_paths(paths, &diff_opts); 426if(diff_setup_done(&diff_opts) <0) 427die("diff-setup"); 428 429if(is_null_sha1(origin->commit->object.sha1)) 430do_diff_cache(parent->tree->object.sha1, &diff_opts); 431else 432diff_tree_sha1(parent->tree->object.sha1, 433 origin->commit->tree->object.sha1, 434"", &diff_opts); 435diffcore_std(&diff_opts); 436 437for(i =0; i < diff_queued_diff.nr; i++) { 438struct diff_filepair *p = diff_queued_diff.queue[i]; 439if((p->status =='R'|| p->status =='C') && 440!strcmp(p->two->path, origin->path)) { 441 porigin =get_origin(sb, parent, p->one->path); 442hashcpy(porigin->blob_sha1, p->one->sha1); 443break; 444} 445} 446diff_flush(&diff_opts); 447diff_tree_release_paths(&diff_opts); 448return porigin; 449} 450 451/* 452 * Parsing of patch chunks... 453 */ 454struct chunk { 455/* line number in postimage; up to but not including this 456 * line is the same as preimage 457 */ 458int same; 459 460/* preimage line number after this chunk */ 461int p_next; 462 463/* postimage line number after this chunk */ 464int t_next; 465}; 466 467struct patch { 468struct chunk *chunks; 469int num; 470}; 471 472struct blame_diff_state { 473struct xdiff_emit_state xm; 474struct patch *ret; 475unsigned hunk_post_context; 476unsigned hunk_in_pre_context :1; 477}; 478 479static voidprocess_u_diff(void*state_,char*line,unsigned long len) 480{ 481struct blame_diff_state *state = state_; 482struct chunk *chunk; 483int off1, off2, len1, len2, num; 484 485 num = state->ret->num; 486if(len <4|| line[0] !='@'|| line[1] !='@') { 487if(state->hunk_in_pre_context && line[0] ==' ') 488 state->ret->chunks[num -1].same++; 489else{ 490 state->hunk_in_pre_context =0; 491if(line[0] ==' ') 492 state->hunk_post_context++; 493else 494 state->hunk_post_context =0; 495} 496return; 497} 498 499if(num && state->hunk_post_context) { 500 chunk = &state->ret->chunks[num -1]; 501 chunk->p_next -= state->hunk_post_context; 502 chunk->t_next -= state->hunk_post_context; 503} 504 state->ret->num = ++num; 505 state->ret->chunks =xrealloc(state->ret->chunks, 506sizeof(struct chunk) * num); 507 chunk = &state->ret->chunks[num -1]; 508if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 509 state->ret->num--; 510return; 511} 512 513/* Line numbers in patch output are one based. */ 514 off1--; 515 off2--; 516 517 chunk->same = len2 ? off2 : (off2 +1); 518 519 chunk->p_next = off1 + (len1 ? len1 :1); 520 chunk->t_next = chunk->same + len2; 521 state->hunk_in_pre_context =1; 522 state->hunk_post_context =0; 523} 524 525static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 526int context) 527{ 528struct blame_diff_state state; 529 xpparam_t xpp; 530 xdemitconf_t xecfg; 531 xdemitcb_t ecb; 532 533 xpp.flags = xdl_opts; 534memset(&xecfg,0,sizeof(xecfg)); 535 xecfg.ctxlen = context; 536 ecb.outf = xdiff_outf; 537 ecb.priv = &state; 538memset(&state,0,sizeof(state)); 539 state.xm.consume = process_u_diff; 540 state.ret =xmalloc(sizeof(struct patch)); 541 state.ret->chunks = NULL; 542 state.ret->num =0; 543 544xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb); 545 546if(state.ret->num) { 547struct chunk *chunk; 548 chunk = &state.ret->chunks[state.ret->num -1]; 549 chunk->p_next -= state.hunk_post_context; 550 chunk->t_next -= state.hunk_post_context; 551} 552return state.ret; 553} 554 555/* 556 * Run diff between two origins and grab the patch output, so that 557 * we can pass blame for lines origin is currently suspected for 558 * to its parent. 559 */ 560static struct patch *get_patch(struct origin *parent,struct origin *origin) 561{ 562 mmfile_t file_p, file_o; 563struct patch *patch; 564 565fill_origin_blob(parent, &file_p); 566fill_origin_blob(origin, &file_o); 567if(!file_p.ptr || !file_o.ptr) 568return NULL; 569 patch =compare_buffer(&file_p, &file_o,0); 570 num_get_patch++; 571return patch; 572} 573 574static voidfree_patch(struct patch *p) 575{ 576free(p->chunks); 577free(p); 578} 579 580/* 581 * Link in a new blame entry to the scoreboard. Entries that cover the 582 * same line range have been removed from the scoreboard previously. 583 */ 584static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 585{ 586struct blame_entry *ent, *prev = NULL; 587 588origin_incref(e->suspect); 589 590for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 591 prev = ent; 592 593/* prev, if not NULL, is the last one that is below e */ 594 e->prev = prev; 595if(prev) { 596 e->next = prev->next; 597 prev->next = e; 598} 599else{ 600 e->next = sb->ent; 601 sb->ent = e; 602} 603if(e->next) 604 e->next->prev = e; 605} 606 607/* 608 * src typically is on-stack; we want to copy the information in it to 609 * a malloced blame_entry that is already on the linked list of the 610 * scoreboard. The origin of dst loses a refcnt while the origin of src 611 * gains one. 612 */ 613static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 614{ 615struct blame_entry *p, *n; 616 617 p = dst->prev; 618 n = dst->next; 619origin_incref(src->suspect); 620origin_decref(dst->suspect); 621memcpy(dst, src,sizeof(*src)); 622 dst->prev = p; 623 dst->next = n; 624 dst->score =0; 625} 626 627static const char*nth_line(struct scoreboard *sb,int lno) 628{ 629return sb->final_buf + sb->lineno[lno]; 630} 631 632/* 633 * It is known that lines between tlno to same came from parent, and e 634 * has an overlap with that range. it also is known that parent's 635 * line plno corresponds to e's line tlno. 636 * 637 * <---- e -----> 638 * <------> 639 * <------------> 640 * <------------> 641 * <------------------> 642 * 643 * Split e into potentially three parts; before this chunk, the chunk 644 * to be blamed for the parent, and after that portion. 645 */ 646static voidsplit_overlap(struct blame_entry *split, 647struct blame_entry *e, 648int tlno,int plno,int same, 649struct origin *parent) 650{ 651int chunk_end_lno; 652memset(split,0,sizeof(struct blame_entry [3])); 653 654if(e->s_lno < tlno) { 655/* there is a pre-chunk part not blamed on parent */ 656 split[0].suspect =origin_incref(e->suspect); 657 split[0].lno = e->lno; 658 split[0].s_lno = e->s_lno; 659 split[0].num_lines = tlno - e->s_lno; 660 split[1].lno = e->lno + tlno - e->s_lno; 661 split[1].s_lno = plno; 662} 663else{ 664 split[1].lno = e->lno; 665 split[1].s_lno = plno + (e->s_lno - tlno); 666} 667 668if(same < e->s_lno + e->num_lines) { 669/* there is a post-chunk part not blamed on parent */ 670 split[2].suspect =origin_incref(e->suspect); 671 split[2].lno = e->lno + (same - e->s_lno); 672 split[2].s_lno = e->s_lno + (same - e->s_lno); 673 split[2].num_lines = e->s_lno + e->num_lines - same; 674 chunk_end_lno = split[2].lno; 675} 676else 677 chunk_end_lno = e->lno + e->num_lines; 678 split[1].num_lines = chunk_end_lno - split[1].lno; 679 680/* 681 * if it turns out there is nothing to blame the parent for, 682 * forget about the splitting. !split[1].suspect signals this. 683 */ 684if(split[1].num_lines <1) 685return; 686 split[1].suspect =origin_incref(parent); 687} 688 689/* 690 * split_overlap() divided an existing blame e into up to three parts 691 * in split. Adjust the linked list of blames in the scoreboard to 692 * reflect the split. 693 */ 694static voidsplit_blame(struct scoreboard *sb, 695struct blame_entry *split, 696struct blame_entry *e) 697{ 698struct blame_entry *new_entry; 699 700if(split[0].suspect && split[2].suspect) { 701/* The first part (reuse storage for the existing entry e) */ 702dup_entry(e, &split[0]); 703 704/* The last part -- me */ 705 new_entry =xmalloc(sizeof(*new_entry)); 706memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 707add_blame_entry(sb, new_entry); 708 709/* ... and the middle part -- parent */ 710 new_entry =xmalloc(sizeof(*new_entry)); 711memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 712add_blame_entry(sb, new_entry); 713} 714else if(!split[0].suspect && !split[2].suspect) 715/* 716 * The parent covers the entire area; reuse storage for 717 * e and replace it with the parent. 718 */ 719dup_entry(e, &split[1]); 720else if(split[0].suspect) { 721/* me and then parent */ 722dup_entry(e, &split[0]); 723 724 new_entry =xmalloc(sizeof(*new_entry)); 725memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 726add_blame_entry(sb, new_entry); 727} 728else{ 729/* parent and then me */ 730dup_entry(e, &split[1]); 731 732 new_entry =xmalloc(sizeof(*new_entry)); 733memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 734add_blame_entry(sb, new_entry); 735} 736 737if(DEBUG) {/* sanity */ 738struct blame_entry *ent; 739int lno = sb->ent->lno, corrupt =0; 740 741for(ent = sb->ent; ent; ent = ent->next) { 742if(lno != ent->lno) 743 corrupt =1; 744if(ent->s_lno <0) 745 corrupt =1; 746 lno += ent->num_lines; 747} 748if(corrupt) { 749 lno = sb->ent->lno; 750for(ent = sb->ent; ent; ent = ent->next) { 751printf("L%8d l%8d n%8d\n", 752 lno, ent->lno, ent->num_lines); 753 lno = ent->lno + ent->num_lines; 754} 755die("oops"); 756} 757} 758} 759 760/* 761 * After splitting the blame, the origins used by the 762 * on-stack blame_entry should lose one refcnt each. 763 */ 764static voiddecref_split(struct blame_entry *split) 765{ 766int i; 767 768for(i =0; i <3; i++) 769origin_decref(split[i].suspect); 770} 771 772/* 773 * Helper for blame_chunk(). blame_entry e is known to overlap with 774 * the patch hunk; split it and pass blame to the parent. 775 */ 776static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 777int tlno,int plno,int same, 778struct origin *parent) 779{ 780struct blame_entry split[3]; 781 782split_overlap(split, e, tlno, plno, same, parent); 783if(split[1].suspect) 784split_blame(sb, split, e); 785decref_split(split); 786} 787 788/* 789 * Find the line number of the last line the target is suspected for. 790 */ 791static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 792{ 793struct blame_entry *e; 794int last_in_target = -1; 795 796for(e = sb->ent; e; e = e->next) { 797if(e->guilty || !same_suspect(e->suspect, target)) 798continue; 799if(last_in_target < e->s_lno + e->num_lines) 800 last_in_target = e->s_lno + e->num_lines; 801} 802return last_in_target; 803} 804 805/* 806 * Process one hunk from the patch between the current suspect for 807 * blame_entry e and its parent. Find and split the overlap, and 808 * pass blame to the overlapping part to the parent. 809 */ 810static voidblame_chunk(struct scoreboard *sb, 811int tlno,int plno,int same, 812struct origin *target,struct origin *parent) 813{ 814struct blame_entry *e; 815 816for(e = sb->ent; e; e = e->next) { 817if(e->guilty || !same_suspect(e->suspect, target)) 818continue; 819if(same <= e->s_lno) 820continue; 821if(tlno < e->s_lno + e->num_lines) 822blame_overlap(sb, e, tlno, plno, same, parent); 823} 824} 825 826/* 827 * We are looking at the origin 'target' and aiming to pass blame 828 * for the lines it is suspected to its parent. Run diff to find 829 * which lines came from parent and pass blame for them. 830 */ 831static intpass_blame_to_parent(struct scoreboard *sb, 832struct origin *target, 833struct origin *parent) 834{ 835int i, last_in_target, plno, tlno; 836struct patch *patch; 837 838 last_in_target =find_last_in_target(sb, target); 839if(last_in_target <0) 840return1;/* nothing remains for this target */ 841 842 patch =get_patch(parent, target); 843 plno = tlno =0; 844for(i =0; i < patch->num; i++) { 845struct chunk *chunk = &patch->chunks[i]; 846 847blame_chunk(sb, tlno, plno, chunk->same, target, parent); 848 plno = chunk->p_next; 849 tlno = chunk->t_next; 850} 851/* The rest (i.e. anything after tlno) are the same as the parent */ 852blame_chunk(sb, tlno, plno, last_in_target, target, parent); 853 854free_patch(patch); 855return0; 856} 857 858/* 859 * The lines in blame_entry after splitting blames many times can become 860 * very small and trivial, and at some point it becomes pointless to 861 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 862 * ordinary C program, and it is not worth to say it was copied from 863 * totally unrelated file in the parent. 864 * 865 * Compute how trivial the lines in the blame_entry are. 866 */ 867static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 868{ 869unsigned score; 870const char*cp, *ep; 871 872if(e->score) 873return e->score; 874 875 score =1; 876 cp =nth_line(sb, e->lno); 877 ep =nth_line(sb, e->lno + e->num_lines); 878while(cp < ep) { 879unsigned ch = *((unsigned char*)cp); 880if(isalnum(ch)) 881 score++; 882 cp++; 883} 884 e->score = score; 885return score; 886} 887 888/* 889 * best_so_far[] and this[] are both a split of an existing blame_entry 890 * that passes blame to the parent. Maintain best_so_far the best split 891 * so far, by comparing this and best_so_far and copying this into 892 * bst_so_far as needed. 893 */ 894static voidcopy_split_if_better(struct scoreboard *sb, 895struct blame_entry *best_so_far, 896struct blame_entry *this) 897{ 898int i; 899 900if(!this[1].suspect) 901return; 902if(best_so_far[1].suspect) { 903if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 904return; 905} 906 907for(i =0; i <3; i++) 908origin_incref(this[i].suspect); 909decref_split(best_so_far); 910memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 911} 912 913/* 914 * We are looking at a part of the final image represented by 915 * ent (tlno and same are offset by ent->s_lno). 916 * tlno is where we are looking at in the final image. 917 * up to (but not including) same match preimage. 918 * plno is where we are looking at in the preimage. 919 * 920 * <-------------- final image ----------------------> 921 * <------ent------> 922 * ^tlno ^same 923 * <---------preimage-----> 924 * ^plno 925 * 926 * All line numbers are 0-based. 927 */ 928static voidhandle_split(struct scoreboard *sb, 929struct blame_entry *ent, 930int tlno,int plno,int same, 931struct origin *parent, 932struct blame_entry *split) 933{ 934if(ent->num_lines <= tlno) 935return; 936if(tlno < same) { 937struct blame_entry this[3]; 938 tlno += ent->s_lno; 939 same += ent->s_lno; 940split_overlap(this, ent, tlno, plno, same, parent); 941copy_split_if_better(sb, split,this); 942decref_split(this); 943} 944} 945 946/* 947 * Find the lines from parent that are the same as ent so that 948 * we can pass blames to it. file_p has the blob contents for 949 * the parent. 950 */ 951static voidfind_copy_in_blob(struct scoreboard *sb, 952struct blame_entry *ent, 953struct origin *parent, 954struct blame_entry *split, 955 mmfile_t *file_p) 956{ 957const char*cp; 958int cnt; 959 mmfile_t file_o; 960struct patch *patch; 961int i, plno, tlno; 962 963/* 964 * Prepare mmfile that contains only the lines in ent. 965 */ 966 cp =nth_line(sb, ent->lno); 967 file_o.ptr = (char*) cp; 968 cnt = ent->num_lines; 969 970while(cnt && cp < sb->final_buf + sb->final_buf_size) { 971if(*cp++ =='\n') 972 cnt--; 973} 974 file_o.size = cp - file_o.ptr; 975 976 patch =compare_buffer(file_p, &file_o,1); 977 978/* 979 * file_o is a part of final image we are annotating. 980 * file_p partially may match that image. 981 */ 982memset(split,0,sizeof(struct blame_entry [3])); 983 plno = tlno =0; 984for(i =0; i < patch->num; i++) { 985struct chunk *chunk = &patch->chunks[i]; 986 987handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 988 plno = chunk->p_next; 989 tlno = chunk->t_next; 990} 991/* remainder, if any, all match the preimage */ 992handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 993free_patch(patch); 994} 995 996/* 997 * See if lines currently target is suspected for can be attributed to 998 * parent. 999 */1000static intfind_move_in_parent(struct scoreboard *sb,1001struct origin *target,1002struct origin *parent)1003{1004int last_in_target, made_progress;1005struct blame_entry *e, split[3];1006 mmfile_t file_p;10071008 last_in_target =find_last_in_target(sb, target);1009if(last_in_target <0)1010return1;/* nothing remains for this target */10111012fill_origin_blob(parent, &file_p);1013if(!file_p.ptr)1014return0;10151016 made_progress =1;1017while(made_progress) {1018 made_progress =0;1019for(e = sb->ent; e; e = e->next) {1020if(e->guilty || !same_suspect(e->suspect, target))1021continue;1022find_copy_in_blob(sb, e, parent, split, &file_p);1023if(split[1].suspect &&1024 blame_move_score <ent_score(sb, &split[1])) {1025split_blame(sb, split, e);1026 made_progress =1;1027}1028decref_split(split);1029}1030}1031return0;1032}10331034struct blame_list {1035struct blame_entry *ent;1036struct blame_entry split[3];1037};10381039/*1040 * Count the number of entries the target is suspected for,1041 * and prepare a list of entry and the best split.1042 */1043static struct blame_list *setup_blame_list(struct scoreboard *sb,1044struct origin *target,1045int*num_ents_p)1046{1047struct blame_entry *e;1048int num_ents, i;1049struct blame_list *blame_list = NULL;10501051for(e = sb->ent, num_ents =0; e; e = e->next)1052if(!e->guilty &&same_suspect(e->suspect, target))1053 num_ents++;1054if(num_ents) {1055 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1056for(e = sb->ent, i =0; e; e = e->next)1057if(!e->guilty &&same_suspect(e->suspect, target))1058 blame_list[i++].ent = e;1059}1060*num_ents_p = num_ents;1061return blame_list;1062}10631064/*1065 * For lines target is suspected for, see if we can find code movement1066 * across file boundary from the parent commit. porigin is the path1067 * in the parent we already tried.1068 */1069static intfind_copy_in_parent(struct scoreboard *sb,1070struct origin *target,1071struct commit *parent,1072struct origin *porigin,1073int opt)1074{1075struct diff_options diff_opts;1076const char*paths[1];1077int i, j;1078int retval;1079struct blame_list *blame_list;1080int num_ents;10811082 blame_list =setup_blame_list(sb, target, &num_ents);1083if(!blame_list)1084return1;/* nothing remains for this target */10851086diff_setup(&diff_opts);1087DIFF_OPT_SET(&diff_opts, RECURSIVE);1088 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10891090 paths[0] = NULL;1091diff_tree_setup_paths(paths, &diff_opts);1092if(diff_setup_done(&diff_opts) <0)1093die("diff-setup");10941095/* Try "find copies harder" on new path if requested;1096 * we do not want to use diffcore_rename() actually to1097 * match things up; find_copies_harder is set only to1098 * force diff_tree_sha1() to feed all filepairs to diff_queue,1099 * and this code needs to be after diff_setup_done(), which1100 * usually makes find-copies-harder imply copy detection.1101 */1102if((opt & PICKAXE_BLAME_COPY_HARDEST)1103|| ((opt & PICKAXE_BLAME_COPY_HARDER)1104&& (!porigin ||strcmp(target->path, porigin->path))))1105DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);11061107if(is_null_sha1(target->commit->object.sha1))1108do_diff_cache(parent->tree->object.sha1, &diff_opts);1109else1110diff_tree_sha1(parent->tree->object.sha1,1111 target->commit->tree->object.sha1,1112"", &diff_opts);11131114if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1115diffcore_std(&diff_opts);11161117 retval =0;1118while(1) {1119int made_progress =0;11201121for(i =0; i < diff_queued_diff.nr; i++) {1122struct diff_filepair *p = diff_queued_diff.queue[i];1123struct origin *norigin;1124 mmfile_t file_p;1125struct blame_entry this[3];11261127if(!DIFF_FILE_VALID(p->one))1128continue;/* does not exist in parent */1129if(porigin && !strcmp(p->one->path, porigin->path))1130/* find_move already dealt with this path */1131continue;11321133 norigin =get_origin(sb, parent, p->one->path);1134hashcpy(norigin->blob_sha1, p->one->sha1);1135fill_origin_blob(norigin, &file_p);1136if(!file_p.ptr)1137continue;11381139for(j =0; j < num_ents; j++) {1140find_copy_in_blob(sb, blame_list[j].ent,1141 norigin,this, &file_p);1142copy_split_if_better(sb, blame_list[j].split,1143this);1144decref_split(this);1145}1146origin_decref(norigin);1147}11481149for(j =0; j < num_ents; j++) {1150struct blame_entry *split = blame_list[j].split;1151if(split[1].suspect &&1152 blame_copy_score <ent_score(sb, &split[1])) {1153split_blame(sb, split, blame_list[j].ent);1154 made_progress =1;1155}1156decref_split(split);1157}1158free(blame_list);11591160if(!made_progress)1161break;1162 blame_list =setup_blame_list(sb, target, &num_ents);1163if(!blame_list) {1164 retval =1;1165break;1166}1167}1168diff_flush(&diff_opts);1169diff_tree_release_paths(&diff_opts);1170return retval;1171}11721173/*1174 * The blobs of origin and porigin exactly match, so everything1175 * origin is suspected for can be blamed on the parent.1176 */1177static voidpass_whole_blame(struct scoreboard *sb,1178struct origin *origin,struct origin *porigin)1179{1180struct blame_entry *e;11811182if(!porigin->file.ptr && origin->file.ptr) {1183/* Steal its file */1184 porigin->file = origin->file;1185 origin->file.ptr = NULL;1186}1187for(e = sb->ent; e; e = e->next) {1188if(!same_suspect(e->suspect, origin))1189continue;1190origin_incref(porigin);1191origin_decref(e->suspect);1192 e->suspect = porigin;1193}1194}11951196#define MAXPARENT 1611971198static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1199{1200int i, pass;1201struct commit *commit = origin->commit;1202struct commit_list *parent;1203struct origin *parent_origin[MAXPARENT], *porigin;12041205memset(parent_origin,0,sizeof(parent_origin));12061207/* The first pass looks for unrenamed path to optimize for1208 * common cases, then we look for renames in the second pass.1209 */1210for(pass =0; pass <2; pass++) {1211struct origin *(*find)(struct scoreboard *,1212struct commit *,struct origin *);1213 find = pass ? find_rename : find_origin;12141215for(i =0, parent = commit->parents;1216 i < MAXPARENT && parent;1217 parent = parent->next, i++) {1218struct commit *p = parent->item;1219int j, same;12201221if(parent_origin[i])1222continue;1223if(parse_commit(p))1224continue;1225 porigin =find(sb, p, origin);1226if(!porigin)1227continue;1228if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1229pass_whole_blame(sb, origin, porigin);1230origin_decref(porigin);1231goto finish;1232}1233for(j = same =0; j < i; j++)1234if(parent_origin[j] &&1235!hashcmp(parent_origin[j]->blob_sha1,1236 porigin->blob_sha1)) {1237 same =1;1238break;1239}1240if(!same)1241 parent_origin[i] = porigin;1242else1243origin_decref(porigin);1244}1245}12461247 num_commits++;1248for(i =0, parent = commit->parents;1249 i < MAXPARENT && parent;1250 parent = parent->next, i++) {1251struct origin *porigin = parent_origin[i];1252if(!porigin)1253continue;1254if(pass_blame_to_parent(sb, origin, porigin))1255goto finish;1256}12571258/*1259 * Optionally find moves in parents' files.1260 */1261if(opt & PICKAXE_BLAME_MOVE)1262for(i =0, parent = commit->parents;1263 i < MAXPARENT && parent;1264 parent = parent->next, i++) {1265struct origin *porigin = parent_origin[i];1266if(!porigin)1267continue;1268if(find_move_in_parent(sb, origin, porigin))1269goto finish;1270}12711272/*1273 * Optionally find copies from parents' files.1274 */1275if(opt & PICKAXE_BLAME_COPY)1276for(i =0, parent = commit->parents;1277 i < MAXPARENT && parent;1278 parent = parent->next, i++) {1279struct origin *porigin = parent_origin[i];1280if(find_copy_in_parent(sb, origin, parent->item,1281 porigin, opt))1282goto finish;1283}12841285 finish:1286for(i =0; i < MAXPARENT; i++) {1287if(parent_origin[i]) {1288drop_origin_blob(parent_origin[i]);1289origin_decref(parent_origin[i]);1290}1291}1292drop_origin_blob(origin);1293}12941295/*1296 * Information on commits, used for output.1297 */1298struct commit_info1299{1300const char*author;1301const char*author_mail;1302unsigned long author_time;1303const char*author_tz;13041305/* filled only when asked for details */1306const char*committer;1307const char*committer_mail;1308unsigned long committer_time;1309const char*committer_tz;13101311const char*summary;1312};13131314/*1315 * Parse author/committer line in the commit object buffer1316 */1317static voidget_ac_line(const char*inbuf,const char*what,1318int bufsz,char*person,const char**mail,1319unsigned long*time,const char**tz)1320{1321int len, tzlen, maillen;1322char*tmp, *endp, *timepos;13231324 tmp =strstr(inbuf, what);1325if(!tmp)1326goto error_out;1327 tmp +=strlen(what);1328 endp =strchr(tmp,'\n');1329if(!endp)1330 len =strlen(tmp);1331else1332 len = endp - tmp;1333if(bufsz <= len) {1334 error_out:1335/* Ugh */1336*mail = *tz ="(unknown)";1337*time =0;1338return;1339}1340memcpy(person, tmp, len);13411342 tmp = person;1343 tmp += len;1344*tmp =0;1345while(*tmp !=' ')1346 tmp--;1347*tz = tmp+1;1348 tzlen = (person+len)-(tmp+1);13491350*tmp =0;1351while(*tmp !=' ')1352 tmp--;1353*time =strtoul(tmp, NULL,10);1354 timepos = tmp;13551356*tmp =0;1357while(*tmp !=' ')1358 tmp--;1359*mail = tmp +1;1360*tmp =0;1361 maillen = timepos - tmp;13621363if(!mailmap.nr)1364return;13651366/*1367 * mailmap expansion may make the name longer.1368 * make room by pushing stuff down.1369 */1370 tmp = person + bufsz - (tzlen +1);1371memmove(tmp, *tz, tzlen);1372 tmp[tzlen] =0;1373*tz = tmp;13741375 tmp = tmp - (maillen +1);1376memmove(tmp, *mail, maillen);1377 tmp[maillen] =0;1378*mail = tmp;13791380/*1381 * Now, convert e-mail using mailmap1382 */1383map_email(&mailmap, tmp +1, person, tmp-person-1);1384}13851386static voidget_commit_info(struct commit *commit,1387struct commit_info *ret,1388int detailed)1389{1390int len;1391char*tmp, *endp;1392static char author_buf[1024];1393static char committer_buf[1024];1394static char summary_buf[1024];13951396/*1397 * We've operated without save_commit_buffer, so1398 * we now need to populate them for output.1399 */1400if(!commit->buffer) {1401enum object_type type;1402unsigned long size;1403 commit->buffer =1404read_sha1_file(commit->object.sha1, &type, &size);1405if(!commit->buffer)1406die("Cannot read commit%s",1407sha1_to_hex(commit->object.sha1));1408}1409 ret->author = author_buf;1410get_ac_line(commit->buffer,"\nauthor ",1411sizeof(author_buf), author_buf, &ret->author_mail,1412&ret->author_time, &ret->author_tz);14131414if(!detailed)1415return;14161417 ret->committer = committer_buf;1418get_ac_line(commit->buffer,"\ncommitter ",1419sizeof(committer_buf), committer_buf, &ret->committer_mail,1420&ret->committer_time, &ret->committer_tz);14211422 ret->summary = summary_buf;1423 tmp =strstr(commit->buffer,"\n\n");1424if(!tmp) {1425 error_out:1426sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1427return;1428}1429 tmp +=2;1430 endp =strchr(tmp,'\n');1431if(!endp)1432 endp = tmp +strlen(tmp);1433 len = endp - tmp;1434if(len >=sizeof(summary_buf) || len ==0)1435goto error_out;1436memcpy(summary_buf, tmp, len);1437 summary_buf[len] =0;1438}14391440/*1441 * To allow LF and other nonportable characters in pathnames,1442 * they are c-style quoted as needed.1443 */1444static voidwrite_filename_info(const char*path)1445{1446printf("filename ");1447write_name_quoted(path, stdout,'\n');1448}14491450/*1451 * The blame_entry is found to be guilty for the range. Mark it1452 * as such, and show it in incremental output.1453 */1454static voidfound_guilty_entry(struct blame_entry *ent)1455{1456if(ent->guilty)1457return;1458 ent->guilty =1;1459if(incremental) {1460struct origin *suspect = ent->suspect;14611462printf("%s %d %d %d\n",1463sha1_to_hex(suspect->commit->object.sha1),1464 ent->s_lno +1, ent->lno +1, ent->num_lines);1465if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1466struct commit_info ci;1467 suspect->commit->object.flags |= METAINFO_SHOWN;1468get_commit_info(suspect->commit, &ci,1);1469printf("author%s\n", ci.author);1470printf("author-mail%s\n", ci.author_mail);1471printf("author-time%lu\n", ci.author_time);1472printf("author-tz%s\n", ci.author_tz);1473printf("committer%s\n", ci.committer);1474printf("committer-mail%s\n", ci.committer_mail);1475printf("committer-time%lu\n", ci.committer_time);1476printf("committer-tz%s\n", ci.committer_tz);1477printf("summary%s\n", ci.summary);1478if(suspect->commit->object.flags & UNINTERESTING)1479printf("boundary\n");1480}1481write_filename_info(suspect->path);1482maybe_flush_or_die(stdout,"stdout");1483}1484}14851486/*1487 * The main loop -- while the scoreboard has lines whose true origin1488 * is still unknown, pick one blame_entry, and allow its current1489 * suspect to pass blames to its parents.1490 */1491static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1492{1493while(1) {1494struct blame_entry *ent;1495struct commit *commit;1496struct origin *suspect = NULL;14971498/* find one suspect to break down */1499for(ent = sb->ent; !suspect && ent; ent = ent->next)1500if(!ent->guilty)1501 suspect = ent->suspect;1502if(!suspect)1503return;/* all done */15041505/*1506 * We will use this suspect later in the loop,1507 * so hold onto it in the meantime.1508 */1509origin_incref(suspect);1510 commit = suspect->commit;1511if(!commit->object.parsed)1512parse_commit(commit);1513if(!(commit->object.flags & UNINTERESTING) &&1514!(revs->max_age != -1&& commit->date < revs->max_age))1515pass_blame(sb, suspect, opt);1516else{1517 commit->object.flags |= UNINTERESTING;1518if(commit->object.parsed)1519mark_parents_uninteresting(commit);1520}1521/* treat root commit as boundary */1522if(!commit->parents && !show_root)1523 commit->object.flags |= UNINTERESTING;15241525/* Take responsibility for the remaining entries */1526for(ent = sb->ent; ent; ent = ent->next)1527if(same_suspect(ent->suspect, suspect))1528found_guilty_entry(ent);1529origin_decref(suspect);15301531if(DEBUG)/* sanity */1532sanity_check_refcnt(sb);1533}1534}15351536static const char*format_time(unsigned long time,const char*tz_str,1537int show_raw_time)1538{1539static char time_buf[128];1540time_t t = time;1541int minutes, tz;1542struct tm *tm;15431544if(show_raw_time) {1545sprintf(time_buf,"%lu%s", time, tz_str);1546return time_buf;1547}15481549 tz =atoi(tz_str);1550 minutes = tz <0? -tz : tz;1551 minutes = (minutes /100)*60+ (minutes %100);1552 minutes = tz <0? -minutes : minutes;1553 t = time + minutes *60;1554 tm =gmtime(&t);15551556strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1557strcat(time_buf, tz_str);1558return time_buf;1559}15601561#define OUTPUT_ANNOTATE_COMPAT 0011562#define OUTPUT_LONG_OBJECT_NAME 0021563#define OUTPUT_RAW_TIMESTAMP 0041564#define OUTPUT_PORCELAIN 0101565#define OUTPUT_SHOW_NAME 0201566#define OUTPUT_SHOW_NUMBER 0401567#define OUTPUT_SHOW_SCORE 01001568#define OUTPUT_NO_AUTHOR 020015691570static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1571{1572int cnt;1573const char*cp;1574struct origin *suspect = ent->suspect;1575char hex[41];15761577strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1578printf("%s%c%d %d %d\n",1579 hex,1580 ent->guilty ?' ':'*',// purely for debugging1581 ent->s_lno +1,1582 ent->lno +1,1583 ent->num_lines);1584if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1585struct commit_info ci;1586 suspect->commit->object.flags |= METAINFO_SHOWN;1587get_commit_info(suspect->commit, &ci,1);1588printf("author%s\n", ci.author);1589printf("author-mail%s\n", ci.author_mail);1590printf("author-time%lu\n", ci.author_time);1591printf("author-tz%s\n", ci.author_tz);1592printf("committer%s\n", ci.committer);1593printf("committer-mail%s\n", ci.committer_mail);1594printf("committer-time%lu\n", ci.committer_time);1595printf("committer-tz%s\n", ci.committer_tz);1596write_filename_info(suspect->path);1597printf("summary%s\n", ci.summary);1598if(suspect->commit->object.flags & UNINTERESTING)1599printf("boundary\n");1600}1601else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1602write_filename_info(suspect->path);16031604 cp =nth_line(sb, ent->lno);1605for(cnt =0; cnt < ent->num_lines; cnt++) {1606char ch;1607if(cnt)1608printf("%s %d %d\n", hex,1609 ent->s_lno +1+ cnt,1610 ent->lno +1+ cnt);1611putchar('\t');1612do{1613 ch = *cp++;1614putchar(ch);1615}while(ch !='\n'&&1616 cp < sb->final_buf + sb->final_buf_size);1617}1618}16191620static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1621{1622int cnt;1623const char*cp;1624struct origin *suspect = ent->suspect;1625struct commit_info ci;1626char hex[41];1627int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16281629get_commit_info(suspect->commit, &ci,1);1630strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16311632 cp =nth_line(sb, ent->lno);1633for(cnt =0; cnt < ent->num_lines; cnt++) {1634char ch;1635int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16361637if(suspect->commit->object.flags & UNINTERESTING) {1638if(blank_boundary)1639memset(hex,' ', length);1640else if(!cmd_is_annotate) {1641 length--;1642putchar('^');1643}1644}16451646printf("%.*s", length, hex);1647if(opt & OUTPUT_ANNOTATE_COMPAT)1648printf("\t(%10s\t%10s\t%d)", ci.author,1649format_time(ci.author_time, ci.author_tz,1650 show_raw_time),1651 ent->lno +1+ cnt);1652else{1653if(opt & OUTPUT_SHOW_SCORE)1654printf(" %*d%02d",1655 max_score_digits, ent->score,1656 ent->suspect->refcnt);1657if(opt & OUTPUT_SHOW_NAME)1658printf(" %-*.*s", longest_file, longest_file,1659 suspect->path);1660if(opt & OUTPUT_SHOW_NUMBER)1661printf(" %*d", max_orig_digits,1662 ent->s_lno +1+ cnt);16631664if(!(opt & OUTPUT_NO_AUTHOR))1665printf(" (%-*.*s%10s",1666 longest_author, longest_author,1667 ci.author,1668format_time(ci.author_time,1669 ci.author_tz,1670 show_raw_time));1671printf(" %*d) ",1672 max_digits, ent->lno +1+ cnt);1673}1674do{1675 ch = *cp++;1676putchar(ch);1677}while(ch !='\n'&&1678 cp < sb->final_buf + sb->final_buf_size);1679}1680}16811682static voidoutput(struct scoreboard *sb,int option)1683{1684struct blame_entry *ent;16851686if(option & OUTPUT_PORCELAIN) {1687for(ent = sb->ent; ent; ent = ent->next) {1688struct blame_entry *oth;1689struct origin *suspect = ent->suspect;1690struct commit *commit = suspect->commit;1691if(commit->object.flags & MORE_THAN_ONE_PATH)1692continue;1693for(oth = ent->next; oth; oth = oth->next) {1694if((oth->suspect->commit != commit) ||1695!strcmp(oth->suspect->path, suspect->path))1696continue;1697 commit->object.flags |= MORE_THAN_ONE_PATH;1698break;1699}1700}1701}17021703for(ent = sb->ent; ent; ent = ent->next) {1704if(option & OUTPUT_PORCELAIN)1705emit_porcelain(sb, ent);1706else{1707emit_other(sb, ent, option);1708}1709}1710}17111712/*1713 * To allow quick access to the contents of nth line in the1714 * final image, prepare an index in the scoreboard.1715 */1716static intprepare_lines(struct scoreboard *sb)1717{1718const char*buf = sb->final_buf;1719unsigned long len = sb->final_buf_size;1720int num =0, incomplete =0, bol =1;17211722if(len && buf[len-1] !='\n')1723 incomplete++;/* incomplete line at the end */1724while(len--) {1725if(bol) {1726 sb->lineno =xrealloc(sb->lineno,1727sizeof(int* ) * (num +1));1728 sb->lineno[num] = buf - sb->final_buf;1729 bol =0;1730}1731if(*buf++ =='\n') {1732 num++;1733 bol =1;1734}1735}1736 sb->lineno =xrealloc(sb->lineno,1737sizeof(int* ) * (num + incomplete +1));1738 sb->lineno[num + incomplete] = buf - sb->final_buf;1739 sb->num_lines = num + incomplete;1740return sb->num_lines;1741}17421743/*1744 * Add phony grafts for use with -S; this is primarily to1745 * support git-cvsserver that wants to give a linear history1746 * to its clients.1747 */1748static intread_ancestry(const char*graft_file)1749{1750FILE*fp =fopen(graft_file,"r");1751char buf[1024];1752if(!fp)1753return-1;1754while(fgets(buf,sizeof(buf), fp)) {1755/* The format is just "Commit Parent1 Parent2 ...\n" */1756int len =strlen(buf);1757struct commit_graft *graft =read_graft_line(buf, len);1758if(graft)1759register_commit_graft(graft,0);1760}1761fclose(fp);1762return0;1763}17641765/*1766 * How many columns do we need to show line numbers in decimal?1767 */1768static intlineno_width(int lines)1769{1770int i, width;17711772for(width =1, i =10; i <= lines +1; width++)1773 i *=10;1774return width;1775}17761777/*1778 * How many columns do we need to show line numbers, authors,1779 * and filenames?1780 */1781static voidfind_alignment(struct scoreboard *sb,int*option)1782{1783int longest_src_lines =0;1784int longest_dst_lines =0;1785unsigned largest_score =0;1786struct blame_entry *e;17871788for(e = sb->ent; e; e = e->next) {1789struct origin *suspect = e->suspect;1790struct commit_info ci;1791int num;17921793if(strcmp(suspect->path, sb->path))1794*option |= OUTPUT_SHOW_NAME;1795 num =strlen(suspect->path);1796if(longest_file < num)1797 longest_file = num;1798if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1799 suspect->commit->object.flags |= METAINFO_SHOWN;1800get_commit_info(suspect->commit, &ci,1);1801 num =strlen(ci.author);1802if(longest_author < num)1803 longest_author = num;1804}1805 num = e->s_lno + e->num_lines;1806if(longest_src_lines < num)1807 longest_src_lines = num;1808 num = e->lno + e->num_lines;1809if(longest_dst_lines < num)1810 longest_dst_lines = num;1811if(largest_score <ent_score(sb, e))1812 largest_score =ent_score(sb, e);1813}1814 max_orig_digits =lineno_width(longest_src_lines);1815 max_digits =lineno_width(longest_dst_lines);1816 max_score_digits =lineno_width(largest_score);1817}18181819/*1820 * For debugging -- origin is refcounted, and this asserts that1821 * we do not underflow.1822 */1823static voidsanity_check_refcnt(struct scoreboard *sb)1824{1825int baa =0;1826struct blame_entry *ent;18271828for(ent = sb->ent; ent; ent = ent->next) {1829/* Nobody should have zero or negative refcnt */1830if(ent->suspect->refcnt <=0) {1831fprintf(stderr,"%sin%shas negative refcnt%d\n",1832 ent->suspect->path,1833sha1_to_hex(ent->suspect->commit->object.sha1),1834 ent->suspect->refcnt);1835 baa =1;1836}1837}1838for(ent = sb->ent; ent; ent = ent->next) {1839/* Mark the ones that haven't been checked */1840if(0< ent->suspect->refcnt)1841 ent->suspect->refcnt = -ent->suspect->refcnt;1842}1843for(ent = sb->ent; ent; ent = ent->next) {1844/*1845 * ... then pick each and see if they have the the1846 * correct refcnt.1847 */1848int found;1849struct blame_entry *e;1850struct origin *suspect = ent->suspect;18511852if(0< suspect->refcnt)1853continue;1854 suspect->refcnt = -suspect->refcnt;/* Unmark */1855for(found =0, e = sb->ent; e; e = e->next) {1856if(e->suspect != suspect)1857continue;1858 found++;1859}1860if(suspect->refcnt != found) {1861fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1862 ent->suspect->path,1863sha1_to_hex(ent->suspect->commit->object.sha1),1864 ent->suspect->refcnt, found);1865 baa =2;1866}1867}1868if(baa) {1869int opt =0160;1870find_alignment(sb, &opt);1871output(sb, opt);1872die("Baa%d!", baa);1873}1874}18751876/*1877 * Used for the command line parsing; check if the path exists1878 * in the working tree.1879 */1880static inthas_path_in_work_tree(const char*path)1881{1882struct stat st;1883return!lstat(path, &st);1884}18851886static unsignedparse_score(const char*arg)1887{1888char*end;1889unsigned long score =strtoul(arg, &end,10);1890if(*end)1891return0;1892return score;1893}18941895static const char*add_prefix(const char*prefix,const char*path)1896{1897returnprefix_path(prefix, prefix ?strlen(prefix) :0, path);1898}18991900/*1901 * Parsing of (comma separated) one item in the -L option1902 */1903static const char*parse_loc(const char*spec,1904struct scoreboard *sb,long lno,1905long begin,long*ret)1906{1907char*term;1908const char*line;1909long num;1910int reg_error;1911 regex_t regexp;1912 regmatch_t match[1];19131914/* Allow "-L <something>,+20" to mean starting at <something>1915 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1916 * <something>.1917 */1918if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1919 num =strtol(spec +1, &term,10);1920if(term != spec +1) {1921if(spec[0] =='-')1922 num =0- num;1923if(0< num)1924*ret = begin + num -2;1925else if(!num)1926*ret = begin;1927else1928*ret = begin + num;1929return term;1930}1931return spec;1932}1933 num =strtol(spec, &term,10);1934if(term != spec) {1935*ret = num;1936return term;1937}1938if(spec[0] !='/')1939return spec;19401941/* it could be a regexp of form /.../ */1942for(term = (char*) spec +1; *term && *term !='/'; term++) {1943if(*term =='\\')1944 term++;1945}1946if(*term !='/')1947return spec;19481949/* try [spec+1 .. term-1] as regexp */1950*term =0;1951 begin--;/* input is in human terms */1952 line =nth_line(sb, begin);19531954if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1955!(reg_error =regexec(®exp, line,1, match,0))) {1956const char*cp = line + match[0].rm_so;1957const char*nline;19581959while(begin++ < lno) {1960 nline =nth_line(sb, begin);1961if(line <= cp && cp < nline)1962break;1963 line = nline;1964}1965*ret = begin;1966regfree(®exp);1967*term++ ='/';1968return term;1969}1970else{1971char errbuf[1024];1972regerror(reg_error, ®exp, errbuf,1024);1973die("-L parameter '%s':%s", spec +1, errbuf);1974}1975}19761977/*1978 * Parsing of -L option1979 */1980static voidprepare_blame_range(struct scoreboard *sb,1981const char*bottomtop,1982long lno,1983long*bottom,long*top)1984{1985const char*term;19861987 term =parse_loc(bottomtop, sb, lno,1, bottom);1988if(*term ==',') {1989 term =parse_loc(term +1, sb, lno, *bottom +1, top);1990if(*term)1991usage(blame_usage);1992}1993if(*term)1994usage(blame_usage);1995}19961997static intgit_blame_config(const char*var,const char*value)1998{1999if(!strcmp(var,"blame.showroot")) {2000 show_root =git_config_bool(var, value);2001return0;2002}2003if(!strcmp(var,"blame.blankboundary")) {2004 blank_boundary =git_config_bool(var, value);2005return0;2006}2007returngit_default_config(var, value);2008}20092010static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2011{2012struct commit *commit;2013struct origin *origin;2014unsigned char head_sha1[20];2015struct strbuf buf;2016const char*ident;2017time_t now;2018int size, len;2019struct cache_entry *ce;2020unsigned mode;20212022if(get_sha1("HEAD", head_sha1))2023die("No such ref: HEAD");20242025time(&now);2026 commit =xcalloc(1,sizeof(*commit));2027 commit->parents =xcalloc(1,sizeof(*commit->parents));2028 commit->parents->item =lookup_commit_reference(head_sha1);2029 commit->object.parsed =1;2030 commit->date = now;2031 commit->object.type = OBJ_COMMIT;20322033 origin =make_origin(commit, path);20342035strbuf_init(&buf,0);2036if(!contents_from ||strcmp("-", contents_from)) {2037struct stat st;2038const char*read_from;2039unsigned long fin_size;20402041if(contents_from) {2042if(stat(contents_from, &st) <0)2043die("Cannot stat%s", contents_from);2044 read_from = contents_from;2045}2046else{2047if(lstat(path, &st) <0)2048die("Cannot lstat%s", path);2049 read_from = path;2050}2051 fin_size =xsize_t(st.st_size);2052 mode =canon_mode(st.st_mode);2053switch(st.st_mode & S_IFMT) {2054case S_IFREG:2055if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2056die("cannot open or read%s", read_from);2057break;2058case S_IFLNK:2059if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2060die("cannot readlink%s", read_from);2061 buf.len = fin_size;2062break;2063default:2064die("unsupported file type%s", read_from);2065}2066}2067else{2068/* Reading from stdin */2069 contents_from ="standard input";2070 mode =0;2071if(strbuf_read(&buf,0,0) <0)2072die("read error%sfrom stdin",strerror(errno));2073}2074convert_to_git(path, buf.buf, buf.len, &buf,0);2075 origin->file.ptr = buf.buf;2076 origin->file.size = buf.len;2077pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2078 commit->util = origin;20792080/*2081 * Read the current index, replace the path entry with2082 * origin->blob_sha1 without mucking with its mode or type2083 * bits; we are not going to write this index out -- we just2084 * want to run "diff-index --cached".2085 */2086discard_cache();2087read_cache();20882089 len =strlen(path);2090if(!mode) {2091int pos =cache_name_pos(path, len);2092if(0<= pos)2093 mode = active_cache[pos]->ce_mode;2094else2095/* Let's not bother reading from HEAD tree */2096 mode = S_IFREG |0644;2097}2098 size =cache_entry_size(len);2099 ce =xcalloc(1, size);2100hashcpy(ce->sha1, origin->blob_sha1);2101memcpy(ce->name, path, len);2102 ce->ce_flags =create_ce_flags(len,0);2103 ce->ce_mode =create_ce_mode(mode);2104add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);21052106/*2107 * We are not going to write this out, so this does not matter2108 * right now, but someday we might optimize diff-index --cached2109 * with cache-tree information.2110 */2111cache_tree_invalidate_path(active_cache_tree, path);21122113 commit->buffer =xmalloc(400);2114 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2115snprintf(commit->buffer,400,2116"tree 0000000000000000000000000000000000000000\n"2117"parent%s\n"2118"author%s\n"2119"committer%s\n\n"2120"Version of%sfrom%s\n",2121sha1_to_hex(head_sha1),2122 ident, ident, path, contents_from ? contents_from : path);2123return commit;2124}21252126intcmd_blame(int argc,const char**argv,const char*prefix)2127{2128struct rev_info revs;2129const char*path;2130struct scoreboard sb;2131struct origin *o;2132struct blame_entry *ent;2133int i, seen_dashdash, unk, opt;2134long bottom, top, lno;2135int output_option =0;2136int show_stats =0;2137const char*revs_file = NULL;2138const char*final_commit_name = NULL;2139enum object_type type;2140const char*bottomtop = NULL;2141const char*contents_from = NULL;21422143 cmd_is_annotate = !strcmp(argv[0],"annotate");21442145git_config(git_blame_config);2146 save_commit_buffer =0;21472148 opt =0;2149 seen_dashdash =0;2150for(unk = i =1; i < argc; i++) {2151const char*arg = argv[i];2152if(*arg !='-')2153break;2154else if(!strcmp("-b", arg))2155 blank_boundary =1;2156else if(!strcmp("--root", arg))2157 show_root =1;2158else if(!strcmp(arg,"--show-stats"))2159 show_stats =1;2160else if(!strcmp("-c", arg))2161 output_option |= OUTPUT_ANNOTATE_COMPAT;2162else if(!strcmp("-t", arg))2163 output_option |= OUTPUT_RAW_TIMESTAMP;2164else if(!strcmp("-l", arg))2165 output_option |= OUTPUT_LONG_OBJECT_NAME;2166else if(!strcmp("-s", arg))2167 output_option |= OUTPUT_NO_AUTHOR;2168else if(!strcmp("-w", arg))2169 xdl_opts |= XDF_IGNORE_WHITESPACE;2170else if(!strcmp("-S", arg) && ++i < argc)2171 revs_file = argv[i];2172else if(!prefixcmp(arg,"-M")) {2173 opt |= PICKAXE_BLAME_MOVE;2174 blame_move_score =parse_score(arg+2);2175}2176else if(!prefixcmp(arg,"-C")) {2177/*2178 * -C enables copy from removed files;2179 * -C -C enables copy from existing files, but only2180 * when blaming a new file;2181 * -C -C -C enables copy from existing files for2182 * everybody2183 */2184if(opt & PICKAXE_BLAME_COPY_HARDER)2185 opt |= PICKAXE_BLAME_COPY_HARDEST;2186if(opt & PICKAXE_BLAME_COPY)2187 opt |= PICKAXE_BLAME_COPY_HARDER;2188 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2189 blame_copy_score =parse_score(arg+2);2190}2191else if(!prefixcmp(arg,"-L")) {2192if(!arg[2]) {2193if(++i >= argc)2194usage(blame_usage);2195 arg = argv[i];2196}2197else2198 arg +=2;2199if(bottomtop)2200die("More than one '-L n,m' option given");2201 bottomtop = arg;2202}2203else if(!strcmp("--contents", arg)) {2204if(++i >= argc)2205usage(blame_usage);2206 contents_from = argv[i];2207}2208else if(!strcmp("--incremental", arg))2209 incremental =1;2210else if(!strcmp("--score-debug", arg))2211 output_option |= OUTPUT_SHOW_SCORE;2212else if(!strcmp("-f", arg) ||2213!strcmp("--show-name", arg))2214 output_option |= OUTPUT_SHOW_NAME;2215else if(!strcmp("-n", arg) ||2216!strcmp("--show-number", arg))2217 output_option |= OUTPUT_SHOW_NUMBER;2218else if(!strcmp("-p", arg) ||2219!strcmp("--porcelain", arg))2220 output_option |= OUTPUT_PORCELAIN;2221else if(!strcmp("--", arg)) {2222 seen_dashdash =1;2223 i++;2224break;2225}2226else2227 argv[unk++] = arg;2228}22292230if(!blame_move_score)2231 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2232if(!blame_copy_score)2233 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22342235/*2236 * We have collected options unknown to us in argv[1..unk]2237 * which are to be passed to revision machinery if we are2238 * going to do the "bottom" processing.2239 *2240 * The remaining are:2241 *2242 * (1) if seen_dashdash, its either2243 * "-options -- <path>" or2244 * "-options -- <path> <rev>".2245 * but the latter is allowed only if there is no2246 * options that we passed to revision machinery.2247 *2248 * (2) otherwise, we may have "--" somewhere later and2249 * might be looking at the first one of multiple 'rev'2250 * parameters (e.g. " master ^next ^maint -- path").2251 * See if there is a dashdash first, and give the2252 * arguments before that to revision machinery.2253 * After that there must be one 'path'.2254 *2255 * (3) otherwise, its one of the three:2256 * "-options <path> <rev>"2257 * "-options <rev> <path>"2258 * "-options <path>"2259 * but again the first one is allowed only if2260 * there is no options that we passed to revision2261 * machinery.2262 */22632264if(seen_dashdash) {2265/* (1) */2266if(argc <= i)2267usage(blame_usage);2268 path =add_prefix(prefix, argv[i]);2269if(i +1== argc -1) {2270if(unk !=1)2271usage(blame_usage);2272 argv[unk++] = argv[i +1];2273}2274else if(i +1!= argc)2275/* garbage at end */2276usage(blame_usage);2277}2278else{2279int j;2280for(j = i; !seen_dashdash && j < argc; j++)2281if(!strcmp(argv[j],"--"))2282 seen_dashdash = j;2283if(seen_dashdash) {2284/* (2) */2285if(seen_dashdash +1!= argc -1)2286usage(blame_usage);2287 path =add_prefix(prefix, argv[seen_dashdash +1]);2288for(j = i; j < seen_dashdash; j++)2289 argv[unk++] = argv[j];2290}2291else{2292/* (3) */2293if(argc <= i)2294usage(blame_usage);2295 path =add_prefix(prefix, argv[i]);2296if(i +1== argc -1) {2297 final_commit_name = argv[i +1];22982299/* if (unk == 1) we could be getting2300 * old-style2301 */2302if(unk ==1&& !has_path_in_work_tree(path)) {2303 path =add_prefix(prefix, argv[i +1]);2304 final_commit_name = argv[i];2305}2306}2307else if(i != argc -1)2308usage(blame_usage);/* garbage at end */23092310setup_work_tree();2311if(!has_path_in_work_tree(path))2312die("cannot stat path%s:%s",2313 path,strerror(errno));2314}2315}23162317if(final_commit_name)2318 argv[unk++] = final_commit_name;23192320/*2321 * Now we got rev and path. We do not want the path pruning2322 * but we may want "bottom" processing.2323 */2324 argv[unk++] ="--";/* terminate the rev name */2325 argv[unk] = NULL;23262327init_revisions(&revs, NULL);2328setup_revisions(unk, argv, &revs, NULL);2329memset(&sb,0,sizeof(sb));23302331/*2332 * There must be one and only one positive commit in the2333 * revs->pending array.2334 */2335for(i =0; i < revs.pending.nr; i++) {2336struct object *obj = revs.pending.objects[i].item;2337if(obj->flags & UNINTERESTING)2338continue;2339while(obj->type == OBJ_TAG)2340 obj =deref_tag(obj, NULL,0);2341if(obj->type != OBJ_COMMIT)2342die("Non commit%s?",2343 revs.pending.objects[i].name);2344if(sb.final)2345die("More than one commit to dig from%sand%s?",2346 revs.pending.objects[i].name,2347 final_commit_name);2348 sb.final = (struct commit *) obj;2349 final_commit_name = revs.pending.objects[i].name;2350}23512352if(!sb.final) {2353/*2354 * "--not A B -- path" without anything positive;2355 * do not default to HEAD, but use the working tree2356 * or "--contents".2357 */2358setup_work_tree();2359 sb.final =fake_working_tree_commit(path, contents_from);2360add_pending_object(&revs, &(sb.final->object),":");2361}2362else if(contents_from)2363die("Cannot use --contents with final commit object name");23642365/*2366 * If we have bottom, this will mark the ancestors of the2367 * bottom commits we would reach while traversing as2368 * uninteresting.2369 */2370if(prepare_revision_walk(&revs))2371die("revision walk setup failed");23722373if(is_null_sha1(sb.final->object.sha1)) {2374char*buf;2375 o = sb.final->util;2376 buf =xmalloc(o->file.size +1);2377memcpy(buf, o->file.ptr, o->file.size +1);2378 sb.final_buf = buf;2379 sb.final_buf_size = o->file.size;2380}2381else{2382 o =get_origin(&sb, sb.final, path);2383if(fill_blob_sha1(o))2384die("no such path%sin%s", path, final_commit_name);23852386 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2387&sb.final_buf_size);2388if(!sb.final_buf)2389die("Cannot read blob%sfor path%s",2390sha1_to_hex(o->blob_sha1),2391 path);2392}2393 num_read_blob++;2394 lno =prepare_lines(&sb);23952396 bottom = top =0;2397if(bottomtop)2398prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2399if(bottom && top && top < bottom) {2400long tmp;2401 tmp = top; top = bottom; bottom = tmp;2402}2403if(bottom <1)2404 bottom =1;2405if(top <1)2406 top = lno;2407 bottom--;2408if(lno < top)2409die("file%shas only%lu lines", path, lno);24102411 ent =xcalloc(1,sizeof(*ent));2412 ent->lno = bottom;2413 ent->num_lines = top - bottom;2414 ent->suspect = o;2415 ent->s_lno = bottom;24162417 sb.ent = ent;2418 sb.path = path;24192420if(revs_file &&read_ancestry(revs_file))2421die("reading graft file%sfailed:%s",2422 revs_file,strerror(errno));24232424read_mailmap(&mailmap,".mailmap", NULL);24252426if(!incremental)2427setup_pager();24282429assign_blame(&sb, &revs, opt);24302431if(incremental)2432return0;24332434coalesce(&sb);24352436if(!(output_option & OUTPUT_PORCELAIN))2437find_alignment(&sb, &output_option);24382439output(&sb, output_option);2440free((void*)sb.final_buf);2441for(ent = sb.ent; ent; ) {2442struct blame_entry *e = ent->next;2443free(ent);2444 ent = e;2445}24462447if(show_stats) {2448printf("num read blob:%d\n", num_read_blob);2449printf("num get patch:%d\n", num_get_patch);2450printf("num commits:%d\n", num_commits);2451}2452return0;2453}