Subject: [PATCH v4 2/4] submodule: update core.worktree using git-config

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 20 Jul 2016 19:24:17 +0200

To access a separate repository, the first step should be read its
config file to determine if this repository layout is supported or
not, or if we understand all repo extensions, of it is a linked
worktree. Only then should know where to update the config file.

Unfortunately, our C code base is not ready for doing all that in the
same process. The repo detection is not meant to be used for peeking
in other repository, and config code would read config.worktree that
is in _current_ $GIT_DIR.

For now, let's spawn a new process and let all that done separately.

PS. submodule-helper also updates core.worktree. But in there, we
create a new clone, we know what is the initial repository layout, so
we know we can simply update "config" file without risks.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 submodule.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/submodule.c b/submodule.c
index abc2ac2..b912871 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1128,7 +1128,9 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
 {
 	struct strbuf file_name = STRBUF_INIT;
 	struct strbuf rel_path = STRBUF_INIT;
+	struct strbuf path = STRBUF_INIT;
 	const char *real_work_tree = xstrdup(real_path(work_tree));
+	struct child_process cp = CHILD_PROCESS_INIT;
 
 	/* Update gitfile */
 	strbuf_addf(&file_name, "%s/.git", work_tree);
@@ -1136,13 +1138,17 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
 		   relative_path(git_dir, real_work_tree, &rel_path));
 
 	/* Update core.worktree setting */
-	strbuf_reset(&file_name);
-	strbuf_addf(&file_name, "%s/config", git_dir);
-	git_config_set_in_file(file_name.buf, "core.worktree",
-			       relative_path(real_work_tree, git_dir,
-					     &rel_path));
+	strbuf_addstr(&path, relative_path(real_work_tree, git_dir,
+					   &rel_path));
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "-C", work_tree, NULL);
+	argv_array_pushl(&cp.args, "--work-tree", ".", NULL);
+	argv_array_pushl(&cp.args, "config", "core.worktree", path.buf, NULL);
+	if (run_command(&cp) < 0)
+		die(_("failed to update core.worktree for %s"), git_dir);
 
 	strbuf_release(&file_name);
+	strbuf_release(&path);
 	strbuf_release(&rel_path);
 	free((void *)real_work_tree);
 }
-- 
2.9.1.566.gbd532d4