git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: Preston Tunnell Wilson <prestontunnellwilson@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: Bug report between git hooks and git worktree
Date: Sun, 8 Jan 2023 01:32:52 -0500	[thread overview]
Message-ID: <CAPig+cScBFJCBN97g12v9Eg1-7+m8_EzAnpT2uSSdzTNwqjjVQ@mail.gmail.com> (raw)
In-Reply-To: <CAC-j02OVoFDFjeGUjcs0ZMwPSXKLL_GBme2m2-ttzGTbxXNP-w@mail.gmail.com>

On Sat, Jan 7, 2023 at 2:20 PM Preston Tunnell Wilson
<prestontunnellwilson@gmail.com> 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@mail.gmail.com/
[2]: https://lore.kernel.org/git/CAJFQqN+Z9eX6onaj8vVSqpvf-nOC7-Y0Un4NLUie6x6bGfmvZA@mail.gmail.com/
[3]: https://lore.kernel.org/git/20190516221702.GA11784@sigill.intra.peff.net/

      parent reply	other threads:[~2023-01-08  6:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-07 18:59 Bug report between git hooks and git worktree Preston Tunnell Wilson
2023-01-07 19:29 ` Preston Tunnell Wilson
2023-01-08  6:32 ` Eric Sunshine [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPig+cScBFJCBN97g12v9Eg1-7+m8_EzAnpT2uSSdzTNwqjjVQ@mail.gmail.com \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=prestontunnellwilson@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).