{"thread":{"id":"59045","subject":"Bug report between git hooks and git worktree","startedAt":"2023-01-07T19:00:05Z","lastAt":"2023-01-08T06:33:10Z","messageCount":3,"participants":["Preston Tunnell Wilson","Eric Sunshine"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"469884","messageId":"CAC-j02OVoFDFjeGUjcs0ZMwPSXKLL_GBme2m2-ttzGTbxXNP-w@mail.gmail.com","threadId":"59045","inReplyTo":null,"subject":"Bug report between git hooks and git worktree","fromName":"Preston Tunnell Wilson","fromEmail":"prestontunnellwilson@gmail.com","sentAt":"2023-01-07T18:59:46Z","receivedAt":"2023-01-07T19:00:05Z","isPatch":false,"sender":{"key":"prestontunnellwilson@gmail.com","avatar":null},"body":"Hello!\n\nI ran into a strange issue using git-worktree with git-hooks. I have\nattached the output of `git bugreport` below. More concrete steps to\nreproduce are available in\nhttps://github.com/prestontw/git_hook-git_worktree-minimal-repo. I've\nrun into this both in macOS and Fedora---I've included the system\ninformation from both of these systems below.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n\nAdd a git hook that executes git commands inside of another folder. Let's\ncall this other folder `FolderA`.\n(This might sound weird, but `flutter` distributes itself through a `git`\nfolder in some circumstances and uses `git` commands to see which version\nthe user has installed, etc.).\n\nAdd a git worktree branch/folder, `cd` to it, and commit something there.\nLet's call this git worktree folder `FolderW`.\n\n(Steps are outlined in more detail with an example in\nhttps://github.com/prestontw/git_hook-git_worktree-minimal-repo.)\n\nWhat did you expect to happen? (Expected behavior)\n\nThe git command would report details from `FolderA`.\n\nWhat happened instead? (Actual behavior)\n\nThe git command outputs details from `FolderW`.\n\nWhat's different between what you expected and what actually happened?\n\nI would expect the git command to output details from `FolderA`.\nIt's confusing to me that there is a difference in behavior between\nthe \"main\" branch and `FolderW` in git hooks.\n\nAnything else you want to add:\n\nI'm seeing this both in macOS and Fedora. I've added that system\ninfo below.\n\n\n[System Info]\ngit version:\ngit version 2.32.0 (Apple Git-132)\ncpu: arm64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Darwin 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58\nPST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64\ncompiler info: clang: 13.1.6 (clang-1316.0.21.2)\nlibc info: no libc information available\n$SHELL (typically, interactive shell): /bin/zsh\n\n\n[Enabled Hooks]\npre-commit\n\n\n[System Info]\ngit version:\ngit version 2.37.3\ncpu: aarch64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>\ncompiler info: gnuc: 12.2\nlibc info: glibc: 2.36\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\npre-commit\n"},{"id":"469885","messageId":"CAC-j02OGVx_=0WjHgf3CDSzC0MXYRcfNxf886JK9u7wdg-g8jQ@mail.gmail.com","threadId":"59045","inReplyTo":"CAC-j02OVoFDFjeGUjcs0ZMwPSXKLL_GBme2m2-ttzGTbxXNP-w@mail.gmail.com","subject":"Re: Bug report between git hooks and git worktree","fromName":"Preston Tunnell Wilson","fromEmail":"prestontunnellwilson@gmail.com","sentAt":"2023-01-07T19:29:15Z","receivedAt":"2023-01-07T19:37:35Z","isPatch":false,"sender":{"key":"prestontunnellwilson@gmail.com","avatar":null},"body":"I tried this out on the `next` branch of `git` and still see the\nerror. The version I'm using is\n\n```\n[System Info]\ngit version:\ngit version 2.39.0.246.g2a6d74b583\ncpu: aarch64\nbuilt from commit: 2a6d74b5839c13a139e2856e92b7bd8110056098\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>\ncompiler info: gnuc: 12.2\nlibc info: glibc: 2.36\n$SHELL (typically, interactive shell): /bin/bash\n```\n\nOn Sat, Jan 7, 2023 at 12:59 PM Preston Tunnell Wilson\n<prestontunnellwilson@gmail.com> wrote:\n>\n> Hello!\n>\n> I ran into a strange issue using git-worktree with git-hooks. I have\n> attached the output of `git bugreport` below. More concrete steps to\n> reproduce are available in\n> https://github.com/prestontw/git_hook-git_worktree-minimal-repo. I've\n> run into this both in macOS and Fedora---I've included the system\n> information from both of these systems below.\n>\n> What did you do before the bug happened? (Steps to reproduce your issue)\n>\n> Add a git hook that executes git commands inside of another folder. Let's\n> call this other folder `FolderA`.\n> (This might sound weird, but `flutter` distributes itself through a `git`\n> folder in some circumstances and uses `git` commands to see which version\n> the user has installed, etc.).\n>\n> Add a git worktree branch/folder, `cd` to it, and commit something there.\n> Let's call this git worktree folder `FolderW`.\n>\n> (Steps are outlined in more detail with an example in\n> https://github.com/prestontw/git_hook-git_worktree-minimal-repo.)\n>\n> What did you expect to happen? (Expected behavior)\n>\n> The git command would report details from `FolderA`.\n>\n> What happened instead? (Actual behavior)\n>\n> The git command outputs details from `FolderW`.\n>\n> What's different between what you expected and what actually happened?\n>\n> I would expect the git command to output details from `FolderA`.\n> It's confusing to me that there is a difference in behavior between\n> the \"main\" branch and `FolderW` in git hooks.\n>\n> Anything else you want to add:\n>\n> I'm seeing this both in macOS and Fedora. I've added that system\n> info below.\n>\n>\n> [System Info]\n> git version:\n> git version 2.32.0 (Apple Git-132)\n> cpu: arm64\n> no commit associated with this build\n> sizeof-long: 8\n> sizeof-size_t: 8\n> shell-path: /bin/sh\n> uname: Darwin 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58\n> PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64\n> compiler info: clang: 13.1.6 (clang-1316.0.21.2)\n> libc info: no libc information available\n> $SHELL (typically, interactive shell): /bin/zsh\n>\n>\n> [Enabled Hooks]\n> pre-commit\n>\n>\n> [System Info]\n> git version:\n> git version 2.37.3\n> cpu: aarch64\n> no commit associated with this build\n> sizeof-long: 8\n> sizeof-size_t: 8\n> shell-path: /bin/sh\n> uname: Linux 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 U>\n> compiler info: gnuc: 12.2\n> libc info: glibc: 2.36\n> $SHELL (typically, interactive shell): /bin/bash\n>\n>\n> [Enabled Hooks]\n> pre-commit\n"},{"id":"469904","messageId":"CAPig+cScBFJCBN97g12v9Eg1-7+m8_EzAnpT2uSSdzTNwqjjVQ@mail.gmail.com","threadId":"59045","inReplyTo":"CAC-j02OVoFDFjeGUjcs0ZMwPSXKLL_GBme2m2-ttzGTbxXNP-w@mail.gmail.com","subject":"Re: Bug report between git hooks and git worktree","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2023-01-08T06:32:52Z","receivedAt":"2023-01-08T06:33:10Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sat, Jan 7, 2023 at 2:20 PM Preston Tunnell Wilson\n<prestontunnellwilson@gmail.com> wrote:\n> What did you do before the bug happened? (Steps to reproduce your issue)\n>\n> Add a git hook that executes git commands inside of another folder. Let's\n> call this other folder `FolderA`.\n> Add a git worktree branch/folder, `cd` to it, and commit something there.\n> Let's call this git worktree folder `FolderW`.\n>\n> I would expect the git command to output details from `FolderA`.\n> It's confusing to me that there is a difference in behavior between\n> the \"main\" branch and `FolderW` in git hooks.\n\nThis issue comes up from time to time[1][2]. What is happening is that\nGit is setting environment variables pointing at the original\nrepository, so even though you invoke a Git command in a different\nrepository, it's picking up the environment variables and consulting\nthe original repository instead. When writing hooks which invoke Git\ncommands in a foreign repository, the \"best practice\" approach is to\nclear the environment variables before running the Git command in the\nother repository[3]. If you add this to the top of your hook script,\nit works as expected:\n\n    unset $(git rev-parse --local-env-vars)\n\nUnfortunately, this \"fix\" isn't actually documented anywhere.\n\n[1]: https://lore.kernel.org/git/CAPig+cQEC6CAV-Es9Ok96s8Cj=Dj76PRyOt4qKQus+rppswuyA@mail.gmail.com/\n[2]: https://lore.kernel.org/git/CAJFQqN+Z9eX6onaj8vVSqpvf-nOC7-Y0Un4NLUie6x6bGfmvZA@mail.gmail.com/\n[3]: https://lore.kernel.org/git/20190516221702.GA11784@sigill.intra.peff.net/\n"}]}