Skip to content

Commit db64d11

Browse files
ao2gitster
authored andcommitted
submodule-config: reuse config_from_gitmodules in repo_read_gitmodules
Reuse config_from_gitmodules in repo_read_gitmodules to remove some duplication and also have a single point where the .gitmodules file is read. The change does not introduce any new behavior, the same gitmodules_cb config callback is still used, which only deals with configuration specific to submodules. The check about the repo's worktree is removed from repo_read_gitmodules because it's already performed in config_from_gitmodules. The config_from_gitmodules function is moved up in the file —unchanged— before its users to avoid a forward declaration. Signed-off-by: Antonio Ospite <[email protected]> Acked-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a0fb3e commit db64d11

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

submodule-config.c

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,23 @@ static void submodule_cache_check_init(struct repository *repo)
591591
submodule_cache_init(repo->submodule_cache);
592592
}
593593

594+
/*
595+
* Note: This function is private for a reason, the '.gitmodules' file should
596+
* not be used as as a mechanism to retrieve arbitrary configuration stored in
597+
* the repository.
598+
*
599+
* Runs the provided config function on the '.gitmodules' file found in the
600+
* working directory.
601+
*/
602+
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
603+
{
604+
if (repo->worktree) {
605+
char *file = repo_worktree_path(repo, GITMODULES_FILE);
606+
git_config_from_file(fn, file, data);
607+
free(file);
608+
}
609+
}
610+
594611
static int gitmodules_cb(const char *var, const char *value, void *data)
595612
{
596613
struct repository *repo = data;
@@ -608,19 +625,11 @@ void repo_read_gitmodules(struct repository *repo)
608625
{
609626
submodule_cache_check_init(repo);
610627

611-
if (repo->worktree) {
612-
char *gitmodules;
613-
614-
if (repo_read_index(repo) < 0)
615-
return;
616-
617-
gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
618-
619-
if (!is_gitmodules_unmerged(repo->index))
620-
git_config_from_file(gitmodules_cb, gitmodules, repo);
628+
if (repo_read_index(repo) < 0)
629+
return;
621630

622-
free(gitmodules);
623-
}
631+
if (!is_gitmodules_unmerged(repo->index))
632+
config_from_gitmodules(gitmodules_cb, repo, repo);
624633

625634
repo->submodule_cache->gitmodules_read = 1;
626635
}
@@ -672,23 +681,6 @@ void submodule_free(struct repository *r)
672681
submodule_cache_clear(r->submodule_cache);
673682
}
674683

675-
/*
676-
* Note: This function is private for a reason, the '.gitmodules' file should
677-
* not be used as as a mechanism to retrieve arbitrary configuration stored in
678-
* the repository.
679-
*
680-
* Runs the provided config function on the '.gitmodules' file found in the
681-
* working directory.
682-
*/
683-
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
684-
{
685-
if (repo->worktree) {
686-
char *file = repo_worktree_path(repo, GITMODULES_FILE);
687-
git_config_from_file(fn, file, data);
688-
free(file);
689-
}
690-
}
691-
692684
struct fetch_config {
693685
int *max_children;
694686
int *recurse_submodules;

0 commit comments

Comments
 (0)