1#ifndef BLAME_H 2#define BLAME_H 3 4#include "cache.h" 5#include "commit.h" 6#include "xdiff-interface.h" 7#include "revision.h" 8#include "prio-queue.h" 9#include "diff.h" 10 11#define PICKAXE_BLAME_MOVE 01 12#define PICKAXE_BLAME_COPY 02 13#define PICKAXE_BLAME_COPY_HARDER 04 14#define PICKAXE_BLAME_COPY_HARDEST 010 15 16/* 17 * One blob in a commit that is being suspected 18 */ 19struct blame_origin { 20 int refcnt; 21 /* Record preceding blame record for this blob */ 22 struct blame_origin *previous; 23 /* origins are put in a list linked via `next' hanging off the 24 * corresponding commit's util field in order to make finding 25 * them fast. The presence in this chain does not count 26 * towards the origin's reference count. It is tempting to 27 * let it count as long as the commit is pending examination, 28 * but even under circumstances where the commit will be 29 * present multiple times in the priority queue of unexamined 30 * commits, processing the first instance will not leave any 31 * work requiring the origin data for the second instance. An 32 * interspersed commit changing that would have to be 33 * preexisting with a different ancestry and with the same 34 * commit date in order to wedge itself between two instances 35 * of the same commit in the priority queue _and_ produce 36 * blame entries relevant for it. While we don't want to let 37 * us get tripped up by this case, it certainly does not seem 38 * worth optimizing for. 39 */ 40 struct blame_origin *next; 41 struct commit *commit; 42 /* `suspects' contains blame entries that may be attributed to 43 * this origin's commit or to parent commits. When a commit 44 * is being processed, all suspects will be moved, either by 45 * assigning them to an origin in a different commit, or by 46 * shipping them to the scoreboard's ent list because they 47 * cannot be attributed to a different commit. 48 */ 49 struct blame_entry *suspects; 50 mmfile_t file; 51 struct object_id blob_oid; 52 unsigned mode; 53 /* guilty gets set when shipping any suspects to the final 54 * blame list instead of other commits 55 */ 56 char guilty; 57 char path[FLEX_ARRAY]; 58}; 59 60/* 61 * Each group of lines is described by a blame_entry; it can be split 62 * as we pass blame to the parents. They are arranged in linked lists 63 * kept as `suspects' of some unprocessed origin, or entered (when the 64 * blame origin has been finalized) into the scoreboard structure. 65 * While the scoreboard structure is only sorted at the end of 66 * processing (according to final image line number), the lists 67 * attached to an origin are sorted by the target line number. 68 */ 69struct blame_entry { 70 struct blame_entry *next; 71 72 /* the first line of this group in the final image; 73 * internally all line numbers are 0 based. 74 */ 75 int lno; 76 77 /* how many lines this group has */ 78 int num_lines; 79 80 /* the commit that introduced this group into the final image */ 81 struct blame_origin *suspect; 82 83 /* the line number of the first line of this group in the 84 * suspect's file; internally all line numbers are 0 based. 85 */ 86 int s_lno; 87 88 /* how significant this entry is -- cached to avoid 89 * scanning the lines over and over. 90 */ 91 unsigned score; 92}; 93 94/* 95 * The current state of the blame assignment. 96 */ 97struct blame_scoreboard { 98 /* the final commit (i.e. where we started digging from) */ 99 struct commit *final; 100 /* Priority queue for commits with unassigned blame records */ 101 struct prio_queue commits; 102 struct rev_info *revs; 103 const char *path; 104 105 /* 106 * The contents in the final image. 107 * Used by many functions to obtain contents of the nth line, 108 * indexed with scoreboard.lineno[blame_entry.lno]. 109 */ 110 const char *final_buf; 111 unsigned long final_buf_size; 112 113 /* linked list of blames */ 114 struct blame_entry *ent; 115 116 /* look-up a line in the final buffer */ 117 int num_lines; 118 int *lineno; 119 120 /* stats */ 121 int num_read_blob; 122 int num_get_patch; 123 int num_commits; 124 125 /* 126 * blame for a blame_entry with score lower than these thresholds 127 * is not passed to the parent using move/copy logic. 128 */ 129 unsigned move_score; 130 unsigned copy_score; 131 132 /* use this file's contents as the final image */ 133 const char *contents_from; 134 135 /* flags */ 136 int reverse; 137 int show_root; 138 int xdl_opts; 139 int no_whole_file_rename; 140 int debug; 141 142 /* callbacks */ 143 void(*on_sanity_fail)(struct blame_scoreboard *, int); 144 void(*found_guilty_entry)(struct blame_entry *, void *); 145 146 void *found_guilty_entry_data; 147}; 148 149/* 150 * Origin is refcounted and usually we keep the blob contents to be 151 * reused. 152 */ 153static inline struct blame_origin *blame_origin_incref(struct blame_origin *o) 154{ 155 if (o) 156 o->refcnt++; 157 return o; 158} 159extern void blame_origin_decref(struct blame_origin *o); 160 161extern struct blame_origin *get_origin(struct commit *commit, const char *path); 162 163extern struct commit *fake_working_tree_commit(struct diff_options *opt, const char *path, const char *contents_from); 164 165extern void blame_coalesce(struct blame_scoreboard *sb); 166extern void blame_sort_final(struct blame_scoreboard *sb); 167extern unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e); 168extern void assign_blame(struct blame_scoreboard *sb, int opt); 169extern const char *blame_nth_line(struct blame_scoreboard *sb, long lno); 170 171#endif /* BLAME_H */