Skip to contentSkip to conversation

setup.c: add enter_work_tree()

PATCH In review [email protected] 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 setup.c: add enter_work_tree()

From: Nguyễn Thái Ngọc Duy <[email protected]>

This function, apart from chdir() to worktree topdir, also invalidates inside_work_tree so is_inside_work_tree() should reflect the new situation correctly.

This function may be used when cwd is outside worktree and the running command supports this case.

---
To be used by builtin/grep.c when cwd is outside worktree. More explanation in the next patch.

 cache.h |    1 +
 setup.c |    7 +++++++
 2 files changed, 8 insertions(+), 0 deletions(-)
cache.h +1 -0
index 2ef2fa3..e71db23 100644
@@ -414,6 +414,7 @@ extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
 extern const char *read_gitfile_gently(const char *path);
 extern void set_git_work_tree(const char *tree);
+extern void enter_work_tree();
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
setup.c +7 -0
index a3b76de..3fbe928 100644
@@ -208,6 +208,13 @@ int is_inside_work_tree(void)
 	return inside_work_tree;
 }
 
+void enter_work_tree(void)
+{
+	if (chdir(get_git_work_tree()))
+		die("Could not chdir to %s", get_git_work_tree());
+	inside_work_tree = -1;
+}
+
 /*
  * set_work_tree() is only ever called if you set GIT_DIR explicitly.
  * The old behaviour (which we retain here) is to set the work tree root
Signature · 1 line
1.7.1.rc1.70.g788ca