Skip to content
  • Duy Nguyen's avatar
    setup.c: re-fix d95138e6 (setup: set env $GIT_WORK_TREE when .. · 86d26f24
    Duy Nguyen authored and Junio C Hamano's avatar Junio C Hamano committed
    Commit d95138e6 [1] attempted to fix a .git file problem by
    setting GIT_WORK_TREE whenever GIT_DIR is set. It sounded harmless
    because we handle GIT_DIR and GIT_WORK_TREE side by side for most
    commands, with two exceptions: git-init and git-clone.
    
    "git clone" is not happy with d95138e6. This command ignores GIT_DIR
    but respects GIT_WORK_TREE [2] [3] which means it used to run fine
    from a hook, where GIT_DIR was set but GIT_WORK_TREE was not (*).
    With d95138e6, GIT_WORK_TREE is set all the time and git-clone
    interprets that as "I give you order to put the worktree here",
    usually against the user's intention.
    
    The solution in d95138e6 is reverted earlier, and instead we reuse
    the solution from c0562611 [4].  It fixed another setup-messed-
    up-by-alias by saving and restoring env and spawning a new process,
    but for git-clone and git-init only.
    
    Now we conclude that setup-messed-up-by-alias is always evil. So the
    env restoration is done for _all_ commands, including external ones,
    whenever aliases are involved. It fixes what d95138e6 tried to fix,
    without upsetting git-clone-inside-hooks.
    
    The test from d95138e6 remains to verify it's not broken by this. A new
    test is added to make sure git-clone-inside-hooks remains happy.
    
    (*) GIT_WORK_TREE was not set _most of the time_. In some cases
        GIT_WORK_TREE is set and git-clone will behave differently. The
        use of GIT_WORK_TREE to direct git-clone to put work tree
        elsewhere looks like a mistake because it causes surprises this
        way. But that's a separate story.
    
    [1] d95138e6 (setup: set env $GIT_WORK_TREE when work tree is set, like
                 $GIT_DIR - 2015-06-26)
    [2] 2beebd22 (clone: create intermediate directories of destination
                 repo - 2008-06-25)
    [3] 20ccef49 (make git-clone GIT_WORK_TREE aware - 2007-07-06)
    [4] c0562611
    
     (git potty: restore environments after alias expansion -
                 2014-06-08)
    
    Reported-by: default avatarAnthony Sottile <asottile@umich.edu>
    Signed-off-by: default avatarNguyễn Thái Ngọc Duy <pclouds@gmail.com>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    86d26f24