@@ -183,53 +183,60 @@ 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-
189- static void prepare_commit_graph_one (const char * obj_dir )
186+ static void prepare_commit_graph_one (struct repository * r , const char * obj_dir )
190187{
191188 char * graph_name ;
192189
193- if (commit_graph )
190+ if (r -> 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+ r -> 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- static void prepare_commit_graph (void )
200+ /*
201+ * Return 1 if commit_graph is non-NULL, and 0 otherwise.
202+ *
203+ * On the first invocation, this function attemps to load the commit
204+ * graph if the_repository is configured to have one.
205+ */
206+ static int prepare_commit_graph (struct repository * r )
204207{
205208 struct alternate_object_database * alt ;
206209 char * obj_dir ;
210+ int config_value ;
207211
208- if (prepare_commit_graph_run_once )
209- return ;
210- prepare_commit_graph_run_once = 1 ;
212+ if (r -> objects -> commit_graph_attempted )
213+ return !! r -> objects -> commit_graph ;
214+ r -> objects -> commit_graph_attempted = 1 ;
211215
212- obj_dir = get_object_directory ();
213- prepare_commit_graph_one (obj_dir );
214- prepare_alt_odb (the_repository );
215- for (alt = the_repository -> objects -> alt_odb_list ;
216- !commit_graph && alt ;
216+ if (repo_config_get_bool (r , "core.commitgraph" , & config_value ) ||
217+ !config_value )
218+ /*
219+ * This repository is not configured to use commit graphs, so
220+ * do not load one. (But report commit_graph_attempted anyway
221+ * so that commit graph loading is not attempted again for this
222+ * repository.)
223+ */
224+ return 0 ;
225+
226+ obj_dir = r -> objects -> objectdir ;
227+ prepare_commit_graph_one (r , obj_dir );
228+ prepare_alt_odb (r );
229+ for (alt = r -> objects -> alt_odb_list ;
230+ !r -> objects -> commit_graph && alt ;
217231 alt = alt -> next )
218- prepare_commit_graph_one (alt -> path );
232+ prepare_commit_graph_one (r , alt -> path );
233+ return !!r -> objects -> commit_graph ;
219234}
220235
221236static void close_commit_graph (void )
222237{
223- if (!commit_graph )
224- return ;
225-
226- if (commit_graph -> graph_fd >= 0 ) {
227- munmap ((void * )commit_graph -> data , commit_graph -> data_len );
228- commit_graph -> data = NULL ;
229- close (commit_graph -> graph_fd );
230- }
231-
232- FREE_AND_NULL (commit_graph );
238+ free_commit_graph (the_repository -> objects -> commit_graph );
239+ the_repository -> objects -> commit_graph = NULL ;
233240}
234241
235242static int bsearch_graph (struct commit_graph * g , struct object_id * oid , uint32_t * pos )
@@ -324,8 +331,6 @@ static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item
324331{
325332 uint32_t pos ;
326333
327- if (!core_commit_graph )
328- return 0 ;
329334 if (item -> object .parsed )
330335 return 1 ;
331336
@@ -335,25 +340,20 @@ static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item
335340 return 0 ;
336341}
337342
338- int parse_commit_in_graph (struct commit * item )
343+ int parse_commit_in_graph (struct repository * r , struct commit * item )
339344{
340- if (!core_commit_graph )
345+ if (!prepare_commit_graph ( r ) )
341346 return 0 ;
342-
343- prepare_commit_graph ();
344- if (commit_graph )
345- return parse_commit_in_graph_one (commit_graph , item );
346- return 0 ;
347+ return parse_commit_in_graph_one (r -> objects -> commit_graph , item );
347348}
348349
349- void load_commit_graph_info (struct commit * item )
350+ void load_commit_graph_info (struct repository * r , struct commit * item )
350351{
351352 uint32_t pos ;
352- if (!core_commit_graph )
353+ if (!prepare_commit_graph ( r ) )
353354 return ;
354- prepare_commit_graph ();
355- if (commit_graph && find_commit_in_graph (item , commit_graph , & pos ))
356- fill_commit_graph_info (item , commit_graph , pos );
355+ if (find_commit_in_graph (item , r -> objects -> commit_graph , & pos ))
356+ fill_commit_graph_info (item , r -> objects -> commit_graph , pos );
357357}
358358
359359static struct tree * load_tree_for_commit (struct commit_graph * g , struct commit * c )
@@ -379,9 +379,9 @@ static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
379379 return load_tree_for_commit (g , (struct commit * )c );
380380}
381381
382- struct tree * get_commit_tree_in_graph (const struct commit * c )
382+ struct tree * get_commit_tree_in_graph (struct repository * r , const struct commit * c )
383383{
384- return get_commit_tree_in_graph_one (commit_graph , c );
384+ return get_commit_tree_in_graph_one (r -> objects -> commit_graph , c );
385385}
386386
387387static void write_graph_chunk_fanout (struct hashfile * f ,
@@ -697,16 +697,18 @@ void write_commit_graph(const char *obj_dir,
697697 oids .alloc = approximate_object_count () / 4 ;
698698
699699 if (append ) {
700- prepare_commit_graph_one (obj_dir );
701- if (commit_graph )
702- oids .alloc += commit_graph -> num_commits ;
700+ prepare_commit_graph_one (the_repository , obj_dir );
701+ if (the_repository -> objects -> commit_graph )
702+ oids .alloc += the_repository -> objects -> commit_graph -> num_commits ;
703703 }
704704
705705 if (oids .alloc < 1024 )
706706 oids .alloc = 1024 ;
707707 ALLOC_ARRAY (oids .list , oids .alloc );
708708
709- if (append && commit_graph ) {
709+ if (append && the_repository -> objects -> commit_graph ) {
710+ struct commit_graph * commit_graph =
711+ the_repository -> objects -> commit_graph ;
710712 for (i = 0 ; i < commit_graph -> num_commits ; i ++ ) {
711713 const unsigned char * hash = commit_graph -> chunk_oid_lookup +
712714 commit_graph -> hash_len * i ;
@@ -1027,3 +1029,15 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
10271029
10281030 return verify_commit_graph_error ;
10291031}
1032+
1033+ void free_commit_graph (struct commit_graph * g )
1034+ {
1035+ if (!g )
1036+ return ;
1037+ if (g -> graph_fd >= 0 ) {
1038+ munmap ((void * )g -> data , g -> data_len );
1039+ g -> data = NULL ;
1040+ close (g -> graph_fd );
1041+ }
1042+ free (g );
1043+ }
0 commit comments