Subject: [WIP/RFC 21/23] repo: add repo_read_gitmodules

From: Brandon Williams <[email-masked]>
Date: Thu, 18 May 2017 16:21:32 -0700

Add ability for a repository to poulate its own submodule_cache by
reading the repository's gitmodules file.

Signed-off-by: Brandon Williams <[email-masked]>
---
 repo.c | 26 ++++++++++++++++++++++++++
 repo.h |  3 +++
 2 files changed, 29 insertions(+)

diff --git a/repo.c b/repo.c
index 223adf4c8..5449eb212 100644
--- a/repo.c
+++ b/repo.c
@@ -1,4 +1,6 @@
 #include "cache.h"
+#include "submodule.h"
+#include "submodule-config.h"
 #include "repo.h"
 
 char *
@@ -33,6 +35,27 @@ repo_read_config(struct repo *repo)
 				       NULL, &opts);
 }
 
+static int
+gitmodules_cb(const char *var, const char *value, void *data)
+{
+	struct repo *repo = data;
+	return parse_submodule_config_option_cache(repo->submodule_cache, var, value);
+}
+
+int
+repo_read_gitmodules(struct repo *repo)
+{
+	char *gitmodules_path = xstrfmt("%s/.gitmodules", repo->worktree);
+
+	if (!repo->worktree)
+		die("BUG: no worktree");
+
+	repo->submodule_cache = submodule_cache_alloc();
+	submodule_cache_init(repo->submodule_cache);
+	git_config_from_file(gitmodules_cb, gitmodules_path, repo);
+	return 0;
+}
+
 int
 repo_init(struct repo *repo, const char *gitdir, const char *worktree)
 {
@@ -83,4 +106,7 @@ repo_clear(struct repo *repo)
 		git_configset_clear(repo->config);
 		free(repo->config);
 	}
+
+	if (repo->submodule_cache)
+		submodule_cache_free(repo->submodule_cache);
 }
diff --git a/repo.h b/repo.h
index b4df774c3..9ff144957 100644
--- a/repo.h
+++ b/repo.h
@@ -10,10 +10,13 @@ struct repo {
 	const char *submodule_prefix;
 	struct index_state *index;
 	struct config_set *config;
+	struct submodule_cache *submodule_cache;
 };
 
 extern int repo_read_index(struct repo *repo, const char *index_file);
 extern int repo_init(struct repo *repo, const char *gitdir, const char *worktree);
 extern void repo_clear(struct repo *repo);
 
+extern int repo_read_gitmodules(struct repo *repo);
+
 #endif /* REPO_H */
-- 
2.13.0.303.g4ebf302169-goog