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>
Brandon Williams committed
Jun 22, 2017 at 11:43 UTC
bf12fcdf5ec00e6b7d0978750df9e0146eb57c75
4 files changed
+72
-18
repository.c
+6
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "repository.h"
3
#include "config.h"
4
+#include "submodule-config.h"
5
6
/* The main repository */
7
static struct repository the_repo;
@@ -164,6 +165,11 @@ void repo_clear(struct repository *repo)
165
repo->config = NULL;
166
}
167
168
+ if (repo->submodule_cache) {
169
+ submodule_cache_free(repo->submodule_cache);
170
+ repo->submodule_cache = NULL;
171
+ }
172
+
173
if (repo->index) {
174
discard_index(repo->index);
175
free(repo->index);
repository.h
+4
@@ -3,6 +3,7 @@
3
4
struct config_set;
5
struct index_state;
6
+struct submodule_cache;
7
8
struct repository {
9
/* Environment */
@@ -50,6 +51,9 @@ struct repository {
51
*/
52
struct config_set *config;
53
54
+ /* Repository's submodule config as defined by '.gitmodules' */
55
+ struct submodule_cache *submodule_cache;
56
+
57
/*
58
* Repository's in-memory index.
59
* 'repo_read_index()' can be used to populate 'index'.
submodule-config.c
+52
-18
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "repository.h"
3
#include "config.h"
4
#include "submodule-config.h"
5
#include "submodule.h"
@@ -15,6 +16,7 @@
16
struct submodule_cache {
17
struct hashmap for_path;
18
struct hashmap for_name;
19
+ unsigned initialized:1;
20
};
21
22
/*
@@ -31,9 +33,6 @@ enum lookup_type {
33
lookup_path
34
};
35
34
-static struct submodule_cache the_submodule_cache;
35
-static int is_cache_init;
36
-
36
static int config_path_cmp(const struct submodule_entry *a,
37
const struct submodule_entry *b,
38
const void *unused)
@@ -50,10 +49,16 @@ static int config_name_cmp(const struct submodule_entry *a,
49
hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
50
}
51
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)
58
{
59
hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
60
hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
61
+ cache->initialized = 1;
62
}
63
64
static void free_one_config(struct submodule_entry *entry)
@@ -65,11 +70,14 @@ static void free_one_config(struct submodule_entry *entry)
70
free(entry->config);
71
}
72
68
-static void cache_free(struct submodule_cache *cache)
73
+static void submodule_cache_clear(struct submodule_cache *cache)
74
{
75
struct hashmap_iter iter;
76
struct submodule_entry *entry;
77
78
+ if (!cache->initialized)
79
+ return;
80
+
81
/*
82
* We iterate over the name hash here to be symmetric with the
83
* allocation of struct submodule entries. Each is allocated by
@@ -81,6 +89,13 @@ static void cache_free(struct submodule_cache *cache)
89
90
hashmap_free(&cache->for_path, 1);
91
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);
99
}
100
101
static unsigned int hash_sha1_string(const unsigned char *sha1,
@@ -494,43 +509,62 @@ out:
509
return submodule;
510
}
511
497
-static void ensure_cache_init(void)
512
+static void submodule_cache_check_init(struct repository *repo)
513
{
499
- if (is_cache_init)
514
+ if (repo->submodule_cache && repo->submodule_cache->initialized)
515
return;
516
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);
521
}
522
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)
525
{
526
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;
531
parameter.treeish_name = NULL;
532
parameter.gitmodules_sha1 = null_sha1;
533
parameter.overwrite = 1;
534
514
- ensure_cache_init();
535
return parse_config(var, value, ¶meter);
536
}
537
538
+int parse_submodule_config_option(const char *var, const char *value)
539
+{
540
+ return submodule_config_option(the_repository, var, value);
541
+}
542
+
543
const struct submodule *submodule_from_name(const unsigned char *treeish_name,
544
const char *name)
545
{
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);
548
}
549
550
const struct submodule *submodule_from_path(const unsigned char *treeish_name,
551
const char *path)
552
{
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);
564
}
565
566
void submodule_free(void)
567
{
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);
570
}
submodule-config.h
+10
@@ -22,14 +22,24 @@ struct submodule {
22
int recommend_shallow;
23
};
24
25
+struct submodule_cache;
26
+struct repository;
27
+
28
+extern void submodule_cache_free(struct submodule_cache *cache);
29
+
30
extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
31
extern int parse_update_recurse_submodules_arg(const char *opt, const char *arg);
32
extern int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
33
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);
36
extern const struct submodule *submodule_from_name(
37
const unsigned char *commit_or_tree, const char *name);
38
extern const struct submodule *submodule_from_path(
39
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);
43
extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
44
unsigned char *gitmodules_sha1,
45
struct strbuf *rev);