{"thread":{"id":"26137","subject":"[PATCH 1/2] t1501: avoid bashisms","startedAt":"2010-12-25T13:57:36Z","lastAt":"2010-12-27T01:26:04Z","messageCount":9,"participants":["Nguyễn Thái Ngọc Duy","Andreas Schwab","Nguyen Thai Ngoc Duy","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"158576","messageId":"1293285457-11915-1-git-send-email-pclouds@gmail.com","threadId":"26137","inReplyTo":null,"subject":"[PATCH 1/2] t1501: avoid bashisms","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-25T13:57:36Z","receivedAt":"2010-12-25T13:57:36Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n t/t1501-worktree.sh |    5 ++++-\n 1 files changed, 4 insertions(+), 1 deletions(-)\n\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 2c8f01f..488160e 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -322,7 +322,10 @@ test_expect_success 'git grep' '\n test_expect_success 'git commit' '\n \t(\n \t\tcd repo.git &&\n-\t\tGIT_DIR=. GIT_WORK_TREE=work git commit -a -m done\n+\t\tGIT_DIR=. &&\n+\t\tGIT_WORK_TREE=work &&\n+\t\texport GIT_DIR GIT_WORK_TREE &&\n+\t\tgit commit -a -m done\n \t)\n '\n \n-- \n1.7.3.4.878.g439c7\n"},{"id":"158577","messageId":"1293285457-11915-2-git-send-email-pclouds@gmail.com","threadId":"26137","inReplyTo":"1293285457-11915-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 2/2] setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwd","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-25T13:57:37Z","receivedAt":"2010-12-25T13:57:37Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When setup_work_tree() is called, it moves cwd to $GIT_WORK_TREE and\nmakes internal copy of $GIT_WORK_TREE absolute. The environt variable,\nif set by user, remains unchanged. If the variable is relative, it is\nno longer correct because its base dir has changed.\n\nInstead of making $GIT_WORK_TREE absolute too, we just say \".\" and let\nsubsequence git processes handle it.\n\nReported-by: Michel Briand <michelbriand@free.fr>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n .gitignore          |    1 +\n Makefile            |    1 +\n setup.c             |    8 ++++++++\n t/t1501-worktree.sh |   11 +++++++++++\n test-subprocess.c   |   18 ++++++++++++++++++\n 5 files changed, 39 insertions(+), 0 deletions(-)\n create mode 100644 test-subprocess.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 87b833c..3dd6ef7 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -177,6 +177,7 @@\n /test-sha1\n /test-sigchain\n /test-string-pool\n+/test-subprocess\n /test-svn-fe\n /test-treap\n /common-cmds.h\ndiff --git a/Makefile b/Makefile\nindex 57d9c65..bdf86a3 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -431,6 +431,7 @@ TEST_PROGRAMS_NEED_X += test-run-command\n TEST_PROGRAMS_NEED_X += test-sha1\n TEST_PROGRAMS_NEED_X += test-sigchain\n TEST_PROGRAMS_NEED_X += test-string-pool\n+TEST_PROGRAMS_NEED_X += test-subprocess\n TEST_PROGRAMS_NEED_X += test-svn-fe\n TEST_PROGRAMS_NEED_X += test-treap\n TEST_PROGRAMS_NEED_X += test-index-version\ndiff --git a/setup.c b/setup.c\nindex 91887a4..3833569 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -239,6 +239,14 @@ void setup_work_tree(void)\n \t\tgit_dir = make_absolute_path(git_dir);\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n+\n+\t/*\n+\t * Make sure subsequent git processes find correct worktree\n+\t * if $GIT_WORK_TREE is set relative\n+\t */\n+\tif (getenv(GIT_WORK_TREE_ENVIRONMENT))\n+\t\tsetenv(GIT_WORK_TREE_ENVIRONMENT, \".\", 1);\n+\n \tset_git_dir(make_relative_path(git_dir, work_tree));\n \tinitialized = 1;\n }\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 488160e..16c953b 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -343,4 +343,15 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '\n \tgit --git-dir=\"$(pwd)//repo.git\" --work-tree=\"$(pwd)\" add dummy_file\n '\n \n+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '\n+\t(\n+\tGIT_DIR=repo.git\n+\tGIT_WORK_TREE=repo.git/work\n+\texport GIT_DIR GIT_WORK_TREE\n+\ttest-subprocess rev-parse --show-toplevel >actual &&\n+\techo \"`pwd`/repo.git/work\" >expected &&\n+\ttest_cmp expected actual\n+\t)\n+'\n+\n test_done\ndiff --git a/test-subprocess.c b/test-subprocess.c\nnew file mode 100644\nindex 0000000..55ad719\n--- /dev/null\n+++ b/test-subprocess.c\n@@ -0,0 +1,18 @@\n+#include \"cache.h\"\n+#include \"run-command.h\"\n+\n+int main(int argc, char **argv)\n+{\n+\tconst char *prefix;\n+\tstruct child_process cp;\n+\tint nogit;\n+\n+\tprefix = setup_git_directory_gently(&nogit);\n+\tif (nogit)\n+\t\tdie(\"No git repo found\");\n+\tsetup_work_tree();\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.git_cmd = 1;\n+\tcp.argv = (const char **)argv+1;\n+\treturn run_command(&cp);\n+}\n-- \n1.7.3.4.878.g439c7\n"},{"id":"158578","messageId":"m2aajudjqt.fsf@whitebox.home","threadId":"26137","inReplyTo":"1293285457-11915-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH 1/2] t1501: avoid bashisms","fromName":"Andreas Schwab","fromEmail":"schwab@linux-m68k.org","sentAt":"2010-12-25T14:12:26Z","receivedAt":"2010-12-25T14:12:26Z","isPatch":true,"sender":{"key":"schwab@linux-m68k.org","avatar":"https://avatars.githubusercontent.com/u/2175493?v=4"},"body":"Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:\n\n> @@ -322,7 +322,10 @@ test_expect_success 'git grep' '\n>  test_expect_success 'git commit' '\n>  \t(\n>  \t\tcd repo.git &&\n> -\t\tGIT_DIR=. GIT_WORK_TREE=work git commit -a -m done\n\nIn which way is that not portable?\n\nAndreas.\n\n-- \nAndreas Schwab, schwab@linux-m68k.org\nGPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5\n\"And now for something completely different.\"\n"},{"id":"158579","messageId":"AANLkTinhrBf14446rJBZSYWTSH6sxOchMcgBsrq8DoLO@mail.gmail.com","threadId":"26137","inReplyTo":"m2aajudjqt.fsf@whitebox.home","subject":"Re: [PATCH 1/2] t1501: avoid bashisms","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-25T14:29:20Z","receivedAt":"2010-12-25T14:29:20Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2010/12/25 Andreas Schwab <schwab@linux-m68k.org>:\n> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:\n>\n>> @@ -322,7 +322,10 @@ test_expect_success 'git grep' '\n>>  test_expect_success 'git commit' '\n>>       (\n>>               cd repo.git &&\n>> -             GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done\n>\n> In which way is that not portable?\n\nI admit that I rarely leave bash, so I'll quote Johannes answer [1]\n\n-- 8< --\nSure, it is (bashisms). This:\n\n   GIT_DIR=\"$TRASH_DIRECTORY/2/.git\" test_repo 2/sub\n\ndoes not work the same way in all shells when test_repo is a shell\nfunction. You have to export GIT_DIR explicitly before the function call.\n(But since in this case, test_repo invokes its own subshell anyway, you\nbetter do it in the function.)\n-- 8< --\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/162207\n-- \nDuy\n"},{"id":"158580","messageId":"AANLkTi=fCa4a-POoMWqnraFjz28-Ko4Yp0fcar+d7AsE@mail.gmail.com","threadId":"26137","inReplyTo":"AANLkTinhrBf14446rJBZSYWTSH6sxOchMcgBsrq8DoLO@mail.gmail.com","subject":"Re: [PATCH 1/2] t1501: avoid bashisms","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-25T14:44:25Z","receivedAt":"2010-12-25T14:44:25Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Dec 25, 2010 at 9:29 PM, Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:\n> 2010/12/25 Andreas Schwab <schwab@linux-m68k.org>:\n>> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:\n>>\n>>> @@ -322,7 +322,10 @@ test_expect_success 'git grep' '\n>>>  test_expect_success 'git commit' '\n>>>       (\n>>>               cd repo.git &&\n>>> -             GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done\n>>\n>> In which way is that not portable?\n>\n> I admit that I rarely leave bash, so I'll quote Johannes answer [1]\n>\n> -- 8< --\n> Sure, it is (bashisms). This:\n>\n>   GIT_DIR=\"$TRASH_DIRECTORY/2/.git\" test_repo 2/sub\n>\n> does not work the same way in all shells when test_repo is a shell\n> function. You have to export GIT_DIR explicitly before the function call.\n\nHmm.. I misread it. That's only for shell _functions_. [1] says\nvariable assignments for simple commands are actually exported.\n\nJunio, please don't pick up this patch.\n\n[1] http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html\n-- \nDuy\n"},{"id":"158590","messageId":"1293364014-8463-1-git-send-email-pclouds@gmail.com","threadId":"26137","inReplyTo":"1293285457-11915-2-git-send-email-pclouds@gmail.com","subject":"[PATCH] setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwd","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-26T11:46:53Z","receivedAt":"2010-12-26T11:46:53Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When setup_work_tree() is called, it moves cwd to $GIT_WORK_TREE and\nmakes internal copy of $GIT_WORK_TREE absolute. The environt variable,\nif set by user, remains unchanged. If the variable is relative, it is\nno longer correct because its base dir has changed.\n\nInstead of making $GIT_WORK_TREE absolute too, we just say \".\" and let\nsubsequent git processes handle it.\n\nReported-by: Michel Briand <michelbriand@free.fr>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n This one is better, on top of master because the next one is on top\n of nd/setup.\n\n And I forgot to tell why I did not put the fault in real life [1]\n into tests: I don't like git-merge spawning another process just for\n resetting worktree. Sooner or later it should be replaced to use\n unpack_trees() directly. When that happens, the test would become\n invalid.\n\n [1] http://thread.gmane.org/gmane.comp.version-control.git/164066/focus=164171\n\n .gitignore          |    1 +\n Makefile            |    1 +\n setup.c             |    8 ++++++++\n t/t1501-worktree.sh |   11 +++++++++++\n test-subprocess.c   |   21 +++++++++++++++++++++\n 5 files changed, 42 insertions(+), 0 deletions(-)\n create mode 100644 test-subprocess.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 87b833c..3dd6ef7 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -177,6 +177,7 @@\n /test-sha1\n /test-sigchain\n /test-string-pool\n+/test-subprocess\n /test-svn-fe\n /test-treap\n /common-cmds.h\ndiff --git a/Makefile b/Makefile\nindex 57d9c65..bdf86a3 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -431,6 +431,7 @@ TEST_PROGRAMS_NEED_X += test-run-command\n TEST_PROGRAMS_NEED_X += test-sha1\n TEST_PROGRAMS_NEED_X += test-sigchain\n TEST_PROGRAMS_NEED_X += test-string-pool\n+TEST_PROGRAMS_NEED_X += test-subprocess\n TEST_PROGRAMS_NEED_X += test-svn-fe\n TEST_PROGRAMS_NEED_X += test-treap\n TEST_PROGRAMS_NEED_X += test-index-version\ndiff --git a/setup.c b/setup.c\nindex 91887a4..3833569 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -239,6 +239,14 @@ void setup_work_tree(void)\n \t\tgit_dir = make_absolute_path(git_dir);\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n+\n+\t/*\n+\t * Make sure subsequent git processes find correct worktree\n+\t * if $GIT_WORK_TREE is set relative\n+\t */\n+\tif (getenv(GIT_WORK_TREE_ENVIRONMENT))\n+\t\tsetenv(GIT_WORK_TREE_ENVIRONMENT, \".\", 1);\n+\n \tset_git_dir(make_relative_path(git_dir, work_tree));\n \tinitialized = 1;\n }\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 2c8f01f..1f3b50d 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -340,4 +340,15 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '\n \tgit --git-dir=\"$(pwd)//repo.git\" --work-tree=\"$(pwd)\" add dummy_file\n '\n \n+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '\n+\t(\n+\tGIT_DIR=repo.git &&\n+\tGIT_WORK_TREE=repo.git/work &&\n+\texport GIT_DIR GIT_WORK_TREE &&\n+\ttest-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&\n+\techo \"`pwd`/repo.git/work\" >expected &&\n+\ttest_cmp expected actual\n+\t)\n+'\n+\n test_done\ndiff --git a/test-subprocess.c b/test-subprocess.c\nnew file mode 100644\nindex 0000000..667d3e5\n--- /dev/null\n+++ b/test-subprocess.c\n@@ -0,0 +1,21 @@\n+#include \"cache.h\"\n+#include \"run-command.h\"\n+\n+int main(int argc, char **argv)\n+{\n+\tconst char *prefix;\n+\tstruct child_process cp;\n+\tint nogit = 0;\n+\n+\tprefix = setup_git_directory_gently(&nogit);\n+\tif (nogit)\n+\t\tdie(\"No git repo found\");\n+\tif (!strcmp(argv[1], \"--setup-work-tree\")) {\n+\t\tsetup_work_tree();\n+\t\targv++;\n+\t}\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.git_cmd = 1;\n+\tcp.argv = (const char **)argv+1;\n+\treturn run_command(&cp);\n+}\n-- \n1.7.3.4.878.g439c7\n"},{"id":"158589","messageId":"1293364014-8463-2-git-send-email-pclouds@gmail.com","threadId":"26137","inReplyTo":"1293364014-8463-1-git-send-email-pclouds@gmail.com","subject":"[PATCH] setup_explicit_git_dir: adjust relative $GIT_WORK_TREE after moving cwd","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-26T11:46:54Z","receivedAt":"2010-12-26T11:46:54Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"setup_explicit_git_dir() can move cwd. If $GIT_WORK_TREE is relative\nto original cwd, then the subsequent git processes will take wrong\nworktree.\n\nInstead of making $GIT_WORK_TREE absolute too, we just say \".\" and let\nsubsequent git processes handle it.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n This one is on top of nd/setup. If nd/setup does not make it in the\n next release, I can still make similar patch for master, but I don't\n think this happens in real life, therefore not really urgent to fix.\n\n There are only few cases where git spawns more git processes.\n For those that do that, setup_work_tree() is likely called by git.c\n already, which is what the first patch is for.\n\n setup.c             |    7 +++++++\n t/t1501-worktree.sh |   12 ++++++++++++\n 2 files changed, 19 insertions(+), 0 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 3d73269..10b8f16 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -392,6 +392,13 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,\n \t\tset_git_dir(make_absolute_path(gitdirenv));\n \t\tif (chdir(worktree))\n \t\t\tdie_errno(\"Could not chdir to '%s'\", worktree);\n+\t\t/*\n+\t\t * Make sure subsequent git processes find correct worktree\n+\t\t * if $GIT_WORK_TREE is set relative\n+\t\t */\n+\t\tif (work_tree_env)\n+\t\t\tsetenv(GIT_WORK_TREE_ENVIRONMENT, \".\", 1);\n+\n \t\tcwd[len++] = '/';\n \t\tcwd[len] = '\\0';\n \t\tfree(gitfile);\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 1f3b50d..fa35c3e 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -351,4 +351,16 @@ test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '\n \t)\n '\n \n+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses (2)' '\n+\t(\n+\tcd repo.git/work/sub &&\n+\tGIT_DIR=../.. &&\n+\tGIT_WORK_TREE=.. &&\n+\texport GIT_DIR GIT_WORK_TREE &&\n+\ttest-subprocess rev-parse --show-toplevel >actual &&\n+\techo \"$TRASH_DIRECTORY/repo.git/work\" >expected &&\n+\ttest_cmp expected actual\n+\t)\n+'\n+\n test_done\n-- \n1.7.3.4.878.g439c7\n"},{"id":"158597","messageId":"7v4oa0z6ck.fsf@alter.siamese.dyndns.org","threadId":"26137","inReplyTo":"AANLkTi=fCa4a-POoMWqnraFjz28-Ko4Yp0fcar+d7AsE@mail.gmail.com","subject":"Re: [PATCH 1/2] t1501: avoid bashisms","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-12-26T19:23:07Z","receivedAt":"2010-12-26T19:23:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:\n\n> Junio, please don't pick up this patch.\n\nWon't.  And [2/2] needs to be updated with something like this?\n\n t/t1501-worktree.sh |    8 ++------\n 1 files changed, 2 insertions(+), 6 deletions(-)\n\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 1429a65..8cab065 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -341,14 +341,10 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '\n '\n \n test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '\n-\t(\n-\tGIT_DIR=repo.git\n-\tGIT_WORK_TREE=repo.git/work\n-\texport GIT_DIR GIT_WORK_TREE\n+\tGIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \\\n \ttest-subprocess rev-parse --show-toplevel >actual &&\n-\techo \"`pwd`/repo.git/work\" >expected &&\n+\techo \"$(pwd)/repo.git/work\" >expected &&\n \ttest_cmp expected actual\n-\t)\n '\n \n test_done\n"},{"id":"158606","messageId":"1293413164-14684-1-git-send-email-pclouds@gmail.com","threadId":"26137","inReplyTo":"1293364014-8463-1-git-send-email-pclouds@gmail.com","subject":"[PATCH] setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwd","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-12-27T01:26:04Z","receivedAt":"2010-12-27T01:26:04Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When setup_work_tree() is called, it moves cwd to $GIT_WORK_TREE and\nmakes internal copy of $GIT_WORK_TREE absolute. The environt variable,\nif set by user, remains unchanged. If the variable is relative, it is\nno longer correct because its base dir has changed.\n\nInstead of making $GIT_WORK_TREE absolute too, we just say \".\" and let\nsubsequent git processes handle it.\n\nReported-by: Michel Briand <michelbriand@free.fr>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Third round. Remove subshell and `pwd` in t1501.31\n\n .gitignore          |    1 +\n Makefile            |    1 +\n setup.c             |    8 ++++++++\n t/t1501-worktree.sh |    7 +++++++\n test-subprocess.c   |   21 +++++++++++++++++++++\n 5 files changed, 38 insertions(+), 0 deletions(-)\n create mode 100644 test-subprocess.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 87b833c..3dd6ef7 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -177,6 +177,7 @@\n /test-sha1\n /test-sigchain\n /test-string-pool\n+/test-subprocess\n /test-svn-fe\n /test-treap\n /common-cmds.h\ndiff --git a/Makefile b/Makefile\nindex 57d9c65..bdf86a3 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -431,6 +431,7 @@ TEST_PROGRAMS_NEED_X += test-run-command\n TEST_PROGRAMS_NEED_X += test-sha1\n TEST_PROGRAMS_NEED_X += test-sigchain\n TEST_PROGRAMS_NEED_X += test-string-pool\n+TEST_PROGRAMS_NEED_X += test-subprocess\n TEST_PROGRAMS_NEED_X += test-svn-fe\n TEST_PROGRAMS_NEED_X += test-treap\n TEST_PROGRAMS_NEED_X += test-index-version\ndiff --git a/setup.c b/setup.c\nindex 91887a4..3833569 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -239,6 +239,14 @@ void setup_work_tree(void)\n \t\tgit_dir = make_absolute_path(git_dir);\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n+\n+\t/*\n+\t * Make sure subsequent git processes find correct worktree\n+\t * if $GIT_WORK_TREE is set relative\n+\t */\n+\tif (getenv(GIT_WORK_TREE_ENVIRONMENT))\n+\t\tsetenv(GIT_WORK_TREE_ENVIRONMENT, \".\", 1);\n+\n \tset_git_dir(make_relative_path(git_dir, work_tree));\n \tinitialized = 1;\n }\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 2c8f01f..f072a8e 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -340,4 +340,11 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '\n \tgit --git-dir=\"$(pwd)//repo.git\" --work-tree=\"$(pwd)\" add dummy_file\n '\n \n+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '\n+\tGIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \\\n+\ttest-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&\n+\techo \"$TRASH_DIRECTORY/repo.git/work\" >expected &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\ndiff --git a/test-subprocess.c b/test-subprocess.c\nnew file mode 100644\nindex 0000000..667d3e5\n--- /dev/null\n+++ b/test-subprocess.c\n@@ -0,0 +1,21 @@\n+#include \"cache.h\"\n+#include \"run-command.h\"\n+\n+int main(int argc, char **argv)\n+{\n+\tconst char *prefix;\n+\tstruct child_process cp;\n+\tint nogit = 0;\n+\n+\tprefix = setup_git_directory_gently(&nogit);\n+\tif (nogit)\n+\t\tdie(\"No git repo found\");\n+\tif (!strcmp(argv[1], \"--setup-work-tree\")) {\n+\t\tsetup_work_tree();\n+\t\targv++;\n+\t}\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.git_cmd = 1;\n+\tcp.argv = (const char **)argv+1;\n+\treturn run_command(&cp);\n+}\n-- \n1.7.3.4.878.g439c7\n"}]}