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