git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] setup: set env $GIT_WORK_TREE when work tree is set, like $GIT_DIR

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 26, 2015, 17:15 UTC
Message-ID
<xmqqpp4iv1au.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1435315055-27011-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 6 quoted lines
> Bottom line is, when $GIT_DIR is set, $GIT_WORK_TREE should be set too
> unless there's no work tree. But setting $GIT_WORK_TREE inside
> set_git_dir() may backfire. We don't know at that point if work tree is
> already configured by the caller. So set it when work tree is
> detected. It does not harm if $GIT_WORK_TREE is set while $GIT_DIR is
> not.

Hmmm, setting GIT_WORK_TREE locally without exporting it to our subprocesses would not hurt even when we do not see GIT_DIR, so I agree "It does not harm" is true for _us_, the process that runs this code itself.

But I am not sure if it is true for our children (e.g. hooks, filters etc. that is spawned by us). With this change, they inherit GIT_WORK_TREE and no GIT_DIR, in such a case. If they set GIT_DIR themselves for their own use, perhaps arranging to work in somewhere else they know by chdir'ing there, they did not have to set GIT_WORK_TREE=. before runing git in there, but now they do, because we start exporting GIT_WORK_TREE to interfere what they have been doing.

"It does not harm" is probably false for our children, I would think. The children can do new things to avoid the harm, though.

Show 46 quoted lines
> Reported-by: Bjørnar Snoksrud <snoksrud@gmail.com>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  environment.c      |  2 ++
>  t/t0002-gitfile.sh | 17 +++++++++++++++++
>  2 files changed, 19 insertions(+)
>
> diff --git a/environment.c b/environment.c
> index 61c685b..8f1b249 100644
> --- a/environment.c
> +++ b/environment.c
> @@ -231,6 +231,8 @@ void set_git_work_tree(const char *new_work_tree)
>  	}
>  	git_work_tree_initialized = 1;
>  	work_tree = xstrdup(real_path(new_work_tree));
> +	if (setenv(GIT_WORK_TREE_ENVIRONMENT, work_tree, 1))
> +		error("Could not set GIT_WORK_TREE to '%s'", work_tree);
>  }
>  
>  const char *get_git_work_tree(void)
> diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
> index 37e9396..9393322 100755
> --- a/t/t0002-gitfile.sh
> +++ b/t/t0002-gitfile.sh
> @@ -99,4 +99,21 @@ test_expect_success 'check rev-list' '
>  	test "$SHA" = "$(git rev-list HEAD)"
>  '
>  
> +test_expect_success 'setup_git_dir twice in subdir' '
> +	git init sgd &&
> +	(
> +		cd sgd &&
> +		git config alias.lsfi ls-files &&
> +		mv .git .realgit &&
> +		echo "gitdir: .realgit" >.git &&
> +		mkdir subdir &&
> +		cd subdir &&
> +		>foo &&
> +		git add foo &&
> +		git lsfi >actual &&
> +		echo foo >expected &&
> +		test_cmp expected actual
> +	)
> +'
> +
>  test_done
Previous: Duy NguyenNext: Junio C Hamano
Message 5 of 7 in “Linked workdirs break typo-correction.”
  1. Bjørnar SnoksrudJun 25, 2015
  2. setup: set env $GIT_WORK_TREE when work tree is set, like $GIT_DIRNguyễn Thái Ngọc Duy, Jun 26, 2015
  3. Jeff KingJun 26, 2015
  4. Duy NguyenJun 27, 2015
  5. Junio C HamanoJun 26, 2015
  6. Junio C HamanoJun 26, 2015
  7. Junio C HamanoJun 26, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.