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

From: [email-masked]
Date: Thu, 7 Oct 2010 11:46:52 +0700

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

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.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 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(-)

diff --git a/cache.h b/cache.h
index 2ef2fa3..e71db23 100644
--- a/cache.h
+++ b/cache.h
@@ -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"
 
diff --git a/setup.c b/setup.c
index a3b76de..3fbe928 100644
--- a/setup.c
+++ b/setup.c
@@ -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
-- 
1.7.1.rc1.70.g788ca