On Sun, Nov 26, 2017 at 5:16 PM, Junio C Hamano <gitster@xxxxxxxxx> wrote: > Michael Sloan <mgsloan@xxxxxxxxx> writes: > >> So what's the problem with this choice of environment variables? >> Well, the problem is that if PWD is changed during the execution of >> the script, then GIT_WORK_TREE and GIT_DIR will no longer work >> properly. For example, if the pre-commit hook is >> >> #!/bin/sh >> cd some-dir >> git status >> >> This will fail with >> >> Not a git repository: '.dotfiles-git' > > That is to be expected. It's up to the script to make them absolute > if it cannot cope with relative paths. > >> There is another detail here, which is that when --git-dir / >> --work-tree is not specified, the no GIT_WORK_TREE / GIT_DIR >> environment variable is set. This means that in this case, changing >> PWD in the hook will work fine as long as the search for .git will >> find the right one. > > That also is working as designed. Hmm, I do not think that this is good for the reliability of hooks. It means that hooks written with the common case assumption (no GIT_WORK_TREE / GIT_DIR set) will fail when run in these rarer configurations. I imagine that many authors of hooks are entirely unaware of work-tree / git-dir options. I know that I used git for something like 8 years before encountering a use for them, or really being aware. Perhaps hooks authors are savvier than your average user. It seems to me like something similar to my suggested GIT_DIR_MAPPINGS could be quite powerful for this circumstance as well as others. I guess a temporary hack would be to create a ".git" file that specifies the git-dir, but this doesn't work if something is already called ".git". I do not have a concrete example of this causing problems in practice, I've just observed that it is a potential gotcha for --git-dir + hooks. I can understand keeping things simple even if it means making it much harder for hooks authors to write correct code. It seems bothersome to me that git hooks have to be crafted so carefully to support variations in environment. If we could go back to when hooks were introduced and add a "GIT_IN_HOOK=1" and have it require manual specification of --work-tree and -git-dir, that might have been the best option. However, doing that now would break everyone's hooks, so not really practical. Also, thanks for all your work on git, overall it is excellent software :) -Michael