Skip to content

Commit bf12fcd

Browse files
bmwillgitster
authored andcommitted
submodule-config: store the_submodule_cache in the_repository
Refactor how 'the_submodule_cache' is handled so that it can be stored inside of a repository object. Also migrate 'the_submodule_cache' to be stored in 'the_repository'. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 639e30b commit bf12fcd

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

repository.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "repository.h"
33
#include "config.h"
4+
#include "submodule-config.h"
45

56
/* The main repository */
67
static struct repository the_repo;
@@ -164,6 +165,11 @@ void repo_clear(struct repository *repo)
164165
repo->config = NULL;
165166
}
166167

168+
if (repo->submodule_cache) {
169+
submodule_cache_free(repo->submodule_cache);
170+
repo->submodule_cache = NULL;
171+
}
172+
167173
if (repo->index) {
168174
discard_index(repo->index);
169175
free(repo->index);

repository.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
struct config_set;
55
struct index_state;
6+
struct submodule_cache;
67

78
struct repository {
89
/* Environment */
@@ -50,6 +51,9 @@ struct repository {
5051
*/
5152
struct config_set *config;
5253

54+
/* Repository's submodule config as defined by '.gitmodules' */
55+
struct submodule_cache *submodule_cache;
56+
5357
/*
5458
* Repository's in-memory index.
5559
* 'repo_read_index()' can be used to populate 'index'.

submodule-config.c

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "repository.h"
23
#include "config.h"
34
#include "submodule-config.h"
45
#include "submodule.h"
@@ -15,6 +16,7 @@
1516
struct 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-
3736
static 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

5964
static 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

86101
static 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+
518543
const 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

525550
const 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

532566
void 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
}

submodule-config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@ struct submodule {
2222
int recommend_shallow;
2323
};
2424

25+
struct submodule_cache;
26+
struct repository;
27+
28+
extern void submodule_cache_free(struct submodule_cache *cache);
29+
2530
extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
2631
extern int parse_update_recurse_submodules_arg(const char *opt, const char *arg);
2732
extern int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
2833
extern int parse_submodule_config_option(const char *var, const char *value);
34+
extern int submodule_config_option(struct repository *repo,
35+
const char *var, const char *value);
2936
extern const struct submodule *submodule_from_name(
3037
const unsigned char *commit_or_tree, const char *name);
3138
extern const struct submodule *submodule_from_path(
3239
const unsigned char *commit_or_tree, const char *path);
40+
extern const struct submodule *submodule_from_cache(struct repository *repo,
41+
const unsigned char *treeish_name,
42+
const char *key);
3343
extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
3444
unsigned char *gitmodules_sha1,
3545
struct strbuf *rev);

0 commit comments

Comments
 (0)