Skip to content

Commit 0ed7481

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e7d1efb commit 0ed7481

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
/test-sha1
176176
/test-sigchain
177177
/test-string-pool
178+
/test-subprocess
178179
/test-svn-fe
179180
/test-treap
180181
/common-cmds.h

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ TEST_PROGRAMS_NEED_X += test-run-command
425425
TEST_PROGRAMS_NEED_X += test-sha1
426426
TEST_PROGRAMS_NEED_X += test-sigchain
427427
TEST_PROGRAMS_NEED_X += test-string-pool
428+
TEST_PROGRAMS_NEED_X += test-subprocess
428429
TEST_PROGRAMS_NEED_X += test-svn-fe
429430
TEST_PROGRAMS_NEED_X += test-treap
430431
TEST_PROGRAMS_NEED_X += test-index-version

setup.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ void setup_work_tree(void)
221221
git_dir = make_absolute_path(git_dir);
222222
if (!work_tree || chdir(work_tree))
223223
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+
224232
set_git_dir(make_relative_path(git_dir, work_tree));
225233
initialized = 1;
226234
}

t/t1501-worktree.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,11 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
340340
git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
341341
'
342342

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+
343350
test_done

test-subprocess.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)