Skip to content

Commit 1fc7338

Browse files
Barret Rhodengitster
authored andcommitted
blame: optionally track line fingerprints during fill_blame_origin()
fill_blame_origin() is a convenient place to store data that we will use throughout the lifetime of a blame_origin. Some heuristics for ignoring commits during a blame session can make use of this storage. In particular, we will calculate a fingerprint for each line of a file for blame_origins involved in an ignored commit. In this commit, we only calculate the line_starts, reusing the existing code from the scoreboard's line_starts. In an upcoming commit, we will actually compute the fingerprints. This feature will be used when we attempt to pass blame entries to parents when we "ignore" a commit. Most uses of fill_blame_origin() will not require this feature, hence the flag parameter. Multiple calls to fill_blame_origin() are idempotent, and any of them can request the creation of the fingerprints structure. Suggested-by: Michael Platings <[email protected]> Signed-off-by: Barret Rhoden <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8934ac8 commit 1fc7338

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

blame.c

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,58 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
310310
return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
311311
}
312312

313+
static const char *get_next_line(const char *start, const char *end)
314+
{
315+
const char *nl = memchr(start, '\n', end - start);
316+
317+
return nl ? nl + 1 : end;
318+
}
319+
320+
static int find_line_starts(int **line_starts, const char *buf,
321+
unsigned long len)
322+
{
323+
const char *end = buf + len;
324+
const char *p;
325+
int *lineno;
326+
int num = 0;
327+
328+
for (p = buf; p < end; p = get_next_line(p, end))
329+
num++;
330+
331+
ALLOC_ARRAY(*line_starts, num + 1);
332+
lineno = *line_starts;
333+
334+
for (p = buf; p < end; p = get_next_line(p, end))
335+
*lineno++ = p - buf;
336+
337+
*lineno = len;
338+
339+
return num;
340+
}
341+
342+
static void fill_origin_fingerprints(struct blame_origin *o, mmfile_t *file)
343+
{
344+
int *line_starts;
345+
346+
if (o->fingerprints)
347+
return;
348+
o->num_lines = find_line_starts(&line_starts, o->file.ptr,
349+
o->file.size);
350+
/* TODO: Will fill in fingerprints in a future commit */
351+
free(line_starts);
352+
}
353+
354+
static void drop_origin_fingerprints(struct blame_origin *o)
355+
{
356+
}
357+
313358
/*
314359
* Given an origin, prepare mmfile_t structure to be used by the
315360
* diff machinery
316361
*/
317362
static void fill_origin_blob(struct diff_options *opt,
318-
struct blame_origin *o, mmfile_t *file, int *num_read_blob)
363+
struct blame_origin *o, mmfile_t *file,
364+
int *num_read_blob, int fill_fingerprints)
319365
{
320366
if (!o->file.ptr) {
321367
enum object_type type;
@@ -339,11 +385,14 @@ static void fill_origin_blob(struct diff_options *opt,
339385
}
340386
else
341387
*file = o->file;
388+
if (fill_fingerprints)
389+
fill_origin_fingerprints(o, file);
342390
}
343391

344392
static void drop_origin_blob(struct blame_origin *o)
345393
{
346394
FREE_AND_NULL(o->file.ptr);
395+
drop_origin_fingerprints(o);
347396
}
348397

349398
/*
@@ -1140,8 +1189,10 @@ static void pass_blame_to_parent(struct blame_scoreboard *sb,
11401189
d.ignore_diffs = ignore_diffs;
11411190
d.dstq = &newdest; d.srcq = &target->suspects;
11421191

1143-
fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1144-
fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
1192+
fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
1193+
&sb->num_read_blob, ignore_diffs);
1194+
fill_origin_blob(&sb->revs->diffopt, target, &file_o,
1195+
&sb->num_read_blob, ignore_diffs);
11451196
sb->num_get_patch++;
11461197

11471198
if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
@@ -1352,7 +1403,8 @@ static void find_move_in_parent(struct blame_scoreboard *sb,
13521403
if (!unblamed)
13531404
return; /* nothing remains for this target */
13541405

1355-
fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1406+
fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
1407+
&sb->num_read_blob, 0);
13561408
if (!file_p.ptr)
13571409
return;
13581410

@@ -1481,7 +1533,8 @@ static void find_copy_in_parent(struct blame_scoreboard *sb,
14811533
norigin = get_origin(parent, p->one->path);
14821534
oidcpy(&norigin->blob_oid, &p->one->oid);
14831535
norigin->mode = p->one->mode;
1484-
fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1536+
fill_origin_blob(&sb->revs->diffopt, norigin, &file_p,
1537+
&sb->num_read_blob, 0);
14851538
if (!file_p.ptr)
14861539
continue;
14871540

@@ -1820,37 +1873,14 @@ void assign_blame(struct blame_scoreboard *sb, int opt)
18201873
}
18211874
}
18221875

1823-
static const char *get_next_line(const char *start, const char *end)
1824-
{
1825-
const char *nl = memchr(start, '\n', end - start);
1826-
return nl ? nl + 1 : end;
1827-
}
1828-
18291876
/*
18301877
* To allow quick access to the contents of nth line in the
18311878
* final image, prepare an index in the scoreboard.
18321879
*/
18331880
static int prepare_lines(struct blame_scoreboard *sb)
18341881
{
1835-
const char *buf = sb->final_buf;
1836-
unsigned long len = sb->final_buf_size;
1837-
const char *end = buf + len;
1838-
const char *p;
1839-
int *lineno;
1840-
int num = 0;
1841-
1842-
for (p = buf; p < end; p = get_next_line(p, end))
1843-
num++;
1844-
1845-
ALLOC_ARRAY(sb->lineno, num + 1);
1846-
lineno = sb->lineno;
1847-
1848-
for (p = buf; p < end; p = get_next_line(p, end))
1849-
*lineno++ = p - buf;
1850-
1851-
*lineno = len;
1852-
1853-
sb->num_lines = num;
1882+
sb->num_lines = find_line_starts(&sb->lineno, sb->final_buf,
1883+
sb->final_buf_size);
18541884
return sb->num_lines;
18551885
}
18561886

blame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct blame_origin {
5151
*/
5252
struct blame_entry *suspects;
5353
mmfile_t file;
54+
int num_lines;
55+
void *fingerprints;
5456
struct object_id blob_oid;
5557
unsigned mode;
5658
/* guilty gets set when shipping any suspects to the final

0 commit comments

Comments
 (0)