# [bug report] git diff --relative not doing well with worktree in hooks

5 messages from 2024-05-31 to 2024-06-03. Participants: Antoine Bolvy, Eric Sunshine, Phillip Wood.
Thread: https://gitlist.dev/t/61570

## Antoine Bolvy, 2024-05-31 11:38

Subject: [bug report] git diff --relative not doing well with worktree in hooks
Message-ID: <CADg0FA_9shzJKN=dBfnavu5eTDNhbz=g0WP2sehAjSqHP4WFkA@mail.gmail.com>
URL: https://gitlist.dev/e/CADg0FA_9shzJKN%3DdBfnavu5eTDNhbz%3Dg0WP2sehAjSqHP4WFkA%40mail.gmail.com

```
Hello,

I noticed a weird behavior when using git diff --relative with worktrees and
hooks. When called from a pre-commit hook from a worktree, the relative option
has no effect.

Here is how to reproduce the issue:

ˋˋˋbash
mkdir hook-repro && cd hook-repro
git init test && cd test
mkdir folder && touch folder/.gitkeep && git add folder
git commit -m 'init'
cat <<EOF > .git/hooks/pre-commit
#!/bin/bash

cd folder || exit

pwd # display the current working directory

git diff --cached --relative --name-only
EOF
chmod +x .git/hooks/pre-commit
ˋˋˋ

ˋˋˋbash
echo "foo" > folder/bar
git add folder
git commit -m "test"
ˋˋˋ

Displays
ˋˋˋ
/home/arch/git/awfus/hook-repro/test/folder
bar
ˋˋˋ

Now creating a worktree:

ˋˋˋbash
git worktree add ../worktree && cd ../worktree
echo "bar" > folder/foo
git add folder
git commit -m "worktree"
ˋˋˋ

Displays
ˋˋˋ
/home/arch/git/awfus/hook-repro/worktree/folder
folder/foo
ˋˋˋ

The path is no longer show relative. This causes issues with more complex
scripts.

Git version: 2.45.0 (x86_64) on Arch Linux, shell is zsh (bash for the hook
script)

Let me know if you need any more information :)

```

## Eric Sunshine, 2024-05-31 21:42

Subject: Re: [bug report] git diff --relative not doing well with worktree in hooks
Message-ID: <CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcT1pTkKd1A0o_qjP%2BOyx%2BzyCevV8EAg5Ub9guAyd3UjgA%40mail.gmail.com
In-Reply-To: <CADg0FA_9shzJKN=dBfnavu5eTDNhbz=g0WP2sehAjSqHP4WFkA@mail.gmail.com>

```
On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:
> I noticed a weird behavior when using git diff --relative with worktrees and
> hooks. When called from a pre-commit hook from a worktree, the relative option
> has no effect.
>
> [main tree] Displays
> ˋˋˋ
> /home/arch/git/awfus/hook-repro/test/folder
> bar
> ˋˋˋ
> [in worktree] Displays
> ˋˋˋ
> /home/arch/git/awfus/hook-repro/worktree/folder
> folder/foo
> ˋˋˋ
> The path is no longer show relative. This causes issues with more complex
> scripts.

I'm not sure there's a satisfactory resolution here. Your hook is
running afoul of the environment variables Git sets up when the hook
is run outside of the "main" worktree.

If you change your hook from:

    #!/bin/bash
    cd folder || exit
    pwd # display the current working directory
    git diff --cached --relative --name-only

to:

    #!/bin/bash
    cd folder || exit
    pwd # display the current working directory
    unset $(git rev-parse --local-env-vars)
    git diff --cached --relative --name-only

then it works as expected.

The relevant portion from the "githooks" manual page is:

    Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
    exported so that Git commands run by the hook can correctly locate
    the repository. If your hook needs to invoke Git commands in a
    foreign repository or in a different working tree of the same
    repository, then it should clear these environment variables so
    they do not interfere with Git operations at the foreign
    location. For example:

        local_desc=$(git describe)
        foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C
../foreign-repo describe)

```

## Antoine Bolvy, 2024-06-03 08:38

Subject: Re: [bug report] git diff --relative not doing well with worktree in hooks
Message-ID: <CADg0FA_2Z-Hz-ahaHWM-w7qhjf0+06XuhZv1MR3dOtfnAvD09w@mail.gmail.com>
URL: https://gitlist.dev/e/CADg0FA_2Z-Hz-ahaHWM-w7qhjf0%2B06XuhZv1MR3dOtfnAvD09w%40mail.gmail.com
In-Reply-To: <CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com>

```
> If you change your hook from:
>
>    #!/bin/bash
>    cd folder || exit
>    pwd # display the current working directory
>    git diff --cached --relative --name-only
>
>to:
>
>    #!/bin/bash
>    cd folder || exit
>    pwd # display the current working directory
>    unset $(git rev-parse --local-env-vars)
>    git diff --cached --relative --name-only
>
>then it works as expected.

It seems that unsetting GIT_DIR only is enough to make it work:

    #!/bin/bash
    cd folder || exit
    pwd # display the current working directory
    unset GIT_DIR
    git diff --cached --relative --name-only

Thanks for the help and pointing me to the right documentation!

Antoine BOLVY
+33(0)675455349 • https://saveman71.com


On Fri, May 31, 2024 at 11:42 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:
> > I noticed a weird behavior when using git diff --relative with worktrees and
> > hooks. When called from a pre-commit hook from a worktree, the relative option
> > has no effect.
> >
> > [main tree] Displays
> > ˋˋˋ
> > /home/arch/git/awfus/hook-repro/test/folder
> > bar
> > ˋˋˋ
> > [in worktree] Displays
> > ˋˋˋ
> > /home/arch/git/awfus/hook-repro/worktree/folder
> > folder/foo
> > ˋˋˋ
> > The path is no longer show relative. This causes issues with more complex
> > scripts.
>
> I'm not sure there's a satisfactory resolution here. Your hook is
> running afoul of the environment variables Git sets up when the hook
> is run outside of the "main" worktree.
>
> If you change your hook from:
>
>     #!/bin/bash
>     cd folder || exit
>     pwd # display the current working directory
>     git diff --cached --relative --name-only
>
> to:
>
>     #!/bin/bash
>     cd folder || exit
>     pwd # display the current working directory
>     unset $(git rev-parse --local-env-vars)
>     git diff --cached --relative --name-only
>
> then it works as expected.
>
> The relevant portion from the "githooks" manual page is:
>
>     Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
>     exported so that Git commands run by the hook can correctly locate
>     the repository. If your hook needs to invoke Git commands in a
>     foreign repository or in a different working tree of the same
>     repository, then it should clear these environment variables so
>     they do not interfere with Git operations at the foreign
>     location. For example:
>
>         local_desc=$(git describe)
>         foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C
> ../foreign-repo describe)

```

## Phillip Wood, 2024-06-03 09:29

Subject: Re: [bug report] git diff --relative not doing well with worktree in hooks
Message-ID: <e0ea930f-3d43-49de-b2e7-c057d95fd7cc@gmail.com>
URL: https://gitlist.dev/e/e0ea930f-3d43-49de-b2e7-c057d95fd7cc%40gmail.com
In-Reply-To: <CAPig+cT1pTkKd1A0o_qjP+Oyx+zyCevV8EAg5Ub9guAyd3UjgA@mail.gmail.com>

```
Hi Eric

On 31/05/2024 22:42, Eric Sunshine wrote:
> On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@gmail.com> wrote:
>> The path is no longer show relative. This causes issues with more complex
>> scripts.
> 
> I'm not sure there's a satisfactory resolution here. Your hook is
> running afoul of the environment variables Git sets up when the hook
> is run outside of the "main" worktree.
> [...] 
> The relevant portion from the "githooks" manual page is:
> 
>      Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
>      exported so that Git commands run by the hook can correctly locate
>      the repository. If your hook needs to invoke Git commands in a
>      foreign repository or in a different working tree of the same
>      repository, then it should clear these environment variables so
>      they do not interfere with Git operations at the foreign
>      location. For example:

Maybe I'm missing something but in Antonine's example the hook is being 
run in the same worktree as the "git commit" - we're changing into a 
subdirectory of the worktree, not changing to a different worktree so 
why doesn't it work?

Best Wishes

Phillip

```

## Eric Sunshine, 2024-06-03 21:59

Subject: Re: [bug report] git diff --relative not doing well with worktree in hooks
Message-ID: <CAPig+cSzY1YO6KByb3fG0P1jWHbEu_a3s_7QBieDoD6un-MJXQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcSzY1YO6KByb3fG0P1jWHbEu_a3s_7QBieDoD6un-MJXQ%40mail.gmail.com
In-Reply-To: <e0ea930f-3d43-49de-b2e7-c057d95fd7cc@gmail.com>

```
On Mon, Jun 3, 2024 at 5:30 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
> On 31/05/2024 22:42, Eric Sunshine wrote:
> > I'm not sure there's a satisfactory resolution here. Your hook is
> > running afoul of the environment variables Git sets up when the hook
> > is run outside of the "main" worktree.
> > [...]
> > The relevant portion from the "githooks" manual page is:
> >
> >      Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
> >      exported so that Git commands run by the hook can correctly locate
> >      the repository. If your hook needs to invoke Git commands in a
> >      foreign repository or in a different working tree of the same
> >      repository, then it should clear these environment variables so
> >      they do not interfere with Git operations at the foreign
> >      location. For example:
>
> Maybe I'm missing something but in Antonine's example the hook is being
> run in the same worktree as the "git commit" - we're changing into a
> subdirectory of the worktree, not changing to a different worktree so
> why doesn't it work?

It's been a while since I looked at the code, but my recollection is
that the hook-running machinery unconditionally sets the environment
variables whenever the directory in which the hook is being run is not
the "main" worktree. This is the case whether his hook runs at the
root of the worktree or in a subdirectory.

It's quite possible that this behavior is entirely accidental since
the hook-running machinery existed long before multiple-worktree
support was added, and it may be that the hook-running machinery
simply wasn't revisited when worktree support was implemented. So,
perhaps a "satisfactory resolution" is to "fix" the hook-running
machinery itself to avoid setting those environment variables
unnecessarily.

```
