commit-graph: store graph in struct object_store

Instead of storing commit graphs in static variables, store them in struct object_store. There are no changes to the signatures of existing functions - they all still only support the_repository, and support for other instances of struct repository will be added in a subsequent commit. 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 8527750626f8a1b0fe641a5163760be054cc1d64
3 files changed +27 -21
commit-graph.c
+19 -21
@@ -183,24 +183,20 @@ cleanup_fail:
183 exit(1);
184 }
185
186 -/* global storage */
187 -static struct commit_graph *commit_graph = NULL;
188 -
186 static void prepare_commit_graph_one(const char *obj_dir)
187 {
188 char *graph_name;
189
193 - if (commit_graph)
190 + if (the_repository->objects->commit_graph)
191 return;
192
193 graph_name = get_commit_graph_filename(obj_dir);
197 - commit_graph = load_commit_graph_one(graph_name);
194 + the_repository->objects->commit_graph =
195 + load_commit_graph_one(graph_name);
196
197 FREE_AND_NULL(graph_name);
198 }
199
202 -static int prepare_commit_graph_run_once = 0;
203 -
200 /*
201 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
202 *
@@ -212,9 +208,9 @@ static int prepare_commit_graph(void)
208 struct alternate_object_database *alt;
209 char *obj_dir;
210
215 - if (prepare_commit_graph_run_once)
216 - return !!commit_graph;
217 - prepare_commit_graph_run_once = 1;
211 + if (the_repository->objects->commit_graph_attempted)
212 + return !!the_repository->objects->commit_graph;
213 + the_repository->objects->commit_graph_attempted = 1;
214
215 if (!core_commit_graph)
216 return 0;
@@ -223,16 +219,16 @@ static int prepare_commit_graph(void)
219 prepare_commit_graph_one(obj_dir);
220 prepare_alt_odb(the_repository);
221 for (alt = the_repository->objects->alt_odb_list;
226 - !commit_graph && alt;
222 + !the_repository->objects->commit_graph && alt;
223 alt = alt->next)
224 prepare_commit_graph_one(alt->path);
229 - return !!commit_graph;
225 + return !!the_repository->objects->commit_graph;
226 }
227
228 static void close_commit_graph(void)
229 {
234 - free_commit_graph(commit_graph);
235 - commit_graph = NULL;
230 + free_commit_graph(the_repository->objects->commit_graph);
231 + the_repository->objects->commit_graph = NULL;
232 }
233
234 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
@@ -342,7 +338,7 @@ int parse_commit_in_graph(struct commit *item)
338 {
339 if (!prepare_commit_graph())
340 return 0;
345 - return parse_commit_in_graph_one(commit_graph, item);
341 + return parse_commit_in_graph_one(the_repository->objects->commit_graph, item);
342 }
343
344 void load_commit_graph_info(struct commit *item)
@@ -350,8 +346,8 @@ void load_commit_graph_info(struct commit *item)
346 uint32_t pos;
347 if (!prepare_commit_graph())
348 return;
353 - if (find_commit_in_graph(item, commit_graph, &pos))
354 - fill_commit_graph_info(item, commit_graph, pos);
349 + if (find_commit_in_graph(item, the_repository->objects->commit_graph, &pos))
350 + fill_commit_graph_info(item, the_repository->objects->commit_graph, pos);
351 }
352
353 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
@@ -379,7 +375,7 @@ static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
375
376 struct tree *get_commit_tree_in_graph(const struct commit *c)
377 {
382 - return get_commit_tree_in_graph_one(commit_graph, c);
378 + return get_commit_tree_in_graph_one(the_repository->objects->commit_graph, c);
379 }
380
381 static void write_graph_chunk_fanout(struct hashfile *f,
@@ -696,15 +692,17 @@ void write_commit_graph(const char *obj_dir,
692
693 if (append) {
694 prepare_commit_graph_one(obj_dir);
699 - if (commit_graph)
700 - oids.alloc += commit_graph->num_commits;
695 + if (the_repository->objects->commit_graph)
696 + oids.alloc += the_repository->objects->commit_graph->num_commits;
697 }
698
699 if (oids.alloc < 1024)
700 oids.alloc = 1024;
701 ALLOC_ARRAY(oids.list, oids.alloc);
702
707 - if (append && commit_graph) {
703 + if (append && the_repository->objects->commit_graph) {
704 + struct commit_graph *commit_graph =
705 + the_repository->objects->commit_graph;
706 for (i = 0; i < commit_graph->num_commits; i++) {
707 const unsigned char *hash = commit_graph->chunk_oid_lookup +
708 commit_graph->hash_len * i;
object-store.h
+3
@@ -106,6 +106,9 @@ struct raw_object_store {
106 */
107 struct oidmap *replace_map;
108
109 + struct commit_graph *commit_graph;
110 + unsigned commit_graph_attempted : 1; /* if loading has been attempted */
111 +
112 /*
113 * private data
114 *
object.c
+5
@@ -9,6 +9,7 @@
9 #include "alloc.h"
10 #include "object-store.h"
11 #include "packfile.h"
12 +#include "commit-graph.h"
13
14 unsigned int get_max_object_index(void)
15 {
@@ -507,6 +508,10 @@ void raw_object_store_clear(struct raw_object_store *o)
508 oidmap_free(o->replace_map, 1);
509 FREE_AND_NULL(o->replace_map);
510
511 + free_commit_graph(o->commit_graph);
512 + o->commit_graph = NULL;
513 + o->commit_graph_attempted = 0;
514 +
515 free_alt_odbs(o);
516 o->alt_odb_tail = NULL;
517