Skip to content

Commit ff6f1f5

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b796ac commit ff6f1f5

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

builtin/ls-files.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ static void show_submodule(struct repository *superproject,
211211
if (repo_read_index(&submodule) < 0)
212212
die("index file corrupt");
213213

214-
repo_read_gitmodules(&submodule);
215-
216214
show_files(&submodule, dir);
217215

218216
repo_clear(&submodule);
@@ -611,9 +609,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
611609
if (require_work_tree && !is_inside_work_tree())
612610
setup_work_tree();
613611

614-
if (recurse_submodules)
615-
repo_read_gitmodules(the_repository);
616-
617612
if (recurse_submodules &&
618613
(show_stage || show_deleted || show_others || show_unmerged ||
619614
show_killed || show_modified || show_resolve_undo || with_tree))

submodule-config.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct submodule_cache {
1818
struct hashmap for_path;
1919
struct hashmap for_name;
2020
unsigned initialized:1;
21+
unsigned gitmodules_read:1;
2122
};
2223

2324
/*
@@ -93,6 +94,7 @@ static void submodule_cache_clear(struct submodule_cache *cache)
9394
hashmap_free(&cache->for_path, 1);
9495
hashmap_free(&cache->for_name, 1);
9596
cache->initialized = 0;
97+
cache->gitmodules_read = 0;
9698
}
9799

98100
void submodule_cache_free(struct submodule_cache *cache)
@@ -557,8 +559,6 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
557559
struct repository *repo = data;
558560
struct parse_config_parameter parameter;
559561

560-
submodule_cache_check_init(repo);
561-
562562
parameter.cache = repo->submodule_cache;
563563
parameter.treeish_name = NULL;
564564
parameter.gitmodules_sha1 = null_sha1;
@@ -569,6 +569,8 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
569569

570570
void repo_read_gitmodules(struct repository *repo)
571571
{
572+
submodule_cache_check_init(repo);
573+
572574
if (repo->worktree) {
573575
char *gitmodules;
574576

@@ -582,39 +584,54 @@ void repo_read_gitmodules(struct repository *repo)
582584

583585
free(gitmodules);
584586
}
587+
588+
repo->submodule_cache->gitmodules_read = 1;
585589
}
586590

587591
void gitmodules_config_oid(const struct object_id *commit_oid)
588592
{
589593
struct strbuf rev = STRBUF_INIT;
590594
struct object_id oid;
591595

596+
submodule_cache_check_init(the_repository);
597+
592598
if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
593599
git_config_from_blob_oid(gitmodules_cb, rev.buf,
594600
&oid, the_repository);
595601
}
596602
strbuf_release(&rev);
603+
604+
the_repository->submodule_cache->gitmodules_read = 1;
605+
}
606+
607+
static void gitmodules_read_check(struct repository *repo)
608+
{
609+
submodule_cache_check_init(repo);
610+
611+
/* read the repo's .gitmodules file if it hasn't been already */
612+
if (!repo->submodule_cache->gitmodules_read)
613+
repo_read_gitmodules(repo);
597614
}
598615

599616
const struct submodule *submodule_from_name(const struct object_id *treeish_name,
600617
const char *name)
601618
{
602-
submodule_cache_check_init(the_repository);
619+
gitmodules_read_check(the_repository);
603620
return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
604621
}
605622

606623
const struct submodule *submodule_from_path(const struct object_id *treeish_name,
607624
const char *path)
608625
{
609-
submodule_cache_check_init(the_repository);
626+
gitmodules_read_check(the_repository);
610627
return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
611628
}
612629

613630
const struct submodule *submodule_from_cache(struct repository *repo,
614631
const struct object_id *treeish_name,
615632
const char *key)
616633
{
617-
submodule_cache_check_init(repo);
634+
gitmodules_read_check(repo);
618635
return config_from(repo->submodule_cache, treeish_name,
619636
key, lookup_path);
620637
}

0 commit comments

Comments
 (0)