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 voidfill_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; 109} 110 111/* 112 * Origin is refcounted and usually we keep the blob contents to be 113 * reused. 114 */ 115staticinlinestruct origin *origin_incref(struct origin *o) 116{ 117if(o) 118 o->refcnt++; 119return o; 120} 121 122static voidorigin_decref(struct origin *o) 123{ 124if(o && --o->refcnt <=0) { 125free(o->file.ptr); 126free(o); 127} 128} 129 130static voiddrop_origin_blob(struct origin *o) 131{ 132if(o->file.ptr) { 133free(o->file.ptr); 134 o->file.ptr = NULL; 135} 136} 137 138/* 139 * Each group of lines is described by a blame_entry; it can be split 140 * as we pass blame to the parents. They form a linked list in the 141 * scoreboard structure, sorted by the target line number. 142 */ 143struct blame_entry { 144struct blame_entry *prev; 145struct blame_entry *next; 146 147/* the first line of this group in the final image; 148 * internally all line numbers are 0 based. 149 */ 150int lno; 151 152/* how many lines this group has */ 153int num_lines; 154 155/* the commit that introduced this group into the final image */ 156struct origin *suspect; 157 158/* true if the suspect is truly guilty; false while we have not 159 * checked if the group came from one of its parents. 160 */ 161char guilty; 162 163/* the line number of the first line of this group in the 164 * suspect's file; internally all line numbers are 0 based. 165 */ 166int s_lno; 167 168/* how significant this entry is -- cached to avoid 169 * scanning the lines over and over. 170 */ 171unsigned score; 172}; 173 174/* 175 * The current state of the blame assignment. 176 */ 177struct scoreboard { 178/* the final commit (i.e. where we started digging from) */ 179struct commit *final; 180 181const char*path; 182 183/* 184 * The contents in the final image. 185 * Used by many functions to obtain contents of the nth line, 186 * indexed with scoreboard.lineno[blame_entry.lno]. 187 */ 188const char*final_buf; 189unsigned long final_buf_size; 190 191/* linked list of blames */ 192struct blame_entry *ent; 193 194/* look-up a line in the final buffer */ 195int num_lines; 196int*lineno; 197}; 198 199staticinlineintsame_suspect(struct origin *a,struct origin *b) 200{ 201if(a == b) 202return1; 203if(a->commit != b->commit) 204return0; 205return!strcmp(a->path, b->path); 206} 207 208static voidsanity_check_refcnt(struct scoreboard *); 209 210/* 211 * If two blame entries that are next to each other came from 212 * contiguous lines in the same origin (i.e. <commit, path> pair), 213 * merge them together. 214 */ 215static voidcoalesce(struct scoreboard *sb) 216{ 217struct blame_entry *ent, *next; 218 219for(ent = sb->ent; ent && (next = ent->next); ent = next) { 220if(same_suspect(ent->suspect, next->suspect) && 221 ent->guilty == next->guilty && 222 ent->s_lno + ent->num_lines == next->s_lno) { 223 ent->num_lines += next->num_lines; 224 ent->next = next->next; 225if(ent->next) 226 ent->next->prev = ent; 227origin_decref(next->suspect); 228free(next); 229 ent->score =0; 230 next = ent;/* again */ 231} 232} 233 234if(DEBUG)/* sanity */ 235sanity_check_refcnt(sb); 236} 237 238/* 239 * Given a commit and a path in it, create a new origin structure. 240 * The callers that add blame to the scoreboard should use 241 * get_origin() to obtain shared, refcounted copy instead of calling 242 * this function directly. 243 */ 244static struct origin *make_origin(struct commit *commit,const char*path) 245{ 246struct origin *o; 247 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 248 o->commit = commit; 249 o->refcnt =1; 250strcpy(o->path, path); 251return o; 252} 253 254/* 255 * Locate an existing origin or create a new one. 256 */ 257static struct origin *get_origin(struct scoreboard *sb, 258struct commit *commit, 259const char*path) 260{ 261struct blame_entry *e; 262 263for(e = sb->ent; e; e = e->next) { 264if(e->suspect->commit == commit && 265!strcmp(e->suspect->path, path)) 266returnorigin_incref(e->suspect); 267} 268returnmake_origin(commit, path); 269} 270 271/* 272 * Fill the blob_sha1 field of an origin if it hasn't, so that later 273 * call to fill_origin_blob() can use it to locate the data. blob_sha1 274 * for an origin is also used to pass the blame for the entire file to 275 * the parent to detect the case where a child's blob is identical to 276 * that of its parent's. 277 */ 278static intfill_blob_sha1(struct origin *origin) 279{ 280unsigned mode; 281 282if(!is_null_sha1(origin->blob_sha1)) 283return0; 284if(get_tree_entry(origin->commit->object.sha1, 285 origin->path, 286 origin->blob_sha1, &mode)) 287goto error_out; 288if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 289goto error_out; 290return0; 291 error_out: 292hashclr(origin->blob_sha1); 293return-1; 294} 295 296/* 297 * We have an origin -- check if the same path exists in the 298 * parent and return an origin structure to represent it. 299 */ 300static struct origin *find_origin(struct scoreboard *sb, 301struct commit *parent, 302struct origin *origin) 303{ 304struct origin *porigin = NULL; 305struct diff_options diff_opts; 306const char*paths[2]; 307 308if(parent->util) { 309/* 310 * Each commit object can cache one origin in that 311 * commit. This is a freestanding copy of origin and 312 * not refcounted. 313 */ 314struct origin *cached = parent->util; 315if(!strcmp(cached->path, origin->path)) { 316/* 317 * The same path between origin and its parent 318 * without renaming -- the most common case. 319 */ 320 porigin =get_origin(sb, parent, cached->path); 321 322/* 323 * If the origin was newly created (i.e. get_origin 324 * would call make_origin if none is found in the 325 * scoreboard), it does not know the blob_sha1, 326 * so copy it. Otherwise porigin was in the 327 * scoreboard and already knows blob_sha1. 328 */ 329if(porigin->refcnt ==1) 330hashcpy(porigin->blob_sha1, cached->blob_sha1); 331return porigin; 332} 333/* otherwise it was not very useful; free it */ 334free(parent->util); 335 parent->util = NULL; 336} 337 338/* See if the origin->path is different between parent 339 * and origin first. Most of the time they are the 340 * same and diff-tree is fairly efficient about this. 341 */ 342diff_setup(&diff_opts); 343DIFF_OPT_SET(&diff_opts, RECURSIVE); 344 diff_opts.detect_rename =0; 345 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 346 paths[0] = origin->path; 347 paths[1] = NULL; 348 349diff_tree_setup_paths(paths, &diff_opts); 350if(diff_setup_done(&diff_opts) <0) 351die("diff-setup"); 352 353if(is_null_sha1(origin->commit->object.sha1)) 354do_diff_cache(parent->tree->object.sha1, &diff_opts); 355else 356diff_tree_sha1(parent->tree->object.sha1, 357 origin->commit->tree->object.sha1, 358"", &diff_opts); 359diffcore_std(&diff_opts); 360 361/* It is either one entry that says "modified", or "created", 362 * or nothing. 363 */ 364if(!diff_queued_diff.nr) { 365/* The path is the same as parent */ 366 porigin =get_origin(sb, parent, origin->path); 367hashcpy(porigin->blob_sha1, origin->blob_sha1); 368} 369else if(diff_queued_diff.nr !=1) 370die("internal error in blame::find_origin"); 371else{ 372struct diff_filepair *p = diff_queued_diff.queue[0]; 373switch(p->status) { 374default: 375die("internal error in blame::find_origin (%c)", 376 p->status); 377case'M': 378 porigin =get_origin(sb, parent, origin->path); 379hashcpy(porigin->blob_sha1, p->one->sha1); 380break; 381case'A': 382case'T': 383/* Did not exist in parent, or type changed */ 384break; 385} 386} 387diff_flush(&diff_opts); 388diff_tree_release_paths(&diff_opts); 389if(porigin) { 390/* 391 * Create a freestanding copy that is not part of 392 * the refcounted origin found in the scoreboard, and 393 * cache it in the commit. 394 */ 395struct origin *cached; 396 397 cached =make_origin(porigin->commit, porigin->path); 398hashcpy(cached->blob_sha1, porigin->blob_sha1); 399 parent->util = cached; 400} 401return porigin; 402} 403 404/* 405 * We have an origin -- find the path that corresponds to it in its 406 * parent and return an origin structure to represent it. 407 */ 408static struct origin *find_rename(struct scoreboard *sb, 409struct commit *parent, 410struct origin *origin) 411{ 412struct origin *porigin = NULL; 413struct diff_options diff_opts; 414int i; 415const char*paths[2]; 416 417diff_setup(&diff_opts); 418DIFF_OPT_SET(&diff_opts, RECURSIVE); 419 diff_opts.detect_rename = DIFF_DETECT_RENAME; 420 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 421 diff_opts.single_follow = origin->path; 422 paths[0] = NULL; 423diff_tree_setup_paths(paths, &diff_opts); 424if(diff_setup_done(&diff_opts) <0) 425die("diff-setup"); 426 427if(is_null_sha1(origin->commit->object.sha1)) 428do_diff_cache(parent->tree->object.sha1, &diff_opts); 429else 430diff_tree_sha1(parent->tree->object.sha1, 431 origin->commit->tree->object.sha1, 432"", &diff_opts); 433diffcore_std(&diff_opts); 434 435for(i =0; i < diff_queued_diff.nr; i++) { 436struct diff_filepair *p = diff_queued_diff.queue[i]; 437if((p->status =='R'|| p->status =='C') && 438!strcmp(p->two->path, origin->path)) { 439 porigin =get_origin(sb, parent, p->one->path); 440hashcpy(porigin->blob_sha1, p->one->sha1); 441break; 442} 443} 444diff_flush(&diff_opts); 445diff_tree_release_paths(&diff_opts); 446return porigin; 447} 448 449/* 450 * Parsing of patch chunks... 451 */ 452struct chunk { 453/* line number in postimage; up to but not including this 454 * line is the same as preimage 455 */ 456int same; 457 458/* preimage line number after this chunk */ 459int p_next; 460 461/* postimage line number after this chunk */ 462int t_next; 463}; 464 465struct patch { 466struct chunk *chunks; 467int num; 468}; 469 470struct blame_diff_state { 471struct xdiff_emit_state xm; 472struct patch *ret; 473unsigned hunk_post_context; 474unsigned hunk_in_pre_context :1; 475}; 476 477static voidprocess_u_diff(void*state_,char*line,unsigned long len) 478{ 479struct blame_diff_state *state = state_; 480struct chunk *chunk; 481int off1, off2, len1, len2, num; 482 483 num = state->ret->num; 484if(len <4|| line[0] !='@'|| line[1] !='@') { 485if(state->hunk_in_pre_context && line[0] ==' ') 486 state->ret->chunks[num -1].same++; 487else{ 488 state->hunk_in_pre_context =0; 489if(line[0] ==' ') 490 state->hunk_post_context++; 491else 492 state->hunk_post_context =0; 493} 494return; 495} 496 497if(num && state->hunk_post_context) { 498 chunk = &state->ret->chunks[num -1]; 499 chunk->p_next -= state->hunk_post_context; 500 chunk->t_next -= state->hunk_post_context; 501} 502 state->ret->num = ++num; 503 state->ret->chunks =xrealloc(state->ret->chunks, 504sizeof(struct chunk) * num); 505 chunk = &state->ret->chunks[num -1]; 506if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 507 state->ret->num--; 508return; 509} 510 511/* Line numbers in patch output are one based. */ 512 off1--; 513 off2--; 514 515 chunk->same = len2 ? off2 : (off2 +1); 516 517 chunk->p_next = off1 + (len1 ? len1 :1); 518 chunk->t_next = chunk->same + len2; 519 state->hunk_in_pre_context =1; 520 state->hunk_post_context =0; 521} 522 523static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 524int context) 525{ 526struct blame_diff_state state; 527 xpparam_t xpp; 528 xdemitconf_t xecfg; 529 xdemitcb_t ecb; 530 531 xpp.flags = xdl_opts; 532memset(&xecfg,0,sizeof(xecfg)); 533 xecfg.ctxlen = context; 534 ecb.outf = xdiff_outf; 535 ecb.priv = &state; 536memset(&state,0,sizeof(state)); 537 state.xm.consume = process_u_diff; 538 state.ret =xmalloc(sizeof(struct patch)); 539 state.ret->chunks = NULL; 540 state.ret->num =0; 541 542xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb); 543 544if(state.ret->num) { 545struct chunk *chunk; 546 chunk = &state.ret->chunks[state.ret->num -1]; 547 chunk->p_next -= state.hunk_post_context; 548 chunk->t_next -= state.hunk_post_context; 549} 550return state.ret; 551} 552 553/* 554 * Run diff between two origins and grab the patch output, so that 555 * we can pass blame for lines origin is currently suspected for 556 * to its parent. 557 */ 558static struct patch *get_patch(struct origin *parent,struct origin *origin) 559{ 560 mmfile_t file_p, file_o; 561struct patch *patch; 562 563fill_origin_blob(parent, &file_p); 564fill_origin_blob(origin, &file_o); 565if(!file_p.ptr || !file_o.ptr) 566return NULL; 567 patch =compare_buffer(&file_p, &file_o,0); 568 num_get_patch++; 569return patch; 570} 571 572static voidfree_patch(struct patch *p) 573{ 574free(p->chunks); 575free(p); 576} 577 578/* 579 * Link in a new blame entry to the scoreboard. Entries that cover the 580 * same line range have been removed from the scoreboard previously. 581 */ 582static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 583{ 584struct blame_entry *ent, *prev = NULL; 585 586origin_incref(e->suspect); 587 588for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 589 prev = ent; 590 591/* prev, if not NULL, is the last one that is below e */ 592 e->prev = prev; 593if(prev) { 594 e->next = prev->next; 595 prev->next = e; 596} 597else{ 598 e->next = sb->ent; 599 sb->ent = e; 600} 601if(e->next) 602 e->next->prev = e; 603} 604 605/* 606 * src typically is on-stack; we want to copy the information in it to 607 * a malloced blame_entry that is already on the linked list of the 608 * scoreboard. The origin of dst loses a refcnt while the origin of src 609 * gains one. 610 */ 611static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 612{ 613struct blame_entry *p, *n; 614 615 p = dst->prev; 616 n = dst->next; 617origin_incref(src->suspect); 618origin_decref(dst->suspect); 619memcpy(dst, src,sizeof(*src)); 620 dst->prev = p; 621 dst->next = n; 622 dst->score =0; 623} 624 625static const char*nth_line(struct scoreboard *sb,int lno) 626{ 627return sb->final_buf + sb->lineno[lno]; 628} 629 630/* 631 * It is known that lines between tlno to same came from parent, and e 632 * has an overlap with that range. it also is known that parent's 633 * line plno corresponds to e's line tlno. 634 * 635 * <---- e -----> 636 * <------> 637 * <------------> 638 * <------------> 639 * <------------------> 640 * 641 * Split e into potentially three parts; before this chunk, the chunk 642 * to be blamed for the parent, and after that portion. 643 */ 644static voidsplit_overlap(struct blame_entry *split, 645struct blame_entry *e, 646int tlno,int plno,int same, 647struct origin *parent) 648{ 649int chunk_end_lno; 650memset(split,0,sizeof(struct blame_entry [3])); 651 652if(e->s_lno < tlno) { 653/* there is a pre-chunk part not blamed on parent */ 654 split[0].suspect =origin_incref(e->suspect); 655 split[0].lno = e->lno; 656 split[0].s_lno = e->s_lno; 657 split[0].num_lines = tlno - e->s_lno; 658 split[1].lno = e->lno + tlno - e->s_lno; 659 split[1].s_lno = plno; 660} 661else{ 662 split[1].lno = e->lno; 663 split[1].s_lno = plno + (e->s_lno - tlno); 664} 665 666if(same < e->s_lno + e->num_lines) { 667/* there is a post-chunk part not blamed on parent */ 668 split[2].suspect =origin_incref(e->suspect); 669 split[2].lno = e->lno + (same - e->s_lno); 670 split[2].s_lno = e->s_lno + (same - e->s_lno); 671 split[2].num_lines = e->s_lno + e->num_lines - same; 672 chunk_end_lno = split[2].lno; 673} 674else 675 chunk_end_lno = e->lno + e->num_lines; 676 split[1].num_lines = chunk_end_lno - split[1].lno; 677 678/* 679 * if it turns out there is nothing to blame the parent for, 680 * forget about the splitting. !split[1].suspect signals this. 681 */ 682if(split[1].num_lines <1) 683return; 684 split[1].suspect =origin_incref(parent); 685} 686 687/* 688 * split_overlap() divided an existing blame e into up to three parts 689 * in split. Adjust the linked list of blames in the scoreboard to 690 * reflect the split. 691 */ 692static voidsplit_blame(struct scoreboard *sb, 693struct blame_entry *split, 694struct blame_entry *e) 695{ 696struct blame_entry *new_entry; 697 698if(split[0].suspect && split[2].suspect) { 699/* The first part (reuse storage for the existing entry e) */ 700dup_entry(e, &split[0]); 701 702/* The last part -- me */ 703 new_entry =xmalloc(sizeof(*new_entry)); 704memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 705add_blame_entry(sb, new_entry); 706 707/* ... and the middle part -- parent */ 708 new_entry =xmalloc(sizeof(*new_entry)); 709memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 710add_blame_entry(sb, new_entry); 711} 712else if(!split[0].suspect && !split[2].suspect) 713/* 714 * The parent covers the entire area; reuse storage for 715 * e and replace it with the parent. 716 */ 717dup_entry(e, &split[1]); 718else if(split[0].suspect) { 719/* me and then parent */ 720dup_entry(e, &split[0]); 721 722 new_entry =xmalloc(sizeof(*new_entry)); 723memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 724add_blame_entry(sb, new_entry); 725} 726else{ 727/* parent and then me */ 728dup_entry(e, &split[1]); 729 730 new_entry =xmalloc(sizeof(*new_entry)); 731memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 732add_blame_entry(sb, new_entry); 733} 734 735if(DEBUG) {/* sanity */ 736struct blame_entry *ent; 737int lno = sb->ent->lno, corrupt =0; 738 739for(ent = sb->ent; ent; ent = ent->next) { 740if(lno != ent->lno) 741 corrupt =1; 742if(ent->s_lno <0) 743 corrupt =1; 744 lno += ent->num_lines; 745} 746if(corrupt) { 747 lno = sb->ent->lno; 748for(ent = sb->ent; ent; ent = ent->next) { 749printf("L%8d l%8d n%8d\n", 750 lno, ent->lno, ent->num_lines); 751 lno = ent->lno + ent->num_lines; 752} 753die("oops"); 754} 755} 756} 757 758/* 759 * After splitting the blame, the origins used by the 760 * on-stack blame_entry should lose one refcnt each. 761 */ 762static voiddecref_split(struct blame_entry *split) 763{ 764int i; 765 766for(i =0; i <3; i++) 767origin_decref(split[i].suspect); 768} 769 770/* 771 * Helper for blame_chunk(). blame_entry e is known to overlap with 772 * the patch hunk; split it and pass blame to the parent. 773 */ 774static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 775int tlno,int plno,int same, 776struct origin *parent) 777{ 778struct blame_entry split[3]; 779 780split_overlap(split, e, tlno, plno, same, parent); 781if(split[1].suspect) 782split_blame(sb, split, e); 783decref_split(split); 784} 785 786/* 787 * Find the line number of the last line the target is suspected for. 788 */ 789static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 790{ 791struct blame_entry *e; 792int last_in_target = -1; 793 794for(e = sb->ent; e; e = e->next) { 795if(e->guilty || !same_suspect(e->suspect, target)) 796continue; 797if(last_in_target < e->s_lno + e->num_lines) 798 last_in_target = e->s_lno + e->num_lines; 799} 800return last_in_target; 801} 802 803/* 804 * Process one hunk from the patch between the current suspect for 805 * blame_entry e and its parent. Find and split the overlap, and 806 * pass blame to the overlapping part to the parent. 807 */ 808static voidblame_chunk(struct scoreboard *sb, 809int tlno,int plno,int same, 810struct origin *target,struct origin *parent) 811{ 812struct blame_entry *e; 813 814for(e = sb->ent; e; e = e->next) { 815if(e->guilty || !same_suspect(e->suspect, target)) 816continue; 817if(same <= e->s_lno) 818continue; 819if(tlno < e->s_lno + e->num_lines) 820blame_overlap(sb, e, tlno, plno, same, parent); 821} 822} 823 824/* 825 * We are looking at the origin 'target' and aiming to pass blame 826 * for the lines it is suspected to its parent. Run diff to find 827 * which lines came from parent and pass blame for them. 828 */ 829static intpass_blame_to_parent(struct scoreboard *sb, 830struct origin *target, 831struct origin *parent) 832{ 833int i, last_in_target, plno, tlno; 834struct patch *patch; 835 836 last_in_target =find_last_in_target(sb, target); 837if(last_in_target <0) 838return1;/* nothing remains for this target */ 839 840 patch =get_patch(parent, target); 841 plno = tlno =0; 842for(i =0; i < patch->num; i++) { 843struct chunk *chunk = &patch->chunks[i]; 844 845blame_chunk(sb, tlno, plno, chunk->same, target, parent); 846 plno = chunk->p_next; 847 tlno = chunk->t_next; 848} 849/* The rest (i.e. anything after tlno) are the same as the parent */ 850blame_chunk(sb, tlno, plno, last_in_target, target, parent); 851 852free_patch(patch); 853return0; 854} 855 856/* 857 * The lines in blame_entry after splitting blames many times can become 858 * very small and trivial, and at some point it becomes pointless to 859 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 860 * ordinary C program, and it is not worth to say it was copied from 861 * totally unrelated file in the parent. 862 * 863 * Compute how trivial the lines in the blame_entry are. 864 */ 865static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 866{ 867unsigned score; 868const char*cp, *ep; 869 870if(e->score) 871return e->score; 872 873 score =1; 874 cp =nth_line(sb, e->lno); 875 ep =nth_line(sb, e->lno + e->num_lines); 876while(cp < ep) { 877unsigned ch = *((unsigned char*)cp); 878if(isalnum(ch)) 879 score++; 880 cp++; 881} 882 e->score = score; 883return score; 884} 885 886/* 887 * best_so_far[] and this[] are both a split of an existing blame_entry 888 * that passes blame to the parent. Maintain best_so_far the best split 889 * so far, by comparing this and best_so_far and copying this into 890 * bst_so_far as needed. 891 */ 892static voidcopy_split_if_better(struct scoreboard *sb, 893struct blame_entry *best_so_far, 894struct blame_entry *this) 895{ 896int i; 897 898if(!this[1].suspect) 899return; 900if(best_so_far[1].suspect) { 901if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 902return; 903} 904 905for(i =0; i <3; i++) 906origin_incref(this[i].suspect); 907decref_split(best_so_far); 908memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 909} 910 911/* 912 * We are looking at a part of the final image represented by 913 * ent (tlno and same are offset by ent->s_lno). 914 * tlno is where we are looking at in the final image. 915 * up to (but not including) same match preimage. 916 * plno is where we are looking at in the preimage. 917 * 918 * <-------------- final image ----------------------> 919 * <------ent------> 920 * ^tlno ^same 921 * <---------preimage-----> 922 * ^plno 923 * 924 * All line numbers are 0-based. 925 */ 926static voidhandle_split(struct scoreboard *sb, 927struct blame_entry *ent, 928int tlno,int plno,int same, 929struct origin *parent, 930struct blame_entry *split) 931{ 932if(ent->num_lines <= tlno) 933return; 934if(tlno < same) { 935struct blame_entry this[3]; 936 tlno += ent->s_lno; 937 same += ent->s_lno; 938split_overlap(this, ent, tlno, plno, same, parent); 939copy_split_if_better(sb, split,this); 940decref_split(this); 941} 942} 943 944/* 945 * Find the lines from parent that are the same as ent so that 946 * we can pass blames to it. file_p has the blob contents for 947 * the parent. 948 */ 949static voidfind_copy_in_blob(struct scoreboard *sb, 950struct blame_entry *ent, 951struct origin *parent, 952struct blame_entry *split, 953 mmfile_t *file_p) 954{ 955const char*cp; 956int cnt; 957 mmfile_t file_o; 958struct patch *patch; 959int i, plno, tlno; 960 961/* 962 * Prepare mmfile that contains only the lines in ent. 963 */ 964 cp =nth_line(sb, ent->lno); 965 file_o.ptr = (char*) cp; 966 cnt = ent->num_lines; 967 968while(cnt && cp < sb->final_buf + sb->final_buf_size) { 969if(*cp++ =='\n') 970 cnt--; 971} 972 file_o.size = cp - file_o.ptr; 973 974 patch =compare_buffer(file_p, &file_o,1); 975 976/* 977 * file_o is a part of final image we are annotating. 978 * file_p partially may match that image. 979 */ 980memset(split,0,sizeof(struct blame_entry [3])); 981 plno = tlno =0; 982for(i =0; i < patch->num; i++) { 983struct chunk *chunk = &patch->chunks[i]; 984 985handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 986 plno = chunk->p_next; 987 tlno = chunk->t_next; 988} 989/* remainder, if any, all match the preimage */ 990handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 991free_patch(patch); 992} 993 994/* 995 * See if lines currently target is suspected for can be attributed to 996 * parent. 997 */ 998static intfind_move_in_parent(struct scoreboard *sb, 999struct origin *target,1000struct origin *parent)1001{1002int last_in_target, made_progress;1003struct blame_entry *e, split[3];1004 mmfile_t file_p;10051006 last_in_target =find_last_in_target(sb, target);1007if(last_in_target <0)1008return1;/* nothing remains for this target */10091010fill_origin_blob(parent, &file_p);1011if(!file_p.ptr)1012return0;10131014 made_progress =1;1015while(made_progress) {1016 made_progress =0;1017for(e = sb->ent; e; e = e->next) {1018if(e->guilty || !same_suspect(e->suspect, target))1019continue;1020find_copy_in_blob(sb, e, parent, split, &file_p);1021if(split[1].suspect &&1022 blame_move_score <ent_score(sb, &split[1])) {1023split_blame(sb, split, e);1024 made_progress =1;1025}1026decref_split(split);1027}1028}1029return0;1030}10311032struct blame_list {1033struct blame_entry *ent;1034struct blame_entry split[3];1035};10361037/*1038 * Count the number of entries the target is suspected for,1039 * and prepare a list of entry and the best split.1040 */1041static struct blame_list *setup_blame_list(struct scoreboard *sb,1042struct origin *target,1043int*num_ents_p)1044{1045struct blame_entry *e;1046int num_ents, i;1047struct blame_list *blame_list = NULL;10481049for(e = sb->ent, num_ents =0; e; e = e->next)1050if(!e->guilty &&same_suspect(e->suspect, target))1051 num_ents++;1052if(num_ents) {1053 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1054for(e = sb->ent, i =0; e; e = e->next)1055if(!e->guilty &&same_suspect(e->suspect, target))1056 blame_list[i++].ent = e;1057}1058*num_ents_p = num_ents;1059return blame_list;1060}10611062/*1063 * For lines target is suspected for, see if we can find code movement1064 * across file boundary from the parent commit. porigin is the path1065 * in the parent we already tried.1066 */1067static intfind_copy_in_parent(struct scoreboard *sb,1068struct origin *target,1069struct commit *parent,1070struct origin *porigin,1071int opt)1072{1073struct diff_options diff_opts;1074const char*paths[1];1075int i, j;1076int retval;1077struct blame_list *blame_list;1078int num_ents;10791080 blame_list =setup_blame_list(sb, target, &num_ents);1081if(!blame_list)1082return1;/* nothing remains for this target */10831084diff_setup(&diff_opts);1085DIFF_OPT_SET(&diff_opts, RECURSIVE);1086 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10871088 paths[0] = NULL;1089diff_tree_setup_paths(paths, &diff_opts);1090if(diff_setup_done(&diff_opts) <0)1091die("diff-setup");10921093/* Try "find copies harder" on new path if requested;1094 * we do not want to use diffcore_rename() actually to1095 * match things up; find_copies_harder is set only to1096 * force diff_tree_sha1() to feed all filepairs to diff_queue,1097 * and this code needs to be after diff_setup_done(), which1098 * usually makes find-copies-harder imply copy detection.1099 */1100if((opt & PICKAXE_BLAME_COPY_HARDEST)1101|| ((opt & PICKAXE_BLAME_COPY_HARDER)1102&& (!porigin ||strcmp(target->path, porigin->path))))1103DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);11041105if(is_null_sha1(target->commit->object.sha1))1106do_diff_cache(parent->tree->object.sha1, &diff_opts);1107else1108diff_tree_sha1(parent->tree->object.sha1,1109 target->commit->tree->object.sha1,1110"", &diff_opts);11111112if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1113diffcore_std(&diff_opts);11141115 retval =0;1116while(1) {1117int made_progress =0;11181119for(i =0; i < diff_queued_diff.nr; i++) {1120struct diff_filepair *p = diff_queued_diff.queue[i];1121struct origin *norigin;1122 mmfile_t file_p;1123struct blame_entry this[3];11241125if(!DIFF_FILE_VALID(p->one))1126continue;/* does not exist in parent */1127if(porigin && !strcmp(p->one->path, porigin->path))1128/* find_move already dealt with this path */1129continue;11301131 norigin =get_origin(sb, parent, p->one->path);1132hashcpy(norigin->blob_sha1, p->one->sha1);1133fill_origin_blob(norigin, &file_p);1134if(!file_p.ptr)1135continue;11361137for(j =0; j < num_ents; j++) {1138find_copy_in_blob(sb, blame_list[j].ent,1139 norigin,this, &file_p);1140copy_split_if_better(sb, blame_list[j].split,1141this);1142decref_split(this);1143}1144origin_decref(norigin);1145}11461147for(j =0; j < num_ents; j++) {1148struct blame_entry *split = blame_list[j].split;1149if(split[1].suspect &&1150 blame_copy_score <ent_score(sb, &split[1])) {1151split_blame(sb, split, blame_list[j].ent);1152 made_progress =1;1153}1154decref_split(split);1155}1156free(blame_list);11571158if(!made_progress)1159break;1160 blame_list =setup_blame_list(sb, target, &num_ents);1161if(!blame_list) {1162 retval =1;1163break;1164}1165}1166diff_flush(&diff_opts);1167diff_tree_release_paths(&diff_opts);1168return retval;1169}11701171/*1172 * The blobs of origin and porigin exactly match, so everything1173 * origin is suspected for can be blamed on the parent.1174 */1175static voidpass_whole_blame(struct scoreboard *sb,1176struct origin *origin,struct origin *porigin)1177{1178struct blame_entry *e;11791180if(!porigin->file.ptr && origin->file.ptr) {1181/* Steal its file */1182 porigin->file = origin->file;1183 origin->file.ptr = NULL;1184}1185for(e = sb->ent; e; e = e->next) {1186if(!same_suspect(e->suspect, origin))1187continue;1188origin_incref(porigin);1189origin_decref(e->suspect);1190 e->suspect = porigin;1191}1192}11931194#define MAXPARENT 1611951196static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1197{1198int i, pass;1199struct commit *commit = origin->commit;1200struct commit_list *parent;1201struct origin *parent_origin[MAXPARENT], *porigin;12021203memset(parent_origin,0,sizeof(parent_origin));12041205/* The first pass looks for unrenamed path to optimize for1206 * common cases, then we look for renames in the second pass.1207 */1208for(pass =0; pass <2; pass++) {1209struct origin *(*find)(struct scoreboard *,1210struct commit *,struct origin *);1211 find = pass ? find_rename : find_origin;12121213for(i =0, parent = commit->parents;1214 i < MAXPARENT && parent;1215 parent = parent->next, i++) {1216struct commit *p = parent->item;1217int j, same;12181219if(parent_origin[i])1220continue;1221if(parse_commit(p))1222continue;1223 porigin =find(sb, p, origin);1224if(!porigin)1225continue;1226if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1227pass_whole_blame(sb, origin, porigin);1228origin_decref(porigin);1229goto finish;1230}1231for(j = same =0; j < i; j++)1232if(parent_origin[j] &&1233!hashcmp(parent_origin[j]->blob_sha1,1234 porigin->blob_sha1)) {1235 same =1;1236break;1237}1238if(!same)1239 parent_origin[i] = porigin;1240else1241origin_decref(porigin);1242}1243}12441245 num_commits++;1246for(i =0, parent = commit->parents;1247 i < MAXPARENT && parent;1248 parent = parent->next, i++) {1249struct origin *porigin = parent_origin[i];1250if(!porigin)1251continue;1252if(pass_blame_to_parent(sb, origin, porigin))1253goto finish;1254}12551256/*1257 * Optionally find moves in parents' files.1258 */1259if(opt & PICKAXE_BLAME_MOVE)1260for(i =0, parent = commit->parents;1261 i < MAXPARENT && parent;1262 parent = parent->next, i++) {1263struct origin *porigin = parent_origin[i];1264if(!porigin)1265continue;1266if(find_move_in_parent(sb, origin, porigin))1267goto finish;1268}12691270/*1271 * Optionally find copies from parents' files.1272 */1273if(opt & PICKAXE_BLAME_COPY)1274for(i =0, parent = commit->parents;1275 i < MAXPARENT && parent;1276 parent = parent->next, i++) {1277struct origin *porigin = parent_origin[i];1278if(find_copy_in_parent(sb, origin, parent->item,1279 porigin, opt))1280goto finish;1281}12821283 finish:1284for(i =0; i < MAXPARENT; i++) {1285if(parent_origin[i]) {1286drop_origin_blob(parent_origin[i]);1287origin_decref(parent_origin[i]);1288}1289}1290drop_origin_blob(origin);1291}12921293/*1294 * Information on commits, used for output.1295 */1296struct commit_info1297{1298const char*author;1299const char*author_mail;1300unsigned long author_time;1301const char*author_tz;13021303/* filled only when asked for details */1304const char*committer;1305const char*committer_mail;1306unsigned long committer_time;1307const char*committer_tz;13081309const char*summary;1310};13111312/*1313 * Parse author/committer line in the commit object buffer1314 */1315static voidget_ac_line(const char*inbuf,const char*what,1316int bufsz,char*person,const char**mail,1317unsigned long*time,const char**tz)1318{1319int len, tzlen, maillen;1320char*tmp, *endp, *timepos;13211322 tmp =strstr(inbuf, what);1323if(!tmp)1324goto error_out;1325 tmp +=strlen(what);1326 endp =strchr(tmp,'\n');1327if(!endp)1328 len =strlen(tmp);1329else1330 len = endp - tmp;1331if(bufsz <= len) {1332 error_out:1333/* Ugh */1334*mail = *tz ="(unknown)";1335*time =0;1336return;1337}1338memcpy(person, tmp, len);13391340 tmp = person;1341 tmp += len;1342*tmp =0;1343while(*tmp !=' ')1344 tmp--;1345*tz = tmp+1;1346 tzlen = (person+len)-(tmp+1);13471348*tmp =0;1349while(*tmp !=' ')1350 tmp--;1351*time =strtoul(tmp, NULL,10);1352 timepos = tmp;13531354*tmp =0;1355while(*tmp !=' ')1356 tmp--;1357*mail = tmp +1;1358*tmp =0;1359 maillen = timepos - tmp;13601361if(!mailmap.nr)1362return;13631364/*1365 * mailmap expansion may make the name longer.1366 * make room by pushing stuff down.1367 */1368 tmp = person + bufsz - (tzlen +1);1369memmove(tmp, *tz, tzlen);1370 tmp[tzlen] =0;1371*tz = tmp;13721373 tmp = tmp - (maillen +1);1374memmove(tmp, *mail, maillen);1375 tmp[maillen] =0;1376*mail = tmp;13771378/*1379 * Now, convert e-mail using mailmap1380 */1381map_email(&mailmap, tmp +1, person, tmp-person-1);1382}13831384static voidget_commit_info(struct commit *commit,1385struct commit_info *ret,1386int detailed)1387{1388int len;1389char*tmp, *endp;1390static char author_buf[1024];1391static char committer_buf[1024];1392static char summary_buf[1024];13931394/*1395 * We've operated without save_commit_buffer, so1396 * we now need to populate them for output.1397 */1398if(!commit->buffer) {1399enum object_type type;1400unsigned long size;1401 commit->buffer =1402read_sha1_file(commit->object.sha1, &type, &size);1403if(!commit->buffer)1404die("Cannot read commit%s",1405sha1_to_hex(commit->object.sha1));1406}1407 ret->author = author_buf;1408get_ac_line(commit->buffer,"\nauthor ",1409sizeof(author_buf), author_buf, &ret->author_mail,1410&ret->author_time, &ret->author_tz);14111412if(!detailed)1413return;14141415 ret->committer = committer_buf;1416get_ac_line(commit->buffer,"\ncommitter ",1417sizeof(committer_buf), committer_buf, &ret->committer_mail,1418&ret->committer_time, &ret->committer_tz);14191420 ret->summary = summary_buf;1421 tmp =strstr(commit->buffer,"\n\n");1422if(!tmp) {1423 error_out:1424sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1425return;1426}1427 tmp +=2;1428 endp =strchr(tmp,'\n');1429if(!endp)1430 endp = tmp +strlen(tmp);1431 len = endp - tmp;1432if(len >=sizeof(summary_buf) || len ==0)1433goto error_out;1434memcpy(summary_buf, tmp, len);1435 summary_buf[len] =0;1436}14371438/*1439 * To allow LF and other nonportable characters in pathnames,1440 * they are c-style quoted as needed.1441 */1442static voidwrite_filename_info(const char*path)1443{1444printf("filename ");1445write_name_quoted(path, stdout,'\n');1446}14471448/*1449 * The blame_entry is found to be guilty for the range. Mark it1450 * as such, and show it in incremental output.1451 */1452static voidfound_guilty_entry(struct blame_entry *ent)1453{1454if(ent->guilty)1455return;1456 ent->guilty =1;1457if(incremental) {1458struct origin *suspect = ent->suspect;14591460printf("%s %d %d %d\n",1461sha1_to_hex(suspect->commit->object.sha1),1462 ent->s_lno +1, ent->lno +1, ent->num_lines);1463if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1464struct commit_info ci;1465 suspect->commit->object.flags |= METAINFO_SHOWN;1466get_commit_info(suspect->commit, &ci,1);1467printf("author%s\n", ci.author);1468printf("author-mail%s\n", ci.author_mail);1469printf("author-time%lu\n", ci.author_time);1470printf("author-tz%s\n", ci.author_tz);1471printf("committer%s\n", ci.committer);1472printf("committer-mail%s\n", ci.committer_mail);1473printf("committer-time%lu\n", ci.committer_time);1474printf("committer-tz%s\n", ci.committer_tz);1475printf("summary%s\n", ci.summary);1476if(suspect->commit->object.flags & UNINTERESTING)1477printf("boundary\n");1478}1479write_filename_info(suspect->path);1480maybe_flush_or_die(stdout,"stdout");1481}1482}14831484/*1485 * The main loop -- while the scoreboard has lines whose true origin1486 * is still unknown, pick one blame_entry, and allow its current1487 * suspect to pass blames to its parents.1488 */1489static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1490{1491while(1) {1492struct blame_entry *ent;1493struct commit *commit;1494struct origin *suspect = NULL;14951496/* find one suspect to break down */1497for(ent = sb->ent; !suspect && ent; ent = ent->next)1498if(!ent->guilty)1499 suspect = ent->suspect;1500if(!suspect)1501return;/* all done */15021503/*1504 * We will use this suspect later in the loop,1505 * so hold onto it in the meantime.1506 */1507origin_incref(suspect);1508 commit = suspect->commit;1509if(!commit->object.parsed)1510parse_commit(commit);1511if(!(commit->object.flags & UNINTERESTING) &&1512!(revs->max_age != -1&& commit->date < revs->max_age))1513pass_blame(sb, suspect, opt);1514else{1515 commit->object.flags |= UNINTERESTING;1516if(commit->object.parsed)1517mark_parents_uninteresting(commit);1518}1519/* treat root commit as boundary */1520if(!commit->parents && !show_root)1521 commit->object.flags |= UNINTERESTING;15221523/* Take responsibility for the remaining entries */1524for(ent = sb->ent; ent; ent = ent->next)1525if(same_suspect(ent->suspect, suspect))1526found_guilty_entry(ent);1527origin_decref(suspect);15281529if(DEBUG)/* sanity */1530sanity_check_refcnt(sb);1531}1532}15331534static const char*format_time(unsigned long time,const char*tz_str,1535int show_raw_time)1536{1537static char time_buf[128];1538time_t t = time;1539int minutes, tz;1540struct tm *tm;15411542if(show_raw_time) {1543sprintf(time_buf,"%lu%s", time, tz_str);1544return time_buf;1545}15461547 tz =atoi(tz_str);1548 minutes = tz <0? -tz : tz;1549 minutes = (minutes /100)*60+ (minutes %100);1550 minutes = tz <0? -minutes : minutes;1551 t = time + minutes *60;1552 tm =gmtime(&t);15531554strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1555strcat(time_buf, tz_str);1556return time_buf;1557}15581559#define OUTPUT_ANNOTATE_COMPAT 0011560#define OUTPUT_LONG_OBJECT_NAME 0021561#define OUTPUT_RAW_TIMESTAMP 0041562#define OUTPUT_PORCELAIN 0101563#define OUTPUT_SHOW_NAME 0201564#define OUTPUT_SHOW_NUMBER 0401565#define OUTPUT_SHOW_SCORE 01001566#define OUTPUT_NO_AUTHOR 020015671568static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1569{1570int cnt;1571const char*cp;1572struct origin *suspect = ent->suspect;1573char hex[41];15741575strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1576printf("%s%c%d %d %d\n",1577 hex,1578 ent->guilty ?' ':'*',// purely for debugging1579 ent->s_lno +1,1580 ent->lno +1,1581 ent->num_lines);1582if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1583struct commit_info ci;1584 suspect->commit->object.flags |= METAINFO_SHOWN;1585get_commit_info(suspect->commit, &ci,1);1586printf("author%s\n", ci.author);1587printf("author-mail%s\n", ci.author_mail);1588printf("author-time%lu\n", ci.author_time);1589printf("author-tz%s\n", ci.author_tz);1590printf("committer%s\n", ci.committer);1591printf("committer-mail%s\n", ci.committer_mail);1592printf("committer-time%lu\n", ci.committer_time);1593printf("committer-tz%s\n", ci.committer_tz);1594write_filename_info(suspect->path);1595printf("summary%s\n", ci.summary);1596if(suspect->commit->object.flags & UNINTERESTING)1597printf("boundary\n");1598}1599else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1600write_filename_info(suspect->path);16011602 cp =nth_line(sb, ent->lno);1603for(cnt =0; cnt < ent->num_lines; cnt++) {1604char ch;1605if(cnt)1606printf("%s %d %d\n", hex,1607 ent->s_lno +1+ cnt,1608 ent->lno +1+ cnt);1609putchar('\t');1610do{1611 ch = *cp++;1612putchar(ch);1613}while(ch !='\n'&&1614 cp < sb->final_buf + sb->final_buf_size);1615}1616}16171618static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1619{1620int cnt;1621const char*cp;1622struct origin *suspect = ent->suspect;1623struct commit_info ci;1624char hex[41];1625int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16261627get_commit_info(suspect->commit, &ci,1);1628strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16291630 cp =nth_line(sb, ent->lno);1631for(cnt =0; cnt < ent->num_lines; cnt++) {1632char ch;1633int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16341635if(suspect->commit->object.flags & UNINTERESTING) {1636if(blank_boundary)1637memset(hex,' ', length);1638else if(!cmd_is_annotate) {1639 length--;1640putchar('^');1641}1642}16431644printf("%.*s", length, hex);1645if(opt & OUTPUT_ANNOTATE_COMPAT)1646printf("\t(%10s\t%10s\t%d)", ci.author,1647format_time(ci.author_time, ci.author_tz,1648 show_raw_time),1649 ent->lno +1+ cnt);1650else{1651if(opt & OUTPUT_SHOW_SCORE)1652printf(" %*d%02d",1653 max_score_digits, ent->score,1654 ent->suspect->refcnt);1655if(opt & OUTPUT_SHOW_NAME)1656printf(" %-*.*s", longest_file, longest_file,1657 suspect->path);1658if(opt & OUTPUT_SHOW_NUMBER)1659printf(" %*d", max_orig_digits,1660 ent->s_lno +1+ cnt);16611662if(!(opt & OUTPUT_NO_AUTHOR))1663printf(" (%-*.*s%10s",1664 longest_author, longest_author,1665 ci.author,1666format_time(ci.author_time,1667 ci.author_tz,1668 show_raw_time));1669printf(" %*d) ",1670 max_digits, ent->lno +1+ cnt);1671}1672do{1673 ch = *cp++;1674putchar(ch);1675}while(ch !='\n'&&1676 cp < sb->final_buf + sb->final_buf_size);1677}1678}16791680static voidoutput(struct scoreboard *sb,int option)1681{1682struct blame_entry *ent;16831684if(option & OUTPUT_PORCELAIN) {1685for(ent = sb->ent; ent; ent = ent->next) {1686struct blame_entry *oth;1687struct origin *suspect = ent->suspect;1688struct commit *commit = suspect->commit;1689if(commit->object.flags & MORE_THAN_ONE_PATH)1690continue;1691for(oth = ent->next; oth; oth = oth->next) {1692if((oth->suspect->commit != commit) ||1693!strcmp(oth->suspect->path, suspect->path))1694continue;1695 commit->object.flags |= MORE_THAN_ONE_PATH;1696break;1697}1698}1699}17001701for(ent = sb->ent; ent; ent = ent->next) {1702if(option & OUTPUT_PORCELAIN)1703emit_porcelain(sb, ent);1704else{1705emit_other(sb, ent, option);1706}1707}1708}17091710/*1711 * To allow quick access to the contents of nth line in the1712 * final image, prepare an index in the scoreboard.1713 */1714static intprepare_lines(struct scoreboard *sb)1715{1716const char*buf = sb->final_buf;1717unsigned long len = sb->final_buf_size;1718int num =0, incomplete =0, bol =1;17191720if(len && buf[len-1] !='\n')1721 incomplete++;/* incomplete line at the end */1722while(len--) {1723if(bol) {1724 sb->lineno =xrealloc(sb->lineno,1725sizeof(int* ) * (num +1));1726 sb->lineno[num] = buf - sb->final_buf;1727 bol =0;1728}1729if(*buf++ =='\n') {1730 num++;1731 bol =1;1732}1733}1734 sb->lineno =xrealloc(sb->lineno,1735sizeof(int* ) * (num + incomplete +1));1736 sb->lineno[num + incomplete] = buf - sb->final_buf;1737 sb->num_lines = num + incomplete;1738return sb->num_lines;1739}17401741/*1742 * Add phony grafts for use with -S; this is primarily to1743 * support git-cvsserver that wants to give a linear history1744 * to its clients.1745 */1746static intread_ancestry(const char*graft_file)1747{1748FILE*fp =fopen(graft_file,"r");1749char buf[1024];1750if(!fp)1751return-1;1752while(fgets(buf,sizeof(buf), fp)) {1753/* The format is just "Commit Parent1 Parent2 ...\n" */1754int len =strlen(buf);1755struct commit_graft *graft =read_graft_line(buf, len);1756if(graft)1757register_commit_graft(graft,0);1758}1759fclose(fp);1760return0;1761}17621763/*1764 * How many columns do we need to show line numbers in decimal?1765 */1766static intlineno_width(int lines)1767{1768int i, width;17691770for(width =1, i =10; i <= lines +1; width++)1771 i *=10;1772return width;1773}17741775/*1776 * How many columns do we need to show line numbers, authors,1777 * and filenames?1778 */1779static voidfind_alignment(struct scoreboard *sb,int*option)1780{1781int longest_src_lines =0;1782int longest_dst_lines =0;1783unsigned largest_score =0;1784struct blame_entry *e;17851786for(e = sb->ent; e; e = e->next) {1787struct origin *suspect = e->suspect;1788struct commit_info ci;1789int num;17901791if(strcmp(suspect->path, sb->path))1792*option |= OUTPUT_SHOW_NAME;1793 num =strlen(suspect->path);1794if(longest_file < num)1795 longest_file = num;1796if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1797 suspect->commit->object.flags |= METAINFO_SHOWN;1798get_commit_info(suspect->commit, &ci,1);1799 num =strlen(ci.author);1800if(longest_author < num)1801 longest_author = num;1802}1803 num = e->s_lno + e->num_lines;1804if(longest_src_lines < num)1805 longest_src_lines = num;1806 num = e->lno + e->num_lines;1807if(longest_dst_lines < num)1808 longest_dst_lines = num;1809if(largest_score <ent_score(sb, e))1810 largest_score =ent_score(sb, e);1811}1812 max_orig_digits =lineno_width(longest_src_lines);1813 max_digits =lineno_width(longest_dst_lines);1814 max_score_digits =lineno_width(largest_score);1815}18161817/*1818 * For debugging -- origin is refcounted, and this asserts that1819 * we do not underflow.1820 */1821static voidsanity_check_refcnt(struct scoreboard *sb)1822{1823int baa =0;1824struct blame_entry *ent;18251826for(ent = sb->ent; ent; ent = ent->next) {1827/* Nobody should have zero or negative refcnt */1828if(ent->suspect->refcnt <=0) {1829fprintf(stderr,"%sin%shas negative refcnt%d\n",1830 ent->suspect->path,1831sha1_to_hex(ent->suspect->commit->object.sha1),1832 ent->suspect->refcnt);1833 baa =1;1834}1835}1836for(ent = sb->ent; ent; ent = ent->next) {1837/* Mark the ones that haven't been checked */1838if(0< ent->suspect->refcnt)1839 ent->suspect->refcnt = -ent->suspect->refcnt;1840}1841for(ent = sb->ent; ent; ent = ent->next) {1842/*1843 * ... then pick each and see if they have the the1844 * correct refcnt.1845 */1846int found;1847struct blame_entry *e;1848struct origin *suspect = ent->suspect;18491850if(0< suspect->refcnt)1851continue;1852 suspect->refcnt = -suspect->refcnt;/* Unmark */1853for(found =0, e = sb->ent; e; e = e->next) {1854if(e->suspect != suspect)1855continue;1856 found++;1857}1858if(suspect->refcnt != found) {1859fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1860 ent->suspect->path,1861sha1_to_hex(ent->suspect->commit->object.sha1),1862 ent->suspect->refcnt, found);1863 baa =2;1864}1865}1866if(baa) {1867int opt =0160;1868find_alignment(sb, &opt);1869output(sb, opt);1870die("Baa%d!", baa);1871}1872}18731874/*1875 * Used for the command line parsing; check if the path exists1876 * in the working tree.1877 */1878static inthas_path_in_work_tree(const char*path)1879{1880struct stat st;1881return!lstat(path, &st);1882}18831884static unsignedparse_score(const char*arg)1885{1886char*end;1887unsigned long score =strtoul(arg, &end,10);1888if(*end)1889return0;1890return score;1891}18921893static const char*add_prefix(const char*prefix,const char*path)1894{1895returnprefix_path(prefix, prefix ?strlen(prefix) :0, path);1896}18971898/*1899 * Parsing of (comma separated) one item in the -L option1900 */1901static const char*parse_loc(const char*spec,1902struct scoreboard *sb,long lno,1903long begin,long*ret)1904{1905char*term;1906const char*line;1907long num;1908int reg_error;1909 regex_t regexp;1910 regmatch_t match[1];19111912/* Allow "-L <something>,+20" to mean starting at <something>1913 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1914 * <something>.1915 */1916if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1917 num =strtol(spec +1, &term,10);1918if(term != spec +1) {1919if(spec[0] =='-')1920 num =0- num;1921if(0< num)1922*ret = begin + num -2;1923else if(!num)1924*ret = begin;1925else1926*ret = begin + num;1927return term;1928}1929return spec;1930}1931 num =strtol(spec, &term,10);1932if(term != spec) {1933*ret = num;1934return term;1935}1936if(spec[0] !='/')1937return spec;19381939/* it could be a regexp of form /.../ */1940for(term = (char*) spec +1; *term && *term !='/'; term++) {1941if(*term =='\\')1942 term++;1943}1944if(*term !='/')1945return spec;19461947/* try [spec+1 .. term-1] as regexp */1948*term =0;1949 begin--;/* input is in human terms */1950 line =nth_line(sb, begin);19511952if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1953!(reg_error =regexec(®exp, line,1, match,0))) {1954const char*cp = line + match[0].rm_so;1955const char*nline;19561957while(begin++ < lno) {1958 nline =nth_line(sb, begin);1959if(line <= cp && cp < nline)1960break;1961 line = nline;1962}1963*ret = begin;1964regfree(®exp);1965*term++ ='/';1966return term;1967}1968else{1969char errbuf[1024];1970regerror(reg_error, ®exp, errbuf,1024);1971die("-L parameter '%s':%s", spec +1, errbuf);1972}1973}19741975/*1976 * Parsing of -L option1977 */1978static voidprepare_blame_range(struct scoreboard *sb,1979const char*bottomtop,1980long lno,1981long*bottom,long*top)1982{1983const char*term;19841985 term =parse_loc(bottomtop, sb, lno,1, bottom);1986if(*term ==',') {1987 term =parse_loc(term +1, sb, lno, *bottom +1, top);1988if(*term)1989usage(blame_usage);1990}1991if(*term)1992usage(blame_usage);1993}19941995static intgit_blame_config(const char*var,const char*value)1996{1997if(!strcmp(var,"blame.showroot")) {1998 show_root =git_config_bool(var, value);1999return0;2000}2001if(!strcmp(var,"blame.blankboundary")) {2002 blank_boundary =git_config_bool(var, value);2003return0;2004}2005returngit_default_config(var, value);2006}20072008/*2009 * Prepare a dummy commit that represents the work tree (or staged) item.2010 * Note that annotating work tree item never works in the reverse.2011 */2012static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2013{2014struct commit *commit;2015struct origin *origin;2016unsigned char head_sha1[20];2017struct strbuf buf;2018const char*ident;2019time_t now;2020int size, len;2021struct cache_entry *ce;2022unsigned mode;20232024if(get_sha1("HEAD", head_sha1))2025die("No such ref: HEAD");20262027time(&now);2028 commit =xcalloc(1,sizeof(*commit));2029 commit->parents =xcalloc(1,sizeof(*commit->parents));2030 commit->parents->item =lookup_commit_reference(head_sha1);2031 commit->object.parsed =1;2032 commit->date = now;2033 commit->object.type = OBJ_COMMIT;20342035 origin =make_origin(commit, path);20362037strbuf_init(&buf,0);2038if(!contents_from ||strcmp("-", contents_from)) {2039struct stat st;2040const char*read_from;2041unsigned long fin_size;20422043if(contents_from) {2044if(stat(contents_from, &st) <0)2045die("Cannot stat%s", contents_from);2046 read_from = contents_from;2047}2048else{2049if(lstat(path, &st) <0)2050die("Cannot lstat%s", path);2051 read_from = path;2052}2053 fin_size =xsize_t(st.st_size);2054 mode =canon_mode(st.st_mode);2055switch(st.st_mode & S_IFMT) {2056case S_IFREG:2057if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2058die("cannot open or read%s", read_from);2059break;2060case S_IFLNK:2061if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2062die("cannot readlink%s", read_from);2063 buf.len = fin_size;2064break;2065default:2066die("unsupported file type%s", read_from);2067}2068}2069else{2070/* Reading from stdin */2071 contents_from ="standard input";2072 mode =0;2073if(strbuf_read(&buf,0,0) <0)2074die("read error%sfrom stdin",strerror(errno));2075}2076convert_to_git(path, buf.buf, buf.len, &buf,0);2077 origin->file.ptr = buf.buf;2078 origin->file.size = buf.len;2079pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2080 commit->util = origin;20812082/*2083 * Read the current index, replace the path entry with2084 * origin->blob_sha1 without mucking with its mode or type2085 * bits; we are not going to write this index out -- we just2086 * want to run "diff-index --cached".2087 */2088discard_cache();2089read_cache();20902091 len =strlen(path);2092if(!mode) {2093int pos =cache_name_pos(path, len);2094if(0<= pos)2095 mode = active_cache[pos]->ce_mode;2096else2097/* Let's not bother reading from HEAD tree */2098 mode = S_IFREG |0644;2099}2100 size =cache_entry_size(len);2101 ce =xcalloc(1, size);2102hashcpy(ce->sha1, origin->blob_sha1);2103memcpy(ce->name, path, len);2104 ce->ce_flags =create_ce_flags(len,0);2105 ce->ce_mode =create_ce_mode(mode);2106add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);21072108/*2109 * We are not going to write this out, so this does not matter2110 * right now, but someday we might optimize diff-index --cached2111 * with cache-tree information.2112 */2113cache_tree_invalidate_path(active_cache_tree, path);21142115 commit->buffer =xmalloc(400);2116 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2117snprintf(commit->buffer,400,2118"tree 0000000000000000000000000000000000000000\n"2119"parent%s\n"2120"author%s\n"2121"committer%s\n\n"2122"Version of%sfrom%s\n",2123sha1_to_hex(head_sha1),2124 ident, ident, path, contents_from ? contents_from : path);2125return commit;2126}21272128static const char*prepare_final(struct scoreboard *sb,struct rev_info *revs)2129{2130int i;2131const char*final_commit_name = NULL;21322133/*2134 * There must be one and only one positive commit in the2135 * revs->pending array.2136 */2137for(i =0; i < revs->pending.nr; i++) {2138struct object *obj = revs->pending.objects[i].item;2139if(obj->flags & UNINTERESTING)2140continue;2141while(obj->type == OBJ_TAG)2142 obj =deref_tag(obj, NULL,0);2143if(obj->type != OBJ_COMMIT)2144die("Non commit%s?", revs->pending.objects[i].name);2145if(sb->final)2146die("More than one commit to dig from%sand%s?",2147 revs->pending.objects[i].name,2148 final_commit_name);2149 sb->final = (struct commit *) obj;2150 final_commit_name = revs->pending.objects[i].name;2151}2152return final_commit_name;2153}21542155intcmd_blame(int argc,const char**argv,const char*prefix)2156{2157struct rev_info revs;2158const char*path;2159struct scoreboard sb;2160struct origin *o;2161struct blame_entry *ent;2162int i, seen_dashdash, unk, opt;2163long bottom, top, lno;2164int output_option =0;2165int show_stats =0;2166const char*revs_file = NULL;2167const char*final_commit_name = NULL;2168enum object_type type;2169const char*bottomtop = NULL;2170const char*contents_from = NULL;21712172 cmd_is_annotate = !strcmp(argv[0],"annotate");21732174git_config(git_blame_config);2175 save_commit_buffer =0;21762177 opt =0;2178 seen_dashdash =0;2179for(unk = i =1; i < argc; i++) {2180const char*arg = argv[i];2181if(*arg !='-')2182break;2183else if(!strcmp("-b", arg))2184 blank_boundary =1;2185else if(!strcmp("--root", arg))2186 show_root =1;2187else if(!strcmp(arg,"--show-stats"))2188 show_stats =1;2189else if(!strcmp("-c", arg))2190 output_option |= OUTPUT_ANNOTATE_COMPAT;2191else if(!strcmp("-t", arg))2192 output_option |= OUTPUT_RAW_TIMESTAMP;2193else if(!strcmp("-l", arg))2194 output_option |= OUTPUT_LONG_OBJECT_NAME;2195else if(!strcmp("-s", arg))2196 output_option |= OUTPUT_NO_AUTHOR;2197else if(!strcmp("-w", arg))2198 xdl_opts |= XDF_IGNORE_WHITESPACE;2199else if(!strcmp("-S", arg) && ++i < argc)2200 revs_file = argv[i];2201else if(!prefixcmp(arg,"-M")) {2202 opt |= PICKAXE_BLAME_MOVE;2203 blame_move_score =parse_score(arg+2);2204}2205else if(!prefixcmp(arg,"-C")) {2206/*2207 * -C enables copy from removed files;2208 * -C -C enables copy from existing files, but only2209 * when blaming a new file;2210 * -C -C -C enables copy from existing files for2211 * everybody2212 */2213if(opt & PICKAXE_BLAME_COPY_HARDER)2214 opt |= PICKAXE_BLAME_COPY_HARDEST;2215if(opt & PICKAXE_BLAME_COPY)2216 opt |= PICKAXE_BLAME_COPY_HARDER;2217 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2218 blame_copy_score =parse_score(arg+2);2219}2220else if(!prefixcmp(arg,"-L")) {2221if(!arg[2]) {2222if(++i >= argc)2223usage(blame_usage);2224 arg = argv[i];2225}2226else2227 arg +=2;2228if(bottomtop)2229die("More than one '-L n,m' option given");2230 bottomtop = arg;2231}2232else if(!strcmp("--contents", arg)) {2233if(++i >= argc)2234usage(blame_usage);2235 contents_from = argv[i];2236}2237else if(!strcmp("--incremental", arg))2238 incremental =1;2239else if(!strcmp("--score-debug", arg))2240 output_option |= OUTPUT_SHOW_SCORE;2241else if(!strcmp("-f", arg) ||2242!strcmp("--show-name", arg))2243 output_option |= OUTPUT_SHOW_NAME;2244else if(!strcmp("-n", arg) ||2245!strcmp("--show-number", arg))2246 output_option |= OUTPUT_SHOW_NUMBER;2247else if(!strcmp("-p", arg) ||2248!strcmp("--porcelain", arg))2249 output_option |= OUTPUT_PORCELAIN;2250else if(!strcmp("--", arg)) {2251 seen_dashdash =1;2252 i++;2253break;2254}2255else2256 argv[unk++] = arg;2257}22582259if(!blame_move_score)2260 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2261if(!blame_copy_score)2262 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22632264/*2265 * We have collected options unknown to us in argv[1..unk]2266 * which are to be passed to revision machinery if we are2267 * going to do the "bottom" processing.2268 *2269 * The remaining are:2270 *2271 * (1) if seen_dashdash, its either2272 * "-options -- <path>" or2273 * "-options -- <path> <rev>".2274 * but the latter is allowed only if there is no2275 * options that we passed to revision machinery.2276 *2277 * (2) otherwise, we may have "--" somewhere later and2278 * might be looking at the first one of multiple 'rev'2279 * parameters (e.g. " master ^next ^maint -- path").2280 * See if there is a dashdash first, and give the2281 * arguments before that to revision machinery.2282 * After that there must be one 'path'.2283 *2284 * (3) otherwise, its one of the three:2285 * "-options <path> <rev>"2286 * "-options <rev> <path>"2287 * "-options <path>"2288 * but again the first one is allowed only if2289 * there is no options that we passed to revision2290 * machinery.2291 */22922293if(seen_dashdash) {2294/* (1) */2295if(argc <= i)2296usage(blame_usage);2297 path =add_prefix(prefix, argv[i]);2298if(i +1== argc -1) {2299if(unk !=1)2300usage(blame_usage);2301 argv[unk++] = argv[i +1];2302}2303else if(i +1!= argc)2304/* garbage at end */2305usage(blame_usage);2306}2307else{2308int j;2309for(j = i; !seen_dashdash && j < argc; j++)2310if(!strcmp(argv[j],"--"))2311 seen_dashdash = j;2312if(seen_dashdash) {2313/* (2) */2314if(seen_dashdash +1!= argc -1)2315usage(blame_usage);2316 path =add_prefix(prefix, argv[seen_dashdash +1]);2317for(j = i; j < seen_dashdash; j++)2318 argv[unk++] = argv[j];2319}2320else{2321/* (3) */2322if(argc <= i)2323usage(blame_usage);2324 path =add_prefix(prefix, argv[i]);2325if(i +1== argc -1) {2326 final_commit_name = argv[i +1];23272328/* if (unk == 1) we could be getting2329 * old-style2330 */2331if(unk ==1&& !has_path_in_work_tree(path)) {2332 path =add_prefix(prefix, argv[i +1]);2333 final_commit_name = argv[i];2334}2335}2336else if(i != argc -1)2337usage(blame_usage);/* garbage at end */23382339setup_work_tree();2340if(!has_path_in_work_tree(path))2341die("cannot stat path%s:%s",2342 path,strerror(errno));2343}2344}23452346if(final_commit_name)2347 argv[unk++] = final_commit_name;23482349/*2350 * Now we got rev and path. We do not want the path pruning2351 * but we may want "bottom" processing.2352 */2353 argv[unk++] ="--";/* terminate the rev name */2354 argv[unk] = NULL;23552356init_revisions(&revs, NULL);2357setup_revisions(unk, argv, &revs, NULL);2358memset(&sb,0,sizeof(sb));23592360 final_commit_name =prepare_final(&sb, &revs);2361if(!sb.final) {2362/*2363 * "--not A B -- path" without anything positive;2364 * do not default to HEAD, but use the working tree2365 * or "--contents".2366 */2367setup_work_tree();2368 sb.final =fake_working_tree_commit(path, contents_from);2369add_pending_object(&revs, &(sb.final->object),":");2370}2371else if(contents_from)2372die("Cannot use --contents with final commit object name");23732374/*2375 * If we have bottom, this will mark the ancestors of the2376 * bottom commits we would reach while traversing as2377 * uninteresting.2378 */2379if(prepare_revision_walk(&revs))2380die("revision walk setup failed");23812382if(is_null_sha1(sb.final->object.sha1)) {2383char*buf;2384 o = sb.final->util;2385 buf =xmalloc(o->file.size +1);2386memcpy(buf, o->file.ptr, o->file.size +1);2387 sb.final_buf = buf;2388 sb.final_buf_size = o->file.size;2389}2390else{2391 o =get_origin(&sb, sb.final, path);2392if(fill_blob_sha1(o))2393die("no such path%sin%s", path, final_commit_name);23942395 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2396&sb.final_buf_size);2397if(!sb.final_buf)2398die("Cannot read blob%sfor path%s",2399sha1_to_hex(o->blob_sha1),2400 path);2401}2402 num_read_blob++;2403 lno =prepare_lines(&sb);24042405 bottom = top =0;2406if(bottomtop)2407prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2408if(bottom && top && top < bottom) {2409long tmp;2410 tmp = top; top = bottom; bottom = tmp;2411}2412if(bottom <1)2413 bottom =1;2414if(top <1)2415 top = lno;2416 bottom--;2417if(lno < top)2418die("file%shas only%lu lines", path, lno);24192420 ent =xcalloc(1,sizeof(*ent));2421 ent->lno = bottom;2422 ent->num_lines = top - bottom;2423 ent->suspect = o;2424 ent->s_lno = bottom;24252426 sb.ent = ent;2427 sb.path = path;24282429if(revs_file &&read_ancestry(revs_file))2430die("reading graft file%sfailed:%s",2431 revs_file,strerror(errno));24322433read_mailmap(&mailmap,".mailmap", NULL);24342435if(!incremental)2436setup_pager();24372438assign_blame(&sb, &revs, opt);24392440if(incremental)2441return0;24422443coalesce(&sb);24442445if(!(output_option & OUTPUT_PORCELAIN))2446find_alignment(&sb, &output_option);24472448output(&sb, output_option);2449free((void*)sb.final_buf);2450for(ent = sb.ent; ent; ) {2451struct blame_entry *e = ent->next;2452free(ent);2453 ent = e;2454}24552456if(show_stats) {2457printf("num read blob:%d\n", num_read_blob);2458printf("num get patch:%d\n", num_get_patch);2459printf("num commits:%d\n", num_commits);2460}2461return0;2462}