Subject: [WIP/RFC 18/23] repo: add index_state to struct repo

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

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

diff --git a/repo.c b/repo.c
index d47e98d95..7e5c03ac5 100644
--- a/repo.c
+++ b/repo.c
@@ -2,6 +2,20 @@
 #include "repo.h"
 
 int
+repo_read_index(struct repo *repo, const char *index_file)
+{
+	char *index_path = xstrfmt("%s/index", repo->gitdir);
+	const char *file = index_file ? index_file : index_path;
+
+	repo->index = xcalloc(1, sizeof(struct index_state));
+	if (read_index_from(repo->index, file) < 0)
+		die("failure reading index\n");
+
+	free(index_path);
+	return 0;
+}
+
+int
 repo_init(struct repo *repo, const char *gitdir, const char *worktree)
 {
 	int error = 0;
@@ -39,4 +53,9 @@ repo_clear(struct repo *repo)
 	repo->gitdir = NULL;
 	free(repo->worktree);
 	repo->worktree = NULL;
+
+	if (repo->index) {
+		discard_index(repo->index);
+		free(repo->index);
+	}
 }
diff --git a/repo.h b/repo.h
index 55f2dbec6..15a0bdee9 100644
--- a/repo.h
+++ b/repo.h
@@ -7,8 +7,10 @@ struct repo {
 	char *gitdir;
 	char *worktree;
 	const char *submodule_prefix;
+	struct index_state *index;
 };
 
+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);
 
-- 
2.13.0.303.g4ebf302169-goog