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

Re: [bug report] git diff --relative not doing well with worktree in hooks

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jun 3, 2024, 21:59 UTC
Message-ID
<CAPig+cSzY1YO6KByb3fG0P1jWHbEu_a3s_7QBieDoD6un-MJXQ@mail.gmail.com>
In-Reply-To
<e0ea930f-3d43-49de-b2e7-c057d95fd7cc@gmail.com>
On Mon, Jun 3, 2024 at 5:30 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 19 quoted lines
> On 31/05/2024 22:42, Eric Sunshine wrote:
> > I'm not sure there's a satisfactory resolution here. Your hook is
> > running afoul of the environment variables Git sets up when the hook
> > is run outside of the "main" worktree.
> > [...]
> > The relevant portion from the "githooks" manual page is:
> >
> >      Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
> >      exported so that Git commands run by the hook can correctly locate
> >      the repository. If your hook needs to invoke Git commands in a
> >      foreign repository or in a different working tree of the same
> >      repository, then it should clear these environment variables so
> >      they do not interfere with Git operations at the foreign
> >      location. For example:
>
> Maybe I'm missing something but in Antonine's example the hook is being
> run in the same worktree as the "git commit" - we're changing into a
> subdirectory of the worktree, not changing to a different worktree so
> why doesn't it work?

It's been a while since I looked at the code, but my recollection is that the hook-running machinery unconditionally sets the environment variables whenever the directory in which the hook is being run is not the "main" worktree. This is the case whether his hook runs at the root of the worktree or in a subdirectory.

It's quite possible that this behavior is entirely accidental since the hook-running machinery existed long before multiple-worktree support was added, and it may be that the hook-running machinery simply wasn't revisited when worktree support was implemented. So, perhaps a "satisfactory resolution" is to "fix" the hook-running machinery itself to avoid setting those environment variables unnecessarily.

Previous: Phillip Wood
Message 5 of 5 in “[bug report] git diff --relative not doing well with worktree in hooks”
  1. Antoine BolvyMay 31, 2024
  2. Eric SunshineMay 31, 2024
  3. Antoine BolvyJun 3, 2024
  4. Phillip WoodJun 3, 2024
  5. Eric SunshineJun 3, 2024

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.