git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Problem with environment of hook execution when git is run with --work-tree / --git-dir
@ 2017-11-26  7:19 Michael Sloan
  2017-11-27  1:16 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Sloan @ 2017-11-26  7:19 UTC (permalink / raw)
  To: git

Hi!

I noticed a potential bug with the invocation of a pre-commit hook
when running git with --work-tree and --git-dir.  In particular, I was
investigating how hooks can still run git commands properly even when
the work-tree or git-dir is overridden via CLI args.  I put the
following in "/home/mgsloan/.dotfiles-git/hooks/pre-commit":

    #!/bin/sh
    env

after this, running "git --work-tree=/home/mgsloan
--git-dir=/home/mgsloan/.dotfiles-git commit" has output with a bunch
of variables, here are the important ones:

    GIT_WORK_TREE=.
    GIT_DIR=.dotfiles-git/
    PWD=/home/mgsloan

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'

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.  Note that this also means that changing PWD in a
script can change which git repo the command is being run on, for
example, when the hook is interacting with a submodule.

A half-fix to this would be to have the GIT_WORK_TREE and GIT_DIR set
when running hooks use absolute paths.  However, this would not have
the same behavior as when git is used without --git-dir / --work-tree.
As described in the paragraph above, if PWD is relied upon to instead
target a different git repo, then things break.

Not sure what the total fix for this would be.   I think the
information that needs to be conveyed to the hook's git invocations is
that "the work-tree /home/mgsloan should be associated with the
git-dir /home/mgsloan/.dotfiles-git".  Could have an env var like

    GIT_DIR_MAPPINGS="/home/mgsloan!/home/mgsloan/.dotfiles-git"

The idea is that this would be a list of mappings from GIT_WORK_TREE
to GIT_DIR.  If this variable is set, then it will be followed when
git is searching parents of PWD for ".git" directories.  I chose "!"
rather arbitrarily here.  "->" would look nicer, but people might
forget to escape it when programmatically setting this var.

What do y'all think of this idea?

Some of you might be wondering what I'm doing with my work tree being
my home directory.  It is the approach suggested here -
https://developer.atlassian.com/blog/2016/02/best-way-to-store-dotfiles-git-bare-repo/
- for versioning your configuration files directly.

Apologies if this has already been discussed, I could not find a good
way to search the mailinglist archives.

Thanks!
-Michael

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with environment of hook execution when git is run with --work-tree / --git-dir
  2017-11-26  7:19 Problem with environment of hook execution when git is run with --work-tree / --git-dir Michael Sloan
@ 2017-11-27  1:16 ` Junio C Hamano
  2017-11-27  1:54   ` Michael Sloan
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2017-11-27  1:16 UTC (permalink / raw)
  To: Michael Sloan; +Cc: git

Michael Sloan <mgsloan@gmail.com> 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with environment of hook execution when git is run with --work-tree / --git-dir
  2017-11-27  1:16 ` Junio C Hamano
@ 2017-11-27  1:54   ` Michael Sloan
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Sloan @ 2017-11-27  1:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Sun, Nov 26, 2017 at 5:16 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Michael Sloan <mgsloan@gmail.com> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-11-27  1:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-26  7:19 Problem with environment of hook execution when git is run with --work-tree / --git-dir Michael Sloan
2017-11-27  1:16 ` Junio C Hamano
2017-11-27  1:54   ` Michael Sloan

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).