Re: Bug report between git hooks and git worktree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Jan 7, 2023 at 2:20 PM Preston Tunnell Wilson
<prestontunnellwilson@xxxxxxxxx> wrote:
> What did you do before the bug happened? (Steps to reproduce your issue)
>
> Add a git hook that executes git commands inside of another folder. Let's
> call this other folder `FolderA`.
> Add a git worktree branch/folder, `cd` to it, and commit something there.
> Let's call this git worktree folder `FolderW`.
>
> I would expect the git command to output details from `FolderA`.
> It's confusing to me that there is a difference in behavior between
> the "main" branch and `FolderW` in git hooks.

This issue comes up from time to time[1][2]. What is happening is that
Git is setting environment variables pointing at the original
repository, so even though you invoke a Git command in a different
repository, it's picking up the environment variables and consulting
the original repository instead. When writing hooks which invoke Git
commands in a foreign repository, the "best practice" approach is to
clear the environment variables before running the Git command in the
other repository[3]. If you add this to the top of your hook script,
it works as expected:

    unset $(git rev-parse --local-env-vars)

Unfortunately, this "fix" isn't actually documented anywhere.

[1]: https://lore.kernel.org/git/CAPig+cQEC6CAV-Es9Ok96s8Cj=Dj76PRyOt4qKQus+rppswuyA@xxxxxxxxxxxxxx/
[2]: https://lore.kernel.org/git/CAJFQqN+Z9eX6onaj8vVSqpvf-nOC7-Y0Un4NLUie6x6bGfmvZA@xxxxxxxxxxxxxx/
[3]: https://lore.kernel.org/git/20190516221702.GA11784@xxxxxxxxxxxxxxxxxxxxx/



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux