git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Antoine Bolvy <antoine.bolvy@gmail.com>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: git@vger.kernel.org
Subject: Re: [bug report] git diff --relative not doing well with worktree in hooks
Date: Mon, 3 Jun 2024 10:38:50 +0200	[thread overview]
Message-ID: <CADg0FA_2Z-Hz-ahaHWM-w7qhjf0+06XuhZv1MR3dOtfnAvD09w@mail.gmail.com> (raw)
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)

  reply	other threads:[~2024-06-03  8:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-31 11:38 [bug report] git diff --relative not doing well with worktree in hooks Antoine Bolvy
2024-05-31 21:42 ` Eric Sunshine
2024-06-03  8:38   ` Antoine Bolvy [this message]
2024-06-03  9:29   ` Phillip Wood
2024-06-03 21:59     ` Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADg0FA_2Z-Hz-ahaHWM-w7qhjf0+06XuhZv1MR3dOtfnAvD09w@mail.gmail.com \
    --to=antoine.bolvy@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).