Subject: [PATCH 6/8] environment.c: adjust env containing relpath when $CWD is moved

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 28 Mar 2018 19:55:35 +0200

From: Duy Nguyen <[email-masked]>

As noted in the previous patch, when $CWD is moved, we recognize the
problem with relative paths and update $GIT_WORK_TREE and $GIT_DIR
with new ones.

We have plenty more environment variables that can contain paths
though. If they are read and cached before setup_work_tree() is
called, nobody will update them and they become bad paths.

Hook into setup_work_tree() and update all those env variables. The
code to update $GIT_WORK_TREE is no longer needed and removed.

Technically we should remove the setenv() in set_git_dir() as well,
but that is also called _not_ by setup_work_tree() and we should keep
the behavior the same in that case for safety. set_git_dir() will be
removed from setup_work_tree() soon, which achieves the same goal.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 environment.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 setup.c       |  7 -------
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/environment.c b/environment.c
index 39b3d906c8..f9dcc1b99e 100644
--- a/environment.c
+++ b/environment.c
@@ -128,6 +128,20 @@ const char * const local_repo_env[] = {
 	NULL
 };
 
+/* A subset of local_repo_env[] that contains path */
+const char * const local_repo_path_env[] = {
+	ALTERNATE_DB_ENVIRONMENT,
+	CONFIG_ENVIRONMENT,
+	DB_ENVIRONMENT,
+	GIT_COMMON_DIR_ENVIRONMENT,
+	GIT_DIR_ENVIRONMENT,
+	GIT_SHALLOW_FILE_ENVIRONMENT,
+	GIT_WORK_TREE_ENVIRONMENT,
+	GRAFT_ENVIRONMENT,
+	INDEX_ENVIRONMENT,
+	NULL
+};
+
 static char *expand_namespace(const char *raw_namespace)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -149,6 +163,32 @@ static char *expand_namespace(const char *raw_namespace)
 	return strbuf_detach(&buf, NULL);
 }
 
+static void update_path_envs(const char *old_cwd, const char *new_cwd,
+			     void *cb)
+{
+	int i;
+
+	/*
+	 * FIXME: special treatment needed for
+	 * GIT_ALTERNATE_OBJECT_DIRECTORIES because it can contain
+	 * multiple paths.
+	 */
+	for (i = 0; local_repo_path_env[i]; i++) {
+		const char *name = local_repo_path_env[i];
+		const char *value = getenv(name);
+		char *new_value;
+
+		if (!value)
+			continue;
+		if (is_absolute_path(value))
+			continue;
+		new_value = xstrdup(value);
+		setup_adjust_path(name, &new_value, old_cwd, new_cwd);
+		if (setenv(name, new_value, 10))
+			error(_("could not set %s to '%s'"), name, value);
+		free(new_value);
+	}
+}
 /*
  * Wrapper of getenv() that returns a strdup value. This value is kept
  * in argv to be freed later.
@@ -170,6 +210,12 @@ void setup_git_env(const char *git_dir)
 	const char *replace_ref_base;
 	struct set_gitdir_args args = { NULL };
 	struct argv_array to_free = ARGV_ARRAY_INIT;
+	static int added_cwd_callback;
+
+	if (!added_cwd_callback) {
+		add_cwd_update_callback(update_path_envs, NULL);
+		added_cwd_callback = 1;
+	}
 
 	args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
 	args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
diff --git a/setup.c b/setup.c
index e340ee2130..23b8f89ce2 100644
--- a/setup.c
+++ b/setup.c
@@ -435,13 +435,6 @@ void setup_work_tree(void)
 	if (!work_tree || chdir(work_tree))
 		die(_("this operation must be run in a work tree"));
 
-	/*
-	 * Make sure subsequent git processes find correct worktree
-	 * if $GIT_WORK_TREE is set relative
-	 */
-	if (getenv(GIT_WORK_TREE_ENVIRONMENT))
-		setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
-
 	for (i = 0; i < nr_cwd_callbacks; i++)
 		cwd_callbacks[i].fn(old_cwd.buf, work_tree,
 				    cwd_callbacks[i].cb_data);
-- 
2.17.0.rc1.439.gca064e2955