Skip to content

Commit e2838d8

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: always load commit-graph information
Most code paths load commits using lookup_commit() and then parse_commit(). In some cases, including some branch lookups, the commit is parsed using parse_object_buffer() which side-steps parse_commit() in favor of parse_commit_buffer(). With generation numbers in the commit-graph, we need to ensure that any commit that exists in the commit-graph file has its generation number loaded. Create new load_commit_graph_info() method to fill in the information for a commit that exists only in the commit-graph file. Call it from parse_commit_buffer() after loading the other commit information from the given buffer. Only fill this information when specified by the 'check_graph' parameter. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3afc679 commit e2838d8

File tree

6 files changed

+47
-20
lines changed

6 files changed

+47
-20
lines changed

commit-graph.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ static struct commit_list **insert_parent_or_die(struct commit_graph *g,
245245
return &commit_list_insert(c, pptr)->next;
246246
}
247247

248+
static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
249+
{
250+
const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
251+
item->graph_pos = pos;
252+
item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
253+
}
254+
248255
static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
249256
{
250257
uint32_t edge_value;
@@ -292,31 +299,40 @@ static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uin
292299
return 1;
293300
}
294301

302+
static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
303+
{
304+
if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
305+
*pos = item->graph_pos;
306+
return 1;
307+
} else {
308+
return bsearch_graph(g, &(item->object.oid), pos);
309+
}
310+
}
311+
295312
int parse_commit_in_graph(struct commit *item)
296313
{
314+
uint32_t pos;
315+
297316
if (!core_commit_graph)
298317
return 0;
299318
if (item->object.parsed)
300319
return 1;
301-
302320
prepare_commit_graph();
303-
if (commit_graph) {
304-
uint32_t pos;
305-
int found;
306-
if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
307-
pos = item->graph_pos;
308-
found = 1;
309-
} else {
310-
found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
311-
}
312-
313-
if (found)
314-
return fill_commit_in_graph(item, commit_graph, pos);
315-
}
316-
321+
if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
322+
return fill_commit_in_graph(item, commit_graph, pos);
317323
return 0;
318324
}
319325

326+
void load_commit_graph_info(struct commit *item)
327+
{
328+
uint32_t pos;
329+
if (!core_commit_graph)
330+
return;
331+
prepare_commit_graph();
332+
if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
333+
fill_commit_graph_info(item, commit_graph, pos);
334+
}
335+
320336
static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
321337
{
322338
struct object_id oid;

commit-graph.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ char *get_commit_graph_filename(const char *obj_dir);
1717
*/
1818
int parse_commit_in_graph(struct commit *item);
1919

20+
/*
21+
* It is possible that we loaded commit contents from the commit buffer,
22+
* but we also want to ensure the commit-graph content is correctly
23+
* checked and filled. Fill the graph_pos and generation members of
24+
* the given commit.
25+
*/
26+
void load_commit_graph_info(struct commit *item);
27+
2028
struct tree *get_commit_tree_in_graph(const struct commit *c);
2129

2230
struct commit_graph {

commit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
331331
return ret;
332332
}
333333

334-
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
334+
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
335335
{
336336
const char *tail = buffer;
337337
const char *bufptr = buffer;
@@ -386,6 +386,9 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
386386
}
387387
item->date = parse_commit_date(bufptr, tail);
388388

389+
if (check_graph)
390+
load_commit_graph_info(item);
391+
389392
return 0;
390393
}
391394

@@ -412,7 +415,7 @@ int parse_commit_gently(struct commit *item, int quiet_on_missing)
412415
return error("Object %s not a commit",
413416
oid_to_hex(&item->object.oid));
414417
}
415-
ret = parse_commit_buffer(item, buffer, size);
418+
ret = parse_commit_buffer(item, buffer, size, 0);
416419
if (save_commit_buffer && !ret) {
417420
set_commit_buffer(item, buffer, size);
418421
return 0;

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct commit *lookup_commit_reference_by_name(const char *name);
7272
*/
7373
struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name);
7474

75-
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
75+
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph);
7676
int parse_commit_gently(struct commit *item, int quiet_on_missing);
7777
static inline int parse_commit(struct commit *item)
7878
{

object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ struct object *parse_object_buffer(const struct object_id *oid, enum object_type
207207
} else if (type == OBJ_COMMIT) {
208208
struct commit *commit = lookup_commit(oid);
209209
if (commit) {
210-
if (parse_commit_buffer(commit, buffer, size))
210+
if (parse_commit_buffer(commit, buffer, size, 1))
211211
return NULL;
212212
if (!get_cached_commit_buffer(commit, NULL)) {
213213
set_commit_buffer(commit, buffer, size);

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ static void check_commit(const void *buf, size_t size)
17551755
{
17561756
struct commit c;
17571757
memset(&c, 0, sizeof(c));
1758-
if (parse_commit_buffer(&c, buf, size))
1758+
if (parse_commit_buffer(&c, buf, size, 0))
17591759
die("corrupt commit");
17601760
}
17611761

0 commit comments

Comments
 (0)