blob: 5b4e47d44c613e31fc4788c12ee9d8dc2a49d571 [file] [log] [blame]
Jeff Smithdc076ae2017-05-24 00:15:32 -05001#ifndef BLAME_H
2#define BLAME_H
3
Elijah Newrendf6e8742023-05-16 06:34:00 +00004#include "oidset.h"
Jeff Smithdc076ae2017-05-24 00:15:32 -05005#include "xdiff-interface.h"
Jeff Smithdc076ae2017-05-24 00:15:32 -05006#include "prio-queue.h"
7
Jeff Smithb543bb12017-05-24 00:15:35 -05008#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 Smith09002f12017-05-24 00:15:36 -050013#define BLAME_DEFAULT_MOVE_SCORE 20
14#define BLAME_DEFAULT_COPY_SCORE 40
15
René Scharfe27f182b2020-02-23 17:56:31 +010016struct fingerprint;
17
Jeff Smithdc076ae2017-05-24 00:15:32 -050018/*
19 * One blob in a commit that is being suspected
20 */
21struct 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 Rhoden1fc73382019-05-15 17:45:01 -040053 int num_lines;
René Scharfe27f182b2020-02-23 17:56:31 +010054 struct fingerprint *fingerprints;
Jeff Smithdc076ae2017-05-24 00:15:32 -050055 struct object_id blob_oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -070056 unsigned short mode;
Jeff Smithdc076ae2017-05-24 00:15:32 -050057 /* 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 */
73struct 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 Rhoden8934ac82019-05-15 17:45:00 -040096 int ignored;
97 int unblamable;
Jeff Smithdc076ae2017-05-24 00:15:32 -050098};
99
Derrick Stolee0906ac22020-04-16 20:14:04 +0000100struct blame_bloom_data;
101
Jeff Smithdc076ae2017-05-24 00:15:32 -0500102/*
103 * The current state of the blame assignment.
104 */
105struct 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 Duyecbbc0a2018-08-13 18:14:41 +0200110 struct repository *repo;
Jeff Smithdc076ae2017-05-24 00:15:32 -0500111 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 Rhodenae3f36d2019-05-15 17:44:59 -0400125 struct oidset ignore_list;
126
Jeff Smithdc076ae2017-05-24 00:15:32 -0500127 /* 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 Stolee0906ac22020-04-16 20:14:04 +0000158 struct blame_bloom_data *bloom_data;
Jeff Smithdc076ae2017-05-24 00:15:32 -0500159};
160
Jeff Smithf5dd7542017-05-24 00:15:33 -0500161/*
162 * Origin is refcounted and usually we keep the blob contents to be
163 * reused.
164 */
165static 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 Duyfde95222018-06-30 11:20:22 +0200171void blame_origin_decref(struct blame_origin *o);
Jeff Smithf5dd7542017-05-24 00:15:33 -0500172
Nguyễn Thái Ngọc Duyfde95222018-06-30 11:20:22 +0200173void blame_coalesce(struct blame_scoreboard *sb);
174void blame_sort_final(struct blame_scoreboard *sb);
175unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e);
176void assign_blame(struct blame_scoreboard *sb, int opt);
177const char *blame_nth_line(struct blame_scoreboard *sb, long lno);
Jeff Smithb543bb12017-05-24 00:15:35 -0500178
Nguyễn Thái Ngọc Duyfde95222018-06-30 11:20:22 +0200179void init_scoreboard(struct blame_scoreboard *sb);
180void setup_scoreboard(struct blame_scoreboard *sb,
Nguyễn Thái Ngọc Duyfde95222018-06-30 11:20:22 +0200181 struct blame_origin **orig);
Philippe Blain3af31e82020-11-01 17:28:47 +0000182void setup_blame_bloom_data(struct blame_scoreboard *sb);
Derrick Stolee0906ac22020-04-16 20:14:04 +0000183void cleanup_scoreboard(struct blame_scoreboard *sb);
Jeff Smith09002f12017-05-24 00:15:36 -0500184
Nguyễn Thái Ngọc Duyfde95222018-06-30 11:20:22 +0200185struct blame_entry *blame_entry_prepend(struct blame_entry *head,
186 long start, long end,
187 struct blame_origin *o);
Jeff Smithbd481de2017-05-24 00:15:37 -0500188
Denton Liu55454422019-04-29 04:28:14 -0400189struct blame_origin *get_blame_suspects(struct commit *commit);
Nguyễn Thái Ngọc Duy4e0df4e2018-05-19 07:28:19 +0200190
Jeff Smithdc076ae2017-05-24 00:15:32 -0500191#endif /* BLAME_H */