Skip to content
  • Duy Nguyen's avatar
    setup: set env $GIT_WORK_TREE when work tree is set, like $GIT_DIR · d95138e6
    Duy Nguyen authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    In the test case, we run setup_git_dir_gently() the first time to read
    $GIT_DIR/config so that we can resolve aliases. We'll enter
    setup_discovered_git_dir() and may or may not call set_git_dir() near
    the end of the function, depending on whether the detected git dir is
    ".git" or not. This set_git_dir() will set env var $GIT_DIR.
    
    For normal repo, git dir detected via setup_discovered_git_dir() will be
    ".git", and set_git_dir() is not called. If .git file is used however,
    the git dir can't be ".git" and set_git_dir() is called and $GIT_DIR
    set. This is the key of this problem.
    
    If we expand an alias (or autocorrect command names), then
    setup_git_dir_gently() is run the second time. If $GIT_DIR is not set in
    the first run, we run the same setup_discovered_git_dir() as before.
    Nothing to see. If it is, however, we'll enter setup_explicit_git_dir()
    this time.
    
    This is where the "fun" is.  If $GIT_WORK_TREE is not set but
    $GIT_DIR is, you are supposed to be at the root level of the
    worktree.  But if you are in a subdir "foo/bar" (real worktree's top
    is "foo"), this rule bites you: your detected worktree is now
    "foo/bar", even though the first run correctly detected worktree as
    "foo". You get "internal error: work tree has already been set" as a
    result.
    
    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.
    
    Reported-by: default avatarBjørnar Snoksrud <snoksrud@gmail.com>
    Signed-off-by: default avatarNguyễn Thái Ngọc Duy <pclouds@gmail.com>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    d95138e6