{"thread":{"id":"61570","subject":"[bug report] git diff --relative not doing well with worktree in hooks","startedAt":"2024-05-31T11:38:52Z","lastAt":"2024-06-03T21:59:46Z","messageCount":5,"participants":["Antoine Bolvy","Eric Sunshine","Phillip Wood"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"496001","messageId":"CADg0FA_9shzJKN=dBfnavu5eTDNhbz=g0WP2sehAjSqHP4WFkA@mail.gmail.com","threadId":"61570","inReplyTo":null,"subject":"[bug report] git diff --relative not doing well with worktree in hooks","fromName":"Antoine Bolvy","fromEmail":"antoine.bolvy@gmail.com","sentAt":"2024-05-31T11:38:40Z","receivedAt":"2024-05-31T11:38:52Z","isPatch":false,"sender":{"key":"antoine.bolvy@gmail.com","avatar":null},"body":"Hello,\n\nI noticed a weird behavior when using git diff --relative with worktrees and\nhooks. When called from a pre-commit hook from a worktree, the relative option\nhas no effect.\n\nHere is how to reproduce the issue:\n\n```bash\nmkdir hook-repro && cd hook-repro\ngit init test && cd test\nmkdir folder && touch folder/.gitkeep && git add folder\ngit commit -m 'init'\ncat <<EOF > .git/hooks/pre-commit\n#!/bin/bash\n\ncd folder || exit\n\npwd # display the current working directory\n\ngit diff --cached --relative --name-only\nEOF\nchmod +x .git/hooks/pre-commit\n```\n\n```bash\necho \"foo\" > folder/bar\ngit add folder\ngit commit -m \"test\"\n```\n\nDisplays\n```\n/home/arch/git/awfus/hook-repro/test/folder\nbar\n```\n\nNow creating a worktree:\n\n```bash\ngit worktree add ../worktree && cd ../worktree\necho \"bar\" > folder/foo\ngit add folder\ngit commit -m \"worktree\"\n```\n\nDisplays\n```\n/home/arch/git/awfus/hook-repro/worktree/folder\nfolder/foo\n```\n\nThe path is no longer show relative. This causes issues with more complex\nscripts.\n\nGit version: 2.45.0 (x86_64) on Arch Linux, shell is zsh (bash for the hook\nscript)\n\nLet me know if you need any more information :)\n"},{"id":"496029","messageId":"CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com","threadId":"61570","inReplyTo":"CADg0FA_9shzJKN=dBfnavu5eTDNhbz=g0WP2sehAjSqHP4WFkA@mail.gmail.com","subject":"Re: [bug report] git diff --relative not doing well with worktree in hooks","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2024-05-31T21:42:03Z","receivedAt":"2024-05-31T21:42:14Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:\n> I noticed a weird behavior when using git diff --relative with worktrees and\n> hooks. When called from a pre-commit hook from a worktree, the relative option\n> has no effect.\n>\n> [main tree] Displays\n> ```\n> /home/arch/git/awfus/hook-repro/test/folder\n> bar\n> ```\n> [in worktree] Displays\n> ```\n> /home/arch/git/awfus/hook-repro/worktree/folder\n> folder/foo\n> ```\n> The path is no longer show relative. This causes issues with more complex\n> scripts.\n\nI'm not sure there's a satisfactory resolution here. Your hook is\nrunning afoul of the environment variables Git sets up when the hook\nis run outside of the \"main\" worktree.\n\nIf you change your hook from:\n\n    #!/bin/bash\n    cd folder || exit\n    pwd # display the current working directory\n    git diff --cached --relative --name-only\n\nto:\n\n    #!/bin/bash\n    cd folder || exit\n    pwd # display the current working directory\n    unset $(git rev-parse --local-env-vars)\n    git diff --cached --relative --name-only\n\nthen it works as expected.\n\nThe relevant portion from the \"githooks\" manual page is:\n\n    Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are\n    exported so that Git commands run by the hook can correctly locate\n    the repository. If your hook needs to invoke Git commands in a\n    foreign repository or in a different working tree of the same\n    repository, then it should clear these environment variables so\n    they do not interfere with Git operations at the foreign\n    location. For example:\n\n        local_desc=$(git describe)\n        foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C\n../foreign-repo describe)\n"},{"id":"496064","messageId":"CADg0FA_2Z-Hz-ahaHWM-w7qhjf0+06XuhZv1MR3dOtfnAvD09w@mail.gmail.com","threadId":"61570","inReplyTo":"CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com","subject":"Re: [bug report] git diff --relative not doing well with worktree in hooks","fromName":"Antoine Bolvy","fromEmail":"antoine.bolvy@gmail.com","sentAt":"2024-06-03T08:38:50Z","receivedAt":"2024-06-03T08:39:03Z","isPatch":false,"sender":{"key":"antoine.bolvy@gmail.com","avatar":null},"body":"> If you change your hook from:\n>\n>    #!/bin/bash\n>    cd folder || exit\n>    pwd # display the current working directory\n>    git diff --cached --relative --name-only\n>\n>to:\n>\n>    #!/bin/bash\n>    cd folder || exit\n>    pwd # display the current working directory\n>    unset $(git rev-parse --local-env-vars)\n>    git diff --cached --relative --name-only\n>\n>then it works as expected.\n\nIt seems that unsetting GIT_DIR only is enough to make it work:\n\n    #!/bin/bash\n    cd folder || exit\n    pwd # display the current working directory\n    unset GIT_DIR\n    git diff --cached --relative --name-only\n\nThanks for the help and pointing me to the right documentation!\n\nAntoine BOLVY\n+33(0)675455349 • https://saveman71.com\n\n\nOn Fri, May 31, 2024 at 11:42 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>\n> On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:\n> > I noticed a weird behavior when using git diff --relative with worktrees and\n> > hooks. When called from a pre-commit hook from a worktree, the relative option\n> > has no effect.\n> >\n> > [main tree] Displays\n> > ```\n> > /home/arch/git/awfus/hook-repro/test/folder\n> > bar\n> > ```\n> > [in worktree] Displays\n> > ```\n> > /home/arch/git/awfus/hook-repro/worktree/folder\n> > folder/foo\n> > ```\n> > The path is no longer show relative. This causes issues with more complex\n> > scripts.\n>\n> I'm not sure there's a satisfactory resolution here. Your hook is\n> running afoul of the environment variables Git sets up when the hook\n> is run outside of the \"main\" worktree.\n>\n> If you change your hook from:\n>\n>     #!/bin/bash\n>     cd folder || exit\n>     pwd # display the current working directory\n>     git diff --cached --relative --name-only\n>\n> to:\n>\n>     #!/bin/bash\n>     cd folder || exit\n>     pwd # display the current working directory\n>     unset $(git rev-parse --local-env-vars)\n>     git diff --cached --relative --name-only\n>\n> then it works as expected.\n>\n> The relevant portion from the \"githooks\" manual page is:\n>\n>     Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are\n>     exported so that Git commands run by the hook can correctly locate\n>     the repository. If your hook needs to invoke Git commands in a\n>     foreign repository or in a different working tree of the same\n>     repository, then it should clear these environment variables so\n>     they do not interfere with Git operations at the foreign\n>     location. For example:\n>\n>         local_desc=$(git describe)\n>         foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C\n> ../foreign-repo describe)\n"},{"id":"496074","messageId":"e0ea930f-3d43-49de-b2e7-c057d95fd7cc@gmail.com","threadId":"61570","inReplyTo":"CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com","subject":"Re: [bug report] git diff --relative not doing well with worktree in hooks","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-06-03T09:29:56Z","receivedAt":"2024-06-03T09:30:00Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Eric\n\nOn 31/05/2024 22:42, Eric Sunshine wrote:\n> On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:\n>> The path is no longer show relative. This causes issues with more complex\n>> scripts.\n> \n> I'm not sure there's a satisfactory resolution here. Your hook is\n> running afoul of the environment variables Git sets up when the hook\n> is run outside of the \"main\" worktree.\n> [...] \n> The relevant portion from the \"githooks\" manual page is:\n> \n>      Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are\n>      exported so that Git commands run by the hook can correctly locate\n>      the repository. If your hook needs to invoke Git commands in a\n>      foreign repository or in a different working tree of the same\n>      repository, then it should clear these environment variables so\n>      they do not interfere with Git operations at the foreign\n>      location. For example:\n\nMaybe I'm missing something but in Antonine's example the hook is being \nrun in the same worktree as the \"git commit\" - we're changing into a \nsubdirectory of the worktree, not changing to a different worktree so \nwhy doesn't it work?\n\nBest Wishes\n\nPhillip\n"},{"id":"496191","messageId":"CAPig+cSzY1YO6KByb3fG0P1jWHbEu_a3s_7QBieDoD6un-MJXQ@mail.gmail.com","threadId":"61570","inReplyTo":"e0ea930f-3d43-49de-b2e7-c057d95fd7cc@gmail.com","subject":"Re: [bug report] git diff --relative not doing well with worktree in hooks","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2024-06-03T21:59:34Z","receivedAt":"2024-06-03T21:59:46Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Jun 3, 2024 at 5:30 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> On 31/05/2024 22:42, Eric Sunshine wrote:\n> > I'm not sure there's a satisfactory resolution here. Your hook is\n> > running afoul of the environment variables Git sets up when the hook\n> > is run outside of the \"main\" worktree.\n> > [...]\n> > The relevant portion from the \"githooks\" manual page is:\n> >\n> >      Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are\n> >      exported so that Git commands run by the hook can correctly locate\n> >      the repository. If your hook needs to invoke Git commands in a\n> >      foreign repository or in a different working tree of the same\n> >      repository, then it should clear these environment variables so\n> >      they do not interfere with Git operations at the foreign\n> >      location. For example:\n>\n> Maybe I'm missing something but in Antonine's example the hook is being\n> run in the same worktree as the \"git commit\" - we're changing into a\n> subdirectory of the worktree, not changing to a different worktree so\n> why doesn't it work?\n\nIt's been a while since I looked at the code, but my recollection is\nthat the hook-running machinery unconditionally sets the environment\nvariables whenever the directory in which the hook is being run is not\nthe \"main\" worktree. This is the case whether his hook runs at the\nroot of the worktree or in a subdirectory.\n\nIt's quite possible that this behavior is entirely accidental since\nthe hook-running machinery existed long before multiple-worktree\nsupport was added, and it may be that the hook-running machinery\nsimply wasn't revisited when worktree support was implemented. So,\nperhaps a \"satisfactory resolution\" is to \"fix\" the hook-running\nmachinery itself to avoid setting those environment variables\nunnecessarily.\n"}]}