blame: move scoreboard-related methods to libgit
[git/git-svn.git] / blame.h
blobdf824b22674624c95883a6d58846ebb0a04823d6
1 #ifndef BLAME_H
2 #define BLAME_H
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"
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
17 * One blob in a commit that is being suspected
19 struct 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.
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.
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
56 char guilty;
57 char path[FLEX_ARRAY];
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.
69 struct blame_entry {
70 struct blame_entry *next;
72 /* the first line of this group in the final image;
73 * internally all line numbers are 0 based.
75 int lno;
77 /* how many lines this group has */
78 int num_lines;
80 /* the commit that introduced this group into the final image */
81 struct blame_origin *suspect;
83 /* the line number of the first line of this group in the
84 * suspect's file; internally all line numbers are 0 based.
86 int s_lno;
88 /* how significant this entry is -- cached to avoid
89 * scanning the lines over and over.
91 unsigned score;
95 * The current state of the blame assignment.
97 struct 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;
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].
110 const char *final_buf;
111 unsigned long final_buf_size;
113 /* linked list of blames */
114 struct blame_entry *ent;
116 /* look-up a line in the final buffer */
117 int num_lines;
118 int *lineno;
120 /* stats */
121 int num_read_blob;
122 int num_get_patch;
123 int num_commits;
126 * blame for a blame_entry with score lower than these thresholds
127 * is not passed to the parent using move/copy logic.
129 unsigned move_score;
130 unsigned copy_score;
132 /* use this file's contents as the final image */
133 const char *contents_from;
135 /* flags */
136 int reverse;
137 int show_root;
138 int xdl_opts;
139 int no_whole_file_rename;
140 int debug;
142 /* callbacks */
143 void(*on_sanity_fail)(struct blame_scoreboard *, int);
144 void(*found_guilty_entry)(struct blame_entry *, void *);
146 void *found_guilty_entry_data;
150 * Origin is refcounted and usually we keep the blob contents to be
151 * reused.
153 static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
155 if (o)
156 o->refcnt++;
157 return o;
159 extern void blame_origin_decref(struct blame_origin *o);
161 extern struct blame_origin *get_origin(struct commit *commit, const char *path);
163 extern struct commit *fake_working_tree_commit(struct diff_options *opt, const char *path, const char *contents_from);
165 extern void blame_coalesce(struct blame_scoreboard *sb);
166 extern void blame_sort_final(struct blame_scoreboard *sb);
167 extern unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e);
168 extern void assign_blame(struct blame_scoreboard *sb, int opt);
169 extern const char *blame_nth_line(struct blame_scoreboard *sb, long lno);
171 #endif /* BLAME_H */