@matheusbsbs / matheus-bagliano / commits / 0ed7481347
Fork 0

setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwd

When setup_work_tree() is called, it moves cwd to $GIT_WORK_TREE and makes internal copy of $GIT_WORK_TREE absolute. The environt variable, if set by user, remains unchanged. If the variable is relative, it is no longer correct because its base dir has changed. Instead of making $GIT_WORK_TREE absolute too, we just say "." and let subsequent git processes handle it. Reported-by: Michel Briand <michelbriand@free.fr> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Dec 27, 2010 at 08:26 UTC 0ed748134748579a13cf27e1e8b3e55371bcd9dc
5 files changed +38
.gitignore
+1
@@ -175,6 +175,7 @@
175 /test-sha1
176 /test-sigchain
177 /test-string-pool
178 +/test-subprocess
179 /test-svn-fe
180 /test-treap
181 /common-cmds.h
Makefile
+1
@@ -425,6 +425,7 @@ TEST_PROGRAMS_NEED_X += test-run-command
425 TEST_PROGRAMS_NEED_X += test-sha1
426 TEST_PROGRAMS_NEED_X += test-sigchain
427 TEST_PROGRAMS_NEED_X += test-string-pool
428 +TEST_PROGRAMS_NEED_X += test-subprocess
429 TEST_PROGRAMS_NEED_X += test-svn-fe
430 TEST_PROGRAMS_NEED_X += test-treap
431 TEST_PROGRAMS_NEED_X += test-index-version
setup.c
+8
@@ -221,6 +221,14 @@ void setup_work_tree(void)
221 git_dir = make_absolute_path(git_dir);
222 if (!work_tree || chdir(work_tree))
223 die("This operation must be run in a work tree");
224 +
225 + /*
226 + * Make sure subsequent git processes find correct worktree
227 + * if $GIT_WORK_TREE is set relative
228 + */
229 + if (getenv(GIT_WORK_TREE_ENVIRONMENT))
230 + setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
231 +
232 set_git_dir(make_relative_path(git_dir, work_tree));
233 initialized = 1;
234 }
t/t1501-worktree.sh
+7
@@ -340,4 +340,11 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
340 git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
341 '
342
343 +test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
344 + GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
345 + test-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
346 + echo "$TRASH_DIRECTORY/repo.git/work" >expected &&
347 + test_cmp expected actual
348 +'
349 +
350 test_done
test-subprocess.c new
+21
@@ -0,0 +1,21 @@
1 +#include "cache.h"
2 +#include "run-command.h"
3 +
4 +int main(int argc, char **argv)
5 +{
6 + const char *prefix;
7 + struct child_process cp;
8 + int nogit = 0;
9 +
10 + prefix = setup_git_directory_gently(&nogit);
11 + if (nogit)
12 + die("No git repo found");
13 + if (!strcmp(argv[1], "--setup-work-tree")) {
14 + setup_work_tree();
15 + argv++;
16 + }
17 + memset(&cp, 0, sizeof(cp));
18 + cp.git_cmd = 1;
19 + cp.argv = (const char **)argv+1;
20 + return run_command(&cp);
21 +}