1/* 2 * Pickaxe 3 * 4 * Copyright (c) 2006, Junio C Hamano 5 */ 6 7#include"cache.h" 8#include"builtin.h" 9#include"blob.h" 10#include"commit.h" 11#include"tag.h" 12#include"tree-walk.h" 13#include"diff.h" 14#include"diffcore.h" 15#include"revision.h" 16#include"quote.h" 17#include"xdiff-interface.h" 18#include"cache-tree.h" 19#include"path-list.h" 20#include"mailmap.h" 21 22static char blame_usage[] = 23"git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n" 24" -c Use the same output mode as git-annotate (Default: off)\n" 25" -b Show blank SHA-1 for boundary commits (Default: off)\n" 26" -l Show long commit SHA1 (Default: off)\n" 27" --root Do not treat root commits as boundaries (Default: off)\n" 28" -t Show raw timestamp (Default: off)\n" 29" -f, --show-name Show original filename (Default: auto)\n" 30" -n, --show-number Show original linenumber (Default: off)\n" 31" -s Suppress author name and timestamp (Default: off)\n" 32" -p, --porcelain Show in a format designed for machine consumption\n" 33" -w Ignore whitespace differences\n" 34" -L n,m Process only line range n,m, counting from 1\n" 35" -M, -C Find line movements within and across files\n" 36" --incremental Show blame entries as we find them, incrementally\n" 37" --contents file Use <file>'s contents as the final image\n" 38" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"; 39 40static int longest_file; 41static int longest_author; 42static int max_orig_digits; 43static int max_digits; 44static int max_score_digits; 45static int show_root; 46static int blank_boundary; 47static int incremental; 48static int cmd_is_annotate; 49static int xdl_opts = XDF_NEED_MINIMAL; 50static struct path_list mailmap; 51 52#ifndef DEBUG 53#define DEBUG 0 54#endif 55 56/* stats */ 57static int num_read_blob; 58static int num_get_patch; 59static int num_commits; 60 61#define PICKAXE_BLAME_MOVE 01 62#define PICKAXE_BLAME_COPY 02 63#define PICKAXE_BLAME_COPY_HARDER 04 64#define PICKAXE_BLAME_COPY_HARDEST 010 65 66/* 67 * blame for a blame_entry with score lower than these thresholds 68 * is not passed to the parent using move/copy logic. 69 */ 70static unsigned blame_move_score; 71static unsigned blame_copy_score; 72#define BLAME_DEFAULT_MOVE_SCORE 20 73#define BLAME_DEFAULT_COPY_SCORE 40 74 75/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ 76#define METAINFO_SHOWN (1u<<12) 77#define MORE_THAN_ONE_PATH (1u<<13) 78 79/* 80 * One blob in a commit that is being suspected 81 */ 82struct origin { 83int refcnt; 84struct commit *commit; 85 mmfile_t file; 86unsigned char blob_sha1[20]; 87char path[FLEX_ARRAY]; 88}; 89 90/* 91 * Given an origin, prepare mmfile_t structure to be used by the 92 * diff machinery 93 */ 94static char*fill_origin_blob(struct origin *o, mmfile_t *file) 95{ 96if(!o->file.ptr) { 97enum object_type type; 98 num_read_blob++; 99 file->ptr =read_sha1_file(o->blob_sha1, &type, 100(unsigned long*)(&(file->size))); 101if(!file->ptr) 102die("Cannot read blob%sfor path%s", 103sha1_to_hex(o->blob_sha1), 104 o->path); 105 o->file = *file; 106} 107else 108*file = o->file; 109return file->ptr; 110} 111 112/* 113 * Origin is refcounted and usually we keep the blob contents to be 114 * reused. 115 */ 116staticinlinestruct origin *origin_incref(struct origin *o) 117{ 118if(o) 119 o->refcnt++; 120return o; 121} 122 123static voidorigin_decref(struct origin *o) 124{ 125if(o && --o->refcnt <=0) { 126if(o->file.ptr) 127free(o->file.ptr); 128memset(o,0,sizeof(*o)); 129free(o); 130} 131} 132 133static voiddrop_origin_blob(struct origin *o) 134{ 135if(o->file.ptr) { 136free(o->file.ptr); 137 o->file.ptr = NULL; 138} 139} 140 141/* 142 * Each group of lines is described by a blame_entry; it can be split 143 * as we pass blame to the parents. They form a linked list in the 144 * scoreboard structure, sorted by the target line number. 145 */ 146struct blame_entry { 147struct blame_entry *prev; 148struct blame_entry *next; 149 150/* the first line of this group in the final image; 151 * internally all line numbers are 0 based. 152 */ 153int lno; 154 155/* how many lines this group has */ 156int num_lines; 157 158/* the commit that introduced this group into the final image */ 159struct origin *suspect; 160 161/* true if the suspect is truly guilty; false while we have not 162 * checked if the group came from one of its parents. 163 */ 164char guilty; 165 166/* the line number of the first line of this group in the 167 * suspect's file; internally all line numbers are 0 based. 168 */ 169int s_lno; 170 171/* how significant this entry is -- cached to avoid 172 * scanning the lines over and over. 173 */ 174unsigned score; 175}; 176 177/* 178 * The current state of the blame assignment. 179 */ 180struct scoreboard { 181/* the final commit (i.e. where we started digging from) */ 182struct commit *final; 183 184const char*path; 185 186/* 187 * The contents in the final image. 188 * Used by many functions to obtain contents of the nth line, 189 * indexed with scoreboard.lineno[blame_entry.lno]. 190 */ 191const char*final_buf; 192unsigned long final_buf_size; 193 194/* linked list of blames */ 195struct blame_entry *ent; 196 197/* look-up a line in the final buffer */ 198int num_lines; 199int*lineno; 200}; 201 202staticinlineintsame_suspect(struct origin *a,struct origin *b) 203{ 204if(a == b) 205return1; 206if(a->commit != b->commit) 207return0; 208return!strcmp(a->path, b->path); 209} 210 211static voidsanity_check_refcnt(struct scoreboard *); 212 213/* 214 * If two blame entries that are next to each other came from 215 * contiguous lines in the same origin (i.e. <commit, path> pair), 216 * merge them together. 217 */ 218static voidcoalesce(struct scoreboard *sb) 219{ 220struct blame_entry *ent, *next; 221 222for(ent = sb->ent; ent && (next = ent->next); ent = next) { 223if(same_suspect(ent->suspect, next->suspect) && 224 ent->guilty == next->guilty && 225 ent->s_lno + ent->num_lines == next->s_lno) { 226 ent->num_lines += next->num_lines; 227 ent->next = next->next; 228if(ent->next) 229 ent->next->prev = ent; 230origin_decref(next->suspect); 231free(next); 232 ent->score =0; 233 next = ent;/* again */ 234} 235} 236 237if(DEBUG)/* sanity */ 238sanity_check_refcnt(sb); 239} 240 241/* 242 * Given a commit and a path in it, create a new origin structure. 243 * The callers that add blame to the scoreboard should use 244 * get_origin() to obtain shared, refcounted copy instead of calling 245 * this function directly. 246 */ 247static struct origin *make_origin(struct commit *commit,const char*path) 248{ 249struct origin *o; 250 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 251 o->commit = commit; 252 o->refcnt =1; 253strcpy(o->path, path); 254return o; 255} 256 257/* 258 * Locate an existing origin or create a new one. 259 */ 260static struct origin *get_origin(struct scoreboard *sb, 261struct commit *commit, 262const char*path) 263{ 264struct blame_entry *e; 265 266for(e = sb->ent; e; e = e->next) { 267if(e->suspect->commit == commit && 268!strcmp(e->suspect->path, path)) 269returnorigin_incref(e->suspect); 270} 271returnmake_origin(commit, path); 272} 273 274/* 275 * Fill the blob_sha1 field of an origin if it hasn't, so that later 276 * call to fill_origin_blob() can use it to locate the data. blob_sha1 277 * for an origin is also used to pass the blame for the entire file to 278 * the parent to detect the case where a child's blob is identical to 279 * that of its parent's. 280 */ 281static intfill_blob_sha1(struct origin *origin) 282{ 283unsigned mode; 284 285if(!is_null_sha1(origin->blob_sha1)) 286return0; 287if(get_tree_entry(origin->commit->object.sha1, 288 origin->path, 289 origin->blob_sha1, &mode)) 290goto error_out; 291if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 292goto error_out; 293return0; 294 error_out: 295hashclr(origin->blob_sha1); 296return-1; 297} 298 299/* 300 * We have an origin -- check if the same path exists in the 301 * parent and return an origin structure to represent it. 302 */ 303static struct origin *find_origin(struct scoreboard *sb, 304struct commit *parent, 305struct origin *origin) 306{ 307struct origin *porigin = NULL; 308struct diff_options diff_opts; 309const char*paths[2]; 310 311if(parent->util) { 312/* 313 * Each commit object can cache one origin in that 314 * commit. This is a freestanding copy of origin and 315 * not refcounted. 316 */ 317struct origin *cached = parent->util; 318if(!strcmp(cached->path, origin->path)) { 319/* 320 * The same path between origin and its parent 321 * without renaming -- the most common case. 322 */ 323 porigin =get_origin(sb, parent, cached->path); 324 325/* 326 * If the origin was newly created (i.e. get_origin 327 * would call make_origin if none is found in the 328 * scoreboard), it does not know the blob_sha1, 329 * so copy it. Otherwise porigin was in the 330 * scoreboard and already knows blob_sha1. 331 */ 332if(porigin->refcnt ==1) 333hashcpy(porigin->blob_sha1, cached->blob_sha1); 334return porigin; 335} 336/* otherwise it was not very useful; free it */ 337free(parent->util); 338 parent->util = NULL; 339} 340 341/* See if the origin->path is different between parent 342 * and origin first. Most of the time they are the 343 * same and diff-tree is fairly efficient about this. 344 */ 345diff_setup(&diff_opts); 346DIFF_OPT_SET(&diff_opts, RECURSIVE); 347 diff_opts.detect_rename =0; 348 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 349 paths[0] = origin->path; 350 paths[1] = NULL; 351 352diff_tree_setup_paths(paths, &diff_opts); 353if(diff_setup_done(&diff_opts) <0) 354die("diff-setup"); 355 356if(is_null_sha1(origin->commit->object.sha1)) 357do_diff_cache(parent->tree->object.sha1, &diff_opts); 358else 359diff_tree_sha1(parent->tree->object.sha1, 360 origin->commit->tree->object.sha1, 361"", &diff_opts); 362diffcore_std(&diff_opts); 363 364/* It is either one entry that says "modified", or "created", 365 * or nothing. 366 */ 367if(!diff_queued_diff.nr) { 368/* The path is the same as parent */ 369 porigin =get_origin(sb, parent, origin->path); 370hashcpy(porigin->blob_sha1, origin->blob_sha1); 371} 372else if(diff_queued_diff.nr !=1) 373die("internal error in blame::find_origin"); 374else{ 375struct diff_filepair *p = diff_queued_diff.queue[0]; 376switch(p->status) { 377default: 378die("internal error in blame::find_origin (%c)", 379 p->status); 380case'M': 381 porigin =get_origin(sb, parent, origin->path); 382hashcpy(porigin->blob_sha1, p->one->sha1); 383break; 384case'A': 385case'T': 386/* Did not exist in parent, or type changed */ 387break; 388} 389} 390diff_flush(&diff_opts); 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); 447return porigin; 448} 449 450/* 451 * Parsing of patch chunks... 452 */ 453struct chunk { 454/* line number in postimage; up to but not including this 455 * line is the same as preimage 456 */ 457int same; 458 459/* preimage line number after this chunk */ 460int p_next; 461 462/* postimage line number after this chunk */ 463int t_next; 464}; 465 466struct patch { 467struct chunk *chunks; 468int num; 469}; 470 471struct blame_diff_state { 472struct xdiff_emit_state xm; 473struct patch *ret; 474unsigned hunk_post_context; 475unsigned hunk_in_pre_context :1; 476}; 477 478static voidprocess_u_diff(void*state_,char*line,unsigned long len) 479{ 480struct blame_diff_state *state = state_; 481struct chunk *chunk; 482int off1, off2, len1, len2, num; 483 484 num = state->ret->num; 485if(len <4|| line[0] !='@'|| line[1] !='@') { 486if(state->hunk_in_pre_context && line[0] ==' ') 487 state->ret->chunks[num -1].same++; 488else{ 489 state->hunk_in_pre_context =0; 490if(line[0] ==' ') 491 state->hunk_post_context++; 492else 493 state->hunk_post_context =0; 494} 495return; 496} 497 498if(num && state->hunk_post_context) { 499 chunk = &state->ret->chunks[num -1]; 500 chunk->p_next -= state->hunk_post_context; 501 chunk->t_next -= state->hunk_post_context; 502} 503 state->ret->num = ++num; 504 state->ret->chunks =xrealloc(state->ret->chunks, 505sizeof(struct chunk) * num); 506 chunk = &state->ret->chunks[num -1]; 507if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 508 state->ret->num--; 509return; 510} 511 512/* Line numbers in patch output are one based. */ 513 off1--; 514 off2--; 515 516 chunk->same = len2 ? off2 : (off2 +1); 517 518 chunk->p_next = off1 + (len1 ? len1 :1); 519 chunk->t_next = chunk->same + len2; 520 state->hunk_in_pre_context =1; 521 state->hunk_post_context =0; 522} 523 524static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 525int context) 526{ 527struct blame_diff_state state; 528 xpparam_t xpp; 529 xdemitconf_t xecfg; 530 xdemitcb_t ecb; 531 532 xpp.flags = xdl_opts; 533memset(&xecfg,0,sizeof(xecfg)); 534 xecfg.ctxlen = context; 535 ecb.outf = xdiff_outf; 536 ecb.priv = &state; 537memset(&state,0,sizeof(state)); 538 state.xm.consume = process_u_diff; 539 state.ret =xmalloc(sizeof(struct patch)); 540 state.ret->chunks = NULL; 541 state.ret->num =0; 542 543xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb); 544 545if(state.ret->num) { 546struct chunk *chunk; 547 chunk = &state.ret->chunks[state.ret->num -1]; 548 chunk->p_next -= state.hunk_post_context; 549 chunk->t_next -= state.hunk_post_context; 550} 551return state.ret; 552} 553 554/* 555 * Run diff between two origins and grab the patch output, so that 556 * we can pass blame for lines origin is currently suspected for 557 * to its parent. 558 */ 559static struct patch *get_patch(struct origin *parent,struct origin *origin) 560{ 561 mmfile_t file_p, file_o; 562struct patch *patch; 563 564fill_origin_blob(parent, &file_p); 565fill_origin_blob(origin, &file_o); 566if(!file_p.ptr || !file_o.ptr) 567return NULL; 568 patch =compare_buffer(&file_p, &file_o,0); 569 num_get_patch++; 570return patch; 571} 572 573static voidfree_patch(struct patch *p) 574{ 575free(p->chunks); 576free(p); 577} 578 579/* 580 * Link in a new blame entry to the scoreboard. Entries that cover the 581 * same line range have been removed from the scoreboard previously. 582 */ 583static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 584{ 585struct blame_entry *ent, *prev = NULL; 586 587origin_incref(e->suspect); 588 589for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 590 prev = ent; 591 592/* prev, if not NULL, is the last one that is below e */ 593 e->prev = prev; 594if(prev) { 595 e->next = prev->next; 596 prev->next = e; 597} 598else{ 599 e->next = sb->ent; 600 sb->ent = e; 601} 602if(e->next) 603 e->next->prev = e; 604} 605 606/* 607 * src typically is on-stack; we want to copy the information in it to 608 * an malloced blame_entry that is already on the linked list of the 609 * scoreboard. The origin of dst loses a refcnt while the origin of src 610 * gains one. 611 */ 612static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 613{ 614struct blame_entry *p, *n; 615 616 p = dst->prev; 617 n = dst->next; 618origin_incref(src->suspect); 619origin_decref(dst->suspect); 620memcpy(dst, src,sizeof(*src)); 621 dst->prev = p; 622 dst->next = n; 623 dst->score =0; 624} 625 626static const char*nth_line(struct scoreboard *sb,int lno) 627{ 628return sb->final_buf + sb->lineno[lno]; 629} 630 631/* 632 * It is known that lines between tlno to same came from parent, and e 633 * has an overlap with that range. it also is known that parent's 634 * line plno corresponds to e's line tlno. 635 * 636 * <---- e -----> 637 * <------> 638 * <------------> 639 * <------------> 640 * <------------------> 641 * 642 * Split e into potentially three parts; before this chunk, the chunk 643 * to be blamed for the parent, and after that portion. 644 */ 645static voidsplit_overlap(struct blame_entry *split, 646struct blame_entry *e, 647int tlno,int plno,int same, 648struct origin *parent) 649{ 650int chunk_end_lno; 651memset(split,0,sizeof(struct blame_entry [3])); 652 653if(e->s_lno < tlno) { 654/* there is a pre-chunk part not blamed on parent */ 655 split[0].suspect =origin_incref(e->suspect); 656 split[0].lno = e->lno; 657 split[0].s_lno = e->s_lno; 658 split[0].num_lines = tlno - e->s_lno; 659 split[1].lno = e->lno + tlno - e->s_lno; 660 split[1].s_lno = plno; 661} 662else{ 663 split[1].lno = e->lno; 664 split[1].s_lno = plno + (e->s_lno - tlno); 665} 666 667if(same < e->s_lno + e->num_lines) { 668/* there is a post-chunk part not blamed on parent */ 669 split[2].suspect =origin_incref(e->suspect); 670 split[2].lno = e->lno + (same - e->s_lno); 671 split[2].s_lno = e->s_lno + (same - e->s_lno); 672 split[2].num_lines = e->s_lno + e->num_lines - same; 673 chunk_end_lno = split[2].lno; 674} 675else 676 chunk_end_lno = e->lno + e->num_lines; 677 split[1].num_lines = chunk_end_lno - split[1].lno; 678 679/* 680 * if it turns out there is nothing to blame the parent for, 681 * forget about the splitting. !split[1].suspect signals this. 682 */ 683if(split[1].num_lines <1) 684return; 685 split[1].suspect =origin_incref(parent); 686} 687 688/* 689 * split_overlap() divided an existing blame e into up to three parts 690 * in split. Adjust the linked list of blames in the scoreboard to 691 * reflect the split. 692 */ 693static voidsplit_blame(struct scoreboard *sb, 694struct blame_entry *split, 695struct blame_entry *e) 696{ 697struct blame_entry *new_entry; 698 699if(split[0].suspect && split[2].suspect) { 700/* The first part (reuse storage for the existing entry e) */ 701dup_entry(e, &split[0]); 702 703/* The last part -- me */ 704 new_entry =xmalloc(sizeof(*new_entry)); 705memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 706add_blame_entry(sb, new_entry); 707 708/* ... and the middle part -- parent */ 709 new_entry =xmalloc(sizeof(*new_entry)); 710memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 711add_blame_entry(sb, new_entry); 712} 713else if(!split[0].suspect && !split[2].suspect) 714/* 715 * The parent covers the entire area; reuse storage for 716 * e and replace it with the parent. 717 */ 718dup_entry(e, &split[1]); 719else if(split[0].suspect) { 720/* me and then parent */ 721dup_entry(e, &split[0]); 722 723 new_entry =xmalloc(sizeof(*new_entry)); 724memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 725add_blame_entry(sb, new_entry); 726} 727else{ 728/* parent and then me */ 729dup_entry(e, &split[1]); 730 731 new_entry =xmalloc(sizeof(*new_entry)); 732memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 733add_blame_entry(sb, new_entry); 734} 735 736if(DEBUG) {/* sanity */ 737struct blame_entry *ent; 738int lno = sb->ent->lno, corrupt =0; 739 740for(ent = sb->ent; ent; ent = ent->next) { 741if(lno != ent->lno) 742 corrupt =1; 743if(ent->s_lno <0) 744 corrupt =1; 745 lno += ent->num_lines; 746} 747if(corrupt) { 748 lno = sb->ent->lno; 749for(ent = sb->ent; ent; ent = ent->next) { 750printf("L%8d l%8d n%8d\n", 751 lno, ent->lno, ent->num_lines); 752 lno = ent->lno + ent->num_lines; 753} 754die("oops"); 755} 756} 757} 758 759/* 760 * After splitting the blame, the origins used by the 761 * on-stack blame_entry should lose one refcnt each. 762 */ 763static voiddecref_split(struct blame_entry *split) 764{ 765int i; 766 767for(i =0; i <3; i++) 768origin_decref(split[i].suspect); 769} 770 771/* 772 * Helper for blame_chunk(). blame_entry e is known to overlap with 773 * the patch hunk; split it and pass blame to the parent. 774 */ 775static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 776int tlno,int plno,int same, 777struct origin *parent) 778{ 779struct blame_entry split[3]; 780 781split_overlap(split, e, tlno, plno, same, parent); 782if(split[1].suspect) 783split_blame(sb, split, e); 784decref_split(split); 785} 786 787/* 788 * Find the line number of the last line the target is suspected for. 789 */ 790static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 791{ 792struct blame_entry *e; 793int last_in_target = -1; 794 795for(e = sb->ent; e; e = e->next) { 796if(e->guilty || !same_suspect(e->suspect, target)) 797continue; 798if(last_in_target < e->s_lno + e->num_lines) 799 last_in_target = e->s_lno + e->num_lines; 800} 801return last_in_target; 802} 803 804/* 805 * Process one hunk from the patch between the current suspect for 806 * blame_entry e and its parent. Find and split the overlap, and 807 * pass blame to the overlapping part to the parent. 808 */ 809static voidblame_chunk(struct scoreboard *sb, 810int tlno,int plno,int same, 811struct origin *target,struct origin *parent) 812{ 813struct blame_entry *e; 814 815for(e = sb->ent; e; e = e->next) { 816if(e->guilty || !same_suspect(e->suspect, target)) 817continue; 818if(same <= e->s_lno) 819continue; 820if(tlno < e->s_lno + e->num_lines) 821blame_overlap(sb, e, tlno, plno, same, parent); 822} 823} 824 825/* 826 * We are looking at the origin 'target' and aiming to pass blame 827 * for the lines it is suspected to its parent. Run diff to find 828 * which lines came from parent and pass blame for them. 829 */ 830static intpass_blame_to_parent(struct scoreboard *sb, 831struct origin *target, 832struct origin *parent) 833{ 834int i, last_in_target, plno, tlno; 835struct patch *patch; 836 837 last_in_target =find_last_in_target(sb, target); 838if(last_in_target <0) 839return1;/* nothing remains for this target */ 840 841 patch =get_patch(parent, target); 842 plno = tlno =0; 843for(i =0; i < patch->num; i++) { 844struct chunk *chunk = &patch->chunks[i]; 845 846blame_chunk(sb, tlno, plno, chunk->same, target, parent); 847 plno = chunk->p_next; 848 tlno = chunk->t_next; 849} 850/* The rest (i.e. anything after tlno) are the same as the parent */ 851blame_chunk(sb, tlno, plno, last_in_target, target, parent); 852 853free_patch(patch); 854return0; 855} 856 857/* 858 * The lines in blame_entry after splitting blames many times can become 859 * very small and trivial, and at some point it becomes pointless to 860 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 861 * ordinary C program, and it is not worth to say it was copied from 862 * totally unrelated file in the parent. 863 * 864 * Compute how trivial the lines in the blame_entry are. 865 */ 866static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 867{ 868unsigned score; 869const char*cp, *ep; 870 871if(e->score) 872return e->score; 873 874 score =1; 875 cp =nth_line(sb, e->lno); 876 ep =nth_line(sb, e->lno + e->num_lines); 877while(cp < ep) { 878unsigned ch = *((unsigned char*)cp); 879if(isalnum(ch)) 880 score++; 881 cp++; 882} 883 e->score = score; 884return score; 885} 886 887/* 888 * best_so_far[] and this[] are both a split of an existing blame_entry 889 * that passes blame to the parent. Maintain best_so_far the best split 890 * so far, by comparing this and best_so_far and copying this into 891 * bst_so_far as needed. 892 */ 893static voidcopy_split_if_better(struct scoreboard *sb, 894struct blame_entry *best_so_far, 895struct blame_entry *this) 896{ 897int i; 898 899if(!this[1].suspect) 900return; 901if(best_so_far[1].suspect) { 902if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 903return; 904} 905 906for(i =0; i <3; i++) 907origin_incref(this[i].suspect); 908decref_split(best_so_far); 909memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 910} 911 912/* 913 * We are looking at a part of the final image represented by 914 * ent (tlno and same are offset by ent->s_lno). 915 * tlno is where we are looking at in the final image. 916 * up to (but not including) same match preimage. 917 * plno is where we are looking at in the preimage. 918 * 919 * <-------------- final image ----------------------> 920 * <------ent------> 921 * ^tlno ^same 922 * <---------preimage-----> 923 * ^plno 924 * 925 * All line numbers are 0-based. 926 */ 927static voidhandle_split(struct scoreboard *sb, 928struct blame_entry *ent, 929int tlno,int plno,int same, 930struct origin *parent, 931struct blame_entry *split) 932{ 933if(ent->num_lines <= tlno) 934return; 935if(tlno < same) { 936struct blame_entry this[3]; 937 tlno += ent->s_lno; 938 same += ent->s_lno; 939split_overlap(this, ent, tlno, plno, same, parent); 940copy_split_if_better(sb, split,this); 941decref_split(this); 942} 943} 944 945/* 946 * Find the lines from parent that are the same as ent so that 947 * we can pass blames to it. file_p has the blob contents for 948 * the parent. 949 */ 950static voidfind_copy_in_blob(struct scoreboard *sb, 951struct blame_entry *ent, 952struct origin *parent, 953struct blame_entry *split, 954 mmfile_t *file_p) 955{ 956const char*cp; 957int cnt; 958 mmfile_t file_o; 959struct patch *patch; 960int i, plno, tlno; 961 962/* 963 * Prepare mmfile that contains only the lines in ent. 964 */ 965 cp =nth_line(sb, ent->lno); 966 file_o.ptr = (char*) cp; 967 cnt = ent->num_lines; 968 969while(cnt && cp < sb->final_buf + sb->final_buf_size) { 970if(*cp++ =='\n') 971 cnt--; 972} 973 file_o.size = cp - file_o.ptr; 974 975 patch =compare_buffer(file_p, &file_o,1); 976 977/* 978 * file_o is a part of final image we are annotating. 979 * file_p partially may match that image. 980 */ 981memset(split,0,sizeof(struct blame_entry [3])); 982 plno = tlno =0; 983for(i =0; i < patch->num; i++) { 984struct chunk *chunk = &patch->chunks[i]; 985 986handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 987 plno = chunk->p_next; 988 tlno = chunk->t_next; 989} 990/* remainder, if any, all match the preimage */ 991handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 992free_patch(patch); 993} 994 995/* 996 * See if lines currently target is suspected for can be attributed to 997 * parent. 998 */ 999static intfind_move_in_parent(struct scoreboard *sb,1000struct origin *target,1001struct origin *parent)1002{1003int last_in_target, made_progress;1004struct blame_entry *e, split[3];1005 mmfile_t file_p;10061007 last_in_target =find_last_in_target(sb, target);1008if(last_in_target <0)1009return1;/* nothing remains for this target */10101011fill_origin_blob(parent, &file_p);1012if(!file_p.ptr)1013return0;10141015 made_progress =1;1016while(made_progress) {1017 made_progress =0;1018for(e = sb->ent; e; e = e->next) {1019if(e->guilty || !same_suspect(e->suspect, target))1020continue;1021find_copy_in_blob(sb, e, parent, split, &file_p);1022if(split[1].suspect &&1023 blame_move_score <ent_score(sb, &split[1])) {1024split_blame(sb, split, e);1025 made_progress =1;1026}1027decref_split(split);1028}1029}1030return0;1031}10321033struct blame_list {1034struct blame_entry *ent;1035struct blame_entry split[3];1036};10371038/*1039 * Count the number of entries the target is suspected for,1040 * and prepare a list of entry and the best split.1041 */1042static struct blame_list *setup_blame_list(struct scoreboard *sb,1043struct origin *target,1044int*num_ents_p)1045{1046struct blame_entry *e;1047int num_ents, i;1048struct blame_list *blame_list = NULL;10491050for(e = sb->ent, num_ents =0; e; e = e->next)1051if(!e->guilty &&same_suspect(e->suspect, target))1052 num_ents++;1053if(num_ents) {1054 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1055for(e = sb->ent, i =0; e; e = e->next)1056if(!e->guilty &&same_suspect(e->suspect, target))1057 blame_list[i++].ent = e;1058}1059*num_ents_p = num_ents;1060return blame_list;1061}10621063/*1064 * For lines target is suspected for, see if we can find code movement1065 * across file boundary from the parent commit. porigin is the path1066 * in the parent we already tried.1067 */1068static intfind_copy_in_parent(struct scoreboard *sb,1069struct origin *target,1070struct commit *parent,1071struct origin *porigin,1072int opt)1073{1074struct diff_options diff_opts;1075const char*paths[1];1076int i, j;1077int retval;1078struct blame_list *blame_list;1079int num_ents;10801081 blame_list =setup_blame_list(sb, target, &num_ents);1082if(!blame_list)1083return1;/* nothing remains for this target */10841085diff_setup(&diff_opts);1086DIFF_OPT_SET(&diff_opts, RECURSIVE);1087 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10881089 paths[0] = NULL;1090diff_tree_setup_paths(paths, &diff_opts);1091if(diff_setup_done(&diff_opts) <0)1092die("diff-setup");10931094/* Try "find copies harder" on new path if requested;1095 * we do not want to use diffcore_rename() actually to1096 * match things up; find_copies_harder is set only to1097 * force diff_tree_sha1() to feed all filepairs to diff_queue,1098 * and this code needs to be after diff_setup_done(), which1099 * usually makes find-copies-harder imply copy detection.1100 */1101if((opt & PICKAXE_BLAME_COPY_HARDEST)1102|| ((opt & PICKAXE_BLAME_COPY_HARDER)1103&& (!porigin ||strcmp(target->path, porigin->path))))1104DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);11051106if(is_null_sha1(target->commit->object.sha1))1107do_diff_cache(parent->tree->object.sha1, &diff_opts);1108else1109diff_tree_sha1(parent->tree->object.sha1,1110 target->commit->tree->object.sha1,1111"", &diff_opts);11121113if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1114diffcore_std(&diff_opts);11151116 retval =0;1117while(1) {1118int made_progress =0;11191120for(i =0; i < diff_queued_diff.nr; i++) {1121struct diff_filepair *p = diff_queued_diff.queue[i];1122struct origin *norigin;1123 mmfile_t file_p;1124struct blame_entry this[3];11251126if(!DIFF_FILE_VALID(p->one))1127continue;/* does not exist in parent */1128if(porigin && !strcmp(p->one->path, porigin->path))1129/* find_move already dealt with this path */1130continue;11311132 norigin =get_origin(sb, parent, p->one->path);1133hashcpy(norigin->blob_sha1, p->one->sha1);1134fill_origin_blob(norigin, &file_p);1135if(!file_p.ptr)1136continue;11371138for(j =0; j < num_ents; j++) {1139find_copy_in_blob(sb, blame_list[j].ent,1140 norigin,this, &file_p);1141copy_split_if_better(sb, blame_list[j].split,1142this);1143decref_split(this);1144}1145origin_decref(norigin);1146}11471148for(j =0; j < num_ents; j++) {1149struct blame_entry *split = blame_list[j].split;1150if(split[1].suspect &&1151 blame_copy_score <ent_score(sb, &split[1])) {1152split_blame(sb, split, blame_list[j].ent);1153 made_progress =1;1154}1155decref_split(split);1156}1157free(blame_list);11581159if(!made_progress)1160break;1161 blame_list =setup_blame_list(sb, target, &num_ents);1162if(!blame_list) {1163 retval =1;1164break;1165}1166}1167diff_flush(&diff_opts);11681169return retval;1170}11711172/*1173 * The blobs of origin and porigin exactly match, so everything1174 * origin is suspected for can be blamed on the parent.1175 */1176static voidpass_whole_blame(struct scoreboard *sb,1177struct origin *origin,struct origin *porigin)1178{1179struct blame_entry *e;11801181if(!porigin->file.ptr && origin->file.ptr) {1182/* Steal its file */1183 porigin->file = origin->file;1184 origin->file.ptr = NULL;1185}1186for(e = sb->ent; e; e = e->next) {1187if(!same_suspect(e->suspect, origin))1188continue;1189origin_incref(porigin);1190origin_decref(e->suspect);1191 e->suspect = porigin;1192}1193}11941195#define MAXPARENT 1611961197static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1198{1199int i, pass;1200struct commit *commit = origin->commit;1201struct commit_list *parent;1202struct origin *parent_origin[MAXPARENT], *porigin;12031204memset(parent_origin,0,sizeof(parent_origin));12051206/* The first pass looks for unrenamed path to optimize for1207 * common cases, then we look for renames in the second pass.1208 */1209for(pass =0; pass <2; pass++) {1210struct origin *(*find)(struct scoreboard *,1211struct commit *,struct origin *);1212 find = pass ? find_rename : find_origin;12131214for(i =0, parent = commit->parents;1215 i < MAXPARENT && parent;1216 parent = parent->next, i++) {1217struct commit *p = parent->item;1218int j, same;12191220if(parent_origin[i])1221continue;1222if(parse_commit(p))1223continue;1224 porigin =find(sb, p, origin);1225if(!porigin)1226continue;1227if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1228pass_whole_blame(sb, origin, porigin);1229origin_decref(porigin);1230goto finish;1231}1232for(j = same =0; j < i; j++)1233if(parent_origin[j] &&1234!hashcmp(parent_origin[j]->blob_sha1,1235 porigin->blob_sha1)) {1236 same =1;1237break;1238}1239if(!same)1240 parent_origin[i] = porigin;1241else1242origin_decref(porigin);1243}1244}12451246 num_commits++;1247for(i =0, parent = commit->parents;1248 i < MAXPARENT && parent;1249 parent = parent->next, i++) {1250struct origin *porigin = parent_origin[i];1251if(!porigin)1252continue;1253if(pass_blame_to_parent(sb, origin, porigin))1254goto finish;1255}12561257/*1258 * Optionally find moves in parents' files.1259 */1260if(opt & PICKAXE_BLAME_MOVE)1261for(i =0, parent = commit->parents;1262 i < MAXPARENT && parent;1263 parent = parent->next, i++) {1264struct origin *porigin = parent_origin[i];1265if(!porigin)1266continue;1267if(find_move_in_parent(sb, origin, porigin))1268goto finish;1269}12701271/*1272 * Optionally find copies from parents' files.1273 */1274if(opt & PICKAXE_BLAME_COPY)1275for(i =0, parent = commit->parents;1276 i < MAXPARENT && parent;1277 parent = parent->next, i++) {1278struct origin *porigin = parent_origin[i];1279if(find_copy_in_parent(sb, origin, parent->item,1280 porigin, opt))1281goto finish;1282}12831284 finish:1285for(i =0; i < MAXPARENT; i++) {1286if(parent_origin[i]) {1287drop_origin_blob(parent_origin[i]);1288origin_decref(parent_origin[i]);1289}1290}1291drop_origin_blob(origin);1292}12931294/*1295 * Information on commits, used for output.1296 */1297struct commit_info1298{1299const char*author;1300const char*author_mail;1301unsigned long author_time;1302const char*author_tz;13031304/* filled only when asked for details */1305const char*committer;1306const char*committer_mail;1307unsigned long committer_time;1308const char*committer_tz;13091310const char*summary;1311};13121313/*1314 * Parse author/committer line in the commit object buffer1315 */1316static voidget_ac_line(const char*inbuf,const char*what,1317int bufsz,char*person,const char**mail,1318unsigned long*time,const char**tz)1319{1320int len, tzlen, maillen;1321char*tmp, *endp, *timepos;13221323 tmp =strstr(inbuf, what);1324if(!tmp)1325goto error_out;1326 tmp +=strlen(what);1327 endp =strchr(tmp,'\n');1328if(!endp)1329 len =strlen(tmp);1330else1331 len = endp - tmp;1332if(bufsz <= len) {1333 error_out:1334/* Ugh */1335*mail = *tz ="(unknown)";1336*time =0;1337return;1338}1339memcpy(person, tmp, len);13401341 tmp = person;1342 tmp += len;1343*tmp =0;1344while(*tmp !=' ')1345 tmp--;1346*tz = tmp+1;1347 tzlen = (person+len)-(tmp+1);13481349*tmp =0;1350while(*tmp !=' ')1351 tmp--;1352*time =strtoul(tmp, NULL,10);1353 timepos = tmp;13541355*tmp =0;1356while(*tmp !=' ')1357 tmp--;1358*mail = tmp +1;1359*tmp =0;1360 maillen = timepos - tmp;13611362if(!mailmap.nr)1363return;13641365/*1366 * mailmap expansion may make the name longer.1367 * make room by pushing stuff down.1368 */1369 tmp = person + bufsz - (tzlen +1);1370memmove(tmp, *tz, tzlen);1371 tmp[tzlen] =0;1372*tz = tmp;13731374 tmp = tmp - (maillen +1);1375memmove(tmp, *mail, maillen);1376 tmp[maillen] =0;1377*mail = tmp;13781379/*1380 * Now, convert e-mail using mailmap1381 */1382map_email(&mailmap, tmp +1, person, tmp-person-1);1383}13841385static voidget_commit_info(struct commit *commit,1386struct commit_info *ret,1387int detailed)1388{1389int len;1390char*tmp, *endp;1391static char author_buf[1024];1392static char committer_buf[1024];1393static char summary_buf[1024];13941395/*1396 * We've operated without save_commit_buffer, so1397 * we now need to populate them for output.1398 */1399if(!commit->buffer) {1400enum object_type type;1401unsigned long size;1402 commit->buffer =1403read_sha1_file(commit->object.sha1, &type, &size);1404if(!commit->buffer)1405die("Cannot read commit%s",1406sha1_to_hex(commit->object.sha1));1407}1408 ret->author = author_buf;1409get_ac_line(commit->buffer,"\nauthor ",1410sizeof(author_buf), author_buf, &ret->author_mail,1411&ret->author_time, &ret->author_tz);14121413if(!detailed)1414return;14151416 ret->committer = committer_buf;1417get_ac_line(commit->buffer,"\ncommitter ",1418sizeof(committer_buf), committer_buf, &ret->committer_mail,1419&ret->committer_time, &ret->committer_tz);14201421 ret->summary = summary_buf;1422 tmp =strstr(commit->buffer,"\n\n");1423if(!tmp) {1424 error_out:1425sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1426return;1427}1428 tmp +=2;1429 endp =strchr(tmp,'\n');1430if(!endp)1431 endp = tmp +strlen(tmp);1432 len = endp - tmp;1433if(len >=sizeof(summary_buf) || len ==0)1434goto error_out;1435memcpy(summary_buf, tmp, len);1436 summary_buf[len] =0;1437}14381439/*1440 * To allow LF and other nonportable characters in pathnames,1441 * they are c-style quoted as needed.1442 */1443static voidwrite_filename_info(const char*path)1444{1445printf("filename ");1446write_name_quoted(path, stdout,'\n');1447}14481449/*1450 * The blame_entry is found to be guilty for the range. Mark it1451 * as such, and show it in incremental output.1452 */1453static voidfound_guilty_entry(struct blame_entry *ent)1454{1455if(ent->guilty)1456return;1457 ent->guilty =1;1458if(incremental) {1459struct origin *suspect = ent->suspect;14601461printf("%s %d %d %d\n",1462sha1_to_hex(suspect->commit->object.sha1),1463 ent->s_lno +1, ent->lno +1, ent->num_lines);1464if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1465struct commit_info ci;1466 suspect->commit->object.flags |= METAINFO_SHOWN;1467get_commit_info(suspect->commit, &ci,1);1468printf("author%s\n", ci.author);1469printf("author-mail%s\n", ci.author_mail);1470printf("author-time%lu\n", ci.author_time);1471printf("author-tz%s\n", ci.author_tz);1472printf("committer%s\n", ci.committer);1473printf("committer-mail%s\n", ci.committer_mail);1474printf("committer-time%lu\n", ci.committer_time);1475printf("committer-tz%s\n", ci.committer_tz);1476printf("summary%s\n", ci.summary);1477if(suspect->commit->object.flags & UNINTERESTING)1478printf("boundary\n");1479}1480write_filename_info(suspect->path);1481maybe_flush_or_die(stdout,"stdout");1482}1483}14841485/*1486 * The main loop -- while the scoreboard has lines whose true origin1487 * is still unknown, pick one blame_entry, and allow its current1488 * suspect to pass blames to its parents.1489 */1490static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1491{1492while(1) {1493struct blame_entry *ent;1494struct commit *commit;1495struct origin *suspect = NULL;14961497/* find one suspect to break down */1498for(ent = sb->ent; !suspect && ent; ent = ent->next)1499if(!ent->guilty)1500 suspect = ent->suspect;1501if(!suspect)1502return;/* all done */15031504/*1505 * We will use this suspect later in the loop,1506 * so hold onto it in the meantime.1507 */1508origin_incref(suspect);1509 commit = suspect->commit;1510if(!commit->object.parsed)1511parse_commit(commit);1512if(!(commit->object.flags & UNINTERESTING) &&1513!(revs->max_age != -1&& commit->date < revs->max_age))1514pass_blame(sb, suspect, opt);1515else{1516 commit->object.flags |= UNINTERESTING;1517if(commit->object.parsed)1518mark_parents_uninteresting(commit);1519}1520/* treat root commit as boundary */1521if(!commit->parents && !show_root)1522 commit->object.flags |= UNINTERESTING;15231524/* Take responsibility for the remaining entries */1525for(ent = sb->ent; ent; ent = ent->next)1526if(same_suspect(ent->suspect, suspect))1527found_guilty_entry(ent);1528origin_decref(suspect);15291530if(DEBUG)/* sanity */1531sanity_check_refcnt(sb);1532}1533}15341535static const char*format_time(unsigned long time,const char*tz_str,1536int show_raw_time)1537{1538static char time_buf[128];1539time_t t = time;1540int minutes, tz;1541struct tm *tm;15421543if(show_raw_time) {1544sprintf(time_buf,"%lu%s", time, tz_str);1545return time_buf;1546}15471548 tz =atoi(tz_str);1549 minutes = tz <0? -tz : tz;1550 minutes = (minutes /100)*60+ (minutes %100);1551 minutes = tz <0? -minutes : minutes;1552 t = time + minutes *60;1553 tm =gmtime(&t);15541555strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1556strcat(time_buf, tz_str);1557return time_buf;1558}15591560#define OUTPUT_ANNOTATE_COMPAT 0011561#define OUTPUT_LONG_OBJECT_NAME 0021562#define OUTPUT_RAW_TIMESTAMP 0041563#define OUTPUT_PORCELAIN 0101564#define OUTPUT_SHOW_NAME 0201565#define OUTPUT_SHOW_NUMBER 0401566#define OUTPUT_SHOW_SCORE 01001567#define OUTPUT_NO_AUTHOR 020015681569static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1570{1571int cnt;1572const char*cp;1573struct origin *suspect = ent->suspect;1574char hex[41];15751576strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1577printf("%s%c%d %d %d\n",1578 hex,1579 ent->guilty ?' ':'*',// purely for debugging1580 ent->s_lno +1,1581 ent->lno +1,1582 ent->num_lines);1583if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1584struct commit_info ci;1585 suspect->commit->object.flags |= METAINFO_SHOWN;1586get_commit_info(suspect->commit, &ci,1);1587printf("author%s\n", ci.author);1588printf("author-mail%s\n", ci.author_mail);1589printf("author-time%lu\n", ci.author_time);1590printf("author-tz%s\n", ci.author_tz);1591printf("committer%s\n", ci.committer);1592printf("committer-mail%s\n", ci.committer_mail);1593printf("committer-time%lu\n", ci.committer_time);1594printf("committer-tz%s\n", ci.committer_tz);1595write_filename_info(suspect->path);1596printf("summary%s\n", ci.summary);1597if(suspect->commit->object.flags & UNINTERESTING)1598printf("boundary\n");1599}1600else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1601write_filename_info(suspect->path);16021603 cp =nth_line(sb, ent->lno);1604for(cnt =0; cnt < ent->num_lines; cnt++) {1605char ch;1606if(cnt)1607printf("%s %d %d\n", hex,1608 ent->s_lno +1+ cnt,1609 ent->lno +1+ cnt);1610putchar('\t');1611do{1612 ch = *cp++;1613putchar(ch);1614}while(ch !='\n'&&1615 cp < sb->final_buf + sb->final_buf_size);1616}1617}16181619static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1620{1621int cnt;1622const char*cp;1623struct origin *suspect = ent->suspect;1624struct commit_info ci;1625char hex[41];1626int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16271628get_commit_info(suspect->commit, &ci,1);1629strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16301631 cp =nth_line(sb, ent->lno);1632for(cnt =0; cnt < ent->num_lines; cnt++) {1633char ch;1634int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16351636if(suspect->commit->object.flags & UNINTERESTING) {1637if(blank_boundary)1638memset(hex,' ', length);1639else if(!cmd_is_annotate) {1640 length--;1641putchar('^');1642}1643}16441645printf("%.*s", length, hex);1646if(opt & OUTPUT_ANNOTATE_COMPAT)1647printf("\t(%10s\t%10s\t%d)", ci.author,1648format_time(ci.author_time, ci.author_tz,1649 show_raw_time),1650 ent->lno +1+ cnt);1651else{1652if(opt & OUTPUT_SHOW_SCORE)1653printf(" %*d%02d",1654 max_score_digits, ent->score,1655 ent->suspect->refcnt);1656if(opt & OUTPUT_SHOW_NAME)1657printf(" %-*.*s", longest_file, longest_file,1658 suspect->path);1659if(opt & OUTPUT_SHOW_NUMBER)1660printf(" %*d", max_orig_digits,1661 ent->s_lno +1+ cnt);16621663if(!(opt & OUTPUT_NO_AUTHOR))1664printf(" (%-*.*s%10s",1665 longest_author, longest_author,1666 ci.author,1667format_time(ci.author_time,1668 ci.author_tz,1669 show_raw_time));1670printf(" %*d) ",1671 max_digits, ent->lno +1+ cnt);1672}1673do{1674 ch = *cp++;1675putchar(ch);1676}while(ch !='\n'&&1677 cp < sb->final_buf + sb->final_buf_size);1678}1679}16801681static voidoutput(struct scoreboard *sb,int option)1682{1683struct blame_entry *ent;16841685if(option & OUTPUT_PORCELAIN) {1686for(ent = sb->ent; ent; ent = ent->next) {1687struct blame_entry *oth;1688struct origin *suspect = ent->suspect;1689struct commit *commit = suspect->commit;1690if(commit->object.flags & MORE_THAN_ONE_PATH)1691continue;1692for(oth = ent->next; oth; oth = oth->next) {1693if((oth->suspect->commit != commit) ||1694!strcmp(oth->suspect->path, suspect->path))1695continue;1696 commit->object.flags |= MORE_THAN_ONE_PATH;1697break;1698}1699}1700}17011702for(ent = sb->ent; ent; ent = ent->next) {1703if(option & OUTPUT_PORCELAIN)1704emit_porcelain(sb, ent);1705else{1706emit_other(sb, ent, option);1707}1708}1709}17101711/*1712 * To allow quick access to the contents of nth line in the1713 * final image, prepare an index in the scoreboard.1714 */1715static intprepare_lines(struct scoreboard *sb)1716{1717const char*buf = sb->final_buf;1718unsigned long len = sb->final_buf_size;1719int num =0, incomplete =0, bol =1;17201721if(len && buf[len-1] !='\n')1722 incomplete++;/* incomplete line at the end */1723while(len--) {1724if(bol) {1725 sb->lineno =xrealloc(sb->lineno,1726sizeof(int* ) * (num +1));1727 sb->lineno[num] = buf - sb->final_buf;1728 bol =0;1729}1730if(*buf++ =='\n') {1731 num++;1732 bol =1;1733}1734}1735 sb->lineno =xrealloc(sb->lineno,1736sizeof(int* ) * (num + incomplete +1));1737 sb->lineno[num + incomplete] = buf - sb->final_buf;1738 sb->num_lines = num + incomplete;1739return sb->num_lines;1740}17411742/*1743 * Add phony grafts for use with -S; this is primarily to1744 * support git-cvsserver that wants to give a linear history1745 * to its clients.1746 */1747static intread_ancestry(const char*graft_file)1748{1749FILE*fp =fopen(graft_file,"r");1750char buf[1024];1751if(!fp)1752return-1;1753while(fgets(buf,sizeof(buf), fp)) {1754/* The format is just "Commit Parent1 Parent2 ...\n" */1755int len =strlen(buf);1756struct commit_graft *graft =read_graft_line(buf, len);1757if(graft)1758register_commit_graft(graft,0);1759}1760fclose(fp);1761return0;1762}17631764/*1765 * How many columns do we need to show line numbers in decimal?1766 */1767static intlineno_width(int lines)1768{1769int i, width;17701771for(width =1, i =10; i <= lines +1; width++)1772 i *=10;1773return width;1774}17751776/*1777 * How many columns do we need to show line numbers, authors,1778 * and filenames?1779 */1780static voidfind_alignment(struct scoreboard *sb,int*option)1781{1782int longest_src_lines =0;1783int longest_dst_lines =0;1784unsigned largest_score =0;1785struct blame_entry *e;17861787for(e = sb->ent; e; e = e->next) {1788struct origin *suspect = e->suspect;1789struct commit_info ci;1790int num;17911792if(strcmp(suspect->path, sb->path))1793*option |= OUTPUT_SHOW_NAME;1794 num =strlen(suspect->path);1795if(longest_file < num)1796 longest_file = num;1797if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1798 suspect->commit->object.flags |= METAINFO_SHOWN;1799get_commit_info(suspect->commit, &ci,1);1800 num =strlen(ci.author);1801if(longest_author < num)1802 longest_author = num;1803}1804 num = e->s_lno + e->num_lines;1805if(longest_src_lines < num)1806 longest_src_lines = num;1807 num = e->lno + e->num_lines;1808if(longest_dst_lines < num)1809 longest_dst_lines = num;1810if(largest_score <ent_score(sb, e))1811 largest_score =ent_score(sb, e);1812}1813 max_orig_digits =lineno_width(longest_src_lines);1814 max_digits =lineno_width(longest_dst_lines);1815 max_score_digits =lineno_width(largest_score);1816}18171818/*1819 * For debugging -- origin is refcounted, and this asserts that1820 * we do not underflow.1821 */1822static voidsanity_check_refcnt(struct scoreboard *sb)1823{1824int baa =0;1825struct blame_entry *ent;18261827for(ent = sb->ent; ent; ent = ent->next) {1828/* Nobody should have zero or negative refcnt */1829if(ent->suspect->refcnt <=0) {1830fprintf(stderr,"%sin%shas negative refcnt%d\n",1831 ent->suspect->path,1832sha1_to_hex(ent->suspect->commit->object.sha1),1833 ent->suspect->refcnt);1834 baa =1;1835}1836}1837for(ent = sb->ent; ent; ent = ent->next) {1838/* Mark the ones that haven't been checked */1839if(0< ent->suspect->refcnt)1840 ent->suspect->refcnt = -ent->suspect->refcnt;1841}1842for(ent = sb->ent; ent; ent = ent->next) {1843/*1844 * ... then pick each and see if they have the the1845 * correct refcnt.1846 */1847int found;1848struct blame_entry *e;1849struct origin *suspect = ent->suspect;18501851if(0< suspect->refcnt)1852continue;1853 suspect->refcnt = -suspect->refcnt;/* Unmark */1854for(found =0, e = sb->ent; e; e = e->next) {1855if(e->suspect != suspect)1856continue;1857 found++;1858}1859if(suspect->refcnt != found) {1860fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1861 ent->suspect->path,1862sha1_to_hex(ent->suspect->commit->object.sha1),1863 ent->suspect->refcnt, found);1864 baa =2;1865}1866}1867if(baa) {1868int opt =0160;1869find_alignment(sb, &opt);1870output(sb, opt);1871die("Baa%d!", baa);1872}1873}18741875/*1876 * Used for the command line parsing; check if the path exists1877 * in the working tree.1878 */1879static inthas_path_in_work_tree(const char*path)1880{1881struct stat st;1882return!lstat(path, &st);1883}18841885static unsignedparse_score(const char*arg)1886{1887char*end;1888unsigned long score =strtoul(arg, &end,10);1889if(*end)1890return0;1891return score;1892}18931894static const char*add_prefix(const char*prefix,const char*path)1895{1896if(!prefix || !prefix[0])1897return path;1898returnprefix_path(prefix,strlen(prefix), path);1899}19001901/*1902 * Parsing of (comma separated) one item in the -L option1903 */1904static const char*parse_loc(const char*spec,1905struct scoreboard *sb,long lno,1906long begin,long*ret)1907{1908char*term;1909const char*line;1910long num;1911int reg_error;1912 regex_t regexp;1913 regmatch_t match[1];19141915/* Allow "-L <something>,+20" to mean starting at <something>1916 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1917 * <something>.1918 */1919if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1920 num =strtol(spec +1, &term,10);1921if(term != spec +1) {1922if(spec[0] =='-')1923 num =0- num;1924if(0< num)1925*ret = begin + num -2;1926else if(!num)1927*ret = begin;1928else1929*ret = begin + num;1930return term;1931}1932return spec;1933}1934 num =strtol(spec, &term,10);1935if(term != spec) {1936*ret = num;1937return term;1938}1939if(spec[0] !='/')1940return spec;19411942/* it could be a regexp of form /.../ */1943for(term = (char*) spec +1; *term && *term !='/'; term++) {1944if(*term =='\\')1945 term++;1946}1947if(*term !='/')1948return spec;19491950/* try [spec+1 .. term-1] as regexp */1951*term =0;1952 begin--;/* input is in human terms */1953 line =nth_line(sb, begin);19541955if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1956!(reg_error =regexec(®exp, line,1, match,0))) {1957const char*cp = line + match[0].rm_so;1958const char*nline;19591960while(begin++ < lno) {1961 nline =nth_line(sb, begin);1962if(line <= cp && cp < nline)1963break;1964 line = nline;1965}1966*ret = begin;1967regfree(®exp);1968*term++ ='/';1969return term;1970}1971else{1972char errbuf[1024];1973regerror(reg_error, ®exp, errbuf,1024);1974die("-L parameter '%s':%s", spec +1, errbuf);1975}1976}19771978/*1979 * Parsing of -L option1980 */1981static voidprepare_blame_range(struct scoreboard *sb,1982const char*bottomtop,1983long lno,1984long*bottom,long*top)1985{1986const char*term;19871988 term =parse_loc(bottomtop, sb, lno,1, bottom);1989if(*term ==',') {1990 term =parse_loc(term +1, sb, lno, *bottom +1, top);1991if(*term)1992usage(blame_usage);1993}1994if(*term)1995usage(blame_usage);1996}19971998static intgit_blame_config(const char*var,const char*value)1999{2000if(!strcmp(var,"blame.showroot")) {2001 show_root =git_config_bool(var, value);2002return0;2003}2004if(!strcmp(var,"blame.blankboundary")) {2005 blank_boundary =git_config_bool(var, value);2006return0;2007}2008returngit_default_config(var, value);2009}20102011static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2012{2013struct commit *commit;2014struct origin *origin;2015unsigned char head_sha1[20];2016struct strbuf buf;2017const char*ident;2018time_t now;2019int size, len;2020struct cache_entry *ce;2021unsigned mode;20222023if(get_sha1("HEAD", head_sha1))2024die("No such ref: HEAD");20252026time(&now);2027 commit =xcalloc(1,sizeof(*commit));2028 commit->parents =xcalloc(1,sizeof(*commit->parents));2029 commit->parents->item =lookup_commit_reference(head_sha1);2030 commit->object.parsed =1;2031 commit->date = now;2032 commit->object.type = OBJ_COMMIT;20332034 origin =make_origin(commit, path);20352036strbuf_init(&buf,0);2037if(!contents_from ||strcmp("-", contents_from)) {2038struct stat st;2039const char*read_from;2040unsigned long fin_size;20412042if(contents_from) {2043if(stat(contents_from, &st) <0)2044die("Cannot stat%s", contents_from);2045 read_from = contents_from;2046}2047else{2048if(lstat(path, &st) <0)2049die("Cannot lstat%s", path);2050 read_from = path;2051}2052 fin_size =xsize_t(st.st_size);2053 mode =canon_mode(st.st_mode);2054switch(st.st_mode & S_IFMT) {2055case S_IFREG:2056if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2057die("cannot open or read%s", read_from);2058break;2059case S_IFLNK:2060if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2061die("cannot readlink%s", read_from);2062 buf.len = fin_size;2063break;2064default:2065die("unsupported file type%s", read_from);2066}2067}2068else{2069/* Reading from stdin */2070 contents_from ="standard input";2071 mode =0;2072if(strbuf_read(&buf,0,0) <0)2073die("read error%sfrom stdin",strerror(errno));2074}2075convert_to_git(path, buf.buf, buf.len, &buf);2076 origin->file.ptr = buf.buf;2077 origin->file.size = buf.len;2078pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2079 commit->util = origin;20802081/*2082 * Read the current index, replace the path entry with2083 * origin->blob_sha1 without mucking with its mode or type2084 * bits; we are not going to write this index out -- we just2085 * want to run "diff-index --cached".2086 */2087discard_cache();2088read_cache();20892090 len =strlen(path);2091if(!mode) {2092int pos =cache_name_pos(path, len);2093if(0<= pos)2094 mode =ntohl(active_cache[pos]->ce_mode);2095else2096/* Let's not bother reading from HEAD tree */2097 mode = S_IFREG |0644;2098}2099 size =cache_entry_size(len);2100 ce =xcalloc(1, size);2101hashcpy(ce->sha1, origin->blob_sha1);2102memcpy(ce->name, path, len);2103 ce->ce_flags =create_ce_flags(len,0);2104 ce->ce_mode =create_ce_mode(mode);2105add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);21062107/*2108 * We are not going to write this out, so this does not matter2109 * right now, but someday we might optimize diff-index --cached2110 * with cache-tree information.2111 */2112cache_tree_invalidate_path(active_cache_tree, path);21132114 commit->buffer =xmalloc(400);2115 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2116snprintf(commit->buffer,400,2117"tree 0000000000000000000000000000000000000000\n"2118"parent%s\n"2119"author%s\n"2120"committer%s\n\n"2121"Version of%sfrom%s\n",2122sha1_to_hex(head_sha1),2123 ident, ident, path, contents_from ? contents_from : path);2124return commit;2125}21262127intcmd_blame(int argc,const char**argv,const char*prefix)2128{2129struct rev_info revs;2130const char*path;2131struct scoreboard sb;2132struct origin *o;2133struct blame_entry *ent;2134int i, seen_dashdash, unk, opt;2135long bottom, top, lno;2136int output_option =0;2137int show_stats =0;2138const char*revs_file = NULL;2139const char*final_commit_name = NULL;2140enum object_type type;2141const char*bottomtop = NULL;2142const char*contents_from = NULL;21432144 cmd_is_annotate = !strcmp(argv[0],"annotate");21452146git_config(git_blame_config);2147 save_commit_buffer =0;21482149 opt =0;2150 seen_dashdash =0;2151for(unk = i =1; i < argc; i++) {2152const char*arg = argv[i];2153if(*arg !='-')2154break;2155else if(!strcmp("-b", arg))2156 blank_boundary =1;2157else if(!strcmp("--root", arg))2158 show_root =1;2159else if(!strcmp(arg,"--show-stats"))2160 show_stats =1;2161else if(!strcmp("-c", arg))2162 output_option |= OUTPUT_ANNOTATE_COMPAT;2163else if(!strcmp("-t", arg))2164 output_option |= OUTPUT_RAW_TIMESTAMP;2165else if(!strcmp("-l", arg))2166 output_option |= OUTPUT_LONG_OBJECT_NAME;2167else if(!strcmp("-s", arg))2168 output_option |= OUTPUT_NO_AUTHOR;2169else if(!strcmp("-w", arg))2170 xdl_opts |= XDF_IGNORE_WHITESPACE;2171else if(!strcmp("-S", arg) && ++i < argc)2172 revs_file = argv[i];2173else if(!prefixcmp(arg,"-M")) {2174 opt |= PICKAXE_BLAME_MOVE;2175 blame_move_score =parse_score(arg+2);2176}2177else if(!prefixcmp(arg,"-C")) {2178/*2179 * -C enables copy from removed files;2180 * -C -C enables copy from existing files, but only2181 * when blaming a new file;2182 * -C -C -C enables copy from existing files for2183 * everybody2184 */2185if(opt & PICKAXE_BLAME_COPY_HARDER)2186 opt |= PICKAXE_BLAME_COPY_HARDEST;2187if(opt & PICKAXE_BLAME_COPY)2188 opt |= PICKAXE_BLAME_COPY_HARDER;2189 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2190 blame_copy_score =parse_score(arg+2);2191}2192else if(!prefixcmp(arg,"-L")) {2193if(!arg[2]) {2194if(++i >= argc)2195usage(blame_usage);2196 arg = argv[i];2197}2198else2199 arg +=2;2200if(bottomtop)2201die("More than one '-L n,m' option given");2202 bottomtop = arg;2203}2204else if(!strcmp("--contents", arg)) {2205if(++i >= argc)2206usage(blame_usage);2207 contents_from = argv[i];2208}2209else if(!strcmp("--incremental", arg))2210 incremental =1;2211else if(!strcmp("--score-debug", arg))2212 output_option |= OUTPUT_SHOW_SCORE;2213else if(!strcmp("-f", arg) ||2214!strcmp("--show-name", arg))2215 output_option |= OUTPUT_SHOW_NAME;2216else if(!strcmp("-n", arg) ||2217!strcmp("--show-number", arg))2218 output_option |= OUTPUT_SHOW_NUMBER;2219else if(!strcmp("-p", arg) ||2220!strcmp("--porcelain", arg))2221 output_option |= OUTPUT_PORCELAIN;2222else if(!strcmp("--", arg)) {2223 seen_dashdash =1;2224 i++;2225break;2226}2227else2228 argv[unk++] = arg;2229}22302231if(!blame_move_score)2232 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2233if(!blame_copy_score)2234 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22352236/*2237 * We have collected options unknown to us in argv[1..unk]2238 * which are to be passed to revision machinery if we are2239 * going to do the "bottom" processing.2240 *2241 * The remaining are:2242 *2243 * (1) if seen_dashdash, its either2244 * "-options -- <path>" or2245 * "-options -- <path> <rev>".2246 * but the latter is allowed only if there is no2247 * options that we passed to revision machinery.2248 *2249 * (2) otherwise, we may have "--" somewhere later and2250 * might be looking at the first one of multiple 'rev'2251 * parameters (e.g. " master ^next ^maint -- path").2252 * See if there is a dashdash first, and give the2253 * arguments before that to revision machinery.2254 * After that there must be one 'path'.2255 *2256 * (3) otherwise, its one of the three:2257 * "-options <path> <rev>"2258 * "-options <rev> <path>"2259 * "-options <path>"2260 * but again the first one is allowed only if2261 * there is no options that we passed to revision2262 * machinery.2263 */22642265if(seen_dashdash) {2266/* (1) */2267if(argc <= i)2268usage(blame_usage);2269 path =add_prefix(prefix, argv[i]);2270if(i +1== argc -1) {2271if(unk !=1)2272usage(blame_usage);2273 argv[unk++] = argv[i +1];2274}2275else if(i +1!= argc)2276/* garbage at end */2277usage(blame_usage);2278}2279else{2280int j;2281for(j = i; !seen_dashdash && j < argc; j++)2282if(!strcmp(argv[j],"--"))2283 seen_dashdash = j;2284if(seen_dashdash) {2285/* (2) */2286if(seen_dashdash +1!= argc -1)2287usage(blame_usage);2288 path =add_prefix(prefix, argv[seen_dashdash +1]);2289for(j = i; j < seen_dashdash; j++)2290 argv[unk++] = argv[j];2291}2292else{2293/* (3) */2294if(argc <= i)2295usage(blame_usage);2296 path =add_prefix(prefix, argv[i]);2297if(i +1== argc -1) {2298 final_commit_name = argv[i +1];22992300/* if (unk == 1) we could be getting2301 * old-style2302 */2303if(unk ==1&& !has_path_in_work_tree(path)) {2304 path =add_prefix(prefix, argv[i +1]);2305 final_commit_name = argv[i];2306}2307}2308else if(i != argc -1)2309usage(blame_usage);/* garbage at end */23102311setup_work_tree();2312if(!has_path_in_work_tree(path))2313die("cannot stat path%s:%s",2314 path,strerror(errno));2315}2316}23172318if(final_commit_name)2319 argv[unk++] = final_commit_name;23202321/*2322 * Now we got rev and path. We do not want the path pruning2323 * but we may want "bottom" processing.2324 */2325 argv[unk++] ="--";/* terminate the rev name */2326 argv[unk] = NULL;23272328init_revisions(&revs, NULL);2329setup_revisions(unk, argv, &revs, NULL);2330memset(&sb,0,sizeof(sb));23312332/*2333 * There must be one and only one positive commit in the2334 * revs->pending array.2335 */2336for(i =0; i < revs.pending.nr; i++) {2337struct object *obj = revs.pending.objects[i].item;2338if(obj->flags & UNINTERESTING)2339continue;2340while(obj->type == OBJ_TAG)2341 obj =deref_tag(obj, NULL,0);2342if(obj->type != OBJ_COMMIT)2343die("Non commit%s?",2344 revs.pending.objects[i].name);2345if(sb.final)2346die("More than one commit to dig from%sand%s?",2347 revs.pending.objects[i].name,2348 final_commit_name);2349 sb.final = (struct commit *) obj;2350 final_commit_name = revs.pending.objects[i].name;2351}23522353if(!sb.final) {2354/*2355 * "--not A B -- path" without anything positive;2356 * do not default to HEAD, but use the working tree2357 * or "--contents".2358 */2359setup_work_tree();2360 sb.final =fake_working_tree_commit(path, contents_from);2361add_pending_object(&revs, &(sb.final->object),":");2362}2363else if(contents_from)2364die("Cannot use --contents with final commit object name");23652366/*2367 * If we have bottom, this will mark the ancestors of the2368 * bottom commits we would reach while traversing as2369 * uninteresting.2370 */2371prepare_revision_walk(&revs);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}