blame: move origin-related methods to libgit

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 f5dd754c36f7d9cc7dd4c3f1a22e0e4a557b2021
4 files changed +78 -72
Makefile
+1
@@ -718,6 +718,7 @@ LIB_OBJS += argv-array.o
718 LIB_OBJS += attr.o
719 LIB_OBJS += base85.o
720 LIB_OBJS += bisect.o
721 +LIB_OBJS += blame.o
722 LIB_OBJS += blob.o
723 LIB_OBJS += branch.o
724 LIB_OBJS += bulk-checkin.o
blame.c new
+62
@@ -0,0 +1,62 @@
1 +#include "blame.h"
2 +
3 +void blame_origin_decref(struct blame_origin *o)
4 +{
5 + if (o && --o->refcnt <= 0) {
6 + struct blame_origin *p, *l = NULL;
7 + if (o->previous)
8 + blame_origin_decref(o->previous);
9 + free(o->file.ptr);
10 + /* Should be present exactly once in commit chain */
11 + for (p = o->commit->util; p; l = p, p = p->next) {
12 + if (p == o) {
13 + if (l)
14 + l->next = p->next;
15 + else
16 + o->commit->util = p->next;
17 + free(o);
18 + return;
19 + }
20 + }
21 + die("internal error in blame_origin_decref");
22 + }
23 +}
24 +
25 +/*
26 + * Given a commit and a path in it, create a new origin structure.
27 + * The callers that add blame to the scoreboard should use
28 + * get_origin() to obtain shared, refcounted copy instead of calling
29 + * this function directly.
30 + */
31 +struct blame_origin *make_origin(struct commit *commit, const char *path)
32 +{
33 + struct blame_origin *o;
34 + FLEX_ALLOC_STR(o, path, path);
35 + o->commit = commit;
36 + o->refcnt = 1;
37 + o->next = commit->util;
38 + commit->util = o;
39 + return o;
40 +}
41 +
42 +/*
43 + * Locate an existing origin or create a new one.
44 + * This moves the origin to front position in the commit util list.
45 + */
46 +struct blame_origin *get_origin(struct commit *commit, const char *path)
47 +{
48 + struct blame_origin *o, *l;
49 +
50 + for (o = commit->util, l = NULL; o; l = o, o = o->next) {
51 + if (!strcmp(o->path, path)) {
52 + /* bump to front */
53 + if (l) {
54 + l->next = o->next;
55 + o->next = commit->util;
56 + commit->util = o;
57 + }
58 + return blame_origin_incref(o);
59 + }
60 + }
61 + return make_origin(commit, path);
62 +}
blame.h
+15
@@ -140,4 +140,19 @@ struct blame_scoreboard {
140 void *found_guilty_entry_data;
141 };
142
143 +/*
144 + * Origin is refcounted and usually we keep the blob contents to be
145 + * reused.
146 + */
147 +static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
148 +{
149 + if (o)
150 + o->refcnt++;
151 + return o;
152 +}
153 +extern void blame_origin_decref(struct blame_origin *o);
154 +
155 +extern struct blame_origin *make_origin(struct commit *commit, const char *path);
156 +extern struct blame_origin *get_origin(struct commit *commit, const char *path);
157 +
158 #endif /* BLAME_H */
builtin/blame.c
-72
@@ -124,39 +124,6 @@ static void fill_origin_blob(struct diff_options *opt,
124 *file = o->file;
125 }
126
127 -/*
128 - * Origin is refcounted and usually we keep the blob contents to be
129 - * reused.
130 - */
131 -static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
132 -{
133 - if (o)
134 - o->refcnt++;
135 - return o;
136 -}
137 -
138 -static void blame_origin_decref(struct blame_origin *o)
139 -{
140 - if (o && --o->refcnt <= 0) {
141 - struct blame_origin *p, *l = NULL;
142 - if (o->previous)
143 - blame_origin_decref(o->previous);
144 - free(o->file.ptr);
145 - /* Should be present exactly once in commit chain */
146 - for (p = o->commit->util; p; l = p, p = p->next) {
147 - if (p == o) {
148 - if (l)
149 - l->next = p->next;
150 - else
151 - o->commit->util = p->next;
152 - free(o);
153 - return;
154 - }
155 - }
156 - die("internal error in blame_origin_decref");
157 - }
158 -}
159 -
127 static void drop_origin_blob(struct blame_origin *o)
128 {
129 if (o->file.ptr) {
@@ -315,45 +282,6 @@ static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porig
282 }
283 }
284
318 -/*
319 - * Given a commit and a path in it, create a new origin structure.
320 - * The callers that add blame to the scoreboard should use
321 - * get_origin() to obtain shared, refcounted copy instead of calling
322 - * this function directly.
323 - */
324 -static struct blame_origin *make_origin(struct commit *commit, const char *path)
325 -{
326 - struct blame_origin *o;
327 - FLEX_ALLOC_STR(o, path, path);
328 - o->commit = commit;
329 - o->refcnt = 1;
330 - o->next = commit->util;
331 - commit->util = o;
332 - return o;
333 -}
334 -
335 -/*
336 - * Locate an existing origin or create a new one.
337 - * This moves the origin to front position in the commit util list.
338 - */
339 -static struct blame_origin *get_origin(struct commit *commit, const char *path)
340 -{
341 - struct blame_origin *o, *l;
342 -
343 - for (o = commit->util, l = NULL; o; l = o, o = o->next) {
344 - if (!strcmp(o->path, path)) {
345 - /* bump to front */
346 - if (l) {
347 - l->next = o->next;
348 - o->next = commit->util;
349 - commit->util = o;
350 - }
351 - return blame_origin_incref(o);
352 - }
353 - }
354 - return make_origin(commit, path);
355 -}
356 -
285 /*
286 * Fill the blob_sha1 field of an origin if it hasn't, so that later
287 * call to fill_origin_blob() can use it to locate the data. blob_sha1