commit-graph: refactor preparing commit graph

Two functions in the code (1) check if the repository is configured for commit graphs, (2) call prepare_commit_graph(), and (3) check if the graph exists. Move (1) and (3) into prepare_commit_graph(), reducing duplication of code. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jul 11, 2018 at 15:42 UTC 5faf357b4314fcc7976f75c7f3ba205d9eba8e77
1 file changed +17 -11
commit-graph.c
+17 -11
@@ -200,15 +200,25 @@ static void prepare_commit_graph_one(const char *obj_dir)
200 }
201
202 static int prepare_commit_graph_run_once = 0;
203 -static void prepare_commit_graph(void)
203 +
204 +/*
205 + * Return 1 if commit_graph is non-NULL, and 0 otherwise.
206 + *
207 + * On the first invocation, this function attemps to load the commit
208 + * graph if the_repository is configured to have one.
209 + */
210 +static int prepare_commit_graph(void)
211 {
212 struct alternate_object_database *alt;
213 char *obj_dir;
214
215 if (prepare_commit_graph_run_once)
209 - return;
216 + return !!commit_graph;
217 prepare_commit_graph_run_once = 1;
218
219 + if (!core_commit_graph)
220 + return 0;
221 +
222 obj_dir = get_object_directory();
223 prepare_commit_graph_one(obj_dir);
224 prepare_alt_odb(the_repository);
@@ -216,6 +226,7 @@ static void prepare_commit_graph(void)
226 !commit_graph && alt;
227 alt = alt->next)
228 prepare_commit_graph_one(alt->path);
229 + return !!commit_graph;
230 }
231
232 static void close_commit_graph(void)
@@ -337,22 +348,17 @@ static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item
348
349 int parse_commit_in_graph(struct commit *item)
350 {
340 - if (!core_commit_graph)
351 + if (!prepare_commit_graph())
352 return 0;
342 -
343 - prepare_commit_graph();
344 - if (commit_graph)
345 - return parse_commit_in_graph_one(commit_graph, item);
346 - return 0;
353 + return parse_commit_in_graph_one(commit_graph, item);
354 }
355
356 void load_commit_graph_info(struct commit *item)
357 {
358 uint32_t pos;
352 - if (!core_commit_graph)
359 + if (!prepare_commit_graph())
360 return;
354 - prepare_commit_graph();
355 - if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
361 + if (find_commit_in_graph(item, commit_graph, &pos))
362 fill_commit_graph_info(item, commit_graph, pos);
363 }
364