11#include "cache.h"
2+ #include "repository.h"
23#include "config.h"
34#include "submodule-config.h"
45#include "submodule.h"
1516struct submodule_cache {
1617 struct hashmap for_path ;
1718 struct hashmap for_name ;
19+ unsigned initialized :1 ;
1820};
1921
2022/*
@@ -31,9 +33,6 @@ enum lookup_type {
3133 lookup_path
3234};
3335
34- static struct submodule_cache the_submodule_cache ;
35- static int is_cache_init ;
36-
3736static int config_path_cmp (const struct submodule_entry * a ,
3837 const struct submodule_entry * b ,
3938 const void * unused )
@@ -50,10 +49,16 @@ static int config_name_cmp(const struct submodule_entry *a,
5049 hashcmp (a -> config -> gitmodules_sha1 , b -> config -> gitmodules_sha1 );
5150}
5251
53- static void cache_init (struct submodule_cache * cache )
52+ static struct submodule_cache * submodule_cache_alloc (void )
53+ {
54+ return xcalloc (1 , sizeof (struct submodule_cache ));
55+ }
56+
57+ static void submodule_cache_init (struct submodule_cache * cache )
5458{
5559 hashmap_init (& cache -> for_path , (hashmap_cmp_fn ) config_path_cmp , 0 );
5660 hashmap_init (& cache -> for_name , (hashmap_cmp_fn ) config_name_cmp , 0 );
61+ cache -> initialized = 1 ;
5762}
5863
5964static void free_one_config (struct submodule_entry * entry )
@@ -65,11 +70,14 @@ static void free_one_config(struct submodule_entry *entry)
6570 free (entry -> config );
6671}
6772
68- static void cache_free (struct submodule_cache * cache )
73+ static void submodule_cache_clear (struct submodule_cache * cache )
6974{
7075 struct hashmap_iter iter ;
7176 struct submodule_entry * entry ;
7277
78+ if (!cache -> initialized )
79+ return ;
80+
7381 /*
7482 * We iterate over the name hash here to be symmetric with the
7583 * allocation of struct submodule entries. Each is allocated by
@@ -81,6 +89,13 @@ static void cache_free(struct submodule_cache *cache)
8189
8290 hashmap_free (& cache -> for_path , 1 );
8391 hashmap_free (& cache -> for_name , 1 );
92+ cache -> initialized = 0 ;
93+ }
94+
95+ void submodule_cache_free (struct submodule_cache * cache )
96+ {
97+ submodule_cache_clear (cache );
98+ free (cache );
8499}
85100
86101static unsigned int hash_sha1_string (const unsigned char * sha1 ,
@@ -494,43 +509,62 @@ static const struct submodule *config_from(struct submodule_cache *cache,
494509 return submodule ;
495510}
496511
497- static void ensure_cache_init ( void )
512+ static void submodule_cache_check_init ( struct repository * repo )
498513{
499- if (is_cache_init )
514+ if (repo -> submodule_cache && repo -> submodule_cache -> initialized )
500515 return ;
501516
502- cache_init (& the_submodule_cache );
503- is_cache_init = 1 ;
517+ if (!repo -> submodule_cache )
518+ repo -> submodule_cache = submodule_cache_alloc ();
519+
520+ submodule_cache_init (repo -> submodule_cache );
504521}
505522
506- int parse_submodule_config_option (const char * var , const char * value )
523+ int submodule_config_option (struct repository * repo ,
524+ const char * var , const char * value )
507525{
508526 struct parse_config_parameter parameter ;
509- parameter .cache = & the_submodule_cache ;
527+
528+ submodule_cache_check_init (repo );
529+
530+ parameter .cache = repo -> submodule_cache ;
510531 parameter .treeish_name = NULL ;
511532 parameter .gitmodules_sha1 = null_sha1 ;
512533 parameter .overwrite = 1 ;
513534
514- ensure_cache_init ();
515535 return parse_config (var , value , & parameter );
516536}
517537
538+ int parse_submodule_config_option (const char * var , const char * value )
539+ {
540+ return submodule_config_option (the_repository , var , value );
541+ }
542+
518543const struct submodule * submodule_from_name (const unsigned char * treeish_name ,
519544 const char * name )
520545{
521- ensure_cache_init ( );
522- return config_from (& the_submodule_cache , treeish_name , name , lookup_name );
546+ submodule_cache_check_init ( the_repository );
547+ return config_from (the_repository -> submodule_cache , treeish_name , name , lookup_name );
523548}
524549
525550const struct submodule * submodule_from_path (const unsigned char * treeish_name ,
526551 const char * path )
527552{
528- ensure_cache_init ();
529- return config_from (& the_submodule_cache , treeish_name , path , lookup_path );
553+ submodule_cache_check_init (the_repository );
554+ return config_from (the_repository -> submodule_cache , treeish_name , path , lookup_path );
555+ }
556+
557+ const struct submodule * submodule_from_cache (struct repository * repo ,
558+ const unsigned char * treeish_name ,
559+ const char * key )
560+ {
561+ submodule_cache_check_init (repo );
562+ return config_from (repo -> submodule_cache , treeish_name ,
563+ key , lookup_path );
530564}
531565
532566void submodule_free (void )
533567{
534- cache_free ( & the_submodule_cache );
535- is_cache_init = 0 ;
568+ if ( the_repository -> submodule_cache )
569+ submodule_cache_clear ( the_repository -> submodule_cache ) ;
536570}
0 commit comments