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#include"strbuf.h" 22 23static char blame_usage[] = 24"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" 25" -c Use the same output mode as git-annotate (Default: off)\n" 26" -b Show blank SHA-1 for boundary commits (Default: off)\n" 27" -l Show long commit SHA1 (Default: off)\n" 28" --root Do not treat root commits as boundaries (Default: off)\n" 29" -t Show raw timestamp (Default: off)\n" 30" -f, --show-name Show original filename (Default: auto)\n" 31" -n, --show-number Show original linenumber (Default: off)\n" 32" -s Suppress author name and timestamp (Default: off)\n" 33" -p, --porcelain Show in a format designed for machine consumption\n" 34" -w Ignore whitespace differences\n" 35" -L n,m Process only line range n,m, counting from 1\n" 36" -M, -C Find line movements within and across files\n" 37" --incremental Show blame entries as we find them, incrementally\n" 38" --contents file Use <file>'s contents as the final image\n" 39" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"; 40 41static int longest_file; 42static int longest_author; 43static int max_orig_digits; 44static int max_digits; 45static int max_score_digits; 46static int show_root; 47static int blank_boundary; 48static int incremental; 49static int cmd_is_annotate; 50static int xdl_opts = XDF_NEED_MINIMAL; 51static struct path_list mailmap; 52 53#ifndef DEBUG 54#define DEBUG 0 55#endif 56 57/* stats */ 58static int num_read_blob; 59static int num_get_patch; 60static int num_commits; 61 62#define PICKAXE_BLAME_MOVE 01 63#define PICKAXE_BLAME_COPY 02 64#define PICKAXE_BLAME_COPY_HARDER 04 65#define PICKAXE_BLAME_COPY_HARDEST 010 66 67/* 68 * blame for a blame_entry with score lower than these thresholds 69 * is not passed to the parent using move/copy logic. 70 */ 71static unsigned blame_move_score; 72static unsigned blame_copy_score; 73#define BLAME_DEFAULT_MOVE_SCORE 20 74#define BLAME_DEFAULT_COPY_SCORE 40 75 76/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ 77#define METAINFO_SHOWN (1u<<12) 78#define MORE_THAN_ONE_PATH (1u<<13) 79 80/* 81 * One blob in a commit that is being suspected 82 */ 83struct origin { 84int refcnt; 85struct commit *commit; 86 mmfile_t file; 87unsigned char blob_sha1[20]; 88char path[FLEX_ARRAY]; 89}; 90 91/* 92 * Given an origin, prepare mmfile_t structure to be used by the 93 * diff machinery 94 */ 95static char*fill_origin_blob(struct origin *o, mmfile_t *file) 96{ 97if(!o->file.ptr) { 98enum object_type type; 99 num_read_blob++; 100 file->ptr =read_sha1_file(o->blob_sha1, &type, 101(unsigned long*)(&(file->size))); 102if(!file->ptr) 103die("Cannot read blob%sfor path%s", 104sha1_to_hex(o->blob_sha1), 105 o->path); 106 o->file = *file; 107} 108else 109*file = o->file; 110return file->ptr; 111} 112 113/* 114 * Origin is refcounted and usually we keep the blob contents to be 115 * reused. 116 */ 117staticinlinestruct origin *origin_incref(struct origin *o) 118{ 119if(o) 120 o->refcnt++; 121return o; 122} 123 124static voidorigin_decref(struct origin *o) 125{ 126if(o && --o->refcnt <=0) { 127if(o->file.ptr) 128free(o->file.ptr); 129memset(o,0,sizeof(*o)); 130free(o); 131} 132} 133 134/* 135 * Each group of lines is described by a blame_entry; it can be split 136 * as we pass blame to the parents. They form a linked list in the 137 * scoreboard structure, sorted by the target line number. 138 */ 139struct blame_entry { 140struct blame_entry *prev; 141struct blame_entry *next; 142 143/* the first line of this group in the final image; 144 * internally all line numbers are 0 based. 145 */ 146int lno; 147 148/* how many lines this group has */ 149int num_lines; 150 151/* the commit that introduced this group into the final image */ 152struct origin *suspect; 153 154/* true if the suspect is truly guilty; false while we have not 155 * checked if the group came from one of its parents. 156 */ 157char guilty; 158 159/* the line number of the first line of this group in the 160 * suspect's file; internally all line numbers are 0 based. 161 */ 162int s_lno; 163 164/* how significant this entry is -- cached to avoid 165 * scanning the lines over and over. 166 */ 167unsigned score; 168}; 169 170/* 171 * The current state of the blame assignment. 172 */ 173struct scoreboard { 174/* the final commit (i.e. where we started digging from) */ 175struct commit *final; 176 177const char*path; 178 179/* 180 * The contents in the final image. 181 * Used by many functions to obtain contents of the nth line, 182 * indexed with scoreboard.lineno[blame_entry.lno]. 183 */ 184const char*final_buf; 185unsigned long final_buf_size; 186 187/* linked list of blames */ 188struct blame_entry *ent; 189 190/* look-up a line in the final buffer */ 191int num_lines; 192int*lineno; 193}; 194 195staticinlineintsame_suspect(struct origin *a,struct origin *b) 196{ 197if(a == b) 198return1; 199if(a->commit != b->commit) 200return0; 201return!strcmp(a->path, b->path); 202} 203 204static voidsanity_check_refcnt(struct scoreboard *); 205 206/* 207 * If two blame entries that are next to each other came from 208 * contiguous lines in the same origin (i.e. <commit, path> pair), 209 * merge them together. 210 */ 211static voidcoalesce(struct scoreboard *sb) 212{ 213struct blame_entry *ent, *next; 214 215for(ent = sb->ent; ent && (next = ent->next); ent = next) { 216if(same_suspect(ent->suspect, next->suspect) && 217 ent->guilty == next->guilty && 218 ent->s_lno + ent->num_lines == next->s_lno) { 219 ent->num_lines += next->num_lines; 220 ent->next = next->next; 221if(ent->next) 222 ent->next->prev = ent; 223origin_decref(next->suspect); 224free(next); 225 ent->score =0; 226 next = ent;/* again */ 227} 228} 229 230if(DEBUG)/* sanity */ 231sanity_check_refcnt(sb); 232} 233 234/* 235 * Given a commit and a path in it, create a new origin structure. 236 * The callers that add blame to the scoreboard should use 237 * get_origin() to obtain shared, refcounted copy instead of calling 238 * this function directly. 239 */ 240static struct origin *make_origin(struct commit *commit,const char*path) 241{ 242struct origin *o; 243 o =xcalloc(1,sizeof(*o) +strlen(path) +1); 244 o->commit = commit; 245 o->refcnt =1; 246strcpy(o->path, path); 247return o; 248} 249 250/* 251 * Locate an existing origin or create a new one. 252 */ 253static struct origin *get_origin(struct scoreboard *sb, 254struct commit *commit, 255const char*path) 256{ 257struct blame_entry *e; 258 259for(e = sb->ent; e; e = e->next) { 260if(e->suspect->commit == commit && 261!strcmp(e->suspect->path, path)) 262returnorigin_incref(e->suspect); 263} 264returnmake_origin(commit, path); 265} 266 267/* 268 * Fill the blob_sha1 field of an origin if it hasn't, so that later 269 * call to fill_origin_blob() can use it to locate the data. blob_sha1 270 * for an origin is also used to pass the blame for the entire file to 271 * the parent to detect the case where a child's blob is identical to 272 * that of its parent's. 273 */ 274static intfill_blob_sha1(struct origin *origin) 275{ 276unsigned mode; 277 278if(!is_null_sha1(origin->blob_sha1)) 279return0; 280if(get_tree_entry(origin->commit->object.sha1, 281 origin->path, 282 origin->blob_sha1, &mode)) 283goto error_out; 284if(sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) 285goto error_out; 286return0; 287 error_out: 288hashclr(origin->blob_sha1); 289return-1; 290} 291 292/* 293 * We have an origin -- check if the same path exists in the 294 * parent and return an origin structure to represent it. 295 */ 296static struct origin *find_origin(struct scoreboard *sb, 297struct commit *parent, 298struct origin *origin) 299{ 300struct origin *porigin = NULL; 301struct diff_options diff_opts; 302const char*paths[2]; 303 304if(parent->util) { 305/* 306 * Each commit object can cache one origin in that 307 * commit. This is a freestanding copy of origin and 308 * not refcounted. 309 */ 310struct origin *cached = parent->util; 311if(!strcmp(cached->path, origin->path)) { 312/* 313 * The same path between origin and its parent 314 * without renaming -- the most common case. 315 */ 316 porigin =get_origin(sb, parent, cached->path); 317 318/* 319 * If the origin was newly created (i.e. get_origin 320 * would call make_origin if none is found in the 321 * scoreboard), it does not know the blob_sha1, 322 * so copy it. Otherwise porigin was in the 323 * scoreboard and already knows blob_sha1. 324 */ 325if(porigin->refcnt ==1) 326hashcpy(porigin->blob_sha1, cached->blob_sha1); 327return porigin; 328} 329/* otherwise it was not very useful; free it */ 330free(parent->util); 331 parent->util = NULL; 332} 333 334/* See if the origin->path is different between parent 335 * and origin first. Most of the time they are the 336 * same and diff-tree is fairly efficient about this. 337 */ 338diff_setup(&diff_opts); 339 diff_opts.recursive =1; 340 diff_opts.detect_rename =0; 341 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 342 paths[0] = origin->path; 343 paths[1] = NULL; 344 345diff_tree_setup_paths(paths, &diff_opts); 346if(diff_setup_done(&diff_opts) <0) 347die("diff-setup"); 348 349if(is_null_sha1(origin->commit->object.sha1)) 350do_diff_cache(parent->tree->object.sha1, &diff_opts); 351else 352diff_tree_sha1(parent->tree->object.sha1, 353 origin->commit->tree->object.sha1, 354"", &diff_opts); 355diffcore_std(&diff_opts); 356 357/* It is either one entry that says "modified", or "created", 358 * or nothing. 359 */ 360if(!diff_queued_diff.nr) { 361/* The path is the same as parent */ 362 porigin =get_origin(sb, parent, origin->path); 363hashcpy(porigin->blob_sha1, origin->blob_sha1); 364} 365else if(diff_queued_diff.nr !=1) 366die("internal error in blame::find_origin"); 367else{ 368struct diff_filepair *p = diff_queued_diff.queue[0]; 369switch(p->status) { 370default: 371die("internal error in blame::find_origin (%c)", 372 p->status); 373case'M': 374 porigin =get_origin(sb, parent, origin->path); 375hashcpy(porigin->blob_sha1, p->one->sha1); 376break; 377case'A': 378case'T': 379/* Did not exist in parent, or type changed */ 380break; 381} 382} 383diff_flush(&diff_opts); 384if(porigin) { 385/* 386 * Create a freestanding copy that is not part of 387 * the refcounted origin found in the scoreboard, and 388 * cache it in the commit. 389 */ 390struct origin *cached; 391 392 cached =make_origin(porigin->commit, porigin->path); 393hashcpy(cached->blob_sha1, porigin->blob_sha1); 394 parent->util = cached; 395} 396return porigin; 397} 398 399/* 400 * We have an origin -- find the path that corresponds to it in its 401 * parent and return an origin structure to represent it. 402 */ 403static struct origin *find_rename(struct scoreboard *sb, 404struct commit *parent, 405struct origin *origin) 406{ 407struct origin *porigin = NULL; 408struct diff_options diff_opts; 409int i; 410const char*paths[2]; 411 412diff_setup(&diff_opts); 413 diff_opts.recursive =1; 414 diff_opts.detect_rename = DIFF_DETECT_RENAME; 415 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 416 diff_opts.single_follow = origin->path; 417 paths[0] = NULL; 418diff_tree_setup_paths(paths, &diff_opts); 419if(diff_setup_done(&diff_opts) <0) 420die("diff-setup"); 421 422if(is_null_sha1(origin->commit->object.sha1)) 423do_diff_cache(parent->tree->object.sha1, &diff_opts); 424else 425diff_tree_sha1(parent->tree->object.sha1, 426 origin->commit->tree->object.sha1, 427"", &diff_opts); 428diffcore_std(&diff_opts); 429 430for(i =0; i < diff_queued_diff.nr; i++) { 431struct diff_filepair *p = diff_queued_diff.queue[i]; 432if((p->status =='R'|| p->status =='C') && 433!strcmp(p->two->path, origin->path)) { 434 porigin =get_origin(sb, parent, p->one->path); 435hashcpy(porigin->blob_sha1, p->one->sha1); 436break; 437} 438} 439diff_flush(&diff_opts); 440return porigin; 441} 442 443/* 444 * Parsing of patch chunks... 445 */ 446struct chunk { 447/* line number in postimage; up to but not including this 448 * line is the same as preimage 449 */ 450int same; 451 452/* preimage line number after this chunk */ 453int p_next; 454 455/* postimage line number after this chunk */ 456int t_next; 457}; 458 459struct patch { 460struct chunk *chunks; 461int num; 462}; 463 464struct blame_diff_state { 465struct xdiff_emit_state xm; 466struct patch *ret; 467unsigned hunk_post_context; 468unsigned hunk_in_pre_context :1; 469}; 470 471static voidprocess_u_diff(void*state_,char*line,unsigned long len) 472{ 473struct blame_diff_state *state = state_; 474struct chunk *chunk; 475int off1, off2, len1, len2, num; 476 477 num = state->ret->num; 478if(len <4|| line[0] !='@'|| line[1] !='@') { 479if(state->hunk_in_pre_context && line[0] ==' ') 480 state->ret->chunks[num -1].same++; 481else{ 482 state->hunk_in_pre_context =0; 483if(line[0] ==' ') 484 state->hunk_post_context++; 485else 486 state->hunk_post_context =0; 487} 488return; 489} 490 491if(num && state->hunk_post_context) { 492 chunk = &state->ret->chunks[num -1]; 493 chunk->p_next -= state->hunk_post_context; 494 chunk->t_next -= state->hunk_post_context; 495} 496 state->ret->num = ++num; 497 state->ret->chunks =xrealloc(state->ret->chunks, 498sizeof(struct chunk) * num); 499 chunk = &state->ret->chunks[num -1]; 500if(parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { 501 state->ret->num--; 502return; 503} 504 505/* Line numbers in patch output are one based. */ 506 off1--; 507 off2--; 508 509 chunk->same = len2 ? off2 : (off2 +1); 510 511 chunk->p_next = off1 + (len1 ? len1 :1); 512 chunk->t_next = chunk->same + len2; 513 state->hunk_in_pre_context =1; 514 state->hunk_post_context =0; 515} 516 517static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, 518int context) 519{ 520struct blame_diff_state state; 521 xpparam_t xpp; 522 xdemitconf_t xecfg; 523 xdemitcb_t ecb; 524 525 xpp.flags = xdl_opts; 526memset(&xecfg,0,sizeof(xecfg)); 527 xecfg.ctxlen = context; 528 ecb.outf = xdiff_outf; 529 ecb.priv = &state; 530memset(&state,0,sizeof(state)); 531 state.xm.consume = process_u_diff; 532 state.ret =xmalloc(sizeof(struct patch)); 533 state.ret->chunks = NULL; 534 state.ret->num =0; 535 536xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb); 537 538if(state.ret->num) { 539struct chunk *chunk; 540 chunk = &state.ret->chunks[state.ret->num -1]; 541 chunk->p_next -= state.hunk_post_context; 542 chunk->t_next -= state.hunk_post_context; 543} 544return state.ret; 545} 546 547/* 548 * Run diff between two origins and grab the patch output, so that 549 * we can pass blame for lines origin is currently suspected for 550 * to its parent. 551 */ 552static struct patch *get_patch(struct origin *parent,struct origin *origin) 553{ 554 mmfile_t file_p, file_o; 555struct patch *patch; 556 557fill_origin_blob(parent, &file_p); 558fill_origin_blob(origin, &file_o); 559if(!file_p.ptr || !file_o.ptr) 560return NULL; 561 patch =compare_buffer(&file_p, &file_o,0); 562 num_get_patch++; 563return patch; 564} 565 566static voidfree_patch(struct patch *p) 567{ 568free(p->chunks); 569free(p); 570} 571 572/* 573 * Link in a new blame entry to the scoreboard. Entries that cover the 574 * same line range have been removed from the scoreboard previously. 575 */ 576static voidadd_blame_entry(struct scoreboard *sb,struct blame_entry *e) 577{ 578struct blame_entry *ent, *prev = NULL; 579 580origin_incref(e->suspect); 581 582for(ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) 583 prev = ent; 584 585/* prev, if not NULL, is the last one that is below e */ 586 e->prev = prev; 587if(prev) { 588 e->next = prev->next; 589 prev->next = e; 590} 591else{ 592 e->next = sb->ent; 593 sb->ent = e; 594} 595if(e->next) 596 e->next->prev = e; 597} 598 599/* 600 * src typically is on-stack; we want to copy the information in it to 601 * an malloced blame_entry that is already on the linked list of the 602 * scoreboard. The origin of dst loses a refcnt while the origin of src 603 * gains one. 604 */ 605static voiddup_entry(struct blame_entry *dst,struct blame_entry *src) 606{ 607struct blame_entry *p, *n; 608 609 p = dst->prev; 610 n = dst->next; 611origin_incref(src->suspect); 612origin_decref(dst->suspect); 613memcpy(dst, src,sizeof(*src)); 614 dst->prev = p; 615 dst->next = n; 616 dst->score =0; 617} 618 619static const char*nth_line(struct scoreboard *sb,int lno) 620{ 621return sb->final_buf + sb->lineno[lno]; 622} 623 624/* 625 * It is known that lines between tlno to same came from parent, and e 626 * has an overlap with that range. it also is known that parent's 627 * line plno corresponds to e's line tlno. 628 * 629 * <---- e -----> 630 * <------> 631 * <------------> 632 * <------------> 633 * <------------------> 634 * 635 * Split e into potentially three parts; before this chunk, the chunk 636 * to be blamed for the parent, and after that portion. 637 */ 638static voidsplit_overlap(struct blame_entry *split, 639struct blame_entry *e, 640int tlno,int plno,int same, 641struct origin *parent) 642{ 643int chunk_end_lno; 644memset(split,0,sizeof(struct blame_entry [3])); 645 646if(e->s_lno < tlno) { 647/* there is a pre-chunk part not blamed on parent */ 648 split[0].suspect =origin_incref(e->suspect); 649 split[0].lno = e->lno; 650 split[0].s_lno = e->s_lno; 651 split[0].num_lines = tlno - e->s_lno; 652 split[1].lno = e->lno + tlno - e->s_lno; 653 split[1].s_lno = plno; 654} 655else{ 656 split[1].lno = e->lno; 657 split[1].s_lno = plno + (e->s_lno - tlno); 658} 659 660if(same < e->s_lno + e->num_lines) { 661/* there is a post-chunk part not blamed on parent */ 662 split[2].suspect =origin_incref(e->suspect); 663 split[2].lno = e->lno + (same - e->s_lno); 664 split[2].s_lno = e->s_lno + (same - e->s_lno); 665 split[2].num_lines = e->s_lno + e->num_lines - same; 666 chunk_end_lno = split[2].lno; 667} 668else 669 chunk_end_lno = e->lno + e->num_lines; 670 split[1].num_lines = chunk_end_lno - split[1].lno; 671 672/* 673 * if it turns out there is nothing to blame the parent for, 674 * forget about the splitting. !split[1].suspect signals this. 675 */ 676if(split[1].num_lines <1) 677return; 678 split[1].suspect =origin_incref(parent); 679} 680 681/* 682 * split_overlap() divided an existing blame e into up to three parts 683 * in split. Adjust the linked list of blames in the scoreboard to 684 * reflect the split. 685 */ 686static voidsplit_blame(struct scoreboard *sb, 687struct blame_entry *split, 688struct blame_entry *e) 689{ 690struct blame_entry *new_entry; 691 692if(split[0].suspect && split[2].suspect) { 693/* The first part (reuse storage for the existing entry e) */ 694dup_entry(e, &split[0]); 695 696/* The last part -- me */ 697 new_entry =xmalloc(sizeof(*new_entry)); 698memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 699add_blame_entry(sb, new_entry); 700 701/* ... and the middle part -- parent */ 702 new_entry =xmalloc(sizeof(*new_entry)); 703memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 704add_blame_entry(sb, new_entry); 705} 706else if(!split[0].suspect && !split[2].suspect) 707/* 708 * The parent covers the entire area; reuse storage for 709 * e and replace it with the parent. 710 */ 711dup_entry(e, &split[1]); 712else if(split[0].suspect) { 713/* me and then parent */ 714dup_entry(e, &split[0]); 715 716 new_entry =xmalloc(sizeof(*new_entry)); 717memcpy(new_entry, &(split[1]),sizeof(struct blame_entry)); 718add_blame_entry(sb, new_entry); 719} 720else{ 721/* parent and then me */ 722dup_entry(e, &split[1]); 723 724 new_entry =xmalloc(sizeof(*new_entry)); 725memcpy(new_entry, &(split[2]),sizeof(struct blame_entry)); 726add_blame_entry(sb, new_entry); 727} 728 729if(DEBUG) {/* sanity */ 730struct blame_entry *ent; 731int lno = sb->ent->lno, corrupt =0; 732 733for(ent = sb->ent; ent; ent = ent->next) { 734if(lno != ent->lno) 735 corrupt =1; 736if(ent->s_lno <0) 737 corrupt =1; 738 lno += ent->num_lines; 739} 740if(corrupt) { 741 lno = sb->ent->lno; 742for(ent = sb->ent; ent; ent = ent->next) { 743printf("L%8d l%8d n%8d\n", 744 lno, ent->lno, ent->num_lines); 745 lno = ent->lno + ent->num_lines; 746} 747die("oops"); 748} 749} 750} 751 752/* 753 * After splitting the blame, the origins used by the 754 * on-stack blame_entry should lose one refcnt each. 755 */ 756static voiddecref_split(struct blame_entry *split) 757{ 758int i; 759 760for(i =0; i <3; i++) 761origin_decref(split[i].suspect); 762} 763 764/* 765 * Helper for blame_chunk(). blame_entry e is known to overlap with 766 * the patch hunk; split it and pass blame to the parent. 767 */ 768static voidblame_overlap(struct scoreboard *sb,struct blame_entry *e, 769int tlno,int plno,int same, 770struct origin *parent) 771{ 772struct blame_entry split[3]; 773 774split_overlap(split, e, tlno, plno, same, parent); 775if(split[1].suspect) 776split_blame(sb, split, e); 777decref_split(split); 778} 779 780/* 781 * Find the line number of the last line the target is suspected for. 782 */ 783static intfind_last_in_target(struct scoreboard *sb,struct origin *target) 784{ 785struct blame_entry *e; 786int last_in_target = -1; 787 788for(e = sb->ent; e; e = e->next) { 789if(e->guilty || !same_suspect(e->suspect, target)) 790continue; 791if(last_in_target < e->s_lno + e->num_lines) 792 last_in_target = e->s_lno + e->num_lines; 793} 794return last_in_target; 795} 796 797/* 798 * Process one hunk from the patch between the current suspect for 799 * blame_entry e and its parent. Find and split the overlap, and 800 * pass blame to the overlapping part to the parent. 801 */ 802static voidblame_chunk(struct scoreboard *sb, 803int tlno,int plno,int same, 804struct origin *target,struct origin *parent) 805{ 806struct blame_entry *e; 807 808for(e = sb->ent; e; e = e->next) { 809if(e->guilty || !same_suspect(e->suspect, target)) 810continue; 811if(same <= e->s_lno) 812continue; 813if(tlno < e->s_lno + e->num_lines) 814blame_overlap(sb, e, tlno, plno, same, parent); 815} 816} 817 818/* 819 * We are looking at the origin 'target' and aiming to pass blame 820 * for the lines it is suspected to its parent. Run diff to find 821 * which lines came from parent and pass blame for them. 822 */ 823static intpass_blame_to_parent(struct scoreboard *sb, 824struct origin *target, 825struct origin *parent) 826{ 827int i, last_in_target, plno, tlno; 828struct patch *patch; 829 830 last_in_target =find_last_in_target(sb, target); 831if(last_in_target <0) 832return1;/* nothing remains for this target */ 833 834 patch =get_patch(parent, target); 835 plno = tlno =0; 836for(i =0; i < patch->num; i++) { 837struct chunk *chunk = &patch->chunks[i]; 838 839blame_chunk(sb, tlno, plno, chunk->same, target, parent); 840 plno = chunk->p_next; 841 tlno = chunk->t_next; 842} 843/* The rest (i.e. anything after tlno) are the same as the parent */ 844blame_chunk(sb, tlno, plno, last_in_target, target, parent); 845 846free_patch(patch); 847return0; 848} 849 850/* 851 * The lines in blame_entry after splitting blames many times can become 852 * very small and trivial, and at some point it becomes pointless to 853 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 854 * ordinary C program, and it is not worth to say it was copied from 855 * totally unrelated file in the parent. 856 * 857 * Compute how trivial the lines in the blame_entry are. 858 */ 859static unsignedent_score(struct scoreboard *sb,struct blame_entry *e) 860{ 861unsigned score; 862const char*cp, *ep; 863 864if(e->score) 865return e->score; 866 867 score =1; 868 cp =nth_line(sb, e->lno); 869 ep =nth_line(sb, e->lno + e->num_lines); 870while(cp < ep) { 871unsigned ch = *((unsigned char*)cp); 872if(isalnum(ch)) 873 score++; 874 cp++; 875} 876 e->score = score; 877return score; 878} 879 880/* 881 * best_so_far[] and this[] are both a split of an existing blame_entry 882 * that passes blame to the parent. Maintain best_so_far the best split 883 * so far, by comparing this and best_so_far and copying this into 884 * bst_so_far as needed. 885 */ 886static voidcopy_split_if_better(struct scoreboard *sb, 887struct blame_entry *best_so_far, 888struct blame_entry *this) 889{ 890int i; 891 892if(!this[1].suspect) 893return; 894if(best_so_far[1].suspect) { 895if(ent_score(sb, &this[1]) <ent_score(sb, &best_so_far[1])) 896return; 897} 898 899for(i =0; i <3; i++) 900origin_incref(this[i].suspect); 901decref_split(best_so_far); 902memcpy(best_so_far,this,sizeof(struct blame_entry [3])); 903} 904 905/* 906 * We are looking at a part of the final image represented by 907 * ent (tlno and same are offset by ent->s_lno). 908 * tlno is where we are looking at in the final image. 909 * up to (but not including) same match preimage. 910 * plno is where we are looking at in the preimage. 911 * 912 * <-------------- final image ----------------------> 913 * <------ent------> 914 * ^tlno ^same 915 * <---------preimage-----> 916 * ^plno 917 * 918 * All line numbers are 0-based. 919 */ 920static voidhandle_split(struct scoreboard *sb, 921struct blame_entry *ent, 922int tlno,int plno,int same, 923struct origin *parent, 924struct blame_entry *split) 925{ 926if(ent->num_lines <= tlno) 927return; 928if(tlno < same) { 929struct blame_entry this[3]; 930 tlno += ent->s_lno; 931 same += ent->s_lno; 932split_overlap(this, ent, tlno, plno, same, parent); 933copy_split_if_better(sb, split,this); 934decref_split(this); 935} 936} 937 938/* 939 * Find the lines from parent that are the same as ent so that 940 * we can pass blames to it. file_p has the blob contents for 941 * the parent. 942 */ 943static voidfind_copy_in_blob(struct scoreboard *sb, 944struct blame_entry *ent, 945struct origin *parent, 946struct blame_entry *split, 947 mmfile_t *file_p) 948{ 949const char*cp; 950int cnt; 951 mmfile_t file_o; 952struct patch *patch; 953int i, plno, tlno; 954 955/* 956 * Prepare mmfile that contains only the lines in ent. 957 */ 958 cp =nth_line(sb, ent->lno); 959 file_o.ptr = (char*) cp; 960 cnt = ent->num_lines; 961 962while(cnt && cp < sb->final_buf + sb->final_buf_size) { 963if(*cp++ =='\n') 964 cnt--; 965} 966 file_o.size = cp - file_o.ptr; 967 968 patch =compare_buffer(file_p, &file_o,1); 969 970/* 971 * file_o is a part of final image we are annotating. 972 * file_p partially may match that image. 973 */ 974memset(split,0,sizeof(struct blame_entry [3])); 975 plno = tlno =0; 976for(i =0; i < patch->num; i++) { 977struct chunk *chunk = &patch->chunks[i]; 978 979handle_split(sb, ent, tlno, plno, chunk->same, parent, split); 980 plno = chunk->p_next; 981 tlno = chunk->t_next; 982} 983/* remainder, if any, all match the preimage */ 984handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); 985free_patch(patch); 986} 987 988/* 989 * See if lines currently target is suspected for can be attributed to 990 * parent. 991 */ 992static intfind_move_in_parent(struct scoreboard *sb, 993struct origin *target, 994struct origin *parent) 995{ 996int last_in_target, made_progress; 997struct blame_entry *e, split[3]; 998 mmfile_t file_p; 9991000 last_in_target =find_last_in_target(sb, target);1001if(last_in_target <0)1002return1;/* nothing remains for this target */10031004fill_origin_blob(parent, &file_p);1005if(!file_p.ptr)1006return0;10071008 made_progress =1;1009while(made_progress) {1010 made_progress =0;1011for(e = sb->ent; e; e = e->next) {1012if(e->guilty || !same_suspect(e->suspect, target))1013continue;1014find_copy_in_blob(sb, e, parent, split, &file_p);1015if(split[1].suspect &&1016 blame_move_score <ent_score(sb, &split[1])) {1017split_blame(sb, split, e);1018 made_progress =1;1019}1020decref_split(split);1021}1022}1023return0;1024}10251026struct blame_list {1027struct blame_entry *ent;1028struct blame_entry split[3];1029};10301031/*1032 * Count the number of entries the target is suspected for,1033 * and prepare a list of entry and the best split.1034 */1035static struct blame_list *setup_blame_list(struct scoreboard *sb,1036struct origin *target,1037int*num_ents_p)1038{1039struct blame_entry *e;1040int num_ents, i;1041struct blame_list *blame_list = NULL;10421043for(e = sb->ent, num_ents =0; e; e = e->next)1044if(!e->guilty &&same_suspect(e->suspect, target))1045 num_ents++;1046if(num_ents) {1047 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1048for(e = sb->ent, i =0; e; e = e->next)1049if(!e->guilty &&same_suspect(e->suspect, target))1050 blame_list[i++].ent = e;1051}1052*num_ents_p = num_ents;1053return blame_list;1054}10551056/*1057 * For lines target is suspected for, see if we can find code movement1058 * across file boundary from the parent commit. porigin is the path1059 * in the parent we already tried.1060 */1061static intfind_copy_in_parent(struct scoreboard *sb,1062struct origin *target,1063struct commit *parent,1064struct origin *porigin,1065int opt)1066{1067struct diff_options diff_opts;1068const char*paths[1];1069int i, j;1070int retval;1071struct blame_list *blame_list;1072int num_ents;10731074 blame_list =setup_blame_list(sb, target, &num_ents);1075if(!blame_list)1076return1;/* nothing remains for this target */10771078diff_setup(&diff_opts);1079 diff_opts.recursive =1;1080 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;10811082 paths[0] = NULL;1083diff_tree_setup_paths(paths, &diff_opts);1084if(diff_setup_done(&diff_opts) <0)1085die("diff-setup");10861087/* Try "find copies harder" on new path if requested;1088 * we do not want to use diffcore_rename() actually to1089 * match things up; find_copies_harder is set only to1090 * force diff_tree_sha1() to feed all filepairs to diff_queue,1091 * and this code needs to be after diff_setup_done(), which1092 * usually makes find-copies-harder imply copy detection.1093 */1094if((opt & PICKAXE_BLAME_COPY_HARDEST)1095|| ((opt & PICKAXE_BLAME_COPY_HARDER)1096&& (!porigin ||strcmp(target->path, porigin->path))))1097 diff_opts.find_copies_harder =1;10981099if(is_null_sha1(target->commit->object.sha1))1100do_diff_cache(parent->tree->object.sha1, &diff_opts);1101else1102diff_tree_sha1(parent->tree->object.sha1,1103 target->commit->tree->object.sha1,1104"", &diff_opts);11051106if(!diff_opts.find_copies_harder)1107diffcore_std(&diff_opts);11081109 retval =0;1110while(1) {1111int made_progress =0;11121113for(i =0; i < diff_queued_diff.nr; i++) {1114struct diff_filepair *p = diff_queued_diff.queue[i];1115struct origin *norigin;1116 mmfile_t file_p;1117struct blame_entry this[3];11181119if(!DIFF_FILE_VALID(p->one))1120continue;/* does not exist in parent */1121if(porigin && !strcmp(p->one->path, porigin->path))1122/* find_move already dealt with this path */1123continue;11241125 norigin =get_origin(sb, parent, p->one->path);1126hashcpy(norigin->blob_sha1, p->one->sha1);1127fill_origin_blob(norigin, &file_p);1128if(!file_p.ptr)1129continue;11301131for(j =0; j < num_ents; j++) {1132find_copy_in_blob(sb, blame_list[j].ent,1133 norigin,this, &file_p);1134copy_split_if_better(sb, blame_list[j].split,1135this);1136decref_split(this);1137}1138origin_decref(norigin);1139}11401141for(j =0; j < num_ents; j++) {1142struct blame_entry *split = blame_list[j].split;1143if(split[1].suspect &&1144 blame_copy_score <ent_score(sb, &split[1])) {1145split_blame(sb, split, blame_list[j].ent);1146 made_progress =1;1147}1148decref_split(split);1149}1150free(blame_list);11511152if(!made_progress)1153break;1154 blame_list =setup_blame_list(sb, target, &num_ents);1155if(!blame_list) {1156 retval =1;1157break;1158}1159}1160diff_flush(&diff_opts);11611162return retval;1163}11641165/*1166 * The blobs of origin and porigin exactly match, so everything1167 * origin is suspected for can be blamed on the parent.1168 */1169static voidpass_whole_blame(struct scoreboard *sb,1170struct origin *origin,struct origin *porigin)1171{1172struct blame_entry *e;11731174if(!porigin->file.ptr && origin->file.ptr) {1175/* Steal its file */1176 porigin->file = origin->file;1177 origin->file.ptr = NULL;1178}1179for(e = sb->ent; e; e = e->next) {1180if(!same_suspect(e->suspect, origin))1181continue;1182origin_incref(porigin);1183origin_decref(e->suspect);1184 e->suspect = porigin;1185}1186}11871188#define MAXPARENT 1611891190static voidpass_blame(struct scoreboard *sb,struct origin *origin,int opt)1191{1192int i, pass;1193struct commit *commit = origin->commit;1194struct commit_list *parent;1195struct origin *parent_origin[MAXPARENT], *porigin;11961197memset(parent_origin,0,sizeof(parent_origin));11981199/* The first pass looks for unrenamed path to optimize for1200 * common cases, then we look for renames in the second pass.1201 */1202for(pass =0; pass <2; pass++) {1203struct origin *(*find)(struct scoreboard *,1204struct commit *,struct origin *);1205 find = pass ? find_rename : find_origin;12061207for(i =0, parent = commit->parents;1208 i < MAXPARENT && parent;1209 parent = parent->next, i++) {1210struct commit *p = parent->item;1211int j, same;12121213if(parent_origin[i])1214continue;1215if(parse_commit(p))1216continue;1217 porigin =find(sb, p, origin);1218if(!porigin)1219continue;1220if(!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {1221pass_whole_blame(sb, origin, porigin);1222origin_decref(porigin);1223goto finish;1224}1225for(j = same =0; j < i; j++)1226if(parent_origin[j] &&1227!hashcmp(parent_origin[j]->blob_sha1,1228 porigin->blob_sha1)) {1229 same =1;1230break;1231}1232if(!same)1233 parent_origin[i] = porigin;1234else1235origin_decref(porigin);1236}1237}12381239 num_commits++;1240for(i =0, parent = commit->parents;1241 i < MAXPARENT && parent;1242 parent = parent->next, i++) {1243struct origin *porigin = parent_origin[i];1244if(!porigin)1245continue;1246if(pass_blame_to_parent(sb, origin, porigin))1247goto finish;1248}12491250/*1251 * Optionally find moves in parents' files.1252 */1253if(opt & PICKAXE_BLAME_MOVE)1254for(i =0, parent = commit->parents;1255 i < MAXPARENT && parent;1256 parent = parent->next, i++) {1257struct origin *porigin = parent_origin[i];1258if(!porigin)1259continue;1260if(find_move_in_parent(sb, origin, porigin))1261goto finish;1262}12631264/*1265 * Optionally find copies from parents' files.1266 */1267if(opt & PICKAXE_BLAME_COPY)1268for(i =0, parent = commit->parents;1269 i < MAXPARENT && parent;1270 parent = parent->next, i++) {1271struct origin *porigin = parent_origin[i];1272if(find_copy_in_parent(sb, origin, parent->item,1273 porigin, opt))1274goto finish;1275}12761277 finish:1278for(i =0; i < MAXPARENT; i++)1279origin_decref(parent_origin[i]);1280}12811282/*1283 * Information on commits, used for output.1284 */1285struct commit_info1286{1287const char*author;1288const char*author_mail;1289unsigned long author_time;1290const char*author_tz;12911292/* filled only when asked for details */1293const char*committer;1294const char*committer_mail;1295unsigned long committer_time;1296const char*committer_tz;12971298const char*summary;1299};13001301/*1302 * Parse author/committer line in the commit object buffer1303 */1304static voidget_ac_line(const char*inbuf,const char*what,1305int bufsz,char*person,const char**mail,1306unsigned long*time,const char**tz)1307{1308int len, tzlen, maillen;1309char*tmp, *endp, *timepos;13101311 tmp =strstr(inbuf, what);1312if(!tmp)1313goto error_out;1314 tmp +=strlen(what);1315 endp =strchr(tmp,'\n');1316if(!endp)1317 len =strlen(tmp);1318else1319 len = endp - tmp;1320if(bufsz <= len) {1321 error_out:1322/* Ugh */1323*mail = *tz ="(unknown)";1324*time =0;1325return;1326}1327memcpy(person, tmp, len);13281329 tmp = person;1330 tmp += len;1331*tmp =0;1332while(*tmp !=' ')1333 tmp--;1334*tz = tmp+1;1335 tzlen = (person+len)-(tmp+1);13361337*tmp =0;1338while(*tmp !=' ')1339 tmp--;1340*time =strtoul(tmp, NULL,10);1341 timepos = tmp;13421343*tmp =0;1344while(*tmp !=' ')1345 tmp--;1346*mail = tmp +1;1347*tmp =0;1348 maillen = timepos - tmp;13491350if(!mailmap.nr)1351return;13521353/*1354 * mailmap expansion may make the name longer.1355 * make room by pushing stuff down.1356 */1357 tmp = person + bufsz - (tzlen +1);1358memmove(tmp, *tz, tzlen);1359 tmp[tzlen] =0;1360*tz = tmp;13611362 tmp = tmp - (maillen +1);1363memmove(tmp, *mail, maillen);1364 tmp[maillen] =0;1365*mail = tmp;13661367/*1368 * Now, convert e-mail using mailmap1369 */1370map_email(&mailmap, tmp +1, person, tmp-person-1);1371}13721373static voidget_commit_info(struct commit *commit,1374struct commit_info *ret,1375int detailed)1376{1377int len;1378char*tmp, *endp;1379static char author_buf[1024];1380static char committer_buf[1024];1381static char summary_buf[1024];13821383/*1384 * We've operated without save_commit_buffer, so1385 * we now need to populate them for output.1386 */1387if(!commit->buffer) {1388enum object_type type;1389unsigned long size;1390 commit->buffer =1391read_sha1_file(commit->object.sha1, &type, &size);1392if(!commit->buffer)1393die("Cannot read commit%s",1394sha1_to_hex(commit->object.sha1));1395}1396 ret->author = author_buf;1397get_ac_line(commit->buffer,"\nauthor ",1398sizeof(author_buf), author_buf, &ret->author_mail,1399&ret->author_time, &ret->author_tz);14001401if(!detailed)1402return;14031404 ret->committer = committer_buf;1405get_ac_line(commit->buffer,"\ncommitter ",1406sizeof(committer_buf), committer_buf, &ret->committer_mail,1407&ret->committer_time, &ret->committer_tz);14081409 ret->summary = summary_buf;1410 tmp =strstr(commit->buffer,"\n\n");1411if(!tmp) {1412 error_out:1413sprintf(summary_buf,"(%s)",sha1_to_hex(commit->object.sha1));1414return;1415}1416 tmp +=2;1417 endp =strchr(tmp,'\n');1418if(!endp)1419 endp = tmp +strlen(tmp);1420 len = endp - tmp;1421if(len >=sizeof(summary_buf) || len ==0)1422goto error_out;1423memcpy(summary_buf, tmp, len);1424 summary_buf[len] =0;1425}14261427/*1428 * To allow LF and other nonportable characters in pathnames,1429 * they are c-style quoted as needed.1430 */1431static voidwrite_filename_info(const char*path)1432{1433printf("filename ");1434write_name_quoted(NULL,0, path,1, stdout);1435putchar('\n');1436}14371438/*1439 * The blame_entry is found to be guilty for the range. Mark it1440 * as such, and show it in incremental output.1441 */1442static voidfound_guilty_entry(struct blame_entry *ent)1443{1444if(ent->guilty)1445return;1446 ent->guilty =1;1447if(incremental) {1448struct origin *suspect = ent->suspect;14491450printf("%s %d %d %d\n",1451sha1_to_hex(suspect->commit->object.sha1),1452 ent->s_lno +1, ent->lno +1, ent->num_lines);1453if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1454struct commit_info ci;1455 suspect->commit->object.flags |= METAINFO_SHOWN;1456get_commit_info(suspect->commit, &ci,1);1457printf("author%s\n", ci.author);1458printf("author-mail%s\n", ci.author_mail);1459printf("author-time%lu\n", ci.author_time);1460printf("author-tz%s\n", ci.author_tz);1461printf("committer%s\n", ci.committer);1462printf("committer-mail%s\n", ci.committer_mail);1463printf("committer-time%lu\n", ci.committer_time);1464printf("committer-tz%s\n", ci.committer_tz);1465printf("summary%s\n", ci.summary);1466if(suspect->commit->object.flags & UNINTERESTING)1467printf("boundary\n");1468}1469write_filename_info(suspect->path);1470maybe_flush_or_die(stdout,"stdout");1471}1472}14731474/*1475 * The main loop -- while the scoreboard has lines whose true origin1476 * is still unknown, pick one blame_entry, and allow its current1477 * suspect to pass blames to its parents.1478 */1479static voidassign_blame(struct scoreboard *sb,struct rev_info *revs,int opt)1480{1481while(1) {1482struct blame_entry *ent;1483struct commit *commit;1484struct origin *suspect = NULL;14851486/* find one suspect to break down */1487for(ent = sb->ent; !suspect && ent; ent = ent->next)1488if(!ent->guilty)1489 suspect = ent->suspect;1490if(!suspect)1491return;/* all done */14921493/*1494 * We will use this suspect later in the loop,1495 * so hold onto it in the meantime.1496 */1497origin_incref(suspect);1498 commit = suspect->commit;1499if(!commit->object.parsed)1500parse_commit(commit);1501if(!(commit->object.flags & UNINTERESTING) &&1502!(revs->max_age != -1&& commit->date < revs->max_age))1503pass_blame(sb, suspect, opt);1504else{1505 commit->object.flags |= UNINTERESTING;1506if(commit->object.parsed)1507mark_parents_uninteresting(commit);1508}1509/* treat root commit as boundary */1510if(!commit->parents && !show_root)1511 commit->object.flags |= UNINTERESTING;15121513/* Take responsibility for the remaining entries */1514for(ent = sb->ent; ent; ent = ent->next)1515if(same_suspect(ent->suspect, suspect))1516found_guilty_entry(ent);1517origin_decref(suspect);15181519if(DEBUG)/* sanity */1520sanity_check_refcnt(sb);1521}1522}15231524static const char*format_time(unsigned long time,const char*tz_str,1525int show_raw_time)1526{1527static char time_buf[128];1528time_t t = time;1529int minutes, tz;1530struct tm *tm;15311532if(show_raw_time) {1533sprintf(time_buf,"%lu%s", time, tz_str);1534return time_buf;1535}15361537 tz =atoi(tz_str);1538 minutes = tz <0? -tz : tz;1539 minutes = (minutes /100)*60+ (minutes %100);1540 minutes = tz <0? -minutes : minutes;1541 t = time + minutes *60;1542 tm =gmtime(&t);15431544strftime(time_buf,sizeof(time_buf),"%Y-%m-%d %H:%M:%S", tm);1545strcat(time_buf, tz_str);1546return time_buf;1547}15481549#define OUTPUT_ANNOTATE_COMPAT 0011550#define OUTPUT_LONG_OBJECT_NAME 0021551#define OUTPUT_RAW_TIMESTAMP 0041552#define OUTPUT_PORCELAIN 0101553#define OUTPUT_SHOW_NAME 0201554#define OUTPUT_SHOW_NUMBER 0401555#define OUTPUT_SHOW_SCORE 01001556#define OUTPUT_NO_AUTHOR 020015571558static voidemit_porcelain(struct scoreboard *sb,struct blame_entry *ent)1559{1560int cnt;1561const char*cp;1562struct origin *suspect = ent->suspect;1563char hex[41];15641565strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));1566printf("%s%c%d %d %d\n",1567 hex,1568 ent->guilty ?' ':'*',// purely for debugging1569 ent->s_lno +1,1570 ent->lno +1,1571 ent->num_lines);1572if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1573struct commit_info ci;1574 suspect->commit->object.flags |= METAINFO_SHOWN;1575get_commit_info(suspect->commit, &ci,1);1576printf("author%s\n", ci.author);1577printf("author-mail%s\n", ci.author_mail);1578printf("author-time%lu\n", ci.author_time);1579printf("author-tz%s\n", ci.author_tz);1580printf("committer%s\n", ci.committer);1581printf("committer-mail%s\n", ci.committer_mail);1582printf("committer-time%lu\n", ci.committer_time);1583printf("committer-tz%s\n", ci.committer_tz);1584write_filename_info(suspect->path);1585printf("summary%s\n", ci.summary);1586if(suspect->commit->object.flags & UNINTERESTING)1587printf("boundary\n");1588}1589else if(suspect->commit->object.flags & MORE_THAN_ONE_PATH)1590write_filename_info(suspect->path);15911592 cp =nth_line(sb, ent->lno);1593for(cnt =0; cnt < ent->num_lines; cnt++) {1594char ch;1595if(cnt)1596printf("%s %d %d\n", hex,1597 ent->s_lno +1+ cnt,1598 ent->lno +1+ cnt);1599putchar('\t');1600do{1601 ch = *cp++;1602putchar(ch);1603}while(ch !='\n'&&1604 cp < sb->final_buf + sb->final_buf_size);1605}1606}16071608static voidemit_other(struct scoreboard *sb,struct blame_entry *ent,int opt)1609{1610int cnt;1611const char*cp;1612struct origin *suspect = ent->suspect;1613struct commit_info ci;1614char hex[41];1615int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);16161617get_commit_info(suspect->commit, &ci,1);1618strcpy(hex,sha1_to_hex(suspect->commit->object.sha1));16191620 cp =nth_line(sb, ent->lno);1621for(cnt =0; cnt < ent->num_lines; cnt++) {1622char ch;1623int length = (opt & OUTPUT_LONG_OBJECT_NAME) ?40:8;16241625if(suspect->commit->object.flags & UNINTERESTING) {1626if(blank_boundary)1627memset(hex,' ', length);1628else if(!cmd_is_annotate) {1629 length--;1630putchar('^');1631}1632}16331634printf("%.*s", length, hex);1635if(opt & OUTPUT_ANNOTATE_COMPAT)1636printf("\t(%10s\t%10s\t%d)", ci.author,1637format_time(ci.author_time, ci.author_tz,1638 show_raw_time),1639 ent->lno +1+ cnt);1640else{1641if(opt & OUTPUT_SHOW_SCORE)1642printf(" %*d%02d",1643 max_score_digits, ent->score,1644 ent->suspect->refcnt);1645if(opt & OUTPUT_SHOW_NAME)1646printf(" %-*.*s", longest_file, longest_file,1647 suspect->path);1648if(opt & OUTPUT_SHOW_NUMBER)1649printf(" %*d", max_orig_digits,1650 ent->s_lno +1+ cnt);16511652if(!(opt & OUTPUT_NO_AUTHOR))1653printf(" (%-*.*s%10s",1654 longest_author, longest_author,1655 ci.author,1656format_time(ci.author_time,1657 ci.author_tz,1658 show_raw_time));1659printf(" %*d) ",1660 max_digits, ent->lno +1+ cnt);1661}1662do{1663 ch = *cp++;1664putchar(ch);1665}while(ch !='\n'&&1666 cp < sb->final_buf + sb->final_buf_size);1667}1668}16691670static voidoutput(struct scoreboard *sb,int option)1671{1672struct blame_entry *ent;16731674if(option & OUTPUT_PORCELAIN) {1675for(ent = sb->ent; ent; ent = ent->next) {1676struct blame_entry *oth;1677struct origin *suspect = ent->suspect;1678struct commit *commit = suspect->commit;1679if(commit->object.flags & MORE_THAN_ONE_PATH)1680continue;1681for(oth = ent->next; oth; oth = oth->next) {1682if((oth->suspect->commit != commit) ||1683!strcmp(oth->suspect->path, suspect->path))1684continue;1685 commit->object.flags |= MORE_THAN_ONE_PATH;1686break;1687}1688}1689}16901691for(ent = sb->ent; ent; ent = ent->next) {1692if(option & OUTPUT_PORCELAIN)1693emit_porcelain(sb, ent);1694else{1695emit_other(sb, ent, option);1696}1697}1698}16991700/*1701 * To allow quick access to the contents of nth line in the1702 * final image, prepare an index in the scoreboard.1703 */1704static intprepare_lines(struct scoreboard *sb)1705{1706const char*buf = sb->final_buf;1707unsigned long len = sb->final_buf_size;1708int num =0, incomplete =0, bol =1;17091710if(len && buf[len-1] !='\n')1711 incomplete++;/* incomplete line at the end */1712while(len--) {1713if(bol) {1714 sb->lineno =xrealloc(sb->lineno,1715sizeof(int* ) * (num +1));1716 sb->lineno[num] = buf - sb->final_buf;1717 bol =0;1718}1719if(*buf++ =='\n') {1720 num++;1721 bol =1;1722}1723}1724 sb->lineno =xrealloc(sb->lineno,1725sizeof(int* ) * (num + incomplete +1));1726 sb->lineno[num + incomplete] = buf - sb->final_buf;1727 sb->num_lines = num + incomplete;1728return sb->num_lines;1729}17301731/*1732 * Add phony grafts for use with -S; this is primarily to1733 * support git-cvsserver that wants to give a linear history1734 * to its clients.1735 */1736static intread_ancestry(const char*graft_file)1737{1738FILE*fp =fopen(graft_file,"r");1739char buf[1024];1740if(!fp)1741return-1;1742while(fgets(buf,sizeof(buf), fp)) {1743/* The format is just "Commit Parent1 Parent2 ...\n" */1744int len =strlen(buf);1745struct commit_graft *graft =read_graft_line(buf, len);1746if(graft)1747register_commit_graft(graft,0);1748}1749fclose(fp);1750return0;1751}17521753/*1754 * How many columns do we need to show line numbers in decimal?1755 */1756static intlineno_width(int lines)1757{1758int i, width;17591760for(width =1, i =10; i <= lines +1; width++)1761 i *=10;1762return width;1763}17641765/*1766 * How many columns do we need to show line numbers, authors,1767 * and filenames?1768 */1769static voidfind_alignment(struct scoreboard *sb,int*option)1770{1771int longest_src_lines =0;1772int longest_dst_lines =0;1773unsigned largest_score =0;1774struct blame_entry *e;17751776for(e = sb->ent; e; e = e->next) {1777struct origin *suspect = e->suspect;1778struct commit_info ci;1779int num;17801781if(strcmp(suspect->path, sb->path))1782*option |= OUTPUT_SHOW_NAME;1783 num =strlen(suspect->path);1784if(longest_file < num)1785 longest_file = num;1786if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {1787 suspect->commit->object.flags |= METAINFO_SHOWN;1788get_commit_info(suspect->commit, &ci,1);1789 num =strlen(ci.author);1790if(longest_author < num)1791 longest_author = num;1792}1793 num = e->s_lno + e->num_lines;1794if(longest_src_lines < num)1795 longest_src_lines = num;1796 num = e->lno + e->num_lines;1797if(longest_dst_lines < num)1798 longest_dst_lines = num;1799if(largest_score <ent_score(sb, e))1800 largest_score =ent_score(sb, e);1801}1802 max_orig_digits =lineno_width(longest_src_lines);1803 max_digits =lineno_width(longest_dst_lines);1804 max_score_digits =lineno_width(largest_score);1805}18061807/*1808 * For debugging -- origin is refcounted, and this asserts that1809 * we do not underflow.1810 */1811static voidsanity_check_refcnt(struct scoreboard *sb)1812{1813int baa =0;1814struct blame_entry *ent;18151816for(ent = sb->ent; ent; ent = ent->next) {1817/* Nobody should have zero or negative refcnt */1818if(ent->suspect->refcnt <=0) {1819fprintf(stderr,"%sin%shas negative refcnt%d\n",1820 ent->suspect->path,1821sha1_to_hex(ent->suspect->commit->object.sha1),1822 ent->suspect->refcnt);1823 baa =1;1824}1825}1826for(ent = sb->ent; ent; ent = ent->next) {1827/* Mark the ones that haven't been checked */1828if(0< ent->suspect->refcnt)1829 ent->suspect->refcnt = -ent->suspect->refcnt;1830}1831for(ent = sb->ent; ent; ent = ent->next) {1832/*1833 * ... then pick each and see if they have the the1834 * correct refcnt.1835 */1836int found;1837struct blame_entry *e;1838struct origin *suspect = ent->suspect;18391840if(0< suspect->refcnt)1841continue;1842 suspect->refcnt = -suspect->refcnt;/* Unmark */1843for(found =0, e = sb->ent; e; e = e->next) {1844if(e->suspect != suspect)1845continue;1846 found++;1847}1848if(suspect->refcnt != found) {1849fprintf(stderr,"%sin%shas refcnt%d, not%d\n",1850 ent->suspect->path,1851sha1_to_hex(ent->suspect->commit->object.sha1),1852 ent->suspect->refcnt, found);1853 baa =2;1854}1855}1856if(baa) {1857int opt =0160;1858find_alignment(sb, &opt);1859output(sb, opt);1860die("Baa%d!", baa);1861}1862}18631864/*1865 * Used for the command line parsing; check if the path exists1866 * in the working tree.1867 */1868static inthas_path_in_work_tree(const char*path)1869{1870struct stat st;1871return!lstat(path, &st);1872}18731874static unsignedparse_score(const char*arg)1875{1876char*end;1877unsigned long score =strtoul(arg, &end,10);1878if(*end)1879return0;1880return score;1881}18821883static const char*add_prefix(const char*prefix,const char*path)1884{1885if(!prefix || !prefix[0])1886return path;1887returnprefix_path(prefix,strlen(prefix), path);1888}18891890/*1891 * Parsing of (comma separated) one item in the -L option1892 */1893static const char*parse_loc(const char*spec,1894struct scoreboard *sb,long lno,1895long begin,long*ret)1896{1897char*term;1898const char*line;1899long num;1900int reg_error;1901 regex_t regexp;1902 regmatch_t match[1];19031904/* Allow "-L <something>,+20" to mean starting at <something>1905 * for 20 lines, or "-L <something>,-5" for 5 lines ending at1906 * <something>.1907 */1908if(1< begin && (spec[0] =='+'|| spec[0] =='-')) {1909 num =strtol(spec +1, &term,10);1910if(term != spec +1) {1911if(spec[0] =='-')1912 num =0- num;1913if(0< num)1914*ret = begin + num -2;1915else if(!num)1916*ret = begin;1917else1918*ret = begin + num;1919return term;1920}1921return spec;1922}1923 num =strtol(spec, &term,10);1924if(term != spec) {1925*ret = num;1926return term;1927}1928if(spec[0] !='/')1929return spec;19301931/* it could be a regexp of form /.../ */1932for(term = (char*) spec +1; *term && *term !='/'; term++) {1933if(*term =='\\')1934 term++;1935}1936if(*term !='/')1937return spec;19381939/* try [spec+1 .. term-1] as regexp */1940*term =0;1941 begin--;/* input is in human terms */1942 line =nth_line(sb, begin);19431944if(!(reg_error =regcomp(®exp, spec +1, REG_NEWLINE)) &&1945!(reg_error =regexec(®exp, line,1, match,0))) {1946const char*cp = line + match[0].rm_so;1947const char*nline;19481949while(begin++ < lno) {1950 nline =nth_line(sb, begin);1951if(line <= cp && cp < nline)1952break;1953 line = nline;1954}1955*ret = begin;1956regfree(®exp);1957*term++ ='/';1958return term;1959}1960else{1961char errbuf[1024];1962regerror(reg_error, ®exp, errbuf,1024);1963die("-L parameter '%s':%s", spec +1, errbuf);1964}1965}19661967/*1968 * Parsing of -L option1969 */1970static voidprepare_blame_range(struct scoreboard *sb,1971const char*bottomtop,1972long lno,1973long*bottom,long*top)1974{1975const char*term;19761977 term =parse_loc(bottomtop, sb, lno,1, bottom);1978if(*term ==',') {1979 term =parse_loc(term +1, sb, lno, *bottom +1, top);1980if(*term)1981usage(blame_usage);1982}1983if(*term)1984usage(blame_usage);1985}19861987static intgit_blame_config(const char*var,const char*value)1988{1989if(!strcmp(var,"blame.showroot")) {1990 show_root =git_config_bool(var, value);1991return0;1992}1993if(!strcmp(var,"blame.blankboundary")) {1994 blank_boundary =git_config_bool(var, value);1995return0;1996}1997returngit_default_config(var, value);1998}19992000static struct commit *fake_working_tree_commit(const char*path,const char*contents_from)2001{2002struct commit *commit;2003struct origin *origin;2004unsigned char head_sha1[20];2005struct strbuf buf;2006const char*ident;2007int fd;2008time_t now;2009int size, len;2010struct cache_entry *ce;2011unsigned mode;20122013if(get_sha1("HEAD", head_sha1))2014die("No such ref: HEAD");20152016time(&now);2017 commit =xcalloc(1,sizeof(*commit));2018 commit->parents =xcalloc(1,sizeof(*commit->parents));2019 commit->parents->item =lookup_commit_reference(head_sha1);2020 commit->object.parsed =1;2021 commit->date = now;2022 commit->object.type = OBJ_COMMIT;20232024 origin =make_origin(commit, path);20252026strbuf_init(&buf);2027if(!contents_from ||strcmp("-", contents_from)) {2028struct stat st;2029const char*read_from;2030unsigned long fin_size;20312032if(contents_from) {2033if(stat(contents_from, &st) <0)2034die("Cannot stat%s", contents_from);2035 read_from = contents_from;2036}2037else{2038if(lstat(path, &st) <0)2039die("Cannot lstat%s", path);2040 read_from = path;2041}2042 fin_size =xsize_t(st.st_size);2043 mode =canon_mode(st.st_mode);2044switch(st.st_mode & S_IFMT) {2045case S_IFREG:2046 fd =open(read_from, O_RDONLY);2047if(fd <0)2048die("cannot open%s", read_from);2049if(strbuf_read(&buf, fd) !=xsize_t(st.st_size))2050die("cannot read%s", read_from);2051break;2052case S_IFLNK:2053if(readlink(read_from, buf.buf, buf.alloc) != fin_size)2054die("cannot readlink%s", read_from);2055 buf.len = fin_size;2056break;2057default:2058die("unsupported file type%s", read_from);2059}2060}2061else{2062/* Reading from stdin */2063 contents_from ="standard input";2064 mode =0;2065if(strbuf_read(&buf,0) <0)2066die("read error%sfrom stdin",strerror(errno));2067}2068 origin->file.ptr = buf.buf;2069 origin->file.size = buf.len;2070pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);2071 commit->util = origin;20722073/*2074 * Read the current index, replace the path entry with2075 * origin->blob_sha1 without mucking with its mode or type2076 * bits; we are not going to write this index out -- we just2077 * want to run "diff-index --cached".2078 */2079discard_cache();2080read_cache();20812082 len =strlen(path);2083if(!mode) {2084int pos =cache_name_pos(path, len);2085if(0<= pos)2086 mode =ntohl(active_cache[pos]->ce_mode);2087else2088/* Let's not bother reading from HEAD tree */2089 mode = S_IFREG |0644;2090}2091 size =cache_entry_size(len);2092 ce =xcalloc(1, size);2093hashcpy(ce->sha1, origin->blob_sha1);2094memcpy(ce->name, path, len);2095 ce->ce_flags =create_ce_flags(len,0);2096 ce->ce_mode =create_ce_mode(mode);2097add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);20982099/*2100 * We are not going to write this out, so this does not matter2101 * right now, but someday we might optimize diff-index --cached2102 * with cache-tree information.2103 */2104cache_tree_invalidate_path(active_cache_tree, path);21052106 commit->buffer =xmalloc(400);2107 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2108snprintf(commit->buffer,400,2109"tree 0000000000000000000000000000000000000000\n"2110"parent%s\n"2111"author%s\n"2112"committer%s\n\n"2113"Version of%sfrom%s\n",2114sha1_to_hex(head_sha1),2115 ident, ident, path, contents_from ? contents_from : path);2116return commit;2117}21182119intcmd_blame(int argc,const char**argv,const char*prefix)2120{2121struct rev_info revs;2122const char*path;2123struct scoreboard sb;2124struct origin *o;2125struct blame_entry *ent;2126int i, seen_dashdash, unk, opt;2127long bottom, top, lno;2128int output_option =0;2129int show_stats =0;2130const char*revs_file = NULL;2131const char*final_commit_name = NULL;2132enum object_type type;2133const char*bottomtop = NULL;2134const char*contents_from = NULL;21352136 cmd_is_annotate = !strcmp(argv[0],"annotate");21372138git_config(git_blame_config);2139 save_commit_buffer =0;21402141 opt =0;2142 seen_dashdash =0;2143for(unk = i =1; i < argc; i++) {2144const char*arg = argv[i];2145if(*arg !='-')2146break;2147else if(!strcmp("-b", arg))2148 blank_boundary =1;2149else if(!strcmp("--root", arg))2150 show_root =1;2151else if(!strcmp(arg,"--show-stats"))2152 show_stats =1;2153else if(!strcmp("-c", arg))2154 output_option |= OUTPUT_ANNOTATE_COMPAT;2155else if(!strcmp("-t", arg))2156 output_option |= OUTPUT_RAW_TIMESTAMP;2157else if(!strcmp("-l", arg))2158 output_option |= OUTPUT_LONG_OBJECT_NAME;2159else if(!strcmp("-s", arg))2160 output_option |= OUTPUT_NO_AUTHOR;2161else if(!strcmp("-w", arg))2162 xdl_opts |= XDF_IGNORE_WHITESPACE;2163else if(!strcmp("-S", arg) && ++i < argc)2164 revs_file = argv[i];2165else if(!prefixcmp(arg,"-M")) {2166 opt |= PICKAXE_BLAME_MOVE;2167 blame_move_score =parse_score(arg+2);2168}2169else if(!prefixcmp(arg,"-C")) {2170/*2171 * -C enables copy from removed files;2172 * -C -C enables copy from existing files, but only2173 * when blaming a new file;2174 * -C -C -C enables copy from existing files for2175 * everybody2176 */2177if(opt & PICKAXE_BLAME_COPY_HARDER)2178 opt |= PICKAXE_BLAME_COPY_HARDEST;2179if(opt & PICKAXE_BLAME_COPY)2180 opt |= PICKAXE_BLAME_COPY_HARDER;2181 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;2182 blame_copy_score =parse_score(arg+2);2183}2184else if(!prefixcmp(arg,"-L")) {2185if(!arg[2]) {2186if(++i >= argc)2187usage(blame_usage);2188 arg = argv[i];2189}2190else2191 arg +=2;2192if(bottomtop)2193die("More than one '-L n,m' option given");2194 bottomtop = arg;2195}2196else if(!strcmp("--contents", arg)) {2197if(++i >= argc)2198usage(blame_usage);2199 contents_from = argv[i];2200}2201else if(!strcmp("--incremental", arg))2202 incremental =1;2203else if(!strcmp("--score-debug", arg))2204 output_option |= OUTPUT_SHOW_SCORE;2205else if(!strcmp("-f", arg) ||2206!strcmp("--show-name", arg))2207 output_option |= OUTPUT_SHOW_NAME;2208else if(!strcmp("-n", arg) ||2209!strcmp("--show-number", arg))2210 output_option |= OUTPUT_SHOW_NUMBER;2211else if(!strcmp("-p", arg) ||2212!strcmp("--porcelain", arg))2213 output_option |= OUTPUT_PORCELAIN;2214else if(!strcmp("--", arg)) {2215 seen_dashdash =1;2216 i++;2217break;2218}2219else2220 argv[unk++] = arg;2221}22222223if(!incremental)2224setup_pager();22252226if(!blame_move_score)2227 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;2228if(!blame_copy_score)2229 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;22302231/*2232 * We have collected options unknown to us in argv[1..unk]2233 * which are to be passed to revision machinery if we are2234 * going to do the "bottom" processing.2235 *2236 * The remaining are:2237 *2238 * (1) if seen_dashdash, its either2239 * "-options -- <path>" or2240 * "-options -- <path> <rev>".2241 * but the latter is allowed only if there is no2242 * options that we passed to revision machinery.2243 *2244 * (2) otherwise, we may have "--" somewhere later and2245 * might be looking at the first one of multiple 'rev'2246 * parameters (e.g. " master ^next ^maint -- path").2247 * See if there is a dashdash first, and give the2248 * arguments before that to revision machinery.2249 * After that there must be one 'path'.2250 *2251 * (3) otherwise, its one of the three:2252 * "-options <path> <rev>"2253 * "-options <rev> <path>"2254 * "-options <path>"2255 * but again the first one is allowed only if2256 * there is no options that we passed to revision2257 * machinery.2258 */22592260if(seen_dashdash) {2261/* (1) */2262if(argc <= i)2263usage(blame_usage);2264 path =add_prefix(prefix, argv[i]);2265if(i +1== argc -1) {2266if(unk !=1)2267usage(blame_usage);2268 argv[unk++] = argv[i +1];2269}2270else if(i +1!= argc)2271/* garbage at end */2272usage(blame_usage);2273}2274else{2275int j;2276for(j = i; !seen_dashdash && j < argc; j++)2277if(!strcmp(argv[j],"--"))2278 seen_dashdash = j;2279if(seen_dashdash) {2280/* (2) */2281if(seen_dashdash +1!= argc -1)2282usage(blame_usage);2283 path =add_prefix(prefix, argv[seen_dashdash +1]);2284for(j = i; j < seen_dashdash; j++)2285 argv[unk++] = argv[j];2286}2287else{2288/* (3) */2289if(argc <= i)2290usage(blame_usage);2291 path =add_prefix(prefix, argv[i]);2292if(i +1== argc -1) {2293 final_commit_name = argv[i +1];22942295/* if (unk == 1) we could be getting2296 * old-style2297 */2298if(unk ==1&& !has_path_in_work_tree(path)) {2299 path =add_prefix(prefix, argv[i +1]);2300 final_commit_name = argv[i];2301}2302}2303else if(i != argc -1)2304usage(blame_usage);/* garbage at end */23052306if(!has_path_in_work_tree(path))2307die("cannot stat path%s:%s",2308 path,strerror(errno));2309}2310}23112312if(final_commit_name)2313 argv[unk++] = final_commit_name;23142315/*2316 * Now we got rev and path. We do not want the path pruning2317 * but we may want "bottom" processing.2318 */2319 argv[unk++] ="--";/* terminate the rev name */2320 argv[unk] = NULL;23212322init_revisions(&revs, NULL);2323setup_revisions(unk, argv, &revs, NULL);2324memset(&sb,0,sizeof(sb));23252326/*2327 * There must be one and only one positive commit in the2328 * revs->pending array.2329 */2330for(i =0; i < revs.pending.nr; i++) {2331struct object *obj = revs.pending.objects[i].item;2332if(obj->flags & UNINTERESTING)2333continue;2334while(obj->type == OBJ_TAG)2335 obj =deref_tag(obj, NULL,0);2336if(obj->type != OBJ_COMMIT)2337die("Non commit%s?",2338 revs.pending.objects[i].name);2339if(sb.final)2340die("More than one commit to dig from%sand%s?",2341 revs.pending.objects[i].name,2342 final_commit_name);2343 sb.final = (struct commit *) obj;2344 final_commit_name = revs.pending.objects[i].name;2345}23462347if(!sb.final) {2348/*2349 * "--not A B -- path" without anything positive;2350 * do not default to HEAD, but use the working tree2351 * or "--contents".2352 */2353 sb.final =fake_working_tree_commit(path, contents_from);2354add_pending_object(&revs, &(sb.final->object),":");2355}2356else if(contents_from)2357die("Cannot use --contents with final commit object name");23582359/*2360 * If we have bottom, this will mark the ancestors of the2361 * bottom commits we would reach while traversing as2362 * uninteresting.2363 */2364prepare_revision_walk(&revs);23652366if(is_null_sha1(sb.final->object.sha1)) {2367char*buf;2368 o = sb.final->util;2369 buf =xmalloc(o->file.size +1);2370memcpy(buf, o->file.ptr, o->file.size +1);2371 sb.final_buf = buf;2372 sb.final_buf_size = o->file.size;2373}2374else{2375 o =get_origin(&sb, sb.final, path);2376if(fill_blob_sha1(o))2377die("no such path%sin%s", path, final_commit_name);23782379 sb.final_buf =read_sha1_file(o->blob_sha1, &type,2380&sb.final_buf_size);2381if(!sb.final_buf)2382die("Cannot read blob%sfor path%s",2383sha1_to_hex(o->blob_sha1),2384 path);2385}2386 num_read_blob++;2387 lno =prepare_lines(&sb);23882389 bottom = top =0;2390if(bottomtop)2391prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);2392if(bottom && top && top < bottom) {2393long tmp;2394 tmp = top; top = bottom; bottom = tmp;2395}2396if(bottom <1)2397 bottom =1;2398if(top <1)2399 top = lno;2400 bottom--;2401if(lno < top)2402die("file%shas only%lu lines", path, lno);24032404 ent =xcalloc(1,sizeof(*ent));2405 ent->lno = bottom;2406 ent->num_lines = top - bottom;2407 ent->suspect = o;2408 ent->s_lno = bottom;24092410 sb.ent = ent;2411 sb.path = path;24122413if(revs_file &&read_ancestry(revs_file))2414die("reading graft file%sfailed:%s",2415 revs_file,strerror(errno));24162417read_mailmap(&mailmap,".mailmap", NULL);24182419assign_blame(&sb, &revs, opt);24202421if(incremental)2422return0;24232424coalesce(&sb);24252426if(!(output_option & OUTPUT_PORCELAIN))2427find_alignment(&sb, &output_option);24282429output(&sb, output_option);2430free((void*)sb.final_buf);2431for(ent = sb.ent; ent; ) {2432struct blame_entry *e = ent->next;2433free(ent);2434 ent = e;2435}24362437if(show_stats) {2438printf("num read blob:%d\n", num_read_blob);2439printf("num get patch:%d\n", num_get_patch);2440printf("num commits:%d\n", num_commits);2441}2442return0;2443}