From 1b796ace7b5566d7cd2ed2ee56d3e5b1f7605272 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 3 Aug 2017 11:19:57 -0700 Subject: submodule-config: move submodule-config functions to submodule-config.c Migrate the functions used to initialize the submodule-config to submodule-config.c so that the callback routine used in the initialization process can be static and prevent it from being used outside of initializing the submodule-config through the main API. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- submodule-config.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'submodule-config.c') diff --git a/submodule-config.c b/submodule-config.c index 0b429e942..86636654b 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -449,9 +449,9 @@ static int parse_config(const char *var, const char *value, void *data) return ret; } -int gitmodule_oid_from_commit(const struct object_id *treeish_name, - struct object_id *gitmodules_oid, - struct strbuf *rev) +static int gitmodule_oid_from_commit(const struct object_id *treeish_name, + struct object_id *gitmodules_oid, + struct strbuf *rev) { int ret = 0; @@ -552,9 +552,9 @@ static void submodule_cache_check_init(struct repository *repo) submodule_cache_init(repo->submodule_cache); } -int submodule_config_option(struct repository *repo, - const char *var, const char *value) +static int gitmodules_cb(const char *var, const char *value, void *data) { + struct repository *repo = data; struct parse_config_parameter parameter; submodule_cache_check_init(repo); @@ -567,9 +567,33 @@ int submodule_config_option(struct repository *repo, return parse_config(var, value, ¶meter); } -int parse_submodule_config_option(const char *var, const char *value) +void repo_read_gitmodules(struct repository *repo) { - return submodule_config_option(the_repository, var, value); + if (repo->worktree) { + char *gitmodules; + + if (repo_read_index(repo) < 0) + return; + + gitmodules = repo_worktree_path(repo, GITMODULES_FILE); + + if (!is_gitmodules_unmerged(repo->index)) + git_config_from_file(gitmodules_cb, gitmodules, repo); + + free(gitmodules); + } +} + +void gitmodules_config_oid(const struct object_id *commit_oid) +{ + struct strbuf rev = STRBUF_INIT; + struct object_id oid; + + if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { + git_config_from_blob_oid(gitmodules_cb, rev.buf, + &oid, the_repository); + } + strbuf_release(&rev); } const struct submodule *submodule_from_name(const struct object_id *treeish_name, -- cgit v1.2.1 From ff6f1f564c48def1f8e1852826bab58af5044b06 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 3 Aug 2017 11:19:58 -0700 Subject: submodule-config: lazy-load a repository's .gitmodules file In order to use the submodule-config subsystem, callers first need to initialize it by calling 'repo_read_gitmodules()' or 'gitmodules_config()' (which just redirects to 'repo_read_gitmodules()'). There are a couple of callers who need to load an explicit revision of the repository's .gitmodules file (grep) or need to modify the .gitmodules file so they would need to load it before modify the file (checkout), but the majority of callers are simply reading the .gitmodules file present in the working tree. For the common case it would be nice to avoid the boilerplate of initializing the submodule-config system before using it, so instead let's perform lazy-loading of the submodule-config system. Remove the calls to reading the gitmodules file from ls-files to show that lazy-loading the .gitmodules file works. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- submodule-config.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'submodule-config.c') diff --git a/submodule-config.c b/submodule-config.c index 86636654b..56d9d76d4 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -18,6 +18,7 @@ struct submodule_cache { struct hashmap for_path; struct hashmap for_name; unsigned initialized:1; + unsigned gitmodules_read:1; }; /* @@ -93,6 +94,7 @@ static void submodule_cache_clear(struct submodule_cache *cache) hashmap_free(&cache->for_path, 1); hashmap_free(&cache->for_name, 1); cache->initialized = 0; + cache->gitmodules_read = 0; } void submodule_cache_free(struct submodule_cache *cache) @@ -557,8 +559,6 @@ static int gitmodules_cb(const char *var, const char *value, void *data) struct repository *repo = data; struct parse_config_parameter parameter; - submodule_cache_check_init(repo); - parameter.cache = repo->submodule_cache; parameter.treeish_name = NULL; parameter.gitmodules_sha1 = null_sha1; @@ -569,6 +569,8 @@ static int gitmodules_cb(const char *var, const char *value, void *data) void repo_read_gitmodules(struct repository *repo) { + submodule_cache_check_init(repo); + if (repo->worktree) { char *gitmodules; @@ -582,6 +584,8 @@ void repo_read_gitmodules(struct repository *repo) free(gitmodules); } + + repo->submodule_cache->gitmodules_read = 1; } void gitmodules_config_oid(const struct object_id *commit_oid) @@ -589,24 +593,37 @@ void gitmodules_config_oid(const struct object_id *commit_oid) struct strbuf rev = STRBUF_INIT; struct object_id oid; + submodule_cache_check_init(the_repository); + if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { git_config_from_blob_oid(gitmodules_cb, rev.buf, &oid, the_repository); } strbuf_release(&rev); + + the_repository->submodule_cache->gitmodules_read = 1; +} + +static void gitmodules_read_check(struct repository *repo) +{ + submodule_cache_check_init(repo); + + /* read the repo's .gitmodules file if it hasn't been already */ + if (!repo->submodule_cache->gitmodules_read) + repo_read_gitmodules(repo); } const struct submodule *submodule_from_name(const struct object_id *treeish_name, const char *name) { - submodule_cache_check_init(the_repository); + gitmodules_read_check(the_repository); return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name); } const struct submodule *submodule_from_path(const struct object_id *treeish_name, const char *path) { - submodule_cache_check_init(the_repository); + gitmodules_read_check(the_repository); return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path); } @@ -614,7 +631,7 @@ const struct submodule *submodule_from_cache(struct repository *repo, const struct object_id *treeish_name, const char *key) { - submodule_cache_check_init(repo); + gitmodules_read_check(repo); return config_from(repo->submodule_cache, treeish_name, key, lookup_path); } -- cgit v1.2.1