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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 9, 2021 at 10:11 UTC 809ea28f809e52d3204b597637b2f5e072c140f8
1 file changed +30 -25
commit-graph.c
+30 -25
@@ -723,7 +723,7 @@ void close_commit_graph(struct raw_object_store *o)
723 o->commit_graph = NULL;
724 }
725
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)
727 {
728 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
729 g->chunk_oid_lookup, g->hash_len, pos);
@@ -864,25 +864,30 @@ static int fill_commit_in_graph(struct repository *r,
864 return 1;
865 }
866
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)
884 {
885 uint32_t graph_pos = commit_graph_position(item);
886 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
887 *pos = graph_pos;
888 return 1;
889 } 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);
891 }
892 }
893
@@ -895,7 +900,7 @@ static int parse_commit_in_graph_one(struct repository *r,
900 if (item->object.parsed)
901 return 1;
902
898 - if (find_commit_in_graph(item, g, &pos))
903 + if (find_commit_pos_in_graph(item, g, &pos))
904 return fill_commit_in_graph(r, item, g, pos);
905
906 return 0;
@@ -921,7 +926,7 @@ void load_commit_graph_info(struct repository *r, struct commit *item)
926 uint32_t pos;
927 if (!prepare_commit_graph(r))
928 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))
930 fill_commit_graph_info(item, r->objects->commit_graph, pos);
931 }
932
@@ -1091,9 +1096,9 @@ static int write_graph_chunk_data(struct hashfile *f,
1096 edge_value += ctx->new_num_commits_in_base;
1097 else if (ctx->new_base_graph) {
1098 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))
1102 edge_value = pos;
1103 }
1104
@@ -1122,9 +1127,9 @@ static int write_graph_chunk_data(struct hashfile *f,
1127 edge_value += ctx->new_num_commits_in_base;
1128 else if (ctx->new_base_graph) {
1129 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))
1133 edge_value = pos;
1134 }
1135
@@ -1235,9 +1240,9 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
1240 edge_value += ctx->new_num_commits_in_base;
1241 else if (ctx->new_base_graph) {
1242 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))
1246 edge_value = pos;
1247 }
1248