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
Jeff King <peff@peff.net>
Date
Jun 26, 2015, 11:56 UTC
Message-ID
<20150626115603.GB4315@peff.net>
In-Reply-To
<1435315055-27011-1-git-send-email-pclouds@gmail.com>
On Fri, Jun 26, 2015 at 05:37:35PM +0700, Nguyễn Thái Ngọc Duy wrote:
Show 7 quoted lines
> This is where the "fun" is. The legacy behavior is, if $GIT_WORK_TREE is
> not set but $GIT_DIR is, cwd is chosen as worktree's top. If you happen
> to stand at worktree's top when you do this, all is well. If you are in
> a subdir "foo/bar" (real worktree's top is "foo"), this behavior bites
> you: your detected worktree is now "foo/bar", but the first run
> correctly detected worktree as "foo". You get "internal error: work tree
> has already been set" as a result.

I think this makes sense. I feel like we've dealt with this before, but the two previous rounds I found were basically:

  - we have GIT_IMPLICIT_WORK_TREE, but that is for the _opposite_ case.
    I.e., when we do not have a work tree and must communicate so to
    later code (including sub-processes).
  - a discussion about switching the "work tree defaults to '.' when
    $GIT_DIR is set" behavior yielded almost the identical patch:
      http://article.gmane.org/gmane.comp.version-control.git/219196
    but we were so wrapped up in the greater discussion we did not apply
    that simple fix. :)
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.

Yeah, it would be nicer if we could make sure we always set GIT_WORK_TREE along with GIT_DIR (since anything else is potentially dangerous), but I don't think it's feasible to do it in set_git_dir for the reasons you state.

I gave a quick peek through setup_explicit_git_dir to see if we missed other cases (spoiler: no):

  - if we have core.bare set, we set $GIT_DIR without setting the
    working tree. But that's OK, because we'll never look at
    $GIT_WORK_TREE in that case. Good.
  - if we have core.worktree set, then we call set_git_work_tree to
    match. Good.
  - If GIT_IMPLICIT_WORK_TREE is turned off, we set_git_dir but do not
    set GIT_WORK_TREE. Good, because otherwise it would take precedence.
  - there are some more calls to set_git_dir at the end, but I think in
    those cases we will already have set up the working tree (or decided
    we do not have one, by the above logic).

So that all seems fine. We also call set_git_dir from enter_repo, but in that case we have already moved into the .git directory itself, so that is OK. setup_discovered_git_dir also makes some calls, but either we are explicitly bare there, or we call set_git_work_tree(".").

So I do not see any cases that will regress, or that are uncovered. Of course, that is just from reading the code. Getting one million monkeys to bang on the keyboard may turn up any real problems. :)

> +	if (setenv(GIT_WORK_TREE_ENVIRONMENT, work_tree, 1))
> +		error("Could not set GIT_WORK_TREE to '%s'", work_tree);

Should this be die()? setenv() should basically never fail, but if it does, it could be confusing and/or dangerous to run without the variable set.

-Peff
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 3 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.