Skip to content

Commit c1a3b50

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a22b234 commit c1a3b50

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

commit-graph.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,20 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
183183
exit(1);
184184
}
185185

186-
/* global storage */
187-
static struct commit_graph *commit_graph = NULL;
188-
189186
static void prepare_commit_graph_one(const char *obj_dir)
190187
{
191188
char *graph_name;
192189

193-
if (commit_graph)
190+
if (the_repository->objects->commit_graph)
194191
return;
195192

196193
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);
198196

199197
FREE_AND_NULL(graph_name);
200198
}
201199

202-
static int prepare_commit_graph_run_once = 0;
203-
204200
/*
205201
* Return 1 if commit_graph is non-NULL, and 0 otherwise.
206202
*
@@ -212,9 +208,9 @@ static int prepare_commit_graph(void)
212208
struct alternate_object_database *alt;
213209
char *obj_dir;
214210

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;
218214

219215
if (!core_commit_graph)
220216
return 0;
@@ -223,16 +219,16 @@ static int prepare_commit_graph(void)
223219
prepare_commit_graph_one(obj_dir);
224220
prepare_alt_odb(the_repository);
225221
for (alt = the_repository->objects->alt_odb_list;
226-
!commit_graph && alt;
222+
!the_repository->objects->commit_graph && alt;
227223
alt = alt->next)
228224
prepare_commit_graph_one(alt->path);
229-
return !!commit_graph;
225+
return !!the_repository->objects->commit_graph;
230226
}
231227

232228
static void close_commit_graph(void)
233229
{
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;
236232
}
237233

238234
static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
@@ -342,16 +338,16 @@ int parse_commit_in_graph(struct commit *item)
342338
{
343339
if (!prepare_commit_graph())
344340
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);
346342
}
347343

348344
void load_commit_graph_info(struct commit *item)
349345
{
350346
uint32_t pos;
351347
if (!prepare_commit_graph())
352348
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);
355351
}
356352

357353
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,
379375

380376
struct tree *get_commit_tree_in_graph(const struct commit *c)
381377
{
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);
383379
}
384380

385381
static void write_graph_chunk_fanout(struct hashfile *f,
@@ -696,15 +692,17 @@ void write_commit_graph(const char *obj_dir,
696692

697693
if (append) {
698694
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;
701697
}
702698

703699
if (oids.alloc < 1024)
704700
oids.alloc = 1024;
705701
ALLOC_ARRAY(oids.list, oids.alloc);
706702

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;
708706
for (i = 0; i < commit_graph->num_commits; i++) {
709707
const unsigned char *hash = commit_graph->chunk_oid_lookup +
710708
commit_graph->hash_len * i;

object-store.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ struct raw_object_store {
106106
*/
107107
struct oidmap *replace_map;
108108

109+
struct commit_graph *commit_graph;
110+
unsigned commit_graph_attempted : 1; /* if loading has been attempted */
111+
109112
/*
110113
* private data
111114
*

object.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "alloc.h"
1010
#include "object-store.h"
1111
#include "packfile.h"
12+
#include "commit-graph.h"
1213

1314
unsigned int get_max_object_index(void)
1415
{
@@ -507,6 +508,10 @@ void raw_object_store_clear(struct raw_object_store *o)
507508
oidmap_free(o->replace_map, 1);
508509
FREE_AND_NULL(o->replace_map);
509510

511+
free_commit_graph(o->commit_graph);
512+
o->commit_graph = NULL;
513+
o->commit_graph_attempted = 0;
514+
510515
free_alt_odbs(o);
511516
o->alt_odb_tail = NULL;
512517

0 commit comments

Comments
 (0)