Skip to content

Commit f5dd754

Browse files
whydoubtgitster
authored andcommitted
blame: move origin-related methods to libgit
Signed-off-by: Jeff Smith <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc076ae commit f5dd754

File tree

4 files changed

+78
-72
lines changed

4 files changed

+78
-72
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ LIB_OBJS += argv-array.o
718718
LIB_OBJS += attr.o
719719
LIB_OBJS += base85.o
720720
LIB_OBJS += bisect.o
721+
LIB_OBJS += blame.o
721722
LIB_OBJS += blob.o
722723
LIB_OBJS += branch.o
723724
LIB_OBJS += bulk-checkin.o

blame.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,19 @@ struct blame_scoreboard {
140140
void *found_guilty_entry_data;
141141
};
142142

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+
143158
#endif /* BLAME_H */

builtin/blame.c

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,6 @@ static void fill_origin_blob(struct diff_options *opt,
124124
*file = o->file;
125125
}
126126

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-
160127
static void drop_origin_blob(struct blame_origin *o)
161128
{
162129
if (o->file.ptr) {
@@ -315,45 +282,6 @@ static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porig
315282
}
316283
}
317284

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-
357285
/*
358286
* Fill the blob_sha1 field of an origin if it hasn't, so that later
359287
* call to fill_origin_blob() can use it to locate the data. blob_sha1

0 commit comments

Comments
 (0)