1/* 2 * Blame 3 * 4 * Copyright (c) 2006, 2014 by its authors 5 * See COPYING for licensing conditions 6 */ 7 8#include"cache.h" 9#include"refs.h" 10#include"builtin.h" 11#include"commit.h" 12#include"tag.h" 13#include"tree-walk.h" 14#include"diff.h" 15#include"diffcore.h" 16#include"revision.h" 17#include"quote.h" 18#include"xdiff-interface.h" 19#include"cache-tree.h" 20#include"string-list.h" 21#include"mailmap.h" 22#include"mergesort.h" 23#include"parse-options.h" 24#include"prio-queue.h" 25#include"utf8.h" 26#include"userdiff.h" 27#include"line-range.h" 28#include"line-log.h" 29#include"dir.h" 30#include"progress.h" 31 32static char blame_usage[] =N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>"); 33 34static const char*blame_opt_usage[] = { 35 blame_usage, 36"", 37N_("<rev-opts> are documented in git-rev-list(1)"), 38 NULL 39}; 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 reverse; 48static int blank_boundary; 49static int incremental; 50static int xdl_opts; 51static int abbrev = -1; 52static int no_whole_file_rename; 53static int show_progress; 54 55static struct date_mode blame_date_mode = { DATE_ISO8601 }; 56static size_t blame_date_width; 57 58static struct string_list mailmap = STRING_LIST_INIT_NODUP; 59 60#ifndef DEBUG 61#define DEBUG 0 62#endif 63 64#define PICKAXE_BLAME_MOVE 01 65#define PICKAXE_BLAME_COPY 02 66#define PICKAXE_BLAME_COPY_HARDER 04 67#define PICKAXE_BLAME_COPY_HARDEST 010 68 69static unsigned blame_move_score; 70static unsigned blame_copy_score; 71#define BLAME_DEFAULT_MOVE_SCORE 20 72#define BLAME_DEFAULT_COPY_SCORE 40 73 74/* Remember to update object flag allocation in object.h */ 75#define METAINFO_SHOWN (1u<<12) 76#define MORE_THAN_ONE_PATH (1u<<13) 77 78/* 79 * One blob in a commit that is being suspected 80 */ 81struct blame_origin { 82int refcnt; 83/* Record preceding blame record for this blob */ 84struct blame_origin *previous; 85/* origins are put in a list linked via `next' hanging off the 86 * corresponding commit's util field in order to make finding 87 * them fast. The presence in this chain does not count 88 * towards the origin's reference count. It is tempting to 89 * let it count as long as the commit is pending examination, 90 * but even under circumstances where the commit will be 91 * present multiple times in the priority queue of unexamined 92 * commits, processing the first instance will not leave any 93 * work requiring the origin data for the second instance. An 94 * interspersed commit changing that would have to be 95 * preexisting with a different ancestry and with the same 96 * commit date in order to wedge itself between two instances 97 * of the same commit in the priority queue _and_ produce 98 * blame entries relevant for it. While we don't want to let 99 * us get tripped up by this case, it certainly does not seem 100 * worth optimizing for. 101 */ 102struct blame_origin *next; 103struct commit *commit; 104/* `suspects' contains blame entries that may be attributed to 105 * this origin's commit or to parent commits. When a commit 106 * is being processed, all suspects will be moved, either by 107 * assigning them to an origin in a different commit, or by 108 * shipping them to the scoreboard's ent list because they 109 * cannot be attributed to a different commit. 110 */ 111struct blame_entry *suspects; 112 mmfile_t file; 113struct object_id blob_oid; 114unsigned mode; 115/* guilty gets set when shipping any suspects to the final 116 * blame list instead of other commits 117 */ 118char guilty; 119char path[FLEX_ARRAY]; 120}; 121 122struct progress_info { 123struct progress *progress; 124int blamed_lines; 125}; 126 127static intdiff_hunks(mmfile_t *file_a, mmfile_t *file_b, 128 xdl_emit_hunk_consume_func_t hunk_func,void*cb_data,int xdl_opts) 129{ 130 xpparam_t xpp = {0}; 131 xdemitconf_t xecfg = {0}; 132 xdemitcb_t ecb = {NULL}; 133 134 xpp.flags = xdl_opts; 135 xecfg.hunk_func = hunk_func; 136 ecb.priv = cb_data; 137returnxdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); 138} 139 140/* 141 * Given an origin, prepare mmfile_t structure to be used by the 142 * diff machinery 143 */ 144static voidfill_origin_blob(struct diff_options *opt, 145struct blame_origin *o, mmfile_t *file,int*num_read_blob) 146{ 147if(!o->file.ptr) { 148enum object_type type; 149unsigned long file_size; 150 151(*num_read_blob)++; 152if(DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && 153textconv_object(o->path, o->mode, &o->blob_oid,1, &file->ptr, &file_size)) 154; 155else 156 file->ptr =read_sha1_file(o->blob_oid.hash, &type, 157&file_size); 158 file->size = file_size; 159 160if(!file->ptr) 161die("Cannot read blob%sfor path%s", 162oid_to_hex(&o->blob_oid), 163 o->path); 164 o->file = *file; 165} 166else 167*file = o->file; 168} 169 170/* 171 * Origin is refcounted and usually we keep the blob contents to be 172 * reused. 173 */ 174staticinlinestruct blame_origin *blame_origin_incref(struct blame_origin *o) 175{ 176if(o) 177 o->refcnt++; 178return o; 179} 180 181static voidblame_origin_decref(struct blame_origin *o) 182{ 183if(o && --o->refcnt <=0) { 184struct blame_origin *p, *l = NULL; 185if(o->previous) 186blame_origin_decref(o->previous); 187free(o->file.ptr); 188/* Should be present exactly once in commit chain */ 189for(p = o->commit->util; p; l = p, p = p->next) { 190if(p == o) { 191if(l) 192 l->next = p->next; 193else 194 o->commit->util = p->next; 195free(o); 196return; 197} 198} 199die("internal error in blame_origin_decref"); 200} 201} 202 203static voiddrop_origin_blob(struct blame_origin *o) 204{ 205if(o->file.ptr) { 206free(o->file.ptr); 207 o->file.ptr = NULL; 208} 209} 210 211/* 212 * Each group of lines is described by a blame_entry; it can be split 213 * as we pass blame to the parents. They are arranged in linked lists 214 * kept as `suspects' of some unprocessed origin, or entered (when the 215 * blame origin has been finalized) into the scoreboard structure. 216 * While the scoreboard structure is only sorted at the end of 217 * processing (according to final image line number), the lists 218 * attached to an origin are sorted by the target line number. 219 */ 220struct blame_entry { 221struct blame_entry *next; 222 223/* the first line of this group in the final image; 224 * internally all line numbers are 0 based. 225 */ 226int lno; 227 228/* how many lines this group has */ 229int num_lines; 230 231/* the commit that introduced this group into the final image */ 232struct blame_origin *suspect; 233 234/* the line number of the first line of this group in the 235 * suspect's file; internally all line numbers are 0 based. 236 */ 237int s_lno; 238 239/* how significant this entry is -- cached to avoid 240 * scanning the lines over and over. 241 */ 242unsigned score; 243}; 244 245/* 246 * Any merge of blames happens on lists of blames that arrived via 247 * different parents in a single suspect. In this case, we want to 248 * sort according to the suspect line numbers as opposed to the final 249 * image line numbers. The function body is somewhat longish because 250 * it avoids unnecessary writes. 251 */ 252 253static struct blame_entry *blame_merge(struct blame_entry *list1, 254struct blame_entry *list2) 255{ 256struct blame_entry *p1 = list1, *p2 = list2, 257**tail = &list1; 258 259if(!p1) 260return p2; 261if(!p2) 262return p1; 263 264if(p1->s_lno <= p2->s_lno) { 265do{ 266 tail = &p1->next; 267if((p1 = *tail) == NULL) { 268*tail = p2; 269return list1; 270} 271}while(p1->s_lno <= p2->s_lno); 272} 273for(;;) { 274*tail = p2; 275do{ 276 tail = &p2->next; 277if((p2 = *tail) == NULL) { 278*tail = p1; 279return list1; 280} 281}while(p1->s_lno > p2->s_lno); 282*tail = p1; 283do{ 284 tail = &p1->next; 285if((p1 = *tail) == NULL) { 286*tail = p2; 287return list1; 288} 289}while(p1->s_lno <= p2->s_lno); 290} 291} 292 293static void*get_next_blame(const void*p) 294{ 295return((struct blame_entry *)p)->next; 296} 297 298static voidset_next_blame(void*p1,void*p2) 299{ 300((struct blame_entry *)p1)->next = p2; 301} 302 303/* 304 * Final image line numbers are all different, so we don't need a 305 * three-way comparison here. 306 */ 307 308static intcompare_blame_final(const void*p1,const void*p2) 309{ 310return((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno 311?1: -1; 312} 313 314static intcompare_blame_suspect(const void*p1,const void*p2) 315{ 316const struct blame_entry *s1 = p1, *s2 = p2; 317/* 318 * to allow for collating suspects, we sort according to the 319 * respective pointer value as the primary sorting criterion. 320 * The actual relation is pretty unimportant as long as it 321 * establishes a total order. Comparing as integers gives us 322 * that. 323 */ 324if(s1->suspect != s2->suspect) 325return(intptr_t)s1->suspect > (intptr_t)s2->suspect ?1: -1; 326if(s1->s_lno == s2->s_lno) 327return0; 328return s1->s_lno > s2->s_lno ?1: -1; 329} 330 331static intcompare_commits_by_reverse_commit_date(const void*a, 332const void*b, 333void*c) 334{ 335return-compare_commits_by_commit_date(a, b, c); 336} 337 338/* 339 * The current state of the blame assignment. 340 */ 341struct blame_scoreboard { 342/* the final commit (i.e. where we started digging from) */ 343struct commit *final; 344/* Priority queue for commits with unassigned blame records */ 345struct prio_queue commits; 346struct rev_info *revs; 347const char*path; 348 349/* 350 * The contents in the final image. 351 * Used by many functions to obtain contents of the nth line, 352 * indexed with scoreboard.lineno[blame_entry.lno]. 353 */ 354const char*final_buf; 355unsigned long final_buf_size; 356 357/* linked list of blames */ 358struct blame_entry *ent; 359 360/* look-up a line in the final buffer */ 361int num_lines; 362int*lineno; 363 364/* stats */ 365int num_read_blob; 366int num_get_patch; 367int num_commits; 368 369/* 370 * blame for a blame_entry with score lower than these thresholds 371 * is not passed to the parent using move/copy logic. 372 */ 373unsigned move_score; 374unsigned copy_score; 375 376/* use this file's contents as the final image */ 377const char*contents_from; 378 379/* flags */ 380int reverse; 381int show_root; 382int xdl_opts; 383int no_whole_file_rename; 384int debug; 385 386/* callbacks */ 387void(*on_sanity_fail)(struct blame_scoreboard *,int); 388void(*found_guilty_entry)(struct blame_entry *,void*); 389 390void*found_guilty_entry_data; 391}; 392 393static voidblame_sort_final(struct blame_scoreboard *sb) 394{ 395 sb->ent =llist_mergesort(sb->ent, get_next_blame, set_next_blame, 396 compare_blame_final); 397} 398 399static voidsanity_check_refcnt(struct blame_scoreboard *); 400 401/* 402 * If two blame entries that are next to each other came from 403 * contiguous lines in the same origin (i.e. <commit, path> pair), 404 * merge them together. 405 */ 406static voidblame_coalesce(struct blame_scoreboard *sb) 407{ 408struct blame_entry *ent, *next; 409 410for(ent = sb->ent; ent && (next = ent->next); ent = next) { 411if(ent->suspect == next->suspect && 412 ent->s_lno + ent->num_lines == next->s_lno) { 413 ent->num_lines += next->num_lines; 414 ent->next = next->next; 415blame_origin_decref(next->suspect); 416free(next); 417 ent->score =0; 418 next = ent;/* again */ 419} 420} 421 422if(sb->debug)/* sanity */ 423sanity_check_refcnt(sb); 424} 425 426/* 427 * Merge the given sorted list of blames into a preexisting origin. 428 * If there were no previous blames to that commit, it is entered into 429 * the commit priority queue of the score board. 430 */ 431 432static voidqueue_blames(struct blame_scoreboard *sb,struct blame_origin *porigin, 433struct blame_entry *sorted) 434{ 435if(porigin->suspects) 436 porigin->suspects =blame_merge(porigin->suspects, sorted); 437else{ 438struct blame_origin *o; 439for(o = porigin->commit->util; o; o = o->next) { 440if(o->suspects) { 441 porigin->suspects = sorted; 442return; 443} 444} 445 porigin->suspects = sorted; 446prio_queue_put(&sb->commits, porigin->commit); 447} 448} 449 450/* 451 * Given a commit and a path in it, create a new origin structure. 452 * The callers that add blame to the scoreboard should use 453 * get_origin() to obtain shared, refcounted copy instead of calling 454 * this function directly. 455 */ 456static struct blame_origin *make_origin(struct commit *commit,const char*path) 457{ 458struct blame_origin *o; 459FLEX_ALLOC_STR(o, path, path); 460 o->commit = commit; 461 o->refcnt =1; 462 o->next = commit->util; 463 commit->util = o; 464return o; 465} 466 467/* 468 * Locate an existing origin or create a new one. 469 * This moves the origin to front position in the commit util list. 470 */ 471static struct blame_origin *get_origin(struct commit *commit,const char*path) 472{ 473struct blame_origin *o, *l; 474 475for(o = commit->util, l = NULL; o; l = o, o = o->next) { 476if(!strcmp(o->path, path)) { 477/* bump to front */ 478if(l) { 479 l->next = o->next; 480 o->next = commit->util; 481 commit->util = o; 482} 483returnblame_origin_incref(o); 484} 485} 486returnmake_origin(commit, path); 487} 488 489/* 490 * Fill the blob_sha1 field of an origin if it hasn't, so that later 491 * call to fill_origin_blob() can use it to locate the data. blob_sha1 492 * for an origin is also used to pass the blame for the entire file to 493 * the parent to detect the case where a child's blob is identical to 494 * that of its parent's. 495 * 496 * This also fills origin->mode for corresponding tree path. 497 */ 498static intfill_blob_sha1_and_mode(struct blame_origin *origin) 499{ 500if(!is_null_oid(&origin->blob_oid)) 501return0; 502if(get_tree_entry(origin->commit->object.oid.hash, 503 origin->path, 504 origin->blob_oid.hash, &origin->mode)) 505goto error_out; 506if(sha1_object_info(origin->blob_oid.hash, NULL) != OBJ_BLOB) 507goto error_out; 508return0; 509 error_out: 510oidclr(&origin->blob_oid); 511 origin->mode = S_IFINVALID; 512return-1; 513} 514 515/* 516 * We have an origin -- check if the same path exists in the 517 * parent and return an origin structure to represent it. 518 */ 519static struct blame_origin *find_origin(struct commit *parent, 520struct blame_origin *origin) 521{ 522struct blame_origin *porigin; 523struct diff_options diff_opts; 524const char*paths[2]; 525 526/* First check any existing origins */ 527for(porigin = parent->util; porigin; porigin = porigin->next) 528if(!strcmp(porigin->path, origin->path)) { 529/* 530 * The same path between origin and its parent 531 * without renaming -- the most common case. 532 */ 533returnblame_origin_incref(porigin); 534} 535 536/* See if the origin->path is different between parent 537 * and origin first. Most of the time they are the 538 * same and diff-tree is fairly efficient about this. 539 */ 540diff_setup(&diff_opts); 541DIFF_OPT_SET(&diff_opts, RECURSIVE); 542 diff_opts.detect_rename =0; 543 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 544 paths[0] = origin->path; 545 paths[1] = NULL; 546 547parse_pathspec(&diff_opts.pathspec, 548 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, 549 PATHSPEC_LITERAL_PATH,"", paths); 550diff_setup_done(&diff_opts); 551 552if(is_null_oid(&origin->commit->object.oid)) 553do_diff_cache(parent->tree->object.oid.hash, &diff_opts); 554else 555diff_tree_sha1(parent->tree->object.oid.hash, 556 origin->commit->tree->object.oid.hash, 557"", &diff_opts); 558diffcore_std(&diff_opts); 559 560if(!diff_queued_diff.nr) { 561/* The path is the same as parent */ 562 porigin =get_origin(parent, origin->path); 563oidcpy(&porigin->blob_oid, &origin->blob_oid); 564 porigin->mode = origin->mode; 565}else{ 566/* 567 * Since origin->path is a pathspec, if the parent 568 * commit had it as a directory, we will see a whole 569 * bunch of deletion of files in the directory that we 570 * do not care about. 571 */ 572int i; 573struct diff_filepair *p = NULL; 574for(i =0; i < diff_queued_diff.nr; i++) { 575const char*name; 576 p = diff_queued_diff.queue[i]; 577 name = p->one->path ? p->one->path : p->two->path; 578if(!strcmp(name, origin->path)) 579break; 580} 581if(!p) 582die("internal error in blame::find_origin"); 583switch(p->status) { 584default: 585die("internal error in blame::find_origin (%c)", 586 p->status); 587case'M': 588 porigin =get_origin(parent, origin->path); 589oidcpy(&porigin->blob_oid, &p->one->oid); 590 porigin->mode = p->one->mode; 591break; 592case'A': 593case'T': 594/* Did not exist in parent, or type changed */ 595break; 596} 597} 598diff_flush(&diff_opts); 599clear_pathspec(&diff_opts.pathspec); 600return porigin; 601} 602 603/* 604 * We have an origin -- find the path that corresponds to it in its 605 * parent and return an origin structure to represent it. 606 */ 607static struct blame_origin *find_rename(struct commit *parent, 608struct blame_origin *origin) 609{ 610struct blame_origin *porigin = NULL; 611struct diff_options diff_opts; 612int i; 613 614diff_setup(&diff_opts); 615DIFF_OPT_SET(&diff_opts, RECURSIVE); 616 diff_opts.detect_rename = DIFF_DETECT_RENAME; 617 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 618 diff_opts.single_follow = origin->path; 619diff_setup_done(&diff_opts); 620 621if(is_null_oid(&origin->commit->object.oid)) 622do_diff_cache(parent->tree->object.oid.hash, &diff_opts); 623else 624diff_tree_sha1(parent->tree->object.oid.hash, 625 origin->commit->tree->object.oid.hash, 626"", &diff_opts); 627diffcore_std(&diff_opts); 628 629for(i =0; i < diff_queued_diff.nr; i++) { 630struct diff_filepair *p = diff_queued_diff.queue[i]; 631if((p->status =='R'|| p->status =='C') && 632!strcmp(p->two->path, origin->path)) { 633 porigin =get_origin(parent, p->one->path); 634oidcpy(&porigin->blob_oid, &p->one->oid); 635 porigin->mode = p->one->mode; 636break; 637} 638} 639diff_flush(&diff_opts); 640clear_pathspec(&diff_opts.pathspec); 641return porigin; 642} 643 644/* 645 * Append a new blame entry to a given output queue. 646 */ 647static voidadd_blame_entry(struct blame_entry ***queue, 648const struct blame_entry *src) 649{ 650struct blame_entry *e =xmalloc(sizeof(*e)); 651memcpy(e, src,sizeof(*e)); 652blame_origin_incref(e->suspect); 653 654 e->next = **queue; 655**queue = e; 656*queue = &e->next; 657} 658 659/* 660 * src typically is on-stack; we want to copy the information in it to 661 * a malloced blame_entry that gets added to the given queue. The 662 * origin of dst loses a refcnt. 663 */ 664static voiddup_entry(struct blame_entry ***queue, 665struct blame_entry *dst,struct blame_entry *src) 666{ 667blame_origin_incref(src->suspect); 668blame_origin_decref(dst->suspect); 669memcpy(dst, src,sizeof(*src)); 670 dst->next = **queue; 671**queue = dst; 672*queue = &dst->next; 673} 674 675static const char*blame_nth_line(struct blame_scoreboard *sb,long lno) 676{ 677return sb->final_buf + sb->lineno[lno]; 678} 679 680static const char*nth_line_cb(void*data,long lno) 681{ 682returnblame_nth_line((struct blame_scoreboard *)data, lno); 683} 684 685/* 686 * It is known that lines between tlno to same came from parent, and e 687 * has an overlap with that range. it also is known that parent's 688 * line plno corresponds to e's line tlno. 689 * 690 * <---- e -----> 691 * <------> 692 * <------------> 693 * <------------> 694 * <------------------> 695 * 696 * Split e into potentially three parts; before this chunk, the chunk 697 * to be blamed for the parent, and after that portion. 698 */ 699static voidsplit_overlap(struct blame_entry *split, 700struct blame_entry *e, 701int tlno,int plno,int same, 702struct blame_origin *parent) 703{ 704int chunk_end_lno; 705memset(split,0,sizeof(struct blame_entry [3])); 706 707if(e->s_lno < tlno) { 708/* there is a pre-chunk part not blamed on parent */ 709 split[0].suspect =blame_origin_incref(e->suspect); 710 split[0].lno = e->lno; 711 split[0].s_lno = e->s_lno; 712 split[0].num_lines = tlno - e->s_lno; 713 split[1].lno = e->lno + tlno - e->s_lno; 714 split[1].s_lno = plno; 715} 716else{ 717 split[1].lno = e->lno; 718 split[1].s_lno = plno + (e->s_lno - tlno); 719} 720 721if(same < e->s_lno + e->num_lines) { 722/* there is a post-chunk part not blamed on parent */ 723 split[2].suspect =blame_origin_incref(e->suspect); 724 split[2].lno = e->lno + (same - e->s_lno); 725 split[2].s_lno = e->s_lno + (same - e->s_lno); 726 split[2].num_lines = e->s_lno + e->num_lines - same; 727 chunk_end_lno = split[2].lno; 728} 729else 730 chunk_end_lno = e->lno + e->num_lines; 731 split[1].num_lines = chunk_end_lno - split[1].lno; 732 733/* 734 * if it turns out there is nothing to blame the parent for, 735 * forget about the splitting. !split[1].suspect signals this. 736 */ 737if(split[1].num_lines <1) 738return; 739 split[1].suspect =blame_origin_incref(parent); 740} 741 742/* 743 * split_overlap() divided an existing blame e into up to three parts 744 * in split. Any assigned blame is moved to queue to 745 * reflect the split. 746 */ 747static voidsplit_blame(struct blame_entry ***blamed, 748struct blame_entry ***unblamed, 749struct blame_entry *split, 750struct blame_entry *e) 751{ 752if(split[0].suspect && split[2].suspect) { 753/* The first part (reuse storage for the existing entry e) */ 754dup_entry(unblamed, e, &split[0]); 755 756/* The last part -- me */ 757add_blame_entry(unblamed, &split[2]); 758 759/* ... and the middle part -- parent */ 760add_blame_entry(blamed, &split[1]); 761} 762else if(!split[0].suspect && !split[2].suspect) 763/* 764 * The parent covers the entire area; reuse storage for 765 * e and replace it with the parent. 766 */ 767dup_entry(blamed, e, &split[1]); 768else if(split[0].suspect) { 769/* me and then parent */ 770dup_entry(unblamed, e, &split[0]); 771add_blame_entry(blamed, &split[1]); 772} 773else{ 774/* parent and then me */ 775dup_entry(blamed, e, &split[1]); 776add_blame_entry(unblamed, &split[2]); 777} 778} 779 780/* 781 * After splitting the blame, the origins used by the 782 * on-stack blame_entry should lose one refcnt each. 783 */ 784static voiddecref_split(struct blame_entry *split) 785{ 786int i; 787 788for(i =0; i <3; i++) 789blame_origin_decref(split[i].suspect); 790} 791 792/* 793 * reverse_blame reverses the list given in head, appending tail. 794 * That allows us to build lists in reverse order, then reverse them 795 * afterwards. This can be faster than building the list in proper 796 * order right away. The reason is that building in proper order 797 * requires writing a link in the _previous_ element, while building 798 * in reverse order just requires placing the list head into the 799 * _current_ element. 800 */ 801 802static struct blame_entry *reverse_blame(struct blame_entry *head, 803struct blame_entry *tail) 804{ 805while(head) { 806struct blame_entry *next = head->next; 807 head->next = tail; 808 tail = head; 809 head = next; 810} 811return tail; 812} 813 814/* 815 * Process one hunk from the patch between the current suspect for 816 * blame_entry e and its parent. This first blames any unfinished 817 * entries before the chunk (which is where target and parent start 818 * differing) on the parent, and then splits blame entries at the 819 * start and at the end of the difference region. Since use of -M and 820 * -C options may lead to overlapping/duplicate source line number 821 * ranges, all we can rely on from sorting/merging is the order of the 822 * first suspect line number. 823 */ 824static voidblame_chunk(struct blame_entry ***dstq,struct blame_entry ***srcq, 825int tlno,int offset,int same, 826struct blame_origin *parent) 827{ 828struct blame_entry *e = **srcq; 829struct blame_entry *samep = NULL, *diffp = NULL; 830 831while(e && e->s_lno < tlno) { 832struct blame_entry *next = e->next; 833/* 834 * current record starts before differing portion. If 835 * it reaches into it, we need to split it up and 836 * examine the second part separately. 837 */ 838if(e->s_lno + e->num_lines > tlno) { 839/* Move second half to a new record */ 840int len = tlno - e->s_lno; 841struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 842 n->suspect = e->suspect; 843 n->lno = e->lno + len; 844 n->s_lno = e->s_lno + len; 845 n->num_lines = e->num_lines - len; 846 e->num_lines = len; 847 e->score =0; 848/* Push new record to diffp */ 849 n->next = diffp; 850 diffp = n; 851}else 852blame_origin_decref(e->suspect); 853/* Pass blame for everything before the differing 854 * chunk to the parent */ 855 e->suspect =blame_origin_incref(parent); 856 e->s_lno += offset; 857 e->next = samep; 858 samep = e; 859 e = next; 860} 861/* 862 * As we don't know how much of a common stretch after this 863 * diff will occur, the currently blamed parts are all that we 864 * can assign to the parent for now. 865 */ 866 867if(samep) { 868**dstq =reverse_blame(samep, **dstq); 869*dstq = &samep->next; 870} 871/* 872 * Prepend the split off portions: everything after e starts 873 * after the blameable portion. 874 */ 875 e =reverse_blame(diffp, e); 876 877/* 878 * Now retain records on the target while parts are different 879 * from the parent. 880 */ 881 samep = NULL; 882 diffp = NULL; 883while(e && e->s_lno < same) { 884struct blame_entry *next = e->next; 885 886/* 887 * If current record extends into sameness, need to split. 888 */ 889if(e->s_lno + e->num_lines > same) { 890/* 891 * Move second half to a new record to be 892 * processed by later chunks 893 */ 894int len = same - e->s_lno; 895struct blame_entry *n =xcalloc(1,sizeof(struct blame_entry)); 896 n->suspect =blame_origin_incref(e->suspect); 897 n->lno = e->lno + len; 898 n->s_lno = e->s_lno + len; 899 n->num_lines = e->num_lines - len; 900 e->num_lines = len; 901 e->score =0; 902/* Push new record to samep */ 903 n->next = samep; 904 samep = n; 905} 906 e->next = diffp; 907 diffp = e; 908 e = next; 909} 910**srcq =reverse_blame(diffp,reverse_blame(samep, e)); 911/* Move across elements that are in the unblamable portion */ 912if(diffp) 913*srcq = &diffp->next; 914} 915 916struct blame_chunk_cb_data { 917struct blame_origin *parent; 918long offset; 919struct blame_entry **dstq; 920struct blame_entry **srcq; 921}; 922 923/* diff chunks are from parent to target */ 924static intblame_chunk_cb(long start_a,long count_a, 925long start_b,long count_b,void*data) 926{ 927struct blame_chunk_cb_data *d = data; 928if(start_a - start_b != d->offset) 929die("internal error in blame::blame_chunk_cb"); 930blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, 931 start_b + count_b, d->parent); 932 d->offset = start_a + count_a - (start_b + count_b); 933return0; 934} 935 936/* 937 * We are looking at the origin 'target' and aiming to pass blame 938 * for the lines it is suspected to its parent. Run diff to find 939 * which lines came from parent and pass blame for them. 940 */ 941static voidpass_blame_to_parent(struct blame_scoreboard *sb, 942struct blame_origin *target, 943struct blame_origin *parent) 944{ 945 mmfile_t file_p, file_o; 946struct blame_chunk_cb_data d; 947struct blame_entry *newdest = NULL; 948 949if(!target->suspects) 950return;/* nothing remains for this target */ 951 952 d.parent = parent; 953 d.offset =0; 954 d.dstq = &newdest; d.srcq = &target->suspects; 955 956fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); 957fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); 958 sb->num_get_patch++; 959 960if(diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) 961die("unable to generate diff (%s->%s)", 962oid_to_hex(&parent->commit->object.oid), 963oid_to_hex(&target->commit->object.oid)); 964/* The rest are the same as the parent */ 965blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); 966*d.dstq = NULL; 967queue_blames(sb, parent, newdest); 968 969return; 970} 971 972/* 973 * The lines in blame_entry after splitting blames many times can become 974 * very small and trivial, and at some point it becomes pointless to 975 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any 976 * ordinary C program, and it is not worth to say it was copied from 977 * totally unrelated file in the parent. 978 * 979 * Compute how trivial the lines in the blame_entry are. 980 */ 981static unsignedblame_entry_score(struct blame_scoreboard *sb,struct blame_entry *e) 982{ 983unsigned score; 984const char*cp, *ep; 985 986if(e->score) 987return e->score; 988 989 score =1; 990 cp =blame_nth_line(sb, e->lno); 991 ep =blame_nth_line(sb, e->lno + e->num_lines); 992while(cp < ep) { 993unsigned ch = *((unsigned char*)cp); 994if(isalnum(ch)) 995 score++; 996 cp++; 997} 998 e->score = score; 999return score;1000}10011002/*1003 * best_so_far[] and this[] are both a split of an existing blame_entry1004 * that passes blame to the parent. Maintain best_so_far the best split1005 * so far, by comparing this and best_so_far and copying this into1006 * bst_so_far as needed.1007 */1008static voidcopy_split_if_better(struct blame_scoreboard *sb,1009struct blame_entry *best_so_far,1010struct blame_entry *this)1011{1012int i;10131014if(!this[1].suspect)1015return;1016if(best_so_far[1].suspect) {1017if(blame_entry_score(sb, &this[1]) <blame_entry_score(sb, &best_so_far[1]))1018return;1019}10201021for(i =0; i <3; i++)1022blame_origin_incref(this[i].suspect);1023decref_split(best_so_far);1024memcpy(best_so_far,this,sizeof(struct blame_entry [3]));1025}10261027/*1028 * We are looking at a part of the final image represented by1029 * ent (tlno and same are offset by ent->s_lno).1030 * tlno is where we are looking at in the final image.1031 * up to (but not including) same match preimage.1032 * plno is where we are looking at in the preimage.1033 *1034 * <-------------- final image ---------------------->1035 * <------ent------>1036 * ^tlno ^same1037 * <---------preimage----->1038 * ^plno1039 *1040 * All line numbers are 0-based.1041 */1042static voidhandle_split(struct blame_scoreboard *sb,1043struct blame_entry *ent,1044int tlno,int plno,int same,1045struct blame_origin *parent,1046struct blame_entry *split)1047{1048if(ent->num_lines <= tlno)1049return;1050if(tlno < same) {1051struct blame_entry this[3];1052 tlno += ent->s_lno;1053 same += ent->s_lno;1054split_overlap(this, ent, tlno, plno, same, parent);1055copy_split_if_better(sb, split,this);1056decref_split(this);1057}1058}10591060struct handle_split_cb_data {1061struct blame_scoreboard *sb;1062struct blame_entry *ent;1063struct blame_origin *parent;1064struct blame_entry *split;1065long plno;1066long tlno;1067};10681069static inthandle_split_cb(long start_a,long count_a,1070long start_b,long count_b,void*data)1071{1072struct handle_split_cb_data *d = data;1073handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,1074 d->split);1075 d->plno = start_a + count_a;1076 d->tlno = start_b + count_b;1077return0;1078}10791080/*1081 * Find the lines from parent that are the same as ent so that1082 * we can pass blames to it. file_p has the blob contents for1083 * the parent.1084 */1085static voidfind_copy_in_blob(struct blame_scoreboard *sb,1086struct blame_entry *ent,1087struct blame_origin *parent,1088struct blame_entry *split,1089 mmfile_t *file_p)1090{1091const char*cp;1092 mmfile_t file_o;1093struct handle_split_cb_data d;10941095memset(&d,0,sizeof(d));1096 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;1097/*1098 * Prepare mmfile that contains only the lines in ent.1099 */1100 cp =blame_nth_line(sb, ent->lno);1101 file_o.ptr = (char*) cp;1102 file_o.size =blame_nth_line(sb, ent->lno + ent->num_lines) - cp;11031104/*1105 * file_o is a part of final image we are annotating.1106 * file_p partially may match that image.1107 */1108memset(split,0,sizeof(struct blame_entry [3]));1109if(diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))1110die("unable to generate diff (%s)",1111oid_to_hex(&parent->commit->object.oid));1112/* remainder, if any, all match the preimage */1113handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);1114}11151116/* Move all blame entries from list *source that have a score smaller1117 * than score_min to the front of list *small.1118 * Returns a pointer to the link pointing to the old head of the small list.1119 */11201121static struct blame_entry **filter_small(struct blame_scoreboard *sb,1122struct blame_entry **small,1123struct blame_entry **source,1124unsigned score_min)1125{1126struct blame_entry *p = *source;1127struct blame_entry *oldsmall = *small;1128while(p) {1129if(blame_entry_score(sb, p) <= score_min) {1130*small = p;1131 small = &p->next;1132 p = *small;1133}else{1134*source = p;1135 source = &p->next;1136 p = *source;1137}1138}1139*small = oldsmall;1140*source = NULL;1141return small;1142}11431144/*1145 * See if lines currently target is suspected for can be attributed to1146 * parent.1147 */1148static voidfind_move_in_parent(struct blame_scoreboard *sb,1149struct blame_entry ***blamed,1150struct blame_entry **toosmall,1151struct blame_origin *target,1152struct blame_origin *parent)1153{1154struct blame_entry *e, split[3];1155struct blame_entry *unblamed = target->suspects;1156struct blame_entry *leftover = NULL;1157 mmfile_t file_p;11581159if(!unblamed)1160return;/* nothing remains for this target */11611162fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);1163if(!file_p.ptr)1164return;11651166/* At each iteration, unblamed has a NULL-terminated list of1167 * entries that have not yet been tested for blame. leftover1168 * contains the reversed list of entries that have been tested1169 * without being assignable to the parent.1170 */1171do{1172struct blame_entry **unblamedtail = &unblamed;1173struct blame_entry *next;1174for(e = unblamed; e; e = next) {1175 next = e->next;1176find_copy_in_blob(sb, e, parent, split, &file_p);1177if(split[1].suspect &&1178 sb->move_score <blame_entry_score(sb, &split[1])) {1179split_blame(blamed, &unblamedtail, split, e);1180}else{1181 e->next = leftover;1182 leftover = e;1183}1184decref_split(split);1185}1186*unblamedtail = NULL;1187 toosmall =filter_small(sb, toosmall, &unblamed, sb->move_score);1188}while(unblamed);1189 target->suspects =reverse_blame(leftover, NULL);1190}11911192struct blame_list {1193struct blame_entry *ent;1194struct blame_entry split[3];1195};11961197/*1198 * Count the number of entries the target is suspected for,1199 * and prepare a list of entry and the best split.1200 */1201static struct blame_list *setup_blame_list(struct blame_entry *unblamed,1202int*num_ents_p)1203{1204struct blame_entry *e;1205int num_ents, i;1206struct blame_list *blame_list = NULL;12071208for(e = unblamed, num_ents =0; e; e = e->next)1209 num_ents++;1210if(num_ents) {1211 blame_list =xcalloc(num_ents,sizeof(struct blame_list));1212for(e = unblamed, i =0; e; e = e->next)1213 blame_list[i++].ent = e;1214}1215*num_ents_p = num_ents;1216return blame_list;1217}12181219/*1220 * For lines target is suspected for, see if we can find code movement1221 * across file boundary from the parent commit. porigin is the path1222 * in the parent we already tried.1223 */1224static voidfind_copy_in_parent(struct blame_scoreboard *sb,1225struct blame_entry ***blamed,1226struct blame_entry **toosmall,1227struct blame_origin *target,1228struct commit *parent,1229struct blame_origin *porigin,1230int opt)1231{1232struct diff_options diff_opts;1233int i, j;1234struct blame_list *blame_list;1235int num_ents;1236struct blame_entry *unblamed = target->suspects;1237struct blame_entry *leftover = NULL;12381239if(!unblamed)1240return;/* nothing remains for this target */12411242diff_setup(&diff_opts);1243DIFF_OPT_SET(&diff_opts, RECURSIVE);1244 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;12451246diff_setup_done(&diff_opts);12471248/* Try "find copies harder" on new path if requested;1249 * we do not want to use diffcore_rename() actually to1250 * match things up; find_copies_harder is set only to1251 * force diff_tree_sha1() to feed all filepairs to diff_queue,1252 * and this code needs to be after diff_setup_done(), which1253 * usually makes find-copies-harder imply copy detection.1254 */1255if((opt & PICKAXE_BLAME_COPY_HARDEST)1256|| ((opt & PICKAXE_BLAME_COPY_HARDER)1257&& (!porigin ||strcmp(target->path, porigin->path))))1258DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);12591260if(is_null_oid(&target->commit->object.oid))1261do_diff_cache(parent->tree->object.oid.hash, &diff_opts);1262else1263diff_tree_sha1(parent->tree->object.oid.hash,1264 target->commit->tree->object.oid.hash,1265"", &diff_opts);12661267if(!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))1268diffcore_std(&diff_opts);12691270do{1271struct blame_entry **unblamedtail = &unblamed;1272 blame_list =setup_blame_list(unblamed, &num_ents);12731274for(i =0; i < diff_queued_diff.nr; i++) {1275struct diff_filepair *p = diff_queued_diff.queue[i];1276struct blame_origin *norigin;1277 mmfile_t file_p;1278struct blame_entry this[3];12791280if(!DIFF_FILE_VALID(p->one))1281continue;/* does not exist in parent */1282if(S_ISGITLINK(p->one->mode))1283continue;/* ignore git links */1284if(porigin && !strcmp(p->one->path, porigin->path))1285/* find_move already dealt with this path */1286continue;12871288 norigin =get_origin(parent, p->one->path);1289oidcpy(&norigin->blob_oid, &p->one->oid);1290 norigin->mode = p->one->mode;1291fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);1292if(!file_p.ptr)1293continue;12941295for(j =0; j < num_ents; j++) {1296find_copy_in_blob(sb, blame_list[j].ent,1297 norigin,this, &file_p);1298copy_split_if_better(sb, blame_list[j].split,1299this);1300decref_split(this);1301}1302blame_origin_decref(norigin);1303}13041305for(j =0; j < num_ents; j++) {1306struct blame_entry *split = blame_list[j].split;1307if(split[1].suspect &&1308 sb->copy_score <blame_entry_score(sb, &split[1])) {1309split_blame(blamed, &unblamedtail, split,1310 blame_list[j].ent);1311}else{1312 blame_list[j].ent->next = leftover;1313 leftover = blame_list[j].ent;1314}1315decref_split(split);1316}1317free(blame_list);1318*unblamedtail = NULL;1319 toosmall =filter_small(sb, toosmall, &unblamed, sb->copy_score);1320}while(unblamed);1321 target->suspects =reverse_blame(leftover, NULL);1322diff_flush(&diff_opts);1323clear_pathspec(&diff_opts.pathspec);1324}13251326/*1327 * The blobs of origin and porigin exactly match, so everything1328 * origin is suspected for can be blamed on the parent.1329 */1330static voidpass_whole_blame(struct blame_scoreboard *sb,1331struct blame_origin *origin,struct blame_origin *porigin)1332{1333struct blame_entry *e, *suspects;13341335if(!porigin->file.ptr && origin->file.ptr) {1336/* Steal its file */1337 porigin->file = origin->file;1338 origin->file.ptr = NULL;1339}1340 suspects = origin->suspects;1341 origin->suspects = NULL;1342for(e = suspects; e; e = e->next) {1343blame_origin_incref(porigin);1344blame_origin_decref(e->suspect);1345 e->suspect = porigin;1346}1347queue_blames(sb, porigin, suspects);1348}13491350/*1351 * We pass blame from the current commit to its parents. We keep saying1352 * "parent" (and "porigin"), but what we mean is to find scapegoat to1353 * exonerate ourselves.1354 */1355static struct commit_list *first_scapegoat(struct rev_info *revs,struct commit *commit,1356int reverse)1357{1358if(!reverse) {1359if(revs->first_parent_only &&1360 commit->parents &&1361 commit->parents->next) {1362free_commit_list(commit->parents->next);1363 commit->parents->next = NULL;1364}1365return commit->parents;1366}1367returnlookup_decoration(&revs->children, &commit->object);1368}13691370static intnum_scapegoats(struct rev_info *revs,struct commit *commit,int reverse)1371{1372struct commit_list *l =first_scapegoat(revs, commit, reverse);1373returncommit_list_count(l);1374}13751376/* Distribute collected unsorted blames to the respected sorted lists1377 * in the various origins.1378 */1379static voiddistribute_blame(struct blame_scoreboard *sb,struct blame_entry *blamed)1380{1381 blamed =llist_mergesort(blamed, get_next_blame, set_next_blame,1382 compare_blame_suspect);1383while(blamed)1384{1385struct blame_origin *porigin = blamed->suspect;1386struct blame_entry *suspects = NULL;1387do{1388struct blame_entry *next = blamed->next;1389 blamed->next = suspects;1390 suspects = blamed;1391 blamed = next;1392}while(blamed && blamed->suspect == porigin);1393 suspects =reverse_blame(suspects, NULL);1394queue_blames(sb, porigin, suspects);1395}1396}13971398#define MAXSG 1613991400static voidpass_blame(struct blame_scoreboard *sb,struct blame_origin *origin,int opt)1401{1402struct rev_info *revs = sb->revs;1403int i, pass, num_sg;1404struct commit *commit = origin->commit;1405struct commit_list *sg;1406struct blame_origin *sg_buf[MAXSG];1407struct blame_origin *porigin, **sg_origin = sg_buf;1408struct blame_entry *toosmall = NULL;1409struct blame_entry *blames, **blametail = &blames;14101411 num_sg =num_scapegoats(revs, commit, sb->reverse);1412if(!num_sg)1413goto finish;1414else if(num_sg <ARRAY_SIZE(sg_buf))1415memset(sg_buf,0,sizeof(sg_buf));1416else1417 sg_origin =xcalloc(num_sg,sizeof(*sg_origin));14181419/*1420 * The first pass looks for unrenamed path to optimize for1421 * common cases, then we look for renames in the second pass.1422 */1423for(pass =0; pass <2- sb->no_whole_file_rename; pass++) {1424struct blame_origin *(*find)(struct commit *,struct blame_origin *);1425 find = pass ? find_rename : find_origin;14261427for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1428 i < num_sg && sg;1429 sg = sg->next, i++) {1430struct commit *p = sg->item;1431int j, same;14321433if(sg_origin[i])1434continue;1435if(parse_commit(p))1436continue;1437 porigin =find(p, origin);1438if(!porigin)1439continue;1440if(!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {1441pass_whole_blame(sb, origin, porigin);1442blame_origin_decref(porigin);1443goto finish;1444}1445for(j = same =0; j < i; j++)1446if(sg_origin[j] &&1447!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {1448 same =1;1449break;1450}1451if(!same)1452 sg_origin[i] = porigin;1453else1454blame_origin_decref(porigin);1455}1456}14571458 sb->num_commits++;1459for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1460 i < num_sg && sg;1461 sg = sg->next, i++) {1462struct blame_origin *porigin = sg_origin[i];1463if(!porigin)1464continue;1465if(!origin->previous) {1466blame_origin_incref(porigin);1467 origin->previous = porigin;1468}1469pass_blame_to_parent(sb, origin, porigin);1470if(!origin->suspects)1471goto finish;1472}14731474/*1475 * Optionally find moves in parents' files.1476 */1477if(opt & PICKAXE_BLAME_MOVE) {1478filter_small(sb, &toosmall, &origin->suspects, sb->move_score);1479if(origin->suspects) {1480for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1481 i < num_sg && sg;1482 sg = sg->next, i++) {1483struct blame_origin *porigin = sg_origin[i];1484if(!porigin)1485continue;1486find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);1487if(!origin->suspects)1488break;1489}1490}1491}14921493/*1494 * Optionally find copies from parents' files.1495 */1496if(opt & PICKAXE_BLAME_COPY) {1497if(sb->copy_score > sb->move_score)1498filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1499else if(sb->copy_score < sb->move_score) {1500 origin->suspects =blame_merge(origin->suspects, toosmall);1501 toosmall = NULL;1502filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);1503}1504if(!origin->suspects)1505goto finish;15061507for(i =0, sg =first_scapegoat(revs, commit, sb->reverse);1508 i < num_sg && sg;1509 sg = sg->next, i++) {1510struct blame_origin *porigin = sg_origin[i];1511find_copy_in_parent(sb, &blametail, &toosmall,1512 origin, sg->item, porigin, opt);1513if(!origin->suspects)1514goto finish;1515}1516}15171518finish:1519*blametail = NULL;1520distribute_blame(sb, blames);1521/*1522 * prepend toosmall to origin->suspects1523 *1524 * There is no point in sorting: this ends up on a big1525 * unsorted list in the caller anyway.1526 */1527if(toosmall) {1528struct blame_entry **tail = &toosmall;1529while(*tail)1530 tail = &(*tail)->next;1531*tail = origin->suspects;1532 origin->suspects = toosmall;1533}1534for(i =0; i < num_sg; i++) {1535if(sg_origin[i]) {1536drop_origin_blob(sg_origin[i]);1537blame_origin_decref(sg_origin[i]);1538}1539}1540drop_origin_blob(origin);1541if(sg_buf != sg_origin)1542free(sg_origin);1543}15441545/*1546 * Information on commits, used for output.1547 */1548struct commit_info {1549struct strbuf author;1550struct strbuf author_mail;1551 timestamp_t author_time;1552struct strbuf author_tz;15531554/* filled only when asked for details */1555struct strbuf committer;1556struct strbuf committer_mail;1557 timestamp_t committer_time;1558struct strbuf committer_tz;15591560struct strbuf summary;1561};15621563/*1564 * Parse author/committer line in the commit object buffer1565 */1566static voidget_ac_line(const char*inbuf,const char*what,1567struct strbuf *name,struct strbuf *mail,1568 timestamp_t *time,struct strbuf *tz)1569{1570struct ident_split ident;1571size_t len, maillen, namelen;1572char*tmp, *endp;1573const char*namebuf, *mailbuf;15741575 tmp =strstr(inbuf, what);1576if(!tmp)1577goto error_out;1578 tmp +=strlen(what);1579 endp =strchr(tmp,'\n');1580if(!endp)1581 len =strlen(tmp);1582else1583 len = endp - tmp;15841585if(split_ident_line(&ident, tmp, len)) {1586 error_out:1587/* Ugh */1588 tmp ="(unknown)";1589strbuf_addstr(name, tmp);1590strbuf_addstr(mail, tmp);1591strbuf_addstr(tz, tmp);1592*time =0;1593return;1594}15951596 namelen = ident.name_end - ident.name_begin;1597 namebuf = ident.name_begin;15981599 maillen = ident.mail_end - ident.mail_begin;1600 mailbuf = ident.mail_begin;16011602if(ident.date_begin && ident.date_end)1603*time =strtoul(ident.date_begin, NULL,10);1604else1605*time =0;16061607if(ident.tz_begin && ident.tz_end)1608strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);1609else1610strbuf_addstr(tz,"(unknown)");16111612/*1613 * Now, convert both name and e-mail using mailmap1614 */1615map_user(&mailmap, &mailbuf, &maillen,1616&namebuf, &namelen);16171618strbuf_addf(mail,"<%.*s>", (int)maillen, mailbuf);1619strbuf_add(name, namebuf, namelen);1620}16211622static voidcommit_info_init(struct commit_info *ci)1623{16241625strbuf_init(&ci->author,0);1626strbuf_init(&ci->author_mail,0);1627strbuf_init(&ci->author_tz,0);1628strbuf_init(&ci->committer,0);1629strbuf_init(&ci->committer_mail,0);1630strbuf_init(&ci->committer_tz,0);1631strbuf_init(&ci->summary,0);1632}16331634static voidcommit_info_destroy(struct commit_info *ci)1635{16361637strbuf_release(&ci->author);1638strbuf_release(&ci->author_mail);1639strbuf_release(&ci->author_tz);1640strbuf_release(&ci->committer);1641strbuf_release(&ci->committer_mail);1642strbuf_release(&ci->committer_tz);1643strbuf_release(&ci->summary);1644}16451646static voidget_commit_info(struct commit *commit,1647struct commit_info *ret,1648int detailed)1649{1650int len;1651const char*subject, *encoding;1652const char*message;16531654commit_info_init(ret);16551656 encoding =get_log_output_encoding();1657 message =logmsg_reencode(commit, NULL, encoding);1658get_ac_line(message,"\nauthor ",1659&ret->author, &ret->author_mail,1660&ret->author_time, &ret->author_tz);16611662if(!detailed) {1663unuse_commit_buffer(commit, message);1664return;1665}16661667get_ac_line(message,"\ncommitter ",1668&ret->committer, &ret->committer_mail,1669&ret->committer_time, &ret->committer_tz);16701671 len =find_commit_subject(message, &subject);1672if(len)1673strbuf_add(&ret->summary, subject, len);1674else1675strbuf_addf(&ret->summary,"(%s)",oid_to_hex(&commit->object.oid));16761677unuse_commit_buffer(commit, message);1678}16791680/*1681 * Write out any suspect information which depends on the path. This must be1682 * handled separately from emit_one_suspect_detail(), because a given commit1683 * may have changes in multiple paths. So this needs to appear each time1684 * we mention a new group.1685 *1686 * To allow LF and other nonportable characters in pathnames,1687 * they are c-style quoted as needed.1688 */1689static voidwrite_filename_info(struct blame_origin *suspect)1690{1691if(suspect->previous) {1692struct blame_origin *prev = suspect->previous;1693printf("previous%s",oid_to_hex(&prev->commit->object.oid));1694write_name_quoted(prev->path, stdout,'\n');1695}1696printf("filename ");1697write_name_quoted(suspect->path, stdout,'\n');1698}16991700/*1701 * Porcelain/Incremental format wants to show a lot of details per1702 * commit. Instead of repeating this every line, emit it only once,1703 * the first time each commit appears in the output (unless the1704 * user has specifically asked for us to repeat).1705 */1706static intemit_one_suspect_detail(struct blame_origin *suspect,int repeat)1707{1708struct commit_info ci;17091710if(!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))1711return0;17121713 suspect->commit->object.flags |= METAINFO_SHOWN;1714get_commit_info(suspect->commit, &ci,1);1715printf("author%s\n", ci.author.buf);1716printf("author-mail%s\n", ci.author_mail.buf);1717printf("author-time %"PRItime"\n", ci.author_time);1718printf("author-tz%s\n", ci.author_tz.buf);1719printf("committer%s\n", ci.committer.buf);1720printf("committer-mail%s\n", ci.committer_mail.buf);1721printf("committer-time %"PRItime"\n", ci.committer_time);1722printf("committer-tz%s\n", ci.committer_tz.buf);1723printf("summary%s\n", ci.summary.buf);1724if(suspect->commit->object.flags & UNINTERESTING)1725printf("boundary\n");17261727commit_info_destroy(&ci);17281729return1;1730}17311732/*1733 * The blame_entry is found to be guilty for the range.1734 * Show it in incremental output.1735 */1736static voidfound_guilty_entry(struct blame_entry *ent,void*data)1737{1738struct progress_info *pi = (struct progress_info *)data;17391740if(incremental) {1741struct blame_origin *suspect = ent->suspect;17421743printf("%s %d %d %d\n",1744oid_to_hex(&suspect->commit->object.oid),1745 ent->s_lno +1, ent->lno +1, ent->num_lines);1746emit_one_suspect_detail(suspect,0);1747write_filename_info(suspect);1748maybe_flush_or_die(stdout,"stdout");1749}1750 pi->blamed_lines += ent->num_lines;1751display_progress(pi->progress, pi->blamed_lines);1752}17531754/*1755 * The main loop -- while we have blobs with lines whose true origin1756 * is still unknown, pick one blob, and allow its lines to pass blames1757 * to its parents. */1758static voidassign_blame(struct blame_scoreboard *sb,int opt)1759{1760struct rev_info *revs = sb->revs;1761struct commit *commit =prio_queue_get(&sb->commits);17621763while(commit) {1764struct blame_entry *ent;1765struct blame_origin *suspect = commit->util;17661767/* find one suspect to break down */1768while(suspect && !suspect->suspects)1769 suspect = suspect->next;17701771if(!suspect) {1772 commit =prio_queue_get(&sb->commits);1773continue;1774}17751776assert(commit == suspect->commit);17771778/*1779 * We will use this suspect later in the loop,1780 * so hold onto it in the meantime.1781 */1782blame_origin_incref(suspect);1783parse_commit(commit);1784if(sb->reverse ||1785(!(commit->object.flags & UNINTERESTING) &&1786!(revs->max_age != -1&& commit->date < revs->max_age)))1787pass_blame(sb, suspect, opt);1788else{1789 commit->object.flags |= UNINTERESTING;1790if(commit->object.parsed)1791mark_parents_uninteresting(commit);1792}1793/* treat root commit as boundary */1794if(!commit->parents && !sb->show_root)1795 commit->object.flags |= UNINTERESTING;17961797/* Take responsibility for the remaining entries */1798 ent = suspect->suspects;1799if(ent) {1800 suspect->guilty =1;1801for(;;) {1802struct blame_entry *next = ent->next;1803if(sb->found_guilty_entry)1804 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);1805if(next) {1806 ent = next;1807continue;1808}1809 ent->next = sb->ent;1810 sb->ent = suspect->suspects;1811 suspect->suspects = NULL;1812break;1813}1814}1815blame_origin_decref(suspect);18161817if(sb->debug)/* sanity */1818sanity_check_refcnt(sb);1819}1820}18211822static const char*format_time(timestamp_t time,const char*tz_str,1823int show_raw_time)1824{1825static struct strbuf time_buf = STRBUF_INIT;18261827strbuf_reset(&time_buf);1828if(show_raw_time) {1829strbuf_addf(&time_buf,"%"PRItime"%s", time, tz_str);1830}1831else{1832const char*time_str;1833size_t time_width;1834int tz;1835 tz =atoi(tz_str);1836 time_str =show_date(time, tz, &blame_date_mode);1837strbuf_addstr(&time_buf, time_str);1838/*1839 * Add space paddings to time_buf to display a fixed width1840 * string, and use time_width for display width calibration.1841 */1842for(time_width =utf8_strwidth(time_str);1843 time_width < blame_date_width;1844 time_width++)1845strbuf_addch(&time_buf,' ');1846}1847return time_buf.buf;1848}18491850#define OUTPUT_ANNOTATE_COMPAT 0011851#define OUTPUT_LONG_OBJECT_NAME 0021852#define OUTPUT_RAW_TIMESTAMP 0041853#define OUTPUT_PORCELAIN 0101854#define OUTPUT_SHOW_NAME 0201855#define OUTPUT_SHOW_NUMBER 0401856#define OUTPUT_SHOW_SCORE 01001857#define OUTPUT_NO_AUTHOR 02001858#define OUTPUT_SHOW_EMAIL 04001859#define OUTPUT_LINE_PORCELAIN 0100018601861static voidemit_porcelain_details(struct blame_origin *suspect,int repeat)1862{1863if(emit_one_suspect_detail(suspect, repeat) ||1864(suspect->commit->object.flags & MORE_THAN_ONE_PATH))1865write_filename_info(suspect);1866}18671868static voidemit_porcelain(struct blame_scoreboard *sb,struct blame_entry *ent,1869int opt)1870{1871int repeat = opt & OUTPUT_LINE_PORCELAIN;1872int cnt;1873const char*cp;1874struct blame_origin *suspect = ent->suspect;1875char hex[GIT_MAX_HEXSZ +1];18761877oid_to_hex_r(hex, &suspect->commit->object.oid);1878printf("%s %d %d %d\n",1879 hex,1880 ent->s_lno +1,1881 ent->lno +1,1882 ent->num_lines);1883emit_porcelain_details(suspect, repeat);18841885 cp =blame_nth_line(sb, ent->lno);1886for(cnt =0; cnt < ent->num_lines; cnt++) {1887char ch;1888if(cnt) {1889printf("%s %d %d\n", hex,1890 ent->s_lno +1+ cnt,1891 ent->lno +1+ cnt);1892if(repeat)1893emit_porcelain_details(suspect,1);1894}1895putchar('\t');1896do{1897 ch = *cp++;1898putchar(ch);1899}while(ch !='\n'&&1900 cp < sb->final_buf + sb->final_buf_size);1901}19021903if(sb->final_buf_size && cp[-1] !='\n')1904putchar('\n');1905}19061907static voidemit_other(struct blame_scoreboard *sb,struct blame_entry *ent,int opt)1908{1909int cnt;1910const char*cp;1911struct blame_origin *suspect = ent->suspect;1912struct commit_info ci;1913char hex[GIT_MAX_HEXSZ +1];1914int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);19151916get_commit_info(suspect->commit, &ci,1);1917oid_to_hex_r(hex, &suspect->commit->object.oid);19181919 cp =blame_nth_line(sb, ent->lno);1920for(cnt =0; cnt < ent->num_lines; cnt++) {1921char ch;1922int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;19231924if(suspect->commit->object.flags & UNINTERESTING) {1925if(blank_boundary)1926memset(hex,' ', length);1927else if(!(opt & OUTPUT_ANNOTATE_COMPAT)) {1928 length--;1929putchar('^');1930}1931}19321933printf("%.*s", length, hex);1934if(opt & OUTPUT_ANNOTATE_COMPAT) {1935const char*name;1936if(opt & OUTPUT_SHOW_EMAIL)1937 name = ci.author_mail.buf;1938else1939 name = ci.author.buf;1940printf("\t(%10s\t%10s\t%d)", name,1941format_time(ci.author_time, ci.author_tz.buf,1942 show_raw_time),1943 ent->lno +1+ cnt);1944}else{1945if(opt & OUTPUT_SHOW_SCORE)1946printf(" %*d%02d",1947 max_score_digits, ent->score,1948 ent->suspect->refcnt);1949if(opt & OUTPUT_SHOW_NAME)1950printf(" %-*.*s", longest_file, longest_file,1951 suspect->path);1952if(opt & OUTPUT_SHOW_NUMBER)1953printf(" %*d", max_orig_digits,1954 ent->s_lno +1+ cnt);19551956if(!(opt & OUTPUT_NO_AUTHOR)) {1957const char*name;1958int pad;1959if(opt & OUTPUT_SHOW_EMAIL)1960 name = ci.author_mail.buf;1961else1962 name = ci.author.buf;1963 pad = longest_author -utf8_strwidth(name);1964printf(" (%s%*s%10s",1965 name, pad,"",1966format_time(ci.author_time,1967 ci.author_tz.buf,1968 show_raw_time));1969}1970printf(" %*d) ",1971 max_digits, ent->lno +1+ cnt);1972}1973do{1974 ch = *cp++;1975putchar(ch);1976}while(ch !='\n'&&1977 cp < sb->final_buf + sb->final_buf_size);1978}19791980if(sb->final_buf_size && cp[-1] !='\n')1981putchar('\n');19821983commit_info_destroy(&ci);1984}19851986static voidoutput(struct blame_scoreboard *sb,int option)1987{1988struct blame_entry *ent;19891990if(option & OUTPUT_PORCELAIN) {1991for(ent = sb->ent; ent; ent = ent->next) {1992int count =0;1993struct blame_origin *suspect;1994struct commit *commit = ent->suspect->commit;1995if(commit->object.flags & MORE_THAN_ONE_PATH)1996continue;1997for(suspect = commit->util; suspect; suspect = suspect->next) {1998if(suspect->guilty && count++) {1999 commit->object.flags |= MORE_THAN_ONE_PATH;2000break;2001}2002}2003}2004}20052006for(ent = sb->ent; ent; ent = ent->next) {2007if(option & OUTPUT_PORCELAIN)2008emit_porcelain(sb, ent, option);2009else{2010emit_other(sb, ent, option);2011}2012}2013}20142015static const char*get_next_line(const char*start,const char*end)2016{2017const char*nl =memchr(start,'\n', end - start);2018return nl ? nl +1: end;2019}20202021/*2022 * To allow quick access to the contents of nth line in the2023 * final image, prepare an index in the scoreboard.2024 */2025static intprepare_lines(struct blame_scoreboard *sb)2026{2027const char*buf = sb->final_buf;2028unsigned long len = sb->final_buf_size;2029const char*end = buf + len;2030const char*p;2031int*lineno;2032int num =0;20332034for(p = buf; p < end; p =get_next_line(p, end))2035 num++;20362037ALLOC_ARRAY(sb->lineno, num +1);2038 lineno = sb->lineno;20392040for(p = buf; p < end; p =get_next_line(p, end))2041*lineno++ = p - buf;20422043*lineno = len;20442045 sb->num_lines = num;2046return sb->num_lines;2047}20482049/*2050 * Add phony grafts for use with -S; this is primarily to2051 * support git's cvsserver that wants to give a linear history2052 * to its clients.2053 */2054static intread_ancestry(const char*graft_file)2055{2056FILE*fp =fopen(graft_file,"r");2057struct strbuf buf = STRBUF_INIT;2058if(!fp)2059return-1;2060while(!strbuf_getwholeline(&buf, fp,'\n')) {2061/* The format is just "Commit Parent1 Parent2 ...\n" */2062struct commit_graft *graft =read_graft_line(buf.buf, buf.len);2063if(graft)2064register_commit_graft(graft,0);2065}2066fclose(fp);2067strbuf_release(&buf);2068return0;2069}20702071static intupdate_auto_abbrev(int auto_abbrev,struct blame_origin *suspect)2072{2073const char*uniq =find_unique_abbrev(suspect->commit->object.oid.hash,2074 auto_abbrev);2075int len =strlen(uniq);2076if(auto_abbrev < len)2077return len;2078return auto_abbrev;2079}20802081/*2082 * How many columns do we need to show line numbers, authors,2083 * and filenames?2084 */2085static voidfind_alignment(struct blame_scoreboard *sb,int*option)2086{2087int longest_src_lines =0;2088int longest_dst_lines =0;2089unsigned largest_score =0;2090struct blame_entry *e;2091int compute_auto_abbrev = (abbrev <0);2092int auto_abbrev = DEFAULT_ABBREV;20932094for(e = sb->ent; e; e = e->next) {2095struct blame_origin *suspect = e->suspect;2096int num;20972098if(compute_auto_abbrev)2099 auto_abbrev =update_auto_abbrev(auto_abbrev, suspect);2100if(strcmp(suspect->path, sb->path))2101*option |= OUTPUT_SHOW_NAME;2102 num =strlen(suspect->path);2103if(longest_file < num)2104 longest_file = num;2105if(!(suspect->commit->object.flags & METAINFO_SHOWN)) {2106struct commit_info ci;2107 suspect->commit->object.flags |= METAINFO_SHOWN;2108get_commit_info(suspect->commit, &ci,1);2109if(*option & OUTPUT_SHOW_EMAIL)2110 num =utf8_strwidth(ci.author_mail.buf);2111else2112 num =utf8_strwidth(ci.author.buf);2113if(longest_author < num)2114 longest_author = num;2115commit_info_destroy(&ci);2116}2117 num = e->s_lno + e->num_lines;2118if(longest_src_lines < num)2119 longest_src_lines = num;2120 num = e->lno + e->num_lines;2121if(longest_dst_lines < num)2122 longest_dst_lines = num;2123if(largest_score <blame_entry_score(sb, e))2124 largest_score =blame_entry_score(sb, e);2125}2126 max_orig_digits =decimal_width(longest_src_lines);2127 max_digits =decimal_width(longest_dst_lines);2128 max_score_digits =decimal_width(largest_score);21292130if(compute_auto_abbrev)2131/* one more abbrev length is needed for the boundary commit */2132 abbrev = auto_abbrev +1;2133}21342135/*2136 * For debugging -- origin is refcounted, and this asserts that2137 * we do not underflow.2138 */2139static voidsanity_check_refcnt(struct blame_scoreboard *sb)2140{2141int baa =0;2142struct blame_entry *ent;21432144for(ent = sb->ent; ent; ent = ent->next) {2145/* Nobody should have zero or negative refcnt */2146if(ent->suspect->refcnt <=0) {2147fprintf(stderr,"%sin%shas negative refcnt%d\n",2148 ent->suspect->path,2149oid_to_hex(&ent->suspect->commit->object.oid),2150 ent->suspect->refcnt);2151 baa =1;2152}2153}2154if(baa)2155 sb->on_sanity_fail(sb, baa);2156}21572158static voidsanity_check_on_fail(struct blame_scoreboard *sb,int baa)2159{2160int opt = OUTPUT_SHOW_SCORE | OUTPUT_SHOW_NUMBER | OUTPUT_SHOW_NAME;2161find_alignment(sb, &opt);2162output(sb, opt);2163die("Baa%d!", baa);2164}21652166static unsignedparse_score(const char*arg)2167{2168char*end;2169unsigned long score =strtoul(arg, &end,10);2170if(*end)2171return0;2172return score;2173}21742175static const char*add_prefix(const char*prefix,const char*path)2176{2177returnprefix_path(prefix, prefix ?strlen(prefix) :0, path);2178}21792180static intgit_blame_config(const char*var,const char*value,void*cb)2181{2182if(!strcmp(var,"blame.showroot")) {2183 show_root =git_config_bool(var, value);2184return0;2185}2186if(!strcmp(var,"blame.blankboundary")) {2187 blank_boundary =git_config_bool(var, value);2188return0;2189}2190if(!strcmp(var,"blame.showemail")) {2191int*output_option = cb;2192if(git_config_bool(var, value))2193*output_option |= OUTPUT_SHOW_EMAIL;2194else2195*output_option &= ~OUTPUT_SHOW_EMAIL;2196return0;2197}2198if(!strcmp(var,"blame.date")) {2199if(!value)2200returnconfig_error_nonbool(var);2201parse_date_format(value, &blame_date_mode);2202return0;2203}22042205if(git_diff_heuristic_config(var, value, cb) <0)2206return-1;2207if(userdiff_config(var, value) <0)2208return-1;22092210returngit_default_config(var, value, cb);2211}22122213static voidverify_working_tree_path(struct commit *work_tree,const char*path)2214{2215struct commit_list *parents;2216int pos;22172218for(parents = work_tree->parents; parents; parents = parents->next) {2219const struct object_id *commit_oid = &parents->item->object.oid;2220struct object_id blob_oid;2221unsigned mode;22222223if(!get_tree_entry(commit_oid->hash, path, blob_oid.hash, &mode) &&2224sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)2225return;2226}22272228 pos =cache_name_pos(path,strlen(path));2229if(pos >=0)2230;/* path is in the index */2231else if(-1- pos < active_nr &&2232!strcmp(active_cache[-1- pos]->name, path))2233;/* path is in the index, unmerged */2234else2235die("no such path '%s' in HEAD", path);2236}22372238static struct commit_list **append_parent(struct commit_list **tail,const struct object_id *oid)2239{2240struct commit *parent;22412242 parent =lookup_commit_reference(oid->hash);2243if(!parent)2244die("no such commit%s",oid_to_hex(oid));2245return&commit_list_insert(parent, tail)->next;2246}22472248static voidappend_merge_parents(struct commit_list **tail)2249{2250int merge_head;2251struct strbuf line = STRBUF_INIT;22522253 merge_head =open(git_path_merge_head(), O_RDONLY);2254if(merge_head <0) {2255if(errno == ENOENT)2256return;2257die("cannot open '%s' for reading",git_path_merge_head());2258}22592260while(!strbuf_getwholeline_fd(&line, merge_head,'\n')) {2261struct object_id oid;2262if(line.len < GIT_SHA1_HEXSZ ||get_oid_hex(line.buf, &oid))2263die("unknown line in '%s':%s",git_path_merge_head(), line.buf);2264 tail =append_parent(tail, &oid);2265}2266close(merge_head);2267strbuf_release(&line);2268}22692270/*2271 * This isn't as simple as passing sb->buf and sb->len, because we2272 * want to transfer ownership of the buffer to the commit (so we2273 * must use detach).2274 */2275static voidset_commit_buffer_from_strbuf(struct commit *c,struct strbuf *sb)2276{2277size_t len;2278void*buf =strbuf_detach(sb, &len);2279set_commit_buffer(c, buf, len);2280}22812282/*2283 * Prepare a dummy commit that represents the work tree (or staged) item.2284 * Note that annotating work tree item never works in the reverse.2285 */2286static struct commit *fake_working_tree_commit(struct diff_options *opt,2287const char*path,2288const char*contents_from)2289{2290struct commit *commit;2291struct blame_origin *origin;2292struct commit_list **parent_tail, *parent;2293struct object_id head_oid;2294struct strbuf buf = STRBUF_INIT;2295const char*ident;2296time_t now;2297int size, len;2298struct cache_entry *ce;2299unsigned mode;2300struct strbuf msg = STRBUF_INIT;23012302read_cache();2303time(&now);2304 commit =alloc_commit_node();2305 commit->object.parsed =1;2306 commit->date = now;2307 parent_tail = &commit->parents;23082309if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL))2310die("no such ref: HEAD");23112312 parent_tail =append_parent(parent_tail, &head_oid);2313append_merge_parents(parent_tail);2314verify_working_tree_path(commit, path);23152316 origin =make_origin(commit, path);23172318 ident =fmt_ident("Not Committed Yet","not.committed.yet", NULL,0);2319strbuf_addstr(&msg,"tree 0000000000000000000000000000000000000000\n");2320for(parent = commit->parents; parent; parent = parent->next)2321strbuf_addf(&msg,"parent%s\n",2322oid_to_hex(&parent->item->object.oid));2323strbuf_addf(&msg,2324"author%s\n"2325"committer%s\n\n"2326"Version of%sfrom%s\n",2327 ident, ident, path,2328(!contents_from ? path :2329(!strcmp(contents_from,"-") ?"standard input": contents_from)));2330set_commit_buffer_from_strbuf(commit, &msg);23312332if(!contents_from ||strcmp("-", contents_from)) {2333struct stat st;2334const char*read_from;2335char*buf_ptr;2336unsigned long buf_len;23372338if(contents_from) {2339if(stat(contents_from, &st) <0)2340die_errno("Cannot stat '%s'", contents_from);2341 read_from = contents_from;2342}2343else{2344if(lstat(path, &st) <0)2345die_errno("Cannot lstat '%s'", path);2346 read_from = path;2347}2348 mode =canon_mode(st.st_mode);23492350switch(st.st_mode & S_IFMT) {2351case S_IFREG:2352if(DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&2353textconv_object(read_from, mode, &null_oid,0, &buf_ptr, &buf_len))2354strbuf_attach(&buf, buf_ptr, buf_len, buf_len +1);2355else if(strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)2356die_errno("cannot open or read '%s'", read_from);2357break;2358case S_IFLNK:2359if(strbuf_readlink(&buf, read_from, st.st_size) <0)2360die_errno("cannot readlink '%s'", read_from);2361break;2362default:2363die("unsupported file type%s", read_from);2364}2365}2366else{2367/* Reading from stdin */2368 mode =0;2369if(strbuf_read(&buf,0,0) <0)2370die_errno("failed to read from stdin");2371}2372convert_to_git(path, buf.buf, buf.len, &buf,0);2373 origin->file.ptr = buf.buf;2374 origin->file.size = buf.len;2375pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);23762377/*2378 * Read the current index, replace the path entry with2379 * origin->blob_sha1 without mucking with its mode or type2380 * bits; we are not going to write this index out -- we just2381 * want to run "diff-index --cached".2382 */2383discard_cache();2384read_cache();23852386 len =strlen(path);2387if(!mode) {2388int pos =cache_name_pos(path, len);2389if(0<= pos)2390 mode = active_cache[pos]->ce_mode;2391else2392/* Let's not bother reading from HEAD tree */2393 mode = S_IFREG |0644;2394}2395 size =cache_entry_size(len);2396 ce =xcalloc(1, size);2397oidcpy(&ce->oid, &origin->blob_oid);2398memcpy(ce->name, path, len);2399 ce->ce_flags =create_ce_flags(0);2400 ce->ce_namelen = len;2401 ce->ce_mode =create_ce_mode(mode);2402add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);24032404cache_tree_invalidate_path(&the_index, path);24052406return commit;2407}24082409static struct commit *find_single_final(struct rev_info *revs,2410const char**name_p)2411{2412int i;2413struct commit *found = NULL;2414const char*name = NULL;24152416for(i =0; i < revs->pending.nr; i++) {2417struct object *obj = revs->pending.objects[i].item;2418if(obj->flags & UNINTERESTING)2419continue;2420 obj =deref_tag(obj, NULL,0);2421if(obj->type != OBJ_COMMIT)2422die("Non commit%s?", revs->pending.objects[i].name);2423if(found)2424die("More than one commit to dig from%sand%s?",2425 revs->pending.objects[i].name, name);2426 found = (struct commit *)obj;2427 name = revs->pending.objects[i].name;2428}2429if(name_p)2430*name_p = name;2431return found;2432}24332434static struct commit *dwim_reverse_initial(struct rev_info *revs,2435const char**name_p)2436{2437/*2438 * DWIM "git blame --reverse ONE -- PATH" as2439 * "git blame --reverse ONE..HEAD -- PATH" but only do so2440 * when it makes sense.2441 */2442struct object *obj;2443struct commit *head_commit;2444unsigned char head_sha1[20];24452446if(revs->pending.nr !=1)2447return NULL;24482449/* Is that sole rev a committish? */2450 obj = revs->pending.objects[0].item;2451 obj =deref_tag(obj, NULL,0);2452if(obj->type != OBJ_COMMIT)2453return NULL;24542455/* Do we have HEAD? */2456if(!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))2457return NULL;2458 head_commit =lookup_commit_reference_gently(head_sha1,1);2459if(!head_commit)2460return NULL;24612462/* Turn "ONE" into "ONE..HEAD" then */2463 obj->flags |= UNINTERESTING;2464add_pending_object(revs, &head_commit->object,"HEAD");24652466if(name_p)2467*name_p = revs->pending.objects[0].name;2468return(struct commit *)obj;2469}24702471static struct commit *find_single_initial(struct rev_info *revs,2472const char**name_p)2473{2474int i;2475const char*final_commit_name = NULL;2476struct commit *found = NULL;24772478/*2479 * There must be one and only one negative commit, and it must be2480 * the boundary.2481 */2482for(i =0; i < revs->pending.nr; i++) {2483struct object *obj = revs->pending.objects[i].item;2484if(!(obj->flags & UNINTERESTING))2485continue;2486 obj =deref_tag(obj, NULL,0);2487if(obj->type != OBJ_COMMIT)2488die("Non commit%s?", revs->pending.objects[i].name);2489if(found)2490die("More than one commit to dig up from,%sand%s?",2491 revs->pending.objects[i].name,2492 final_commit_name);2493 found = (struct commit *) obj;2494 final_commit_name = revs->pending.objects[i].name;2495}24962497if(!final_commit_name)2498 found =dwim_reverse_initial(revs, &final_commit_name);2499if(!final_commit_name)2500die("No commit to dig up from?");25012502if(name_p)2503*name_p = final_commit_name;2504return found;2505}25062507static intblame_copy_callback(const struct option *option,const char*arg,int unset)2508{2509int*opt = option->value;25102511/*2512 * -C enables copy from removed files;2513 * -C -C enables copy from existing files, but only2514 * when blaming a new file;2515 * -C -C -C enables copy from existing files for2516 * everybody2517 */2518if(*opt & PICKAXE_BLAME_COPY_HARDER)2519*opt |= PICKAXE_BLAME_COPY_HARDEST;2520if(*opt & PICKAXE_BLAME_COPY)2521*opt |= PICKAXE_BLAME_COPY_HARDER;2522*opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;25232524if(arg)2525 blame_copy_score =parse_score(arg);2526return0;2527}25282529static intblame_move_callback(const struct option *option,const char*arg,int unset)2530{2531int*opt = option->value;25322533*opt |= PICKAXE_BLAME_MOVE;25342535if(arg)2536 blame_move_score =parse_score(arg);2537return0;2538}25392540intcmd_blame(int argc,const char**argv,const char*prefix)2541{2542struct rev_info revs;2543const char*path;2544struct blame_scoreboard sb;2545struct blame_origin *o;2546struct blame_entry *ent = NULL;2547long dashdash_pos, lno;2548const char*final_commit_name = NULL;2549enum object_type type;2550struct commit *final_commit = NULL;2551struct progress_info pi = { NULL,0};25522553struct string_list range_list = STRING_LIST_INIT_NODUP;2554int output_option =0, opt =0;2555int show_stats =0;2556const char*revs_file = NULL;2557const char*contents_from = NULL;2558const struct option options[] = {2559OPT_BOOL(0,"incremental", &incremental,N_("Show blame entries as we find them, incrementally")),2560OPT_BOOL('b', NULL, &blank_boundary,N_("Show blank SHA-1 for boundary commits (Default: off)")),2561OPT_BOOL(0,"root", &show_root,N_("Do not treat root commits as boundaries (Default: off)")),2562OPT_BOOL(0,"show-stats", &show_stats,N_("Show work cost statistics")),2563OPT_BOOL(0,"progress", &show_progress,N_("Force progress reporting")),2564OPT_BIT(0,"score-debug", &output_option,N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),2565OPT_BIT('f',"show-name", &output_option,N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),2566OPT_BIT('n',"show-number", &output_option,N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),2567OPT_BIT('p',"porcelain", &output_option,N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),2568OPT_BIT(0,"line-porcelain", &output_option,N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),2569OPT_BIT('c', NULL, &output_option,N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),2570OPT_BIT('t', NULL, &output_option,N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),2571OPT_BIT('l', NULL, &output_option,N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),2572OPT_BIT('s', NULL, &output_option,N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),2573OPT_BIT('e',"show-email", &output_option,N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),2574OPT_BIT('w', NULL, &xdl_opts,N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),25752576/*2577 * The following two options are parsed by parse_revision_opt()2578 * and are only included here to get included in the "-h"2579 * output:2580 */2581{ OPTION_LOWLEVEL_CALLBACK,0,"indent-heuristic", NULL, NULL,N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },25822583OPT_BIT(0,"minimal", &xdl_opts,N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),2584OPT_STRING('S', NULL, &revs_file,N_("file"),N_("Use revisions from <file> instead of calling git-rev-list")),2585OPT_STRING(0,"contents", &contents_from,N_("file"),N_("Use <file>'s contents as the final image")),2586{ OPTION_CALLBACK,'C', NULL, &opt,N_("score"),N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },2587{ OPTION_CALLBACK,'M', NULL, &opt,N_("score"),N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },2588OPT_STRING_LIST('L', NULL, &range_list,N_("n,m"),N_("Process only line range n,m, counting from 1")),2589OPT__ABBREV(&abbrev),2590OPT_END()2591};25922593struct parse_opt_ctx_t ctx;2594int cmd_is_annotate = !strcmp(argv[0],"annotate");2595struct range_set ranges;2596unsigned int range_i;2597long anchor;25982599git_config(git_blame_config, &output_option);2600init_revisions(&revs, NULL);2601 revs.date_mode = blame_date_mode;2602DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);2603DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);26042605 save_commit_buffer =0;2606 dashdash_pos =0;2607 show_progress = -1;26082609parse_options_start(&ctx, argc, argv, prefix, options,2610 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);2611for(;;) {2612switch(parse_options_step(&ctx, options, blame_opt_usage)) {2613case PARSE_OPT_HELP:2614exit(129);2615case PARSE_OPT_DONE:2616if(ctx.argv[0])2617 dashdash_pos = ctx.cpidx;2618goto parse_done;2619}26202621if(!strcmp(ctx.argv[0],"--reverse")) {2622 ctx.argv[0] ="--children";2623 reverse =1;2624}2625parse_revision_opt(&revs, &ctx, options, blame_opt_usage);2626}2627parse_done:2628 no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);2629 xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;2630DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);2631 argc =parse_options_end(&ctx);26322633if(incremental || (output_option & OUTPUT_PORCELAIN)) {2634if(show_progress >0)2635die(_("--progress can't be used with --incremental or porcelain formats"));2636 show_progress =0;2637}else if(show_progress <0)2638 show_progress =isatty(2);26392640if(0< abbrev && abbrev < GIT_SHA1_HEXSZ)2641/* one more abbrev length is needed for the boundary commit */2642 abbrev++;2643else if(!abbrev)2644 abbrev = GIT_SHA1_HEXSZ;26452646if(revs_file &&read_ancestry(revs_file))2647die_errno("reading graft file '%s' failed", revs_file);26482649if(cmd_is_annotate) {2650 output_option |= OUTPUT_ANNOTATE_COMPAT;2651 blame_date_mode.type = DATE_ISO8601;2652}else{2653 blame_date_mode = revs.date_mode;2654}26552656/* The maximum width used to show the dates */2657switch(blame_date_mode.type) {2658case DATE_RFC2822:2659 blame_date_width =sizeof("Thu, 19 Oct 2006 16:00:04 -0700");2660break;2661case DATE_ISO8601_STRICT:2662 blame_date_width =sizeof("2006-10-19T16:00:04-07:00");2663break;2664case DATE_ISO8601:2665 blame_date_width =sizeof("2006-10-19 16:00:04 -0700");2666break;2667case DATE_RAW:2668 blame_date_width =sizeof("1161298804 -0700");2669break;2670case DATE_UNIX:2671 blame_date_width =sizeof("1161298804");2672break;2673case DATE_SHORT:2674 blame_date_width =sizeof("2006-10-19");2675break;2676case DATE_RELATIVE:2677/* TRANSLATORS: This string is used to tell us the maximum2678 display width for a relative timestamp in "git blame"2679 output. For C locale, "4 years, 11 months ago", which2680 takes 22 places, is the longest among various forms of2681 relative timestamps, but your language may need more or2682 fewer display columns. */2683 blame_date_width =utf8_strwidth(_("4 years, 11 months ago")) +1;/* add the null */2684break;2685case DATE_NORMAL:2686 blame_date_width =sizeof("Thu Oct 19 16:00:04 2006 -0700");2687break;2688case DATE_STRFTIME:2689 blame_date_width =strlen(show_date(0,0, &blame_date_mode)) +1;/* add the null */2690break;2691}2692 blame_date_width -=1;/* strip the null */26932694if(DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))2695 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |2696 PICKAXE_BLAME_COPY_HARDER);26972698/*2699 * We have collected options unknown to us in argv[1..unk]2700 * which are to be passed to revision machinery if we are2701 * going to do the "bottom" processing.2702 *2703 * The remaining are:2704 *2705 * (1) if dashdash_pos != 0, it is either2706 * "blame [revisions] -- <path>" or2707 * "blame -- <path> <rev>"2708 *2709 * (2) otherwise, it is one of the two:2710 * "blame [revisions] <path>"2711 * "blame <path> <rev>"2712 *2713 * Note that we must strip out <path> from the arguments: we do not2714 * want the path pruning but we may want "bottom" processing.2715 */2716if(dashdash_pos) {2717switch(argc - dashdash_pos -1) {2718case2:/* (1b) */2719if(argc !=4)2720usage_with_options(blame_opt_usage, options);2721/* reorder for the new way: <rev> -- <path> */2722 argv[1] = argv[3];2723 argv[3] = argv[2];2724 argv[2] ="--";2725/* FALLTHROUGH */2726case1:/* (1a) */2727 path =add_prefix(prefix, argv[--argc]);2728 argv[argc] = NULL;2729break;2730default:2731usage_with_options(blame_opt_usage, options);2732}2733}else{2734if(argc <2)2735usage_with_options(blame_opt_usage, options);2736 path =add_prefix(prefix, argv[argc -1]);2737if(argc ==3&& !file_exists(path)) {/* (2b) */2738 path =add_prefix(prefix, argv[1]);2739 argv[1] = argv[2];2740}2741 argv[argc -1] ="--";27422743setup_work_tree();2744if(!file_exists(path))2745die_errno("cannot stat path '%s'", path);2746}27472748 revs.disable_stdin =1;2749setup_revisions(argc, argv, &revs, NULL);2750memset(&sb,0,sizeof(sb));2751 sb.move_score = BLAME_DEFAULT_MOVE_SCORE;2752 sb.copy_score = BLAME_DEFAULT_COPY_SCORE;27532754 sb.revs = &revs;2755 sb.contents_from = contents_from;2756 sb.reverse = reverse;27572758if(!reverse) {2759 sb.final =find_single_final(&revs, &final_commit_name);2760 sb.commits.compare = compare_commits_by_commit_date;2761}2762else if(contents_from)2763die(_("--contents and --reverse do not blend well."));2764else{2765 sb.final =find_single_initial(&revs, &final_commit_name);2766 sb.commits.compare = compare_commits_by_reverse_commit_date;2767if(revs.first_parent_only)2768 revs.children.name = NULL;2769}27702771if(!sb.final) {2772/*2773 * "--not A B -- path" without anything positive;2774 * do not default to HEAD, but use the working tree2775 * or "--contents".2776 */2777setup_work_tree();2778 sb.final =fake_working_tree_commit(&sb.revs->diffopt,2779 path, contents_from);2780add_pending_object(&revs, &(sb.final->object),":");2781}2782else if(contents_from)2783die(_("cannot use --contents with final commit object name"));27842785if(reverse && revs.first_parent_only) {2786 final_commit =find_single_final(sb.revs, NULL);2787if(!final_commit)2788die(_("--reverse and --first-parent together require specified latest commit"));2789}27902791/*2792 * If we have bottom, this will mark the ancestors of the2793 * bottom commits we would reach while traversing as2794 * uninteresting.2795 */2796if(prepare_revision_walk(&revs))2797die(_("revision walk setup failed"));27982799if(reverse && revs.first_parent_only) {2800struct commit *c = final_commit;28012802 sb.revs->children.name ="children";2803while(c->parents &&2804oidcmp(&c->object.oid, &sb.final->object.oid)) {2805struct commit_list *l =xcalloc(1,sizeof(*l));28062807 l->item = c;2808if(add_decoration(&sb.revs->children,2809&c->parents->item->object, l))2810die("BUG: not unique item in first-parent chain");2811 c = c->parents->item;2812}28132814if(oidcmp(&c->object.oid, &sb.final->object.oid))2815die(_("--reverse --first-parent together require range along first-parent chain"));2816}28172818if(is_null_oid(&sb.final->object.oid)) {2819 o = sb.final->util;2820 sb.final_buf =xmemdupz(o->file.ptr, o->file.size);2821 sb.final_buf_size = o->file.size;2822}2823else{2824 o =get_origin(sb.final, path);2825if(fill_blob_sha1_and_mode(o))2826die(_("no such path%sin%s"), path, final_commit_name);28272828if(DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&2829textconv_object(path, o->mode, &o->blob_oid,1, (char**) &sb.final_buf,2830&sb.final_buf_size))2831;2832else2833 sb.final_buf =read_sha1_file(o->blob_oid.hash, &type,2834&sb.final_buf_size);28352836if(!sb.final_buf)2837die(_("cannot read blob%sfor path%s"),2838oid_to_hex(&o->blob_oid),2839 path);2840}2841 sb.num_read_blob++;2842 lno =prepare_lines(&sb);28432844if(lno && !range_list.nr)2845string_list_append(&range_list,"1");28462847 anchor =1;2848range_set_init(&ranges, range_list.nr);2849for(range_i =0; range_i < range_list.nr; ++range_i) {2850long bottom, top;2851if(parse_range_arg(range_list.items[range_i].string,2852 nth_line_cb, &sb, lno, anchor,2853&bottom, &top, sb.path))2854usage(blame_usage);2855if(lno < top || ((lno || bottom) && lno < bottom))2856die(Q_("file%shas only%lu line",2857"file%shas only%lu lines",2858 lno), path, lno);2859if(bottom <1)2860 bottom =1;2861if(top <1)2862 top = lno;2863 bottom--;2864range_set_append_unsafe(&ranges, bottom, top);2865 anchor = top +1;2866}2867sort_and_merge_range_set(&ranges);28682869for(range_i = ranges.nr; range_i >0; --range_i) {2870const struct range *r = &ranges.ranges[range_i -1];2871long bottom = r->start;2872long top = r->end;2873struct blame_entry *next = ent;2874 ent =xcalloc(1,sizeof(*ent));2875 ent->lno = bottom;2876 ent->num_lines = top - bottom;2877 ent->suspect = o;2878 ent->s_lno = bottom;2879 ent->next = next;2880blame_origin_incref(o);2881}28822883 o->suspects = ent;2884prio_queue_put(&sb.commits, o->commit);28852886blame_origin_decref(o);28872888range_set_release(&ranges);2889string_list_clear(&range_list,0);28902891 sb.ent = NULL;2892 sb.path = path;28932894if(blame_move_score)2895 sb.move_score = blame_move_score;2896if(blame_copy_score)2897 sb.copy_score = blame_copy_score;28982899 sb.debug = DEBUG;2900 sb.on_sanity_fail = &sanity_check_on_fail;29012902 sb.show_root = show_root;2903 sb.xdl_opts = xdl_opts;2904 sb.no_whole_file_rename = no_whole_file_rename;29052906read_mailmap(&mailmap, NULL);29072908 sb.found_guilty_entry = &found_guilty_entry;2909 sb.found_guilty_entry_data = π2910if(show_progress)2911 pi.progress =start_progress_delay(_("Blaming lines"),2912 sb.num_lines,50,1);29132914assign_blame(&sb, opt);29152916stop_progress(&pi.progress);29172918if(!incremental)2919setup_pager();2920else2921return0;29222923blame_sort_final(&sb);29242925blame_coalesce(&sb);29262927if(!(output_option & OUTPUT_PORCELAIN))2928find_alignment(&sb, &output_option);29292930output(&sb, output_option);2931free((void*)sb.final_buf);2932for(ent = sb.ent; ent; ) {2933struct blame_entry *e = ent->next;2934free(ent);2935 ent = e;2936}29372938if(show_stats) {2939printf("num read blob:%d\n", sb.num_read_blob);2940printf("num get patch:%d\n", sb.num_get_patch);2941printf("num commits:%d\n", sb.num_commits);2942}2943return0;2944}