Thread (4 messages) flat view 4 messages, 3 authors, 2016-06-15

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

From: Jeff King <hidden>
Date: 2016-06-15 23:05:32

On Fri, Jun 26, 2015 at 05:37:35PM +0700, Nguyễn Thái Ngọc Duy wrote:
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. :)
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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help