git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Problem with environment of hook execution when git is run with --work-tree / --git-dir

From
MSMichael Sloan <mgsloan@gmail.com>
Date
Nov 26, 2017, 07:19 UTC
Message-ID
<CAEDDsWdXQ1+UukvbfRoTPzY3Y9sOaxQ7nh+qL_Mcuy3=XKKh7w@mail.gmail.com>
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

Next: Junio C Hamano
Message 1 of 3 in “Problem with environment of hook execution when git is run with --work-tree / --git-dir”
  1. Michael SloanNov 26, 2017
  2. Junio C HamanoNov 27, 2017
  3. Michael SloanNov 27, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.