blame: move core structures to header

The origin, entry, and scoreboard structures are core to the blame interface and need to be exposed for blame functionality. Signed-off-by: Jeff Smith <whydoubt@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Smith committed May 24, 2017 at 00:15 UTC dc076ae5d9fe8c2fbf238b265c1bc1f6f089fd16
2 files changed +144 -133
blame.h new
+143
@@ -0,0 +1,143 @@
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 +
10 +/*
11 + * One blob in a commit that is being suspected
12 + */
13 +struct blame_origin {
14 + int refcnt;
15 + /* Record preceding blame record for this blob */
16 + struct blame_origin *previous;
17 + /* origins are put in a list linked via `next' hanging off the
18 + * corresponding commit's util field in order to make finding
19 + * them fast. The presence in this chain does not count
20 + * towards the origin's reference count. It is tempting to
21 + * let it count as long as the commit is pending examination,
22 + * but even under circumstances where the commit will be
23 + * present multiple times in the priority queue of unexamined
24 + * commits, processing the first instance will not leave any
25 + * work requiring the origin data for the second instance. An
26 + * interspersed commit changing that would have to be
27 + * preexisting with a different ancestry and with the same
28 + * commit date in order to wedge itself between two instances
29 + * of the same commit in the priority queue _and_ produce
30 + * blame entries relevant for it. While we don't want to let
31 + * us get tripped up by this case, it certainly does not seem
32 + * worth optimizing for.
33 + */
34 + struct blame_origin *next;
35 + struct commit *commit;
36 + /* `suspects' contains blame entries that may be attributed to
37 + * this origin's commit or to parent commits. When a commit
38 + * is being processed, all suspects will be moved, either by
39 + * assigning them to an origin in a different commit, or by
40 + * shipping them to the scoreboard's ent list because they
41 + * cannot be attributed to a different commit.
42 + */
43 + struct blame_entry *suspects;
44 + mmfile_t file;
45 + struct object_id blob_oid;
46 + unsigned mode;
47 + /* guilty gets set when shipping any suspects to the final
48 + * blame list instead of other commits
49 + */
50 + char guilty;
51 + char path[FLEX_ARRAY];
52 +};
53 +
54 +/*
55 + * Each group of lines is described by a blame_entry; it can be split
56 + * as we pass blame to the parents. They are arranged in linked lists
57 + * kept as `suspects' of some unprocessed origin, or entered (when the
58 + * blame origin has been finalized) into the scoreboard structure.
59 + * While the scoreboard structure is only sorted at the end of
60 + * processing (according to final image line number), the lists
61 + * attached to an origin are sorted by the target line number.
62 + */
63 +struct blame_entry {
64 + struct blame_entry *next;
65 +
66 + /* the first line of this group in the final image;
67 + * internally all line numbers are 0 based.
68 + */
69 + int lno;
70 +
71 + /* how many lines this group has */
72 + int num_lines;
73 +
74 + /* the commit that introduced this group into the final image */
75 + struct blame_origin *suspect;
76 +
77 + /* the line number of the first line of this group in the
78 + * suspect's file; internally all line numbers are 0 based.
79 + */
80 + int s_lno;
81 +
82 + /* how significant this entry is -- cached to avoid
83 + * scanning the lines over and over.
84 + */
85 + unsigned score;
86 +};
87 +
88 +/*
89 + * The current state of the blame assignment.
90 + */
91 +struct blame_scoreboard {
92 + /* the final commit (i.e. where we started digging from) */
93 + struct commit *final;
94 + /* Priority queue for commits with unassigned blame records */
95 + struct prio_queue commits;
96 + struct rev_info *revs;
97 + const char *path;
98 +
99 + /*
100 + * The contents in the final image.
101 + * Used by many functions to obtain contents of the nth line,
102 + * indexed with scoreboard.lineno[blame_entry.lno].
103 + */
104 + const char *final_buf;
105 + unsigned long final_buf_size;
106 +
107 + /* linked list of blames */
108 + struct blame_entry *ent;
109 +
110 + /* look-up a line in the final buffer */
111 + int num_lines;
112 + int *lineno;
113 +
114 + /* stats */
115 + int num_read_blob;
116 + int num_get_patch;
117 + int num_commits;
118 +
119 + /*
120 + * blame for a blame_entry with score lower than these thresholds
121 + * is not passed to the parent using move/copy logic.
122 + */
123 + unsigned move_score;
124 + unsigned copy_score;
125 +
126 + /* use this file's contents as the final image */
127 + const char *contents_from;
128 +
129 + /* flags */
130 + int reverse;
131 + int show_root;
132 + int xdl_opts;
133 + int no_whole_file_rename;
134 + int debug;
135 +
136 + /* callbacks */
137 + void(*on_sanity_fail)(struct blame_scoreboard *, int);
138 + void(*found_guilty_entry)(struct blame_entry *, void *);
139 +
140 + void *found_guilty_entry_data;
141 +};
142 +
143 +#endif /* BLAME_H */
builtin/blame.c
+1 -133
@@ -28,6 +28,7 @@
28 #include "line-log.h"
29 #include "dir.h"
30 #include "progress.h"
31 +#include "blame.h"
32
33 static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
34
@@ -75,50 +76,6 @@ static unsigned blame_copy_score;
76 #define METAINFO_SHOWN (1u<<12)
77 #define MORE_THAN_ONE_PATH (1u<<13)
78
78 -/*
79 - * One blob in a commit that is being suspected
80 - */
81 -struct blame_origin {
82 - int refcnt;
83 - /* Record preceding blame record for this blob */
84 - struct 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 - */
102 - struct blame_origin *next;
103 - struct 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 - */
111 - struct blame_entry *suspects;
112 - mmfile_t file;
113 - struct object_id blob_oid;
114 - unsigned mode;
115 - /* guilty gets set when shipping any suspects to the final
116 - * blame list instead of other commits
117 - */
118 - char guilty;
119 - char path[FLEX_ARRAY];
120 -};
121 -
79 struct progress_info {
80 struct progress *progress;
81 int blamed_lines;
@@ -208,40 +165,6 @@ static void drop_origin_blob(struct blame_origin *o)
165 }
166 }
167
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 - */
220 -struct blame_entry {
221 - struct blame_entry *next;
222 -
223 - /* the first line of this group in the final image;
224 - * internally all line numbers are 0 based.
225 - */
226 - int lno;
227 -
228 - /* how many lines this group has */
229 - int num_lines;
230 -
231 - /* the commit that introduced this group into the final image */
232 - struct 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 - */
237 - int s_lno;
238 -
239 - /* how significant this entry is -- cached to avoid
240 - * scanning the lines over and over.
241 - */
242 - unsigned score;
243 -};
244 -
168 /*
169 * Any merge of blames happens on lists of blames that arrived via
170 * different parents in a single suspect. In this case, we want to
@@ -335,61 +258,6 @@ static int compare_commits_by_reverse_commit_date(const void *a,
258 return -compare_commits_by_commit_date(a, b, c);
259 }
260
338 -/*
339 - * The current state of the blame assignment.
340 - */
341 -struct blame_scoreboard {
342 - /* the final commit (i.e. where we started digging from) */
343 - struct commit *final;
344 - /* Priority queue for commits with unassigned blame records */
345 - struct prio_queue commits;
346 - struct rev_info *revs;
347 - const 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 - */
354 - const char *final_buf;
355 - unsigned long final_buf_size;
356 -
357 - /* linked list of blames */
358 - struct blame_entry *ent;
359 -
360 - /* look-up a line in the final buffer */
361 - int num_lines;
362 - int *lineno;
363 -
364 - /* stats */
365 - int num_read_blob;
366 - int num_get_patch;
367 - int 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 - */
373 - unsigned move_score;
374 - unsigned copy_score;
375 -
376 - /* use this file's contents as the final image */
377 - const char *contents_from;
378 -
379 - /* flags */
380 - int reverse;
381 - int show_root;
382 - int xdl_opts;
383 - int no_whole_file_rename;
384 - int debug;
385 -
386 - /* callbacks */
387 - void(*on_sanity_fail)(struct blame_scoreboard *, int);
388 - void(*found_guilty_entry)(struct blame_entry *, void *);
389 -
390 - void *found_guilty_entry_data;
391 -};
392 -
261 static void blame_sort_final(struct blame_scoreboard *sb)
262 {
263 sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,