Skip to content

Commit 809ea28

Browse files
pks-tgitster
authored andcommitted
commit-graph: split out function to search commit position
The function `find_commit_in_graph()` assumes that the caller has passed an object which was already determined to be a commit given that it will access the commit's graph position, which is stored in a commit slab. In a subsequent patch, we want to search for an object ID though without knowing whether it is a commit or not, which is not currently possible. Split out the logic to search the commit graph for a given object ID to prepare for this change. This commit also renames the function to `find_commit_pos_in_graph()`, which more accurately reflects what this function does. Furthermore, in order to allow for the searched object ID to be const, we need to adjust `bsearch_graph()`'s signature to accept a constant object ID as input, too. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf9c0cb commit 809ea28

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

commit-graph.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ void close_commit_graph(struct raw_object_store *o)
723723
o->commit_graph = NULL;
724724
}
725725

726-
static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
726+
static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
727727
{
728728
return bsearch_hash(oid->hash, g->chunk_oid_fanout,
729729
g->chunk_oid_lookup, g->hash_len, pos);
@@ -864,25 +864,30 @@ static int fill_commit_in_graph(struct repository *r,
864864
return 1;
865865
}
866866

867-
static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
867+
static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
868+
{
869+
struct commit_graph *cur_g = g;
870+
uint32_t lex_index;
871+
872+
while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
873+
cur_g = cur_g->base_graph;
874+
875+
if (cur_g) {
876+
*pos = lex_index + cur_g->num_commits_in_base;
877+
return 1;
878+
}
879+
880+
return 0;
881+
}
882+
883+
static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
868884
{
869885
uint32_t graph_pos = commit_graph_position(item);
870886
if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
871887
*pos = graph_pos;
872888
return 1;
873889
} else {
874-
struct commit_graph *cur_g = g;
875-
uint32_t lex_index;
876-
877-
while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
878-
cur_g = cur_g->base_graph;
879-
880-
if (cur_g) {
881-
*pos = lex_index + cur_g->num_commits_in_base;
882-
return 1;
883-
}
884-
885-
return 0;
890+
return search_commit_pos_in_graph(&item->object.oid, g, pos);
886891
}
887892
}
888893

@@ -895,7 +900,7 @@ static int parse_commit_in_graph_one(struct repository *r,
895900
if (item->object.parsed)
896901
return 1;
897902

898-
if (find_commit_in_graph(item, g, &pos))
903+
if (find_commit_pos_in_graph(item, g, &pos))
899904
return fill_commit_in_graph(r, item, g, pos);
900905

901906
return 0;
@@ -921,7 +926,7 @@ void load_commit_graph_info(struct repository *r, struct commit *item)
921926
uint32_t pos;
922927
if (!prepare_commit_graph(r))
923928
return;
924-
if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
929+
if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
925930
fill_commit_graph_info(item, r->objects->commit_graph, pos);
926931
}
927932

@@ -1091,9 +1096,9 @@ static int write_graph_chunk_data(struct hashfile *f,
10911096
edge_value += ctx->new_num_commits_in_base;
10921097
else if (ctx->new_base_graph) {
10931098
uint32_t pos;
1094-
if (find_commit_in_graph(parent->item,
1095-
ctx->new_base_graph,
1096-
&pos))
1099+
if (find_commit_pos_in_graph(parent->item,
1100+
ctx->new_base_graph,
1101+
&pos))
10971102
edge_value = pos;
10981103
}
10991104

@@ -1122,9 +1127,9 @@ static int write_graph_chunk_data(struct hashfile *f,
11221127
edge_value += ctx->new_num_commits_in_base;
11231128
else if (ctx->new_base_graph) {
11241129
uint32_t pos;
1125-
if (find_commit_in_graph(parent->item,
1126-
ctx->new_base_graph,
1127-
&pos))
1130+
if (find_commit_pos_in_graph(parent->item,
1131+
ctx->new_base_graph,
1132+
&pos))
11281133
edge_value = pos;
11291134
}
11301135

@@ -1235,9 +1240,9 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
12351240
edge_value += ctx->new_num_commits_in_base;
12361241
else if (ctx->new_base_graph) {
12371242
uint32_t pos;
1238-
if (find_commit_in_graph(parent->item,
1239-
ctx->new_base_graph,
1240-
&pos))
1243+
if (find_commit_pos_in_graph(parent->item,
1244+
ctx->new_base_graph,
1245+
&pos))
12411246
edge_value = pos;
12421247
}
12431248

0 commit comments

Comments
 (0)