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 133/* 134 * Each group of lines is described by a blame_entry; it can be split 135 * as we pass blame to the parents. They form a linked list in the 136 * scoreboard structure, sorted by the target line number. 137 */ 138struct blame_entry { 139struct blame_entry *prev; 140struct blame_entry *next; 141 142/* the first line of this group in the final image; 143 * internally all line numbers are 0 based. 144 */ 145int lno; 146 147/* how many lines this group has */ 148int num_lines; 149 150/* the commit that introduced this group into the final image */ 151struct origin *suspect; 152 153/* true if the suspect is truly guilty; false while we have not 154 * checked if the group came from one of its parents. 155 */ 156char guilty; 157 158/* the line number of the first line of this group in the 159 * suspect's file; internally all line numbers are 0 based. 160 */ 161int s_lno; 162 163/* how significant this entry is -- cached to avoid 164 * scanning the lines over and over. 165 */ 166unsigned score; 167}; 168 169/* 170 * The current state of the blame assignment. 171 */ 172struct scoreboard { 173/* the final commit (i.e. where we started digging from) */ 174struct commit *final; 175 176const char*path; 177 178/* 179 * The contents in the final image. 180 * Used by many functions to obtain contents of the nth line, 181 * indexed with scoreboard.lineno[blame_entry.lno]. 182 */ 183const char*final_buf; 184unsigned long final_buf_size; 185 186/* linked list of blames */ 187struct blame_entry *ent; 188 189/* look-up a line in the final buffer */ 190int num_lines; 191int*lineno; 192}; 193 194staticinlineintsame_suspect(struct origin *a,struct origin *b) 195{ 196if(a == b) 197return1; 198if(a->commit != b->commit) 199return0; 200return!strcmp(a->path, b->path); 201} 202 203static voidsanity_check_refcnt(struct scoreboard *); 204 205/* 206 * If two blame entries that are next to each other came from 207 * contiguous lines in the same origin (i.e. <commit, path> pair), 208 * merge them together. 209 */ 210static voidcoalesce(struct scoreboard *sb) 211{ 212struct blame_entry *ent, *next; 213 214for(ent = sb->ent; ent && (next = ent->next); ent = next) { 215if(same_suspect(ent->suspect, next->suspect) && 216 ent->guilty == next->guilty && 217 ent->s_lno + ent->num_lines == next->s_lno) { 218 ent->num_lines += next->num_lines; 219 ent->next = next->next; 220if(ent->next) 221 ent->next->prev = ent; 222origin_decref(next->suspect); 223free(next); 224 ent->score =0; 225 next = ent;/* again */ 226} 227} 228 229if(DEBUG)/* sanity */ 230sanity_check_refcnt(sb); 231} 232 233/* 234 * Given a commit and a path in it, create a new origin structure. 235 * The callers that add blame to the scoreboard should use 236 * get_origin() to obtain shared, refcounted copy instead of calling 237 * this function directly. 238 */ 239static struct origin *make_origin(struct commit *commit,const char*path) 240{ 241struct origin *o; 242 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 243 o->commit = commit; 244 o->refcnt =1; 245strcpy(o->path, path); 246return o; 247} 248 249/* 250 * Locate an existing origin or create a new one. 251 */ 252static struct origin *get_origin(struct scoreboard *sb, 253struct commit *commit, 254const char*path) 255{ 256struct blame_entry *e; 257 258for(e = sb->ent; e; e = e->next) { 259if(e->suspect->commit == commit && 260!strcmp(e->suspect->path, path)) 261returnorigin_incref(e->suspect); 262} 263returnmake_origin(commit, path); 264} 265 266/* 267 * Fill the blob_sha1 field of an origin if it hasn't, so that later 268 * call to fill_origin_blob() can use it to locate the data. blob_sha1 269 * for an origin is also used to pass the blame for the entire file to 270 * the parent to detect the case where a child's blob is identical to 271 * that of its parent's. 272 */ 273static intfill_blob_sha1(struct origin *origin) 274{ 275unsigned mode; 276 277if(!is_null_sha1(origin->blob_sha1)) 278return0; 279if(get_tree_entry(origin->commit->object.sha1, 280 origin->path, 281 origin->blob_sha1, &mode)) 282goto error_out; 283if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 284goto error_out; 285return0; 286 error_out: 287hashclr(origin->blob_sha1); 288return-1; 289} 290 291/* 292 * We have an origin -- check if the same path exists in the 293 * parent and return an origin structure to represent it. 294 */ 295static struct origin *find_origin(struct scoreboard *sb, 296struct commit *parent, 297struct origin *origin) 298{ 299struct origin *porigin = NULL; 300struct diff_options diff_opts; 301const char*paths[2]; 302 303if(parent->util) { 304/* 305 * Each commit object can cache one origin in that 306 * commit. This is a freestanding copy of origin and 307 * not refcounted. 308 */ 309struct origin *cached = parent->util; 310if(!strcmp(cached->path, origin->path)) { 311/* 312 * The same path between origin and its parent 313 * without renaming -- the most common case. 314 */ 315 porigin =get_origin(sb, parent, cached->path); 316 317/* 318 * If the origin was newly created (i.e. get_origin 319 * would call make_origin if none is found in the 320 * scoreboard), it does not know the blob_sha1, 321 * so copy it. Otherwise porigin was in the 322 * scoreboard and already knows blob_sha1. 323 */ 324if(porigin->refcnt ==1) 325hashcpy(porigin->blob_sha1, cached->blob_sha1); 326return porigin; 327} 328/* otherwise it was not very useful; free it */ 329free(parent->util); 330 parent->util = NULL; 331} 332 333/* See if the origin->path is different between parent 334 * and origin first. Most of the time they are the 335 * same and diff-tree is fairly efficient about this. 336 */ 337diff_setup(&diff_opts); 338 diff_opts.recursive =1; 339 diff_opts.detect_rename =0; 340 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 341 paths[0] = origin->path; 342 paths[1] = NULL; 343 344diff_tree_setup_paths(paths, &diff_opts); 345if(diff_setup_done(&diff_opts) <0) 346die("diff-setup"); 347 348if(is_null_sha1(origin->commit->object.sha1)) 349do_diff_cache(parent->tree->object.sha1, &diff_opts); 350else 351diff_tree_sha1(parent->tree->object.sha1, 352 origin->commit->tree->object.sha1, 353"", &diff_opts); 354diffcore_std(&diff_opts); 355 356/* It is either one entry that says "modified", or "created", 357 * or nothing. 358 */ 359if(!diff_queued_diff.nr) { 360/* The path is the same as parent */ 361 porigin =get_origin(sb, parent, origin->path); 362hashcpy(porigin->blob_sha1, origin->blob_sha1); 363} 364else if(diff_queued_diff.nr !=1) 365die("internal error in blame::find_origin"); 366else{ 367struct diff_filepair *p = diff_queued_diff.queue[0]; 368switch(p->status) { 369default: 370die("internal error in blame::find_origin (%c)", 371 p->status); 372case'M': 373 porigin =get_origin(sb, parent, origin->path); 374hashcpy(porigin->blob_sha1, p->one->sha1); 375break; 376case'A': 377case'T': 378/* Did not exist in parent, or type changed */ 379break; 380} 381} 382diff_flush(&diff_opts); 383if(porigin) { 384/* 385 * Create a freestanding copy that is not part of 386 * the refcounted origin found in the scoreboard, and 387 * cache it in the commit. 388 */ 389struct origin *cached; 390 391 cached =make_origin(porigin->commit, porigin->path); 392hashcpy(cached->blob_sha1, porigin->blob_sha1); 393 parent->util = cached; 394} 395return porigin; 396} 397 398/* 399 * We have an origin -- find the path that corresponds to it in its 400 * parent and return an origin structure to represent it. 401 */ 402static struct origin *find_rename(struct scoreboard *sb, 403struct commit *parent, 404struct origin *origin) 405{ 406struct origin *porigin = NULL; 407struct diff_options diff_opts; 408int i; 409const char*paths[2]; 410 411diff_setup(&diff_opts); 412 diff_opts.recursive =1; 413 diff_opts.detect_rename = DIFF_DETECT_RENAME; 414 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 415 diff_opts.single_follow = origin->path; 416 paths[0] = NULL; 417diff_tree_setup_paths(paths, &diff_opts); 418if(diff_setup_done(&diff_opts) <0) 419die("diff-setup"); 420 421if(is_null_sha1(origin->commit->object.sha1)) 422do_diff_cache(parent->tree->object.sha1, &diff_opts); 423else 424diff_tree_sha1(parent->tree->object.sha1, 425 origin->commit->tree->object.sha1, 426"", &diff_opts); 427diffcore_std(&diff_opts); 428 429for(i =0; i < diff_queued_diff.nr; i++) { 430struct diff_filepair *p = diff_queued_diff.queue[i]; 431if((p->status =='R'|| p->status =='C') && 432!strcmp(p->two->path, origin->path)) { 433 porigin =get_origin(sb, parent, p->one->path); 434hashcpy(porigin->blob_sha1, p->one->sha1); 435break; 436} 437} 438diff_flush(&diff_opts); 439return porigin; 440} 441 442/* 443 * Parsing of patch chunks... 444 */ 445struct chunk { 446/* line number in postimage; up to but not including this 447 * line is the same as preimage 448 */ 449int same; 450 451/* preimage line number after this chunk */ 452int p_next; 453 454/* postimage line number after this chunk */ 455int t_next; 456}; 457 458struct patch { 459struct chunk *chunks; 460int num; 461}; 462 463struct blame_diff_state { 464struct xdiff_emit_state xm; 465struct patch *ret; 466unsigned hunk_post_context; 467unsigned hunk_in_pre_context :1; 468}; 469 470static voidprocess_u_diff(void*state_,char*line,unsigned long len) 471{ 472struct blame_diff_state *state = state_; 473struct chunk *chunk; 474int off1, off2, len1, len2, num; 475 476 num = state->ret->num; 477if(len <4|| line[0] !='@'|| line[1] !='@') { 478if(state->hunk_in_pre_context && line[0] ==' ') 479 state->ret->chunks[num -1].same++; 480else{ 481 state->hunk_in_pre_context =0; 482if(line[0] ==' ') 483 state->hunk_post_context++; 484else 485 state->hunk_post_context =0; 486} 487return; 488} 489 490if(num && state->hunk_post_context) { 491 chunk = &state->ret->chunks[num -1]; 492 chunk->p_next -= state->hunk_post_context; 493 chunk->t_next -= state->hunk_post_context; 494} 495 state->ret->num = ++num; 496 state->ret->chunks =xrealloc(state->ret->chunks, 497sizeof(struct chunk) * num); 498 chunk = &state->ret->chunks[num -1]; 499if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 500 state->ret->num--; 501return; 502} 503 504/* Line numbers in patch output are one based. */ 505 off1--; 506 off2--; 507 508 chunk->same = len2 ? off2 : (off2 +1); 509 510 chunk->p_next = off1 + (len1 ? len1 :1); 511 chunk->t_next = chunk->same + len2; 512 state->hunk_in_pre_context =1; 513 state->hunk_post_context =0; 514} 515 516static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 517int context) 518{ 519struct blame_diff_state state; 520 xpparam_t xpp; 521 xdemitconf_t xecfg; 522 xdemitcb_t ecb; 523 524 xpp.flags = xdl_opts; 525memset(&xecfg,0,sizeof(xecfg)); 526 xecfg.ctxlen = context; 527 ecb.outf = xdiff_outf; 528 ecb.priv = &state; 529memset(&state,0,sizeof(state)); 530 state.xm.consume = process_u_diff; 531 state.ret =xmalloc(sizeof(struct patch)); 532 state.ret->chunks = NULL; 533 state.ret->num =0; 534 535xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb); 536 537if(state.ret->num) { 538struct chunk *chunk; 539 chunk = &state.ret->chunks[state.ret->num -1]; 540 chunk->p_next -= state.hunk_post_context; 541 chunk->t_next -= state.hunk_post_context; 542} 543return state.ret; 544} 545 546/* 547 * Run diff between two origins and grab the patch output, so that 548 * we can pass blame for lines origin is currently suspected for 549 * to its parent. 550 */ 551static struct patch *get_patch(struct origin *parent,struct origin *origin) 552{ 553 mmfile_t file_p, file_o; 554struct patch *patch; 555 556fill_origin_blob(parent, &file_p); 557fill_origin_blob(origin, &file_o); 558if(!file_p.ptr || !file_o.ptr) 559return NULL; 560 patch =compare_buffer(&file_p, &file_o,0); 561 num_get_patch++; 562return patch; 563} 564 565static voidfree_patch(struct patch *p) 566{ 567free(p->chunks); 568free(p); 569} 570 571/* 572 * Link in a new blame entry to the scoreboard. Entries that cover the 573 * same line range have been removed from the scoreboard previously. 574 */ 575static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 576{ 577struct blame_entry *ent, *prev = NULL; 578 579origin_incref(e->suspect); 580 581for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 582 prev = ent; 583 584/* prev, if not NULL, is the last one that is below e */ 585 e->prev = prev; 586if(prev) { 587 e->next = prev->next; 588 prev->next = e; 589} 590else{ 591 e->next = sb->ent; 592 sb->ent = e; 593} 594if(e->next) 595 e->next->prev = e; 596} 597 598/* 599 * src typically is on-stack; we want to copy the information in it to 600 * an malloced blame_entry that is already on the linked list of the 601 * scoreboard. The origin of dst loses a refcnt while the origin of src 602 * gains one. 603 */ 604static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 605{ 606struct blame_entry *p, *n; 607 608 p = dst->prev; 609 n = dst->next; 610origin_incref(src->suspect); 611origin_decref(dst->suspect); 612memcpy(dst, src,sizeof(*src)); 613 dst->prev = p; 614 dst->next = n; 615 dst->score =0; 616} 617 618static const char*nth_line(struct scoreboard *sb,int lno) 619{ 620return sb->final_buf + sb->lineno[lno]; 621} 622 623/* 624 * It is known that lines between tlno to same came from parent, and e 625 * has an overlap with that range. it also is known that parent's 626 * line plno corresponds to e's line tlno. 627 * 628 * <---- e -----> 629 * <------> 630 * <------------> 631 * <------------> 632 * <------------------> 633 * 634 * Split e into potentially three parts; before this chunk, the chunk 635 * to be blamed for the parent, and after that portion. 636 */ 637static voidsplit_overlap(struct blame_entry *split, 638struct blame_entry *e, 639int tlno,int plno,int same, 640struct origin *parent) 641{ 642int chunk_end_lno; 643memset(split,0,sizeof(struct blame_entry [3])); 644 645if(e->s_lno < tlno) { 646/* there is a pre-chunk part not blamed on parent */ 647 split[0].suspect =origin_incref(e->suspect); 648 split[0].lno = e->lno; 649 split[0].s_lno = e->s_lno; 650 split[0].num_lines = tlno - e->s_lno; 651 split[1].lno = e->lno + tlno - e->s_lno; 652 split[1].s_lno = plno; 653} 654else{ 655 split[1].lno = e->lno; 656 split[1].s_lno = plno + (e->s_lno - tlno); 657} 658 659if(same < e->s_lno + e->num_lines) { 660/* there is a post-chunk part not blamed on parent */ 661 split[2].suspect =origin_incref(e->suspect); 662 split[2].lno = e->lno + (same - e->s_lno); 663 split[2].s_lno = e->s_lno + (same - e->s_lno); 664 split[2].num_lines = e->s_lno + e->num_lines - same; 665 chunk_end_lno = split[2].lno; 666} 667else 668 chunk_end_lno = e->lno + e->num_lines; 669 split[1].num_lines = chunk_end_lno - split[1].lno; 670 671/* 672 * if it turns out there is nothing to blame the parent for, 673 * forget about the splitting. !split[1].suspect signals this. 674 */ 675if(split[1].num_lines <1) 676return; 677 split[1].suspect =origin_incref(parent); 678} 679 680/* 681 * split_overlap() divided an existing blame e into up to three parts 682 * in split. Adjust the linked list of blames in the scoreboard to 683 * reflect the split. 684 */ 685static voidsplit_blame(struct scoreboard *sb, 686struct blame_entry *split, 687struct blame_entry *e) 688{ 689struct blame_entry *new_entry; 690 691if(split[0].suspect && split[2].suspect) { 692/* The first part (reuse storage for the existing entry e) */ 693dup_entry(e, &split[0]); 694 695/* The last part -- me */ 696 new_entry =xmalloc(sizeof(*new_entry)); 697memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 698add_blame_entry(sb, new_entry); 699 700/* ... and the middle part -- parent */ 701 new_entry =xmalloc(sizeof(*new_entry)); 702memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 703add_blame_entry(sb, new_entry); 704} 705else if(!split[0].suspect && !split[2].suspect) 706/* 707 * The parent covers the entire area; reuse storage for 708 * e and replace it with the parent. 709 */ 710dup_entry(e, &split[1]); 711else if(split[0].suspect) { 712/* me and then parent */ 713dup_entry(e, &split[0]); 714 715 new_entry =xmalloc(sizeof(*new_entry)); 716memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 717add_blame_entry(sb, new_entry); 718} 719else{ 720/* parent and then me */ 721dup_entry(e, &split[1]); 722 723 new_entry =xmalloc(sizeof(*new_entry)); 724memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 725add_blame_entry(sb, new_entry); 726} 727 728if(DEBUG) {/* sanity */ 729struct blame_entry *ent; 730int lno = sb->ent->lno, corrupt =0; 731 732for(ent = sb->ent; ent; ent = ent->next) { 733if(lno != ent->lno) 734 corrupt =1; 735if(ent->s_lno <0) 736 corrupt =1; 737 lno += ent->num_lines; 738} 739if(corrupt) { 740 lno = sb->ent->lno; 741for(ent = sb->ent; ent; ent = ent->next) { 742printf("L%8d l%8d n%8d\n", 743 lno, ent->lno, ent->num_lines); 744 lno = ent->lno + ent->num_lines; 745} 746die("oops"); 747} 748} 749} 750 751/* 752 * After splitting the blame, the origins used by the 753 * on-stack blame_entry should lose one refcnt each. 754 */ 755static voiddecref_split(struct blame_entry *split) 756{ 757int i; 758 759for(i =0; i <3; i++) 760origin_decref(split[i].suspect); 761} 762 763/* 764 * Helper for blame_chunk(). blame_entry e is known to overlap with 765 * the patch hunk; split it and pass blame to the parent. 766 */ 767static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 768int tlno,int plno,int same, 769struct origin *parent) 770{ 771struct blame_entry split[3]; 772 773split_overlap(split, e, tlno, plno, same, parent); 774if(split[1].suspect) 775split_blame(sb, split, e); 776decref_split(split); 777} 778 779/* 780 * Find the line number of the last line the target is suspected for. 781 */ 782static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 783{ 784struct blame_entry *e; 785int last_in_target = -1; 786 787for(e = sb->ent; e; e = e->next) { 788if(e->guilty || !same_suspect(e->suspect, target)) 789continue; 790if(last_in_target < e->s_lno + e->num_lines) 791 last_in_target = e->s_lno + e->num_lines; 792} 793return last_in_target; 794} 795 796/* 797 * Process one hunk from the patch between the current suspect for 798 * blame_entry e and its parent. Find and split the overlap, and 799 * pass blame to the overlapping part to the parent. 800 */ 801static voidblame_chunk(struct scoreboard *sb, 802int tlno,int plno,int same, 803struct origin *target,struct origin *parent) 804{ 805struct blame_entry *e; 806 807for(e = sb->ent; e; e = e->next) { 808if(e->guilty || !same_suspect(e->suspect, target)) 809continue; 810if(same <= e->s_lno) 811continue; 812if(tlno < e->s_lno + e->num_lines) 813blame_overlap(sb, e, tlno, plno, same, parent); 814} 815} 816 817/* 818 * We are looking at the origin 'target' and aiming to pass blame 819 * for the lines it is suspected to its parent. Run diff to find 820 * which lines came from parent and pass blame for them. 821 */ 822static intpass_blame_to_parent(struct scoreboard *sb, 823struct origin *target, 824struct origin *parent) 825{ 826int i, last_in_target, plno, tlno; 827struct patch *patch; 828 829 last_in_target =find_last_in_target(sb, target); 830if(last_in_target <0) 831return1;/* nothing remains for this target */ 832 833 patch =get_patch(parent, target); 834 plno = tlno =0; 835for(i =0; i < patch->num; i++) { 836struct chunk *chunk = &patch->chunks[i]; 837 838blame_chunk(sb, tlno, plno, chunk->same, target, parent); 839 plno = chunk->p_next; 840 tlno = chunk->t_next; 841} 842/* The rest (i.e. anything after tlno) are the same as the parent */ 843blame_chunk(sb, tlno, plno, last_in_target, target, parent); 844 845free_patch(patch); 846return0; 847} 848 849/* 850 * The lines in blame_entry after splitting blames many times can become 851 * very small and trivial, and at some point it becomes pointless to 852 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 853 * ordinary C program, and it is not worth to say it was copied from 854 * totally unrelated file in the parent. 855 * 856 * Compute how trivial the lines in the blame_entry are. 857 */ 858static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 859{ 860unsigned score; 861const char*cp, *ep; 862 863if(e->score) 864return e->score; 865 866 score =1; 867 cp =nth_line(sb, e->lno); 868 ep =nth_line(sb, e->lno + e->num_lines); 869while(cp < ep) { 870unsigned ch = *((unsigned char*)cp); 871if(isalnum(ch)) 872 score++; 873 cp++; 874} 875 e->score = score; 876return score; 877} 878 879/* 880 * best_so_far[] and this[] are both a split of an existing blame_entry 881 * that passes blame to the parent. Maintain best_so_far the best split 882 * so far, by comparing this and best_so_far and copying this into 883 * bst_so_far as needed. 884 */ 885static voidcopy_split_if_better(struct scoreboard *sb, 886struct blame_entry *best_so_far, 887struct blame_entry *this) 888{ 889int i; 890 891if(!this[1].suspect) 892return; 893if(best_so_far[1].suspect) { 894if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 895return; 896} 897 898for(i =0; i <3; i++) 899origin_incref(this[i].suspect); 900decref_split(best_so_far); 901memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 902} 903 904/* 905 * We are looking at a part of the final image represented by 906 * ent (tlno and same are offset by ent->s_lno). 907 * tlno is where we are looking at in the final image. 908 * up to (but not including) same match preimage. 909 * plno is where we are looking at in the preimage. 910 * 911 * <-------------- final image ----------------------> 912 * <------ent------> 913 * ^tlno ^same 914 * <---------preimage-----> 915 * ^plno 916 * 917 * All line numbers are 0-based. 918 */ 919static voidhandle_split(struct scoreboard *sb, 920struct blame_entry *ent, 921int tlno,int plno,int same, 922struct origin *parent, 923struct blame_entry *split) 924{ 925if(ent->num_lines <= tlno) 926return; 927if(tlno < same) { 928struct blame_entry this[3]; 929 tlno += ent->s_lno; 930 same += ent->s_lno; 931split_overlap(this, ent, tlno, plno, same, parent); 932copy_split_if_better(sb, split,this); 933decref_split(this); 934} 935} 936 937/* 938 * Find the lines from parent that are the same as ent so that 939 * we can pass blames to it. file_p has the blob contents for 940 * the parent. 941 */ 942static voidfind_copy_in_blob(struct scoreboard *sb, 943struct blame_entry *ent, 944struct origin *parent, 945struct blame_entry *split, 946 mmfile_t *file_p) 947{ 948const char*cp; 949int cnt; 950 mmfile_t file_o; 951struct patch *patch; 952int i, plno, tlno; 953 954/* 955 * Prepare mmfile that contains only the lines in ent. 956 */ 957 cp =nth_line(sb, ent->lno); 958 file_o.ptr = (char*) cp; 959 cnt = ent->num_lines; 960 961while(cnt && cp < sb->final_buf + sb->final_buf_size) { 962if(*cp++ =='\n') 963 cnt--; 964} 965 file_o.size = cp - file_o.ptr; 966 967 patch =compare_buffer(file_p, &file_o,1); 968 969/* 970 * file_o is a part of final image we are annotating. 971 * file_p partially may match that image. 972 */ 973memset(split,0,sizeof(struct blame_entry [3])); 974 plno = tlno =0; 975for(i =0; i < patch->num; i++) { 976struct chunk *chunk = &patch->chunks[i]; 977 978handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 979 plno = chunk->p_next; 980 tlno = chunk->t_next; 981} 982/* remainder, if any, all match the preimage */ 983handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 984free_patch(patch); 985} 986 987/* 988 * See if lines currently target is suspected for can be attributed to 989 * parent. 990 */ 991static intfind_move_in_parent(struct scoreboard *sb, 992struct origin *target, 993struct origin *parent) 994{ 995int last_in_target, made_progress; 996struct blame_entry *e, split[3]; 997 mmfile_t file_p; 998 999 last_in_target =find_last_in_target(sb, target);1000if(last_in_target <0)1001return1;/* nothing remains for this target */10021003fill_origin_blob(parent, &file_p);1004if(!file_p.ptr)1005return0;10061007 made_progress =1;1008while(made_progress) {1009 made_progress =0;1010for(e = sb->ent; e; e = e->next) {1011if(e->guilty || !same_suspect(e->suspect, target))1012continue;1013find_copy_in_blob(sb, e, parent, split, &file_p);1014if(split[1].suspect &&1015 blame_move_score <ent_score(sb, &split[1])) {1016split_blame(sb, split, e);1017 made_progress =1;1018}1019decref_split(split);1020}1021}1022return0;1023}10241025struct blame_list {1026struct blame_entry *ent;1027struct blame_entry split[3];1028};10291030/*1031 * Count the number of entries the target is suspected for,1032 * and prepare a list of entry and the best split.1033 */1034static struct blame_list *setup_blame_list(struct scoreboard *sb,1035struct origin *target,1036int*num_ents_p)1037{1038struct blame_entry *e;1039int num_ents, i;1040struct blame_list *blame_list = NULL;10411042for(e = sb->ent, num_ents =0; e; e = e->next)1043if(!e->guilty &&same_suspect(e->suspect, target))1044 num_ents++;1045if(num_ents) {1046 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1047for(e = sb->ent, i =0; e; e = e->next)1048if(!e->guilty &&same_suspect(e->suspect, target))1049 blame_list[i++].ent = e;1050}1051*num_ents_p = num_ents;1052return blame_list;1053}10541055/*1056 * For lines target is suspected for, see if we can find code movement1057 * across file boundary from the parent commit. porigin is the path1058 * in the parent we already tried.1059 */1060static intfind_copy_in_parent(struct scoreboard *sb,1061struct origin *target,1062struct commit *parent,1063struct origin *porigin,1064int opt)1065{1066struct diff_options diff_opts;1067const char*paths[1];1068int i, j;1069int retval;1070struct blame_list *blame_list;1071int num_ents;10721073 blame_list =setup_blame_list(sb, target, &num_ents);1074if(!blame_list)1075return1;/* nothing remains for this target */10761077diff_setup(&diff_opts);1078 diff_opts.recursive =1;1079 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10801081 paths[0] = NULL;1082diff_tree_setup_paths(paths, &diff_opts);1083if(diff_setup_done(&diff_opts) <0)1084die("diff-setup");10851086/* Try "find copies harder" on new path if requested;1087 * we do not want to use diffcore_rename() actually to1088 * match things up; find_copies_harder is set only to1089 * force diff_tree_sha1() to feed all filepairs to diff_queue,1090 * and this code needs to be after diff_setup_done(), which1091 * usually makes find-copies-harder imply copy detection.1092 */1093if((opt & PICKAXE_BLAME_COPY_HARDEST)1094|| ((opt & PICKAXE_BLAME_COPY_HARDER)1095&& (!porigin ||strcmp(target->path, porigin->path))))1096 diff_opts.find_copies_harder =1;10971098if(is_null_sha1(target->commit->object.sha1))1099do_diff_cache(parent->tree->object.sha1, &diff_opts);1100else1101diff_tree_sha1(parent->tree->object.sha1,1102 target->commit->tree->object.sha1,1103"", &diff_opts);11041105if(!diff_opts.find_copies_harder)1106diffcore_std(&diff_opts);11071108 retval =0;1109while(1) {1110int made_progress =0;11111112for(i =0; i < diff_queued_diff.nr; i++) {1113struct diff_filepair *p = diff_queued_diff.queue[i];1114struct origin *norigin;1115 mmfile_t file_p;1116struct blame_entry this[3];11171118if(!DIFF_FILE_VALID(p->one))1119continue;/* does not exist in parent */1120if(porigin && !strcmp(p->one->path, porigin->path))1121/* find_move already dealt with this path */1122continue;11231124 norigin =get_origin(sb, parent, p->one->path);1125hashcpy(norigin->blob_sha1, p->one->sha1);1126fill_origin_blob(norigin, &file_p);1127if(!file_p.ptr)1128continue;11291130for(j =0; j < num_ents; j++) {1131find_copy_in_blob(sb, blame_list[j].ent,1132 norigin,this, &file_p);1133copy_split_if_better(sb, blame_list[j].split,1134this);1135decref_split(this);1136}1137origin_decref(norigin);1138}11391140for(j =0; j < num_ents; j++) {1141struct blame_entry *split = blame_list[j].split;1142if(split[1].suspect &&1143 blame_copy_score <ent_score(sb, &split[1])) {1144split_blame(sb, split, blame_list[j].ent);1145 made_progress =1;1146}1147decref_split(split);1148}1149free(blame_list);11501151if(!made_progress)1152break;1153 blame_list =setup_blame_list(sb, target, &num_ents);1154if(!blame_list) {1155 retval =1;1156break;1157}1158}1159diff_flush(&diff_opts);11601161return retval;1162}11631164/*1165 * The blobs of origin and porigin exactly match, so everything1166 * origin is suspected for can be blamed on the parent.1167 */1168static voidpass_whole_blame(struct scoreboard *sb,1169struct origin *origin,struct origin *porigin)1170{1171struct blame_entry *e;11721173if(!porigin->file.ptr && origin->file.ptr) {1174/* Steal its file */1175 porigin->file = origin->file;1176 origin->file.ptr = NULL;1177}1178for(e = sb->ent; e; e = e->next) {1179if(!same_suspect(e->suspect, origin))1180continue;1181origin_incref(porigin);1182origin_decref(e->suspect);1183 e->suspect = porigin;1184}1185}11861187#define MAXPARENT 1611881189static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1190{1191int i, pass;1192struct commit *commit = origin->commit;1193struct commit_list *parent;1194struct origin *parent_origin[MAXPARENT], *porigin;11951196memset(parent_origin,0,sizeof(parent_origin));11971198/* The first pass looks for unrenamed path to optimize for1199 * common cases, then we look for renames in the second pass.1200 */1201for(pass =0; pass <2; pass++) {1202struct origin *(*find)(struct scoreboard *,1203struct commit *,struct origin *);1204 find = pass ? find_rename : find_origin;12051206for(i =0, parent = commit->parents;1207 i < MAXPARENT && parent;1208 parent = parent->next, i++) {1209struct commit *p = parent->item;1210int j, same;12111212if(parent_origin[i])1213continue;1214if(parse_commit(p))1215continue;1216 porigin =find(sb, p, origin);1217if(!porigin)1218continue;1219if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1220pass_whole_blame(sb, origin, porigin);1221origin_decref(porigin);1222goto finish;1223}1224for(j = same =0; j < i; j++)1225if(parent_origin[j] &&1226!hashcmp(parent_origin[j]->blob_sha1,1227 porigin->blob_sha1)) {1228 same =1;1229break;1230}1231if(!same)1232 parent_origin[i] = porigin;1233else1234origin_decref(porigin);1235}1236}12371238 num_commits++;1239for(i =0, parent = commit->parents;1240 i < MAXPARENT && parent;1241 parent = parent->next, i++) {1242struct origin *porigin = parent_origin[i];1243if(!porigin)1244continue;1245if(pass_blame_to_parent(sb, origin, porigin))1246goto finish;1247}12481249/*1250 * Optionally find moves in parents' files.1251 */1252if(opt & PICKAXE_BLAME_MOVE)1253for(i =0, parent = commit->parents;1254 i < MAXPARENT && parent;1255 parent = parent->next, i++) {1256struct origin *porigin = parent_origin[i];1257if(!porigin)1258continue;1259if(find_move_in_parent(sb, origin, porigin))1260goto finish;1261}12621263/*1264 * Optionally find copies from parents' files.1265 */1266if(opt & PICKAXE_BLAME_COPY)1267for(i =0, parent = commit->parents;1268 i < MAXPARENT && parent;1269 parent = parent->next, i++) {1270struct origin *porigin = parent_origin[i];1271if(find_copy_in_parent(sb, origin, parent->item,1272 porigin, opt))1273goto finish;1274}12751276 finish:1277for(i =0; i < MAXPARENT; i++)1278origin_decref(parent_origin[i]);1279}12801281/*1282 * Information on commits, used for output.1283 */1284struct commit_info1285{1286const char*author;1287const char*author_mail;1288unsigned long author_time;1289const char*author_tz;12901291/* filled only when asked for details */1292const char*committer;1293const char*committer_mail;1294unsigned long committer_time;1295const char*committer_tz;12961297const char*summary;1298};12991300/*1301 * Parse author/committer line in the commit object buffer1302 */1303static voidget_ac_line(const char*inbuf,const char*what,1304int bufsz,char*person,const char**mail,1305unsigned long*time,const char**tz)1306{1307int len, tzlen, maillen;1308char*tmp, *endp, *timepos;13091310 tmp =strstr(inbuf, what);1311if(!tmp)1312goto error_out;1313 tmp +=strlen(what);1314 endp =strchr(tmp,'\n');1315if(!endp)1316 len =strlen(tmp);1317else1318 len = endp - tmp;1319if(bufsz <= len) {1320 error_out:1321/* Ugh */1322*mail = *tz ="(unknown)";1323*time =0;1324return;1325}1326memcpy(person, tmp, len);13271328 tmp = person;1329 tmp += len;1330*tmp =0;1331while(*tmp !=' ')1332 tmp--;1333*tz = tmp+1;1334 tzlen = (person+len)-(tmp+1);13351336*tmp =0;1337while(*tmp !=' ')1338 tmp--;1339*time =strtoul(tmp, NULL,10);1340 timepos = tmp;13411342*tmp =0;1343while(*tmp !=' ')1344 tmp--;1345*mail = tmp +1;1346*tmp =0;1347 maillen = timepos - tmp;13481349if(!mailmap.nr)1350return;13511352/*1353 * mailmap expansion may make the name longer.1354 * make room by pushing stuff down.1355 */1356 tmp = person + bufsz - (tzlen +1);1357memmove(tmp, *tz, tzlen);1358 tmp[tzlen] =0;1359*tz = tmp;13601361 tmp = tmp - (maillen +1);1362memmove(tmp, *mail, maillen);1363 tmp[maillen] =0;1364*mail = tmp;13651366/*1367 * Now, convert e-mail using mailmap1368 */1369map_email(&mailmap, tmp +1, person, tmp-person-1);1370}13711372static voidget_commit_info(struct commit *commit,1373struct commit_info *ret,1374int detailed)1375{1376int len;1377char*tmp, *endp;1378static char author_buf[1024];1379static char committer_buf[1024];1380static char summary_buf[1024];13811382/*1383 * We've operated without save_commit_buffer, so1384 * we now need to populate them for output.1385 */1386if(!commit->buffer) {1387enum object_type type;1388unsigned long size;1389 commit->buffer =1390read_sha1_file(commit->object.sha1, &type, &size);1391if(!commit->buffer)1392die("Cannot read commit%s",1393sha1_to_hex(commit->object.sha1));1394}1395 ret->author = author_buf;1396get_ac_line(commit->buffer,"\nauthor ",1397sizeof(author_buf), author_buf, &ret->author_mail,1398&ret->author_time, &ret->author_tz);13991400if(!detailed)1401return;14021403 ret->committer = committer_buf;1404get_ac_line(commit->buffer,"\ncommitter ",1405sizeof(committer_buf), committer_buf, &ret->committer_mail,1406&ret->committer_time, &ret->committer_tz);14071408 ret->summary = summary_buf;1409 tmp =strstr(commit->buffer,"\n\n");1410if(!tmp) {1411 error_out:1412sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1413return;1414}1415 tmp +=2;1416 endp =strchr(tmp,'\n');1417if(!endp)1418 endp = tmp +strlen(tmp);1419 len = endp - tmp;1420if(len >=sizeof(summary_buf) || len ==0)1421goto error_out;1422memcpy(summary_buf, tmp, len);1423 summary_buf[len] =0;1424}14251426/*1427 * To allow LF and other nonportable characters in pathnames,1428 * they are c-style quoted as needed.1429 */1430static voidwrite_filename_info(const char*path)1431{1432printf("filename ");1433write_name_quoted(NULL,0, path,1, stdout);1434putchar('\n');1435}14361437/*1438 * The blame_entry is found to be guilty for the range. Mark it1439 * as such, and show it in incremental output.1440 */1441static voidfound_guilty_entry(struct blame_entry *ent)1442{1443if(ent->guilty)1444return;1445 ent->guilty =1;1446if(incremental) {1447struct origin *suspect = ent->suspect;14481449printf("%s %d %d %d\n",1450sha1_to_hex(suspect->commit->object.sha1),1451 ent->s_lno +1, ent->lno +1, ent->num_lines);1452if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1453struct commit_info ci;1454 suspect->commit->object.flags |= METAINFO_SHOWN;1455get_commit_info(suspect->commit, &ci,1);1456printf("author%s\n", ci.author);1457printf("author-mail%s\n", ci.author_mail);1458printf("author-time%lu\n", ci.author_time);1459printf("author-tz%s\n", ci.author_tz);1460printf("committer%s\n", ci.committer);1461printf("committer-mail%s\n", ci.committer_mail);1462printf("committer-time%lu\n", ci.committer_time);1463printf("committer-tz%s\n", ci.committer_tz);1464printf("summary%s\n", ci.summary);1465if(suspect->commit->object.flags & UNINTERESTING)1466printf("boundary\n");1467}1468write_filename_info(suspect->path);1469maybe_flush_or_die(stdout,"stdout");1470}1471}14721473/*1474 * The main loop -- while the scoreboard has lines whose true origin1475 * is still unknown, pick one blame_entry, and allow its current1476 * suspect to pass blames to its parents.1477 */1478static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1479{1480while(1) {1481struct blame_entry *ent;1482struct commit *commit;1483struct origin *suspect = NULL;14841485/* find one suspect to break down */1486for(ent = sb->ent; !suspect && ent; ent = ent->next)1487if(!ent->guilty)1488 suspect = ent->suspect;1489if(!suspect)1490return;/* all done */14911492/*1493 * We will use this suspect later in the loop,1494 * so hold onto it in the meantime.1495 */1496origin_incref(suspect);1497 commit = suspect->commit;1498if(!commit->object.parsed)1499parse_commit(commit);1500if(!(commit->object.flags & UNINTERESTING) &&1501!(revs->max_age != -1&& commit->date < revs->max_age))1502pass_blame(sb, suspect, opt);1503else{1504 commit->object.flags |= UNINTERESTING;1505if(commit->object.parsed)1506mark_parents_uninteresting(commit);1507}1508/* treat root commit as boundary */1509if(!commit->parents && !show_root)1510 commit->object.flags |= UNINTERESTING;15111512/* Take responsibility for the remaining entries */1513for(ent = sb->ent; ent; ent = ent->next)1514if(same_suspect(ent->suspect, suspect))1515found_guilty_entry(ent);1516origin_decref(suspect);15171518if(DEBUG)/* sanity */1519sanity_check_refcnt(sb);1520}1521}15221523static const char*format_time(unsigned long time,const char*tz_str,1524int show_raw_time)1525{1526static char time_buf[128];1527time_t t = time;1528int minutes, tz;1529struct tm *tm;15301531if(show_raw_time) {1532sprintf(time_buf,"%lu%s", time, tz_str);1533return time_buf;1534}15351536 tz =atoi(tz_str);1537 minutes = tz <0? -tz : tz;1538 minutes = (minutes /100)*60+ (minutes %100);1539 minutes = tz <0? -minutes : minutes;1540 t = time + minutes *60;1541 tm =gmtime(&t);15421543strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1544strcat(time_buf, tz_str);1545return time_buf;1546}15471548#define OUTPUT_ANNOTATE_COMPAT 0011549#define OUTPUT_LONG_OBJECT_NAME 0021550#define OUTPUT_RAW_TIMESTAMP 0041551#define OUTPUT_PORCELAIN 0101552#define OUTPUT_SHOW_NAME 0201553#define OUTPUT_SHOW_NUMBER 0401554#define OUTPUT_SHOW_SCORE 01001555#define OUTPUT_NO_AUTHOR 020015561557static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1558{1559int cnt;1560const char*cp;1561struct origin *suspect = ent->suspect;1562char hex[41];15631564strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1565printf("%s%c%d %d %d\n",1566 hex,1567 ent->guilty ?' ':'*',// purely for debugging1568 ent->s_lno +1,1569 ent->lno +1,1570 ent->num_lines);1571if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1572struct commit_info ci;1573 suspect->commit->object.flags |= METAINFO_SHOWN;1574get_commit_info(suspect->commit, &ci,1);1575printf("author%s\n", ci.author);1576printf("author-mail%s\n", ci.author_mail);1577printf("author-time%lu\n", ci.author_time);1578printf("author-tz%s\n", ci.author_tz);1579printf("committer%s\n", ci.committer);1580printf("committer-mail%s\n", ci.committer_mail);1581printf("committer-time%lu\n", ci.committer_time);1582printf("committer-tz%s\n", ci.committer_tz);1583write_filename_info(suspect->path);1584printf("summary%s\n", ci.summary);1585if(suspect->commit->object.flags & UNINTERESTING)1586printf("boundary\n");1587}1588else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1589write_filename_info(suspect->path);15901591 cp =nth_line(sb, ent->lno);1592for(cnt =0; cnt < ent->num_lines; cnt++) {1593char ch;1594if(cnt)1595printf("%s %d %d\n", hex,1596 ent->s_lno +1+ cnt,1597 ent->lno +1+ cnt);1598putchar('\t');1599do{1600 ch = *cp++;1601putchar(ch);1602}while(ch !='\n'&&1603 cp < sb->final_buf + sb->final_buf_size);1604}1605}16061607static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1608{1609int cnt;1610const char*cp;1611struct origin *suspect = ent->suspect;1612struct commit_info ci;1613char hex[41];1614int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16151616get_commit_info(suspect->commit, &ci,1);1617strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16181619 cp =nth_line(sb, ent->lno);1620for(cnt =0; cnt < ent->num_lines; cnt++) {1621char ch;1622int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16231624if(suspect->commit->object.flags & UNINTERESTING) {1625if(blank_boundary)1626memset(hex,' ', length);1627else if(!cmd_is_annotate) {1628 length--;1629putchar('^');1630}1631}16321633printf("%.*s", length, hex);1634if(opt & OUTPUT_ANNOTATE_COMPAT)1635printf("\t(%10s\t%10s\t%d)", ci.author,1636format_time(ci.author_time, ci.author_tz,1637 show_raw_time),1638 ent->lno +1+ cnt);1639else{1640if(opt & OUTPUT_SHOW_SCORE)1641printf(" %*d%02d",1642 max_score_digits, ent->score,1643 ent->suspect->refcnt);1644if(opt & OUTPUT_SHOW_NAME)1645printf(" %-*.*s", longest_file, longest_file,1646 suspect->path);1647if(opt & OUTPUT_SHOW_NUMBER)1648printf(" %*d", max_orig_digits,1649 ent->s_lno +1+ cnt);16501651if(!(opt & OUTPUT_NO_AUTHOR))1652printf(" (%-*.*s%10s",1653 longest_author, longest_author,1654 ci.author,1655format_time(ci.author_time,1656 ci.author_tz,1657 show_raw_time));1658printf(" %*d) ",1659 max_digits, ent->lno +1+ cnt);1660}1661do{1662 ch = *cp++;1663putchar(ch);1664}while(ch !='\n'&&1665 cp < sb->final_buf + sb->final_buf_size);1666}1667}16681669static voidoutput(struct scoreboard *sb,int option)1670{1671struct blame_entry *ent;16721673if(option & OUTPUT_PORCELAIN) {1674for(ent = sb->ent; ent; ent = ent->next) {1675struct blame_entry *oth;1676struct origin *suspect = ent->suspect;1677struct commit *commit = suspect->commit;1678if(commit->object.flags & MORE_THAN_ONE_PATH)1679continue;1680for(oth = ent->next; oth; oth = oth->next) {1681if((oth->suspect->commit != commit) ||1682!strcmp(oth->suspect->path, suspect->path))1683continue;1684 commit->object.flags |= MORE_THAN_ONE_PATH;1685break;1686}1687}1688}16891690for(ent = sb->ent; ent; ent = ent->next) {1691if(option & OUTPUT_PORCELAIN)1692emit_porcelain(sb, ent);1693else{1694emit_other(sb, ent, option);1695}1696}1697}16981699/*1700 * To allow quick access to the contents of nth line in the1701 * final image, prepare an index in the scoreboard.1702 */1703static intprepare_lines(struct scoreboard *sb)1704{1705const char*buf = sb->final_buf;1706unsigned long len = sb->final_buf_size;1707int num =0, incomplete =0, bol =1;17081709if(len && buf[len-1] !='\n')1710 incomplete++;/* incomplete line at the end */1711while(len--) {1712if(bol) {1713 sb->lineno =xrealloc(sb->lineno,1714sizeof(int* ) * (num +1));1715 sb->lineno[num] = buf - sb->final_buf;1716 bol =0;1717}1718if(*buf++ =='\n') {1719 num++;1720 bol =1;1721}1722}1723 sb->lineno =xrealloc(sb->lineno,1724sizeof(int* ) * (num + incomplete +1));1725 sb->lineno[num + incomplete] = buf - sb->final_buf;1726 sb->num_lines = num + incomplete;1727return sb->num_lines;1728}17291730/*1731 * Add phony grafts for use with -S; this is primarily to1732 * support git-cvsserver that wants to give a linear history1733 * to its clients.1734 */1735static intread_ancestry(const char*graft_file)1736{1737FILE*fp =fopen(graft_file,"r");1738char buf[1024];1739if(!fp)1740return-1;1741while(fgets(buf,sizeof(buf), fp)) {1742/* The format is just "Commit Parent1 Parent2 ...\n" */1743int len =strlen(buf);1744struct commit_graft *graft =read_graft_line(buf, len);1745if(graft)1746register_commit_graft(graft,0);1747}1748fclose(fp);1749return0;1750}17511752/*1753 * How many columns do we need to show line numbers in decimal?1754 */1755static intlineno_width(int lines)1756{1757int i, width;17581759for(width =1, i =10; i <= lines +1; width++)1760 i *=10;1761return width;1762}17631764/*1765 * How many columns do we need to show line numbers, authors,1766 * and filenames?1767 */1768static voidfind_alignment(struct scoreboard *sb,int*option)1769{1770int longest_src_lines =0;1771int longest_dst_lines =0;1772unsigned largest_score =0;1773struct blame_entry *e;17741775for(e = sb->ent; e; e = e->next) {1776struct origin *suspect = e->suspect;1777struct commit_info ci;1778int num;17791780if(strcmp(suspect->path, sb->path))1781*option |= OUTPUT_SHOW_NAME;1782 num =strlen(suspect->path);1783if(longest_file < num)1784 longest_file = num;1785if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1786 suspect->commit->object.flags |= METAINFO_SHOWN;1787get_commit_info(suspect->commit, &ci,1);1788 num =strlen(ci.author);1789if(longest_author < num)1790 longest_author = num;1791}1792 num = e->s_lno + e->num_lines;1793if(longest_src_lines < num)1794 longest_src_lines = num;1795 num = e->lno + e->num_lines;1796if(longest_dst_lines < num)1797 longest_dst_lines = num;1798if(largest_score <ent_score(sb, e))1799 largest_score =ent_score(sb, e);1800}1801 max_orig_digits =lineno_width(longest_src_lines);1802 max_digits =lineno_width(longest_dst_lines);1803 max_score_digits =lineno_width(largest_score);1804}18051806/*1807 * For debugging -- origin is refcounted, and this asserts that1808 * we do not underflow.1809 */1810static voidsanity_check_refcnt(struct scoreboard *sb)1811{1812int baa =0;1813struct blame_entry *ent;18141815for(ent = sb->ent; ent; ent = ent->next) {1816/* Nobody should have zero or negative refcnt */1817if(ent->suspect->refcnt <=0) {1818fprintf(stderr,"%sin%shas negative refcnt%d\n",1819 ent->suspect->path,1820sha1_to_hex(ent->suspect->commit->object.sha1),1821 ent->suspect->refcnt);1822 baa =1;1823}1824}1825for(ent = sb->ent; ent; ent = ent->next) {1826/* Mark the ones that haven't been checked */1827if(0< ent->suspect->refcnt)1828 ent->suspect->refcnt = -ent->suspect->refcnt;1829}1830for(ent = sb->ent; ent; ent = ent->next) {1831/*1832 * ... then pick each and see if they have the the1833 * correct refcnt.1834 */1835int found;1836struct blame_entry *e;1837struct origin *suspect = ent->suspect;18381839if(0< suspect->refcnt)1840continue;1841 suspect->refcnt = -suspect->refcnt;/* Unmark */1842for(found =0, e = sb->ent; e; e = e->next) {1843if(e->suspect != suspect)1844continue;1845 found++;1846}1847if(suspect->refcnt != found) {1848fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1849 ent->suspect->path,1850sha1_to_hex(ent->suspect->commit->object.sha1),1851 ent->suspect->refcnt, found);1852 baa =2;1853}1854}1855if(baa) {1856int opt =0160;1857find_alignment(sb, &opt);1858output(sb, opt);1859die("Baa%d!", baa);1860}1861}18621863/*1864 * Used for the command line parsing; check if the path exists1865 * in the working tree.1866 */1867static inthas_path_in_work_tree(const char*path)1868{1869struct stat st;1870return!lstat(path, &st);1871}18721873static unsignedparse_score(const char*arg)1874{1875char*end;1876unsigned long score =strtoul(arg, &end,10);1877if(*end)1878return0;1879return score;1880}18811882static const char*add_prefix(const char*prefix,const char*path)1883{1884if(!prefix || !prefix[0])1885return path;1886returnprefix_path(prefix,strlen(prefix), path);1887}18881889/*1890 * Parsing of (comma separated) one item in the -L option1891 */1892static const char*parse_loc(const char*spec,1893struct scoreboard *sb,long lno,1894long begin,long*ret)1895{1896char*term;1897const char*line;1898long num;1899int reg_error;1900 regex_t regexp;1901 regmatch_t match[1];19021903/* Allow "-L <something>,+20" to mean starting at <something>1904 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1905 * <something>.1906 */1907if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1908 num =strtol(spec +1, &term,10);1909if(term != spec +1) {1910if(spec[0] =='-')1911 num =0- num;1912if(0< num)1913*ret = begin + num -2;1914else if(!num)1915*ret = begin;1916else1917*ret = begin + num;1918return term;1919}1920return spec;1921}1922 num =strtol(spec, &term,10);1923if(term != spec) {1924*ret = num;1925return term;1926}1927if(spec[0] !='/')1928return spec;19291930/* it could be a regexp of form /.../ */1931for(term = (char*) spec +1; *term && *term !='/'; term++) {1932if(*term =='\\')1933 term++;1934}1935if(*term !='/')1936return spec;19371938/* try [spec+1 .. term-1] as regexp */1939*term =0;1940 begin--;/* input is in human terms */1941 line =nth_line(sb, begin);19421943if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1944!(reg_error =regexec(®exp, line,1, match,0))) {1945const char*cp = line + match[0].rm_so;1946const char*nline;19471948while(begin++ < lno) {1949 nline =nth_line(sb, begin);1950if(line <= cp && cp < nline)1951break;1952 line = nline;1953}1954*ret = begin;1955regfree(®exp);1956*term++ ='/';1957return term;1958}1959else{1960char errbuf[1024];1961regerror(reg_error, ®exp, errbuf,1024);1962die("-L parameter '%s':%s", spec +1, errbuf);1963}1964}19651966/*1967 * Parsing of -L option1968 */1969static voidprepare_blame_range(struct scoreboard *sb,1970const char*bottomtop,1971long lno,1972long*bottom,long*top)1973{1974const char*term;19751976 term =parse_loc(bottomtop, sb, lno,1, bottom);1977if(*term ==',') {1978 term =parse_loc(term +1, sb, lno, *bottom +1, top);1979if(*term)1980usage(blame_usage);1981}1982if(*term)1983usage(blame_usage);1984}19851986static intgit_blame_config(const char*var,const char*value)1987{1988if(!strcmp(var,"blame.showroot")) {1989 show_root =git_config_bool(var, value);1990return0;1991}1992if(!strcmp(var,"blame.blankboundary")) {1993 blank_boundary =git_config_bool(var, value);1994return0;1995}1996returngit_default_config(var, value);1997}19981999static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2000{2001struct commit *commit;2002struct origin *origin;2003unsigned char head_sha1[20];2004struct strbuf buf;2005const char*ident;2006int fd;2007time_t now;2008int size, len;2009struct cache_entry *ce;2010unsigned mode;20112012if(get_sha1("HEAD", head_sha1))2013die("No such ref: HEAD");20142015time(&now);2016 commit =xcalloc(1,sizeof(*commit));2017 commit->parents =xcalloc(1,sizeof(*commit->parents));2018 commit->parents->item =lookup_commit_reference(head_sha1);2019 commit->object.parsed =1;2020 commit->date = now;2021 commit->object.type = OBJ_COMMIT;20222023 origin =make_origin(commit, path);20242025strbuf_init(&buf,0);2026if(!contents_from ||strcmp("-", contents_from)) {2027struct stat st;2028const char*read_from;2029unsigned long fin_size;20302031if(contents_from) {2032if(stat(contents_from, &st) <0)2033die("Cannot stat%s", contents_from);2034 read_from = contents_from;2035}2036else{2037if(lstat(path, &st) <0)2038die("Cannot lstat%s", path);2039 read_from = path;2040}2041 fin_size =xsize_t(st.st_size);2042 mode =canon_mode(st.st_mode);2043switch(st.st_mode & S_IFMT) {2044case S_IFREG:2045 fd =open(read_from, O_RDONLY);2046if(fd <0)2047die("cannot open%s", read_from);2048if(strbuf_read(&buf, fd,0) !=xsize_t(st.st_size))2049die("cannot read%s", read_from);2050break;2051case S_IFLNK:2052if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2053die("cannot readlink%s", read_from);2054 buf.len = fin_size;2055break;2056default:2057die("unsupported file type%s", read_from);2058}2059}2060else{2061/* Reading from stdin */2062 contents_from ="standard input";2063 mode =0;2064if(strbuf_read(&buf,0,0) <0)2065die("read error%sfrom stdin",strerror(errno));2066}2067 origin->file.ptr = buf.buf;2068 origin->file.size = buf.len;2069pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2070 commit->util = origin;20712072/*2073 * Read the current index, replace the path entry with2074 * origin->blob_sha1 without mucking with its mode or type2075 * bits; we are not going to write this index out -- we just2076 * want to run "diff-index --cached".2077 */2078discard_cache();2079read_cache();20802081 len =strlen(path);2082if(!mode) {2083int pos =cache_name_pos(path, len);2084if(0<= pos)2085 mode =ntohl(active_cache[pos]->ce_mode);2086else2087/* Let's not bother reading from HEAD tree */2088 mode = S_IFREG |0644;2089}2090 size =cache_entry_size(len);2091 ce =xcalloc(1, size);2092hashcpy(ce->sha1, origin->blob_sha1);2093memcpy(ce->name, path, len);2094 ce->ce_flags =create_ce_flags(len,0);2095 ce->ce_mode =create_ce_mode(mode);2096add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);20972098/*2099 * We are not going to write this out, so this does not matter2100 * right now, but someday we might optimize diff-index --cached2101 * with cache-tree information.2102 */2103cache_tree_invalidate_path(active_cache_tree, path);21042105 commit->buffer =xmalloc(400);2106 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2107snprintf(commit->buffer,400,2108"tree 0000000000000000000000000000000000000000\n"2109"parent%s\n"2110"author%s\n"2111"committer%s\n\n"2112"Version of%sfrom%s\n",2113sha1_to_hex(head_sha1),2114 ident, ident, path, contents_from ? contents_from : path);2115return commit;2116}21172118intcmd_blame(int argc,const char**argv,const char*prefix)2119{2120struct rev_info revs;2121const char*path;2122struct scoreboard sb;2123struct origin *o;2124struct blame_entry *ent;2125int i, seen_dashdash, unk, opt;2126long bottom, top, lno;2127int output_option =0;2128int show_stats =0;2129const char*revs_file = NULL;2130const char*final_commit_name = NULL;2131enum object_type type;2132const char*bottomtop = NULL;2133const char*contents_from = NULL;21342135 cmd_is_annotate = !strcmp(argv[0],"annotate");21362137git_config(git_blame_config);2138 save_commit_buffer =0;21392140 opt =0;2141 seen_dashdash =0;2142for(unk = i =1; i < argc; i++) {2143const char*arg = argv[i];2144if(*arg !='-')2145break;2146else if(!strcmp("-b", arg))2147 blank_boundary =1;2148else if(!strcmp("--root", arg))2149 show_root =1;2150else if(!strcmp(arg,"--show-stats"))2151 show_stats =1;2152else if(!strcmp("-c", arg))2153 output_option |= OUTPUT_ANNOTATE_COMPAT;2154else if(!strcmp("-t", arg))2155 output_option |= OUTPUT_RAW_TIMESTAMP;2156else if(!strcmp("-l", arg))2157 output_option |= OUTPUT_LONG_OBJECT_NAME;2158else if(!strcmp("-s", arg))2159 output_option |= OUTPUT_NO_AUTHOR;2160else if(!strcmp("-w", arg))2161 xdl_opts |= XDF_IGNORE_WHITESPACE;2162else if(!strcmp("-S", arg) && ++i < argc)2163 revs_file = argv[i];2164else if(!prefixcmp(arg,"-M")) {2165 opt |= PICKAXE_BLAME_MOVE;2166 blame_move_score =parse_score(arg+2);2167}2168else if(!prefixcmp(arg,"-C")) {2169/*2170 * -C enables copy from removed files;2171 * -C -C enables copy from existing files, but only2172 * when blaming a new file;2173 * -C -C -C enables copy from existing files for2174 * everybody2175 */2176if(opt & PICKAXE_BLAME_COPY_HARDER)2177 opt |= PICKAXE_BLAME_COPY_HARDEST;2178if(opt & PICKAXE_BLAME_COPY)2179 opt |= PICKAXE_BLAME_COPY_HARDER;2180 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2181 blame_copy_score =parse_score(arg+2);2182}2183else if(!prefixcmp(arg,"-L")) {2184if(!arg[2]) {2185if(++i >= argc)2186usage(blame_usage);2187 arg = argv[i];2188}2189else2190 arg +=2;2191if(bottomtop)2192die("More than one '-L n,m' option given");2193 bottomtop = arg;2194}2195else if(!strcmp("--contents", arg)) {2196if(++i >= argc)2197usage(blame_usage);2198 contents_from = argv[i];2199}2200else if(!strcmp("--incremental", arg))2201 incremental =1;2202else if(!strcmp("--score-debug", arg))2203 output_option |= OUTPUT_SHOW_SCORE;2204else if(!strcmp("-f", arg) ||2205!strcmp("--show-name", arg))2206 output_option |= OUTPUT_SHOW_NAME;2207else if(!strcmp("-n", arg) ||2208!strcmp("--show-number", arg))2209 output_option |= OUTPUT_SHOW_NUMBER;2210else if(!strcmp("-p", arg) ||2211!strcmp("--porcelain", arg))2212 output_option |= OUTPUT_PORCELAIN;2213else if(!strcmp("--", arg)) {2214 seen_dashdash =1;2215 i++;2216break;2217}2218else2219 argv[unk++] = arg;2220}22212222if(!incremental)2223setup_pager();22242225if(!blame_move_score)2226 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2227if(!blame_copy_score)2228 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22292230/*2231 * We have collected options unknown to us in argv[1..unk]2232 * which are to be passed to revision machinery if we are2233 * going to do the "bottom" processing.2234 *2235 * The remaining are:2236 *2237 * (1) if seen_dashdash, its either2238 * "-options -- <path>" or2239 * "-options -- <path> <rev>".2240 * but the latter is allowed only if there is no2241 * options that we passed to revision machinery.2242 *2243 * (2) otherwise, we may have "--" somewhere later and2244 * might be looking at the first one of multiple 'rev'2245 * parameters (e.g. " master ^next ^maint -- path").2246 * See if there is a dashdash first, and give the2247 * arguments before that to revision machinery.2248 * After that there must be one 'path'.2249 *2250 * (3) otherwise, its one of the three:2251 * "-options <path> <rev>"2252 * "-options <rev> <path>"2253 * "-options <path>"2254 * but again the first one is allowed only if2255 * there is no options that we passed to revision2256 * machinery.2257 */22582259if(seen_dashdash) {2260/* (1) */2261if(argc <= i)2262usage(blame_usage);2263 path =add_prefix(prefix, argv[i]);2264if(i +1== argc -1) {2265if(unk !=1)2266usage(blame_usage);2267 argv[unk++] = argv[i +1];2268}2269else if(i +1!= argc)2270/* garbage at end */2271usage(blame_usage);2272}2273else{2274int j;2275for(j = i; !seen_dashdash && j < argc; j++)2276if(!strcmp(argv[j],"--"))2277 seen_dashdash = j;2278if(seen_dashdash) {2279/* (2) */2280if(seen_dashdash +1!= argc -1)2281usage(blame_usage);2282 path =add_prefix(prefix, argv[seen_dashdash +1]);2283for(j = i; j < seen_dashdash; j++)2284 argv[unk++] = argv[j];2285}2286else{2287/* (3) */2288if(argc <= i)2289usage(blame_usage);2290 path =add_prefix(prefix, argv[i]);2291if(i +1== argc -1) {2292 final_commit_name = argv[i +1];22932294/* if (unk == 1) we could be getting2295 * old-style2296 */2297if(unk ==1&& !has_path_in_work_tree(path)) {2298 path =add_prefix(prefix, argv[i +1]);2299 final_commit_name = argv[i];2300}2301}2302else if(i != argc -1)2303usage(blame_usage);/* garbage at end */23042305if(!has_path_in_work_tree(path))2306die("cannot stat path%s:%s",2307 path,strerror(errno));2308}2309}23102311if(final_commit_name)2312 argv[unk++] = final_commit_name;23132314/*2315 * Now we got rev and path. We do not want the path pruning2316 * but we may want "bottom" processing.2317 */2318 argv[unk++] ="--";/* terminate the rev name */2319 argv[unk] = NULL;23202321init_revisions(&revs, NULL);2322setup_revisions(unk, argv, &revs, NULL);2323memset(&sb,0,sizeof(sb));23242325/*2326 * There must be one and only one positive commit in the2327 * revs->pending array.2328 */2329for(i =0; i < revs.pending.nr; i++) {2330struct object *obj = revs.pending.objects[i].item;2331if(obj->flags & UNINTERESTING)2332continue;2333while(obj->type == OBJ_TAG)2334 obj =deref_tag(obj, NULL,0);2335if(obj->type != OBJ_COMMIT)2336die("Non commit%s?",2337 revs.pending.objects[i].name);2338if(sb.final)2339die("More than one commit to dig from%sand%s?",2340 revs.pending.objects[i].name,2341 final_commit_name);2342 sb.final = (struct commit *) obj;2343 final_commit_name = revs.pending.objects[i].name;2344}23452346if(!sb.final) {2347/*2348 * "--not A B -- path" without anything positive;2349 * do not default to HEAD, but use the working tree2350 * or "--contents".2351 */2352 sb.final =fake_working_tree_commit(path, contents_from);2353add_pending_object(&revs, &(sb.final->object),":");2354}2355else if(contents_from)2356die("Cannot use --contents with final commit object name");23572358/*2359 * If we have bottom, this will mark the ancestors of the2360 * bottom commits we would reach while traversing as2361 * uninteresting.2362 */2363prepare_revision_walk(&revs);23642365if(is_null_sha1(sb.final->object.sha1)) {2366char*buf;2367 o = sb.final->util;2368 buf =xmalloc(o->file.size +1);2369memcpy(buf, o->file.ptr, o->file.size +1);2370 sb.final_buf = buf;2371 sb.final_buf_size = o->file.size;2372}2373else{2374 o =get_origin(&sb, sb.final, path);2375if(fill_blob_sha1(o))2376die("no such path%sin%s", path, final_commit_name);23772378 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2379&sb.final_buf_size);2380if(!sb.final_buf)2381die("Cannot read blob%sfor path%s",2382sha1_to_hex(o->blob_sha1),2383 path);2384}2385 num_read_blob++;2386 lno =prepare_lines(&sb);23872388 bottom = top =0;2389if(bottomtop)2390prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2391if(bottom && top && top < bottom) {2392long tmp;2393 tmp = top; top = bottom; bottom = tmp;2394}2395if(bottom <1)2396 bottom =1;2397if(top <1)2398 top = lno;2399 bottom--;2400if(lno < top)2401die("file%shas only%lu lines", path, lno);24022403 ent =xcalloc(1,sizeof(*ent));2404 ent->lno = bottom;2405 ent->num_lines = top - bottom;2406 ent->suspect = o;2407 ent->s_lno = bottom;24082409 sb.ent = ent;2410 sb.path = path;24112412if(revs_file &&read_ancestry(revs_file))2413die("reading graft file%sfailed:%s",2414 revs_file,strerror(errno));24152416read_mailmap(&mailmap,".mailmap", NULL);24172418assign_blame(&sb, &revs, opt);24192420if(incremental)2421return0;24222423coalesce(&sb);24242425if(!(output_option & OUTPUT_PORCELAIN))2426find_alignment(&sb, &output_option);24272428output(&sb, output_option);2429free((void*)sb.final_buf);2430for(ent = sb.ent; ent; ) {2431struct blame_entry *e = ent->next;2432free(ent);2433 ent = e;2434}24352436if(show_stats) {2437printf("num read blob:%d\n", num_read_blob);2438printf("num get patch:%d\n", num_get_patch);2439printf("num commits:%d\n", num_commits);2440}2441return0;2442}