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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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@xxxxxxxxxxxxxx> wrote:
>
> On Fri, May 31, 2024 at 7:38 AM Antoine Bolvy <antoine.bolvy@xxxxxxxxx> 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)





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux