Subject: [PATCH v2 09/32] environment: store worktree in the_repository

From: Brandon Williams <[email-masked]>
Date: Thu, 8 Jun 2017 16:40:37 -0700

Migrate 'work_tree' to be stored in 'the_repository'.

Signed-off-by: Brandon Williams <[email-masked]>
---
 environment.c |  9 ++++-----
 repository.c  | 12 ++++++++++++
 repository.h  |  3 +++
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/environment.c b/environment.c
index fb52c0daf..e390a6627 100644
--- a/environment.c
+++ b/environment.c
@@ -96,7 +96,6 @@ int ignore_untracked_cache_config;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
-static char *work_tree;
 
 static const char *super_prefix;
 
@@ -220,19 +219,19 @@ void set_git_work_tree(const char *new_work_tree)
 {
 	if (git_work_tree_initialized) {
 		new_work_tree = real_path(new_work_tree);
-		if (strcmp(new_work_tree, work_tree))
+		if (strcmp(new_work_tree, the_repository->worktree))
 			die("internal error: work tree has already been set\n"
 			    "Current worktree: %s\nNew worktree: %s",
-			    work_tree, new_work_tree);
+			    the_repository->worktree, new_work_tree);
 		return;
 	}
 	git_work_tree_initialized = 1;
-	work_tree = real_pathdup(new_work_tree, 1);
+	repo_set_worktree(the_repository, new_work_tree);
 }
 
 const char *get_git_work_tree(void)
 {
-	return work_tree;
+	return the_repository->worktree;
 }
 
 char *get_object_directory(void)
diff --git a/repository.c b/repository.c
index e952238d1..1b48cc816 100644
--- a/repository.c
+++ b/repository.c
@@ -104,6 +104,16 @@ static int repo_init_gitdir(struct repository *repo, const char *gitdir)
 	return ret;
 }
 
+void repo_set_worktree(struct repository *repo, const char *path)
+{
+	repo->worktree = real_pathdup(path, 1);
+}
+
+char *repo_worktree_path(struct repository *repo, const char *path)
+{
+	return xstrfmt("%s/%s", repo->worktree, path);
+}
+
 static int verify_repo_format(struct repository_format *format,
 			      const char *commondir)
 {
@@ -166,6 +176,8 @@ static void repo_clear_env(struct repository *repo)
 void repo_clear(struct repository *repo)
 {
 	repo_clear_env(repo);
+	free(repo->worktree);
+	repo->worktree = NULL;
 
 	memset(repo, 0, sizeof(*repo));
 }
diff --git a/repository.h b/repository.h
index 174ab0f2d..a1163ae91 100644
--- a/repository.h
+++ b/repository.h
@@ -9,6 +9,7 @@ struct repository {
 	char *index_file;
 	char *graft_file;
 	char *namespace;
+	char *worktree;
 
 	/* Configurations */
 	unsigned ignore_env:1;
@@ -19,6 +20,8 @@ struct repository {
 extern struct repository *the_repository;
 
 extern void repo_set_gitdir(struct repository *repo, const char *path);
+extern void repo_set_worktree(struct repository *repo, const char *path);
+extern char *repo_worktree_path(struct repository *repo, const char *path);
 extern int repo_init(struct repository *repo, const char *path);
 extern void repo_clear(struct repository *repo);
 
-- 
2.13.1.508.gb3defc5cc-goog