git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Bug report between git hooks and git worktree
@ 2023-01-07 18:59 Preston Tunnell Wilson
  2023-01-07 19:29 ` Preston Tunnell Wilson
  2023-01-08  6:32 ` Eric Sunshine
  0 siblings, 2 replies; 3+ messages in thread
From: Preston Tunnell Wilson @ 2023-01-07 18:59 UTC (permalink / raw)
  To: git

Hello!

I ran into a strange issue using git-worktree with git-hooks. I have
attached the output of `git bugreport` below. More concrete steps to
reproduce are available in
https://github.com/prestontw/git_hook-git_worktree-minimal-repo. I've
run into this both in macOS and Fedora---I've included the system
information from both of these systems below.

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`.
(This might sound weird, but `flutter` distributes itself through a `git`
folder in some circumstances and uses `git` commands to see which version
the user has installed, etc.).

Add a git worktree branch/folder, `cd` to it, and commit something there.
Let's call this git worktree folder `FolderW`.

(Steps are outlined in more detail with an example in
https://github.com/prestontw/git_hook-git_worktree-minimal-repo.)

What did you expect to happen? (Expected behavior)

The git command would report details from `FolderA`.

What happened instead? (Actual behavior)

The git command outputs details from `FolderW`.

What's different between what you expected and what actually happened?

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.

Anything else you want to add:

I'm seeing this both in macOS and Fedora. I've added that system
info below.


[System Info]
git version:
git version 2.32.0 (Apple Git-132)
cpu: arm64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Darwin 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58
PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64
compiler info: clang: 13.1.6 (clang-1316.0.21.2)
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/zsh


[Enabled Hooks]
pre-commit


[System Info]
git version:
git version 2.37.3
cpu: aarch64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>
compiler info: gnuc: 12.2
libc info: glibc: 2.36
$SHELL (typically, interactive shell): /bin/bash


[Enabled Hooks]
pre-commit

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

* Re: Bug report between git hooks and git worktree
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Preston Tunnell Wilson @ 2023-01-07 19:29 UTC (permalink / raw)
  To: git

I tried this out on the `next` branch of `git` and still see the
error. The version I'm using is

```
[System Info]
git version:
git version 2.39.0.246.g2a6d74b583
cpu: aarch64
built from commit: 2a6d74b5839c13a139e2856e92b7bd8110056098
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>
compiler info: gnuc: 12.2
libc info: glibc: 2.36
$SHELL (typically, interactive shell): /bin/bash
```

On Sat, Jan 7, 2023 at 12:59 PM Preston Tunnell Wilson
<prestontunnellwilson@gmail.com> wrote:
>
> Hello!
>
> I ran into a strange issue using git-worktree with git-hooks. I have
> attached the output of `git bugreport` below. More concrete steps to
> reproduce are available in
> https://github.com/prestontw/git_hook-git_worktree-minimal-repo. I've
> run into this both in macOS and Fedora---I've included the system
> information from both of these systems below.
>
> 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`.
> (This might sound weird, but `flutter` distributes itself through a `git`
> folder in some circumstances and uses `git` commands to see which version
> the user has installed, etc.).
>
> Add a git worktree branch/folder, `cd` to it, and commit something there.
> Let's call this git worktree folder `FolderW`.
>
> (Steps are outlined in more detail with an example in
> https://github.com/prestontw/git_hook-git_worktree-minimal-repo.)
>
> What did you expect to happen? (Expected behavior)
>
> The git command would report details from `FolderA`.
>
> What happened instead? (Actual behavior)
>
> The git command outputs details from `FolderW`.
>
> What's different between what you expected and what actually happened?
>
> 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.
>
> Anything else you want to add:
>
> I'm seeing this both in macOS and Fedora. I've added that system
> info below.
>
>
> [System Info]
> git version:
> git version 2.32.0 (Apple Git-132)
> cpu: arm64
> no commit associated with this build
> sizeof-long: 8
> sizeof-size_t: 8
> shell-path: /bin/sh
> uname: Darwin 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58
> PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64
> compiler info: clang: 13.1.6 (clang-1316.0.21.2)
> libc info: no libc information available
> $SHELL (typically, interactive shell): /bin/zsh
>
>
> [Enabled Hooks]
> pre-commit
>
>
> [System Info]
> git version:
> git version 2.37.3
> cpu: aarch64
> no commit associated with this build
> sizeof-long: 8
> sizeof-size_t: 8
> shell-path: /bin/sh
> uname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>
> compiler info: gnuc: 12.2
> libc info: glibc: 2.36
> $SHELL (typically, interactive shell): /bin/bash
>
>
> [Enabled Hooks]
> pre-commit

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

* Re: Bug report between git hooks and git worktree
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Sunshine @ 2023-01-08  6:32 UTC (permalink / raw)
  To: Preston Tunnell Wilson; +Cc: git

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/

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

end of thread, other threads:[~2023-01-08  6:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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

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).