Subject: [WIP/RFC 19/23] repo: add per repo config

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

Signed-off-by: Brandon Williams <[email-masked]>
---
 cache.h  |  1 +
 config.c |  2 +-
 repo.c   | 27 ++++++++++++++++++++++++++-
 repo.h   |  2 ++
 4 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/cache.h b/cache.h
index 175e58f01..af9ae1173 100644
--- a/cache.h
+++ b/cache.h
@@ -2015,6 +2015,7 @@ struct config_set {
 };
 
 extern void git_configset_init(struct config_set *cs);
+extern int config_set_callback(const char *key, const char *value, void *cb);
 extern int git_configset_add_file(struct config_set *cs, const char *filename);
 extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
 extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
diff --git a/config.c b/config.c
index b4a3205da..d24baec50 100644
--- a/config.c
+++ b/config.c
@@ -1765,7 +1765,7 @@ void git_configset_clear(struct config_set *cs)
 	cs->list.items = NULL;
 }
 
-static int config_set_callback(const char *key, const char *value, void *cb)
+int config_set_callback(const char *key, const char *value, void *cb)
 {
 	struct config_set *cs = cb;
 	configset_add_value(cs, key, value);
diff --git a/repo.c b/repo.c
index 7e5c03ac5..223adf4c8 100644
--- a/repo.c
+++ b/repo.c
@@ -1,10 +1,16 @@
 #include "cache.h"
 #include "repo.h"
 
+char *
+repo_git_pathdup(const struct repo *repo, const char *file)
+{
+	return xstrfmt("%s/%s", repo->gitdir, file);
+}
+
 int
 repo_read_index(struct repo *repo, const char *index_file)
 {
-	char *index_path = xstrfmt("%s/index", repo->gitdir);
+	char *index_path = repo_git_pathdup(repo, "index");
 	const char *file = index_file ? index_file : index_path;
 
 	repo->index = xcalloc(1, sizeof(struct index_state));
@@ -16,6 +22,18 @@ repo_read_index(struct repo *repo, const char *index_file)
 }
 
 int
+repo_read_config(struct repo *repo)
+{
+	struct config_options opts = { 1, repo->gitdir };
+
+	repo->config = xcalloc(1, sizeof(struct config_set));
+	git_configset_init(repo->config);
+
+	return git_config_with_options(config_set_callback, repo->config,
+				       NULL, &opts);
+}
+
+int
 repo_init(struct repo *repo, const char *gitdir, const char *worktree)
 {
 	int error = 0;
@@ -40,6 +58,8 @@ repo_init(struct repo *repo, const char *gitdir, const char *worktree)
 	/* Maybe need a check to verify that a worktree is indeed a worktree? */
 	repo->worktree = xstrdup_or_null(worktree);
 
+	repo_read_config(repo);
+
 	free(abspath);
 	free(suspect);
 
@@ -58,4 +78,9 @@ repo_clear(struct repo *repo)
 		discard_index(repo->index);
 		free(repo->index);
 	}
+
+	if (repo->config) {
+		git_configset_clear(repo->config);
+		free(repo->config);
+	}
 }
diff --git a/repo.h b/repo.h
index 15a0bdee9..b4df774c3 100644
--- a/repo.h
+++ b/repo.h
@@ -2,12 +2,14 @@
 #define REPO_H
 
 struct index_state;
+struct config_set;
 
 struct repo {
 	char *gitdir;
 	char *worktree;
 	const char *submodule_prefix;
 	struct index_state *index;
+	struct config_set *config;
 };
 
 extern int repo_read_index(struct repo *repo, const char *index_file);
-- 
2.13.0.303.g4ebf302169-goog