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 <ao2@ao2.it> Acked-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Antonio Ospite committed Jun 26, 2018 at 12:47 UTC db64d1127f5c76b721830ba390176cf0d6e59d74
1 file changed +21 -29
submodule-config.c
+21 -29
@@ -591,6 +591,23 @@ static void submodule_cache_check_init(struct repository *repo)
591 submodule_cache_init(repo->submodule_cache);
592 }
593
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 +
611 static int gitmodules_cb(const char *var, const char *value, void *data)
612 {
613 struct repository *repo = data;
@@ -608,19 +625,11 @@ void repo_read_gitmodules(struct repository *repo)
625 {
626 submodule_cache_check_init(repo);
627
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;
630
622 - free(gitmodules);
623 - }
631 + if (!is_gitmodules_unmerged(repo->index))
632 + config_from_gitmodules(gitmodules_cb, repo, repo);
633
634 repo->submodule_cache->gitmodules_read = 1;
635 }
@@ -672,23 +681,6 @@ void submodule_free(struct repository *r)
681 submodule_cache_clear(r->submodule_cache);
682 }
683
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 -
684 struct fetch_config {
685 int *max_children;
686 int *recurse_submodules;