# git rebase exec make -C in worktree confuses repo root dir

11 messages from 2024-10-14 to 2024-11-05. Participants: David Moberg, Taylor Blau, Eric Sunshine, Phillip Wood, Elijah Newren, phillip.wood123@gmail.com.
Thread: https://gitlist.dev/t/62336

## David Moberg, 2024-10-14 20:46

Subject: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAL2+Miudq0UXAb=R24v+ftZVkHy2We1CFsFAt__tCYMWtCfOow@mail.gmail.com>
URL: https://gitlist.dev/e/CAL2%2BMiudq0UXAb%3DR24v%2BftZVkHy2We1CFsFAt__tCYMWtCfOow%40mail.gmail.com

```
Hi, I tried to minimize this but I haven't been able to come to a much
smaller example.

/David "kaddkaka" Moberg

What did you do before the bug happened? (Steps to reproduce your issue)

When doing `git rebase --exec` in a "secondary" worktree, it seems
that there is confusion about
where the repo root dir is. This sequence of commands can be run to reproduce:

  mktemp -d /tmp/git_bugreport_rebase_exec_worktree_root.XXXXXX
  cd /tmp/git_bugreport_rebase_exec_worktree_root.*
  mkdir repo
  cd repo/
  mkdir dir
  echo -e 'lint:' > dir/Makefile
  echo -e '\tgit rev-parse --show-toplevel' >> dir/Makefile
  echo -e '\tgit grep "banana" -- "$$BANANA"' >> dir/Makefile
  git init
  git add dir
  git commit -m"Intial commit"
  git worktree add ../repo2
  cd ../repo2/
  BANANA=$PWD GIT_EDITOR='sed -i "1s/noop/exec make -C dir\/ lint/"'
git rebase -i HEAD

What did you expect to happen? (Expected behavior)

1. This command should return the worktree toplevel, not a subdirectory
  $ git rev-parse --show-toplevel
  /tmp/tmp.DUUAVQCIKe/repo2

2. And the git grep command should return the match from dir/Makefile,
not Fatal Error
  $ git grep banana
  Makefile:       git grep "banana" -- "$$BANANA"

What happened instead? (Actual behavior)

1. --show-toplevel reports a subdirectory instead of toplevel:
  $ git rev-parse --show-toplevel
  /tmp/tmp.DUUAVQCIKe/repo2/dir
2. git grep complains that the pathspec is outside the repository with
fatal error:
  $ git grep "banana" -- "$BANANA"
  fatal: /tmp/tmp.DUUAVQCIKe/repo2: '/tmp/tmp.DUUAVQCIKe/repo2' is
outside repository at '/tmp/tmp.DUUAVQCIKe/repo2/dir'
  make: *** [Makefile:3: lint] Error 128
  make: Leaving directory '/tmp/tmp.DUUAVQCIKe/repo2/dir'
  warning: execution failed: make -C dir/ lint

What's different between what you expected and what actually happened?

See above.

[System Info]
git version:
git version 2.43.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug
30 12:02:04 UTC 2024 x86_64
compiler info: gnuc: 13.2
libc info: glibc: 2.39
$SHELL (typically, interactive shell): /usr/bin/bash


[Enabled Hooks]

```

## Taylor Blau, 2024-10-14 21:19

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <Zw2K5xJAOGWitfXr@nand.local>
URL: https://gitlist.dev/e/Zw2K5xJAOGWitfXr%40nand.local
In-Reply-To: <CAL2+Miudq0UXAb=R24v+ftZVkHy2We1CFsFAt__tCYMWtCfOow@mail.gmail.com>

```
On Mon, Oct 14, 2024 at 10:46:45PM +0200, David Moberg wrote:
> What did you expect to happen? (Expected behavior)
>
> 1. This command should return the worktree toplevel, not a subdirectory
>   $ git rev-parse --show-toplevel
>   /tmp/tmp.DUUAVQCIKe/repo2
>
> 2. And the git grep command should return the match from dir/Makefile,
> not Fatal Error
>   $ git grep banana
>   Makefile:       git grep "banana" -- "$$BANANA"

I am not sure if this is expected behavior or not, but it feels
unintentional to me. Perhaps I am missing something funky in your
example that is causing it to behave this way.

Does this bisect to anything interesting, or has it always behaved this
way?

Thanks,
Taylor

```

## David Moberg, 2024-10-14 21:34

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAL2+MiuG4FQdV3c4tGqcRgV+JWxAfvbo91vdGsnFHwDT0y_VRw@mail.gmail.com>
URL: https://gitlist.dev/e/CAL2%2BMiuG4FQdV3c4tGqcRgV%2BJWxAfvbo91vdGsnFHwDT0y_VRw%40mail.gmail.com
In-Reply-To: <Zw2K5xJAOGWitfXr@nand.local>

```
I am not sure if this has worked previously. Where/how far back should
I look? Is 2 samples from 6 months and 1 year prior reasonable and
enough?

I'm not sure if I understand you, What is the behavior you expect?
Are you able to reproduce the behavior?

/David

Den mån 14 okt. 2024 kl 23:19 skrev Taylor Blau <me@ttaylorr.com>:
>
> On Mon, Oct 14, 2024 at 10:46:45PM +0200, David Moberg wrote:
> > What did you expect to happen? (Expected behavior)
> >
> > 1. This command should return the worktree toplevel, not a subdirectory
> >   $ git rev-parse --show-toplevel
> >   /tmp/tmp.DUUAVQCIKe/repo2
> >
> > 2. And the git grep command should return the match from dir/Makefile,
> > not Fatal Error
> >   $ git grep banana
> >   Makefile:       git grep "banana" -- "$$BANANA"
>
> I am not sure if this is expected behavior or not, but it feels
> unintentional to me. Perhaps I am missing something funky in your
> example that is causing it to behave this way.
>
> Does this bisect to anything interesting, or has it always behaved this
> way?
>
> Thanks,
> Taylor

```

## Eric Sunshine, 2024-10-15 07:11

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAPig+cQZoO8tMZ1Gip-at8-9n_tk4axctkX=WbaO1==JRru39A@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQZoO8tMZ1Gip-at8-9n_tk4axctkX%3DWbaO1%3D%3DJRru39A%40mail.gmail.com
In-Reply-To: <Zw2K5xJAOGWitfXr@nand.local>

```
On Mon, Oct 14, 2024 at 5:19 PM Taylor Blau <me@ttaylorr.com> wrote:
> On Mon, Oct 14, 2024 at 10:46:45PM +0200, David Moberg wrote:
> > 1. This command should return the worktree toplevel, not a subdirectory
> >   $ git rev-parse --show-toplevel
> >   /tmp/tmp.DUUAVQCIKe/repo2
> >
> > 2. And the git grep command should return the match from dir/Makefile,
> > not Fatal Error
> >   $ git grep banana
> >   Makefile:       git grep "banana" -- "$$BANANA"
>
> I am not sure if this is expected behavior or not, but it feels
> unintentional to me. Perhaps I am missing something funky in your
> example that is causing it to behave this way.

This looks like unintentional behavior; probably a bug. It seems to be
triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
reproduction recipe:

    % git init foo
    % cd foo
    % mkdir dir
    % echo foo >dir/file
    % git add dir/file
    % git commit -m foo
    % git worktree add ../bar
    % cd ../bar
    % git -C dir rev-parse --show-toplevel
    /.../bar
    % GIT_DIR=../../foo/.git/worktrees/bar \
        git -C dir rev-parse --show-toplevel
    /.../bar/dir

The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
incorrectly returning `/.../bar/dir` rather than `/.../bar`.

```

## David Moberg, 2024-10-15 18:55

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAL2+MivOu=_HYg+2KoMKMUtz+=q2jv-K9u9Zxrhe3OuHLCmwFA@mail.gmail.com>
URL: https://gitlist.dev/e/CAL2%2BMivOu%3D_HYg%2B2KoMKMUtz%2B%3Dq2jv-K9u9Zxrhe3OuHLCmwFA%40mail.gmail.com
In-Reply-To: <CAPig+cQZoO8tMZ1Gip-at8-9n_tk4axctkX=WbaO1==JRru39A@mail.gmail.com>

```
Thanks, that is indeed a much smaller example and it seems to exhibit
the same issue. Can we figure out how to fix it?

Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
>
> On Mon, Oct 14, 2024 at 5:19 PM Taylor Blau <me@ttaylorr.com> wrote:
> > On Mon, Oct 14, 2024 at 10:46:45PM +0200, David Moberg wrote:
> > > 1. This command should return the worktree toplevel, not a subdirectory
> > >   $ git rev-parse --show-toplevel
> > >   /tmp/tmp.DUUAVQCIKe/repo2
> > >
> > > 2. And the git grep command should return the match from dir/Makefile,
> > > not Fatal Error
> > >   $ git grep banana
> > >   Makefile:       git grep "banana" -- "$$BANANA"
> >
> > I am not sure if this is expected behavior or not, but it feels
> > unintentional to me. Perhaps I am missing something funky in your
> > example that is causing it to behave this way.
>
> This looks like unintentional behavior; probably a bug. It seems to be
> triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
> reproduction recipe:
>
>     % git init foo
>     % cd foo
>     % mkdir dir
>     % echo foo >dir/file
>     % git add dir/file
>     % git commit -m foo
>     % git worktree add ../bar
>     % cd ../bar
>     % git -C dir rev-parse --show-toplevel
>     /.../bar
>     % GIT_DIR=../../foo/.git/worktrees/bar \
>         git -C dir rev-parse --show-toplevel
>     /.../bar/dir
>
> The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
> incorrectly returning `/.../bar/dir` rather than `/.../bar`.

```

## Eric Sunshine, 2024-10-15 20:01

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAPig+cQ6=HDD447xTHQ84hmsF3SMbC5nH_PXf3rZWvTWmp18ug@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQ6%3DHDD447xTHQ84hmsF3SMbC5nH_PXf3rZWvTWmp18ug%40mail.gmail.com
In-Reply-To: <CAL2+MivOu=_HYg+2KoMKMUtz+=q2jv-K9u9Zxrhe3OuHLCmwFA@mail.gmail.com>

```
On Tue, Oct 15, 2024 at 2:55 PM David Moberg <kaddkaka@gmail.com> wrote:
> Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
> > This looks like unintentional behavior; probably a bug. It seems to be
> > triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
> > reproduction recipe:
> >
> >     % git init foo
> >     % cd foo
> >     % mkdir dir
> >     % echo foo >dir/file
> >     % git add dir/file
> >     % git commit -m foo
> >     % git worktree add ../bar
> >     % cd ../bar
> >     % git -C dir rev-parse --show-toplevel
> >     /.../bar
> >     % GIT_DIR=../../foo/.git/worktrees/bar \
> >         git -C dir rev-parse --show-toplevel
> >     /.../bar/dir
> >
> > The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
> > incorrectly returning `/.../bar/dir` rather than `/.../bar`.
>
> Thanks, that is indeed a much smaller example and it seems to exhibit
> the same issue. Can we figure out how to fix it?

Someone is going to have to dig into the code, but my Git time is very
limited right now, so perhaps someone else can do the digging.

If you want an immediate workaround for the problem for your specific
use-case, you can probably unset GIT_DIR in your `exec` instruction.

(By the way, please avoid top-posting when you reply on this list;
instead, post inline as I did here.)

```

## Phillip Wood, 2024-10-16 09:15

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <1dc91aa7-04da-4023-bbd5-5b12539477ba@gmail.com>
URL: https://gitlist.dev/e/1dc91aa7-04da-4023-bbd5-5b12539477ba%40gmail.com
In-Reply-To: <CAPig+cQ6=HDD447xTHQ84hmsF3SMbC5nH_PXf3rZWvTWmp18ug@mail.gmail.com>

```
On 15/10/2024 21:01, Eric Sunshine wrote:
> On Tue, Oct 15, 2024 at 2:55 PM David Moberg <kaddkaka@gmail.com> wrote:
>> Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
>>> This looks like unintentional behavior; probably a bug. It seems to be
>>> triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
>>> reproduction recipe:
>>>
>>>      % git init foo
>>>      % cd foo
>>>      % mkdir dir
>>>      % echo foo >dir/file
>>>      % git add dir/file
>>>      % git commit -m foo
>>>      % git worktree add ../bar
>>>      % cd ../bar
>>>      % git -C dir rev-parse --show-toplevel
>>>      /.../bar
>>>      % GIT_DIR=../../foo/.git/worktrees/bar \
>>>          git -C dir rev-parse --show-toplevel
>>>      /.../bar/dir
>>>
>>> The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
>>> incorrectly returning `/.../bar/dir` rather than `/.../bar`.
>>
>> Thanks, that is indeed a much smaller example and it seems to exhibit
>> the same issue. Can we figure out how to fix it?
> 
> Someone is going to have to dig into the code, but my Git time is very
> limited right now, so perhaps someone else can do the digging.

I'm about to go off the list until the 29th so I wont be working on it 
soon either but I think the problem is that git sets $GIT_DIR when it is 
run from a linked worktree. I've reproduced the commit message from 
ff5b7913f0a (sequencer, stash: fix running from worktree subdir, 
2022-01-26) below which I think explains the problem we're seeing here. 
Unfortunately the approach of setting $GIT_WORK_TREE used in that commit 
won't work for exec commands as they may be run in a different worktree. 
Naively I feel that if setup_git_directory() has found ".git" then any 
git subprocesses run in the worktree should also be able to find ".git" 
and so it should not be setting $GIT_DIR but there maybe there is some 
subtlety I'm missing


     commit ff5b7913f0af62c26682b0376d0aa2d7f5d74b2e
     Author: Elijah Newren <newren@gmail.com>
     Date:   Wed Jan 26 01:43:45 2022 +0000

     sequencer, stash: fix running from worktree subdir

     In commits bc3ae46b42 ("rebase: do not attempt to remove
     startup_info->original_cwd", 2021-12-09) and 0fce211ccc ("stash: do
     not attempt to remove startup_info->original_cwd", 2021-12-09), we
     wanted to allow the subprocess to know which directory the parent
     process was running from, so that the subprocess could protect it.
     However...

     When run from a non-main worktree, setup_git_directory() will note
     that the discovered git directory
     (/PATH/TO/.git/worktree/non-main-worktree) does not match
     DEFAULT_GIT_DIR_ENVIRONMENT (see setup_discovered_git_dir()), and
     decide to set GIT_DIR in the environment.  This matters because...

     Whenever git is run with the GIT_DIR environment variable set, and
     GIT_WORK_TREE not set, it presumes that '.' is the working tree.
     So...

     This combination results in the subcommand being very confused about
     the working tree.  Fix it by also setting the GIT_WORK_TREE
     environment variable along with setting cmd.dir.

     A possibly more involved fix we could consider for later would be to
     make setup.c set GIT_WORK_TREE whenever (a) it discovers both the
     git directory and the working tree and (b) it decides to set GIT_DIR
     in the environment.  I did not attempt that here as such would be
     too big of a change for a 2.35.1 release.

     Test-case-by: Glen Choo <chooglen@google.com>
     Signed-off-by: Elijah Newren <newren@gmail.com>
     Signed-off-by: Junio C Hamano <gitster@pobox.com>

> If you want an immediate workaround for the problem for your specific
> use-case, you can probably unset GIT_DIR in your `exec` instruction.

Indeed I think that should work in this case. Unfortunately we cannot do 
that unconditionally when running exec commands as the user may have set 
GIT_DIR when running "git rebase"

Best Wishes

Phillip

> (By the way, please avoid top-posting when you reply on this list;
> instead, post inline as I did here.)
> 


```

## Taylor Blau, 2024-10-16 20:36

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <ZxAjz28QLwWlDVg1@nand.local>
URL: https://gitlist.dev/e/ZxAjz28QLwWlDVg1%40nand.local
In-Reply-To: <1dc91aa7-04da-4023-bbd5-5b12539477ba@gmail.com>

```
On Wed, Oct 16, 2024 at 10:15:49AM +0100, Phillip Wood wrote:
> On 15/10/2024 21:01, Eric Sunshine wrote:
> > On Tue, Oct 15, 2024 at 2:55 PM David Moberg <kaddkaka@gmail.com> wrote:
> > > Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
> > > > This looks like unintentional behavior; probably a bug. It seems to be
> > > > triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
> > > > reproduction recipe:
> > > >
> > > >      % git init foo
> > > >      % cd foo
> > > >      % mkdir dir
> > > >      % echo foo >dir/file
> > > >      % git add dir/file
> > > >      % git commit -m foo
> > > >      % git worktree add ../bar
> > > >      % cd ../bar
> > > >      % git -C dir rev-parse --show-toplevel
> > > >      /.../bar
> > > >      % GIT_DIR=../../foo/.git/worktrees/bar \
> > > >          git -C dir rev-parse --show-toplevel
> > > >      /.../bar/dir
> > > >
> > > > The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
> > > > incorrectly returning `/.../bar/dir` rather than `/.../bar`.
> > >
> > > Thanks, that is indeed a much smaller example and it seems to exhibit
> > > the same issue. Can we figure out how to fix it?
> >
> > Someone is going to have to dig into the code, but my Git time is very
> > limited right now, so perhaps someone else can do the digging.
>
> I'm about to go off the list until the 29th so I wont be working on it soon
> either but I think the problem is that git sets $GIT_DIR when it is run from
> a linked worktree. I've reproduced the commit message from ff5b7913f0a
> (sequencer, stash: fix running from worktree subdir, 2022-01-26) below which
> I think explains the problem we're seeing here. Unfortunately the approach
> of setting $GIT_WORK_TREE used in that commit won't work for exec commands
> as they may be run in a different worktree. Naively I feel that if
> setup_git_directory() has found ".git" then any git subprocesses run in the
> worktree should also be able to find ".git" and so it should not be setting
> $GIT_DIR but there maybe there is some subtlety I'm missing

Let's see if that commit's author Elijah (CC'd) has any other thoughts.

Thanks,
Taylor

```

## Eric Sunshine, 2024-10-16 21:12

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CAPig+cQmGXxDshTovdAYaZn5UMr3nvXHyH0q2HvAbaT_fhhiLQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQmGXxDshTovdAYaZn5UMr3nvXHyH0q2HvAbaT_fhhiLQ%40mail.gmail.com
In-Reply-To: <1dc91aa7-04da-4023-bbd5-5b12539477ba@gmail.com>

```
On Wed, Oct 16, 2024 at 5:15 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
> On 15/10/2024 21:01, Eric Sunshine wrote:
> >> Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
> >>> This looks like unintentional behavior; probably a bug. It seems to be
> >>> triggered by `git rebase -i` setting GIT_DIR. [...]
> >>>      % git -C dir rev-parse --show-toplevel
> >>>      /.../bar
> >>>      % GIT_DIR=../../foo/.git/worktrees/bar \
> >>>          git -C dir rev-parse --show-toplevel
> >>>      /.../bar/dir
> >>>
> >>> The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
> >>> incorrectly returning `/.../bar/dir` rather than `/.../bar`.
>
> I'm about to go off the list until the 29th so I wont be working on it
> soon either but I think the problem is that git sets $GIT_DIR when it is
> run from a linked worktree. I've reproduced the commit message from
> ff5b7913f0a (sequencer, stash: fix running from worktree subdir,
> 2022-01-26) below which I think explains the problem we're seeing here.
> Unfortunately the approach of setting $GIT_WORK_TREE used in that commit
> won't work for exec commands as they may be run in a different worktree.

Maybe. Maybe not. exec'ing a command in a worktree other than the
current worktree may be a "don't do it if it hurts" situation. The
same shortcoming you describe would crop up when exec'ing a command in
a foreign repository from the one in which `git rebase -i` is being
run. If we look at it that way ("don't do it if it hurts"), then
perhaps a documentation update is warranted; something along the lines
[1] which gives explains that GIT_* environment variables should be
cleared by a Git hook if it needs to peek into a foreign repository or
other worktree. It's not a perfectly satisfactory answer, but would at
least (somewhat?) allay your concern about `git rebase -i` setting
GIT_WORK_TREE automatically.

[1]: https://lore.kernel.org/git/pull.1457.git.1673171924727.gitgitgadget@gmail.com/

```

## Elijah Newren, 2024-10-17 21:16

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <CABPp-BG6kYvx_EDRUfTUtcPrpnDT=FyAMDVXrQVyVJF5Rdj_tw@mail.gmail.com>
URL: https://gitlist.dev/e/CABPp-BG6kYvx_EDRUfTUtcPrpnDT%3DFyAMDVXrQVyVJF5Rdj_tw%40mail.gmail.com
In-Reply-To: <ZxAjz28QLwWlDVg1@nand.local>

```
On Wed, Oct 16, 2024 at 1:36 PM Taylor Blau <me@ttaylorr.com> wrote:
>
> On Wed, Oct 16, 2024 at 10:15:49AM +0100, Phillip Wood wrote:
> > On 15/10/2024 21:01, Eric Sunshine wrote:
> > > On Tue, Oct 15, 2024 at 2:55 PM David Moberg <kaddkaka@gmail.com> wrote:
> > > > Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
> > > > > This looks like unintentional behavior; probably a bug. It seems to be
> > > > > triggered by `git rebase -i` setting GIT_DIR. Here's an even simpler
> > > > > reproduction recipe:
> > > > >
> > > > >      % git init foo
> > > > >      % cd foo
> > > > >      % mkdir dir
> > > > >      % echo foo >dir/file
> > > > >      % git add dir/file
> > > > >      % git commit -m foo
> > > > >      % git worktree add ../bar
> > > > >      % cd ../bar
> > > > >      % git -C dir rev-parse --show-toplevel
> > > > >      /.../bar
> > > > >      % GIT_DIR=../../foo/.git/worktrees/bar \
> > > > >          git -C dir rev-parse --show-toplevel
> > > > >      /.../bar/dir
> > > > >
> > > > > The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
> > > > > incorrectly returning `/.../bar/dir` rather than `/.../bar`.
> > > >
> > > > Thanks, that is indeed a much smaller example and it seems to exhibit
> > > > the same issue. Can we figure out how to fix it?
> > >
> > > Someone is going to have to dig into the code, but my Git time is very
> > > limited right now, so perhaps someone else can do the digging.
> >
> > I'm about to go off the list until the 29th so I wont be working on it soon
> > either but I think the problem is that git sets $GIT_DIR when it is run from
> > a linked worktree. I've reproduced the commit message from ff5b7913f0a
> > (sequencer, stash: fix running from worktree subdir, 2022-01-26) below which
> > I think explains the problem we're seeing here. Unfortunately the approach
> > of setting $GIT_WORK_TREE used in that commit won't work for exec commands
> > as they may be run in a different worktree. Naively I feel that if
> > setup_git_directory() has found ".git" then any git subprocesses run in the
> > worktree should also be able to find ".git" and so it should not be setting
> > $GIT_DIR but there maybe there is some subtlety I'm missing
>
> Let's see if that commit's author Elijah (CC'd) has any other thoughts.
>
> Thanks,
> Taylor

My commit merely pointed out that long before that commit came along,
if GIT_DIR is set but GIT_WORK_TREE is not, then the working tree is
assumed to be ".".  As such, a command like the above where
`--show-toplevel` is run with just GIT_DIR set (to anything) will
merely expand "." and show you that path.

If you are going to be having subprocesses that depend upon the git
directory and the git working tree, I think there are two options:
  * Set GIT_WORK_TREE in addition to GIT_DIR (as my patch does in certain cases)
  * Stop setting GIT_DIR if you're not going to set GIT_WORK_TREE

The second point is a bit harder since setup.c automatically sets
GIT_DIR for you in various cases, so if you want to go that route it
really means you'd have to actively unset GIT_DIR in those cases.
But, you'd have to be careful since you only want to unset it when
setup_discovered_git_dir() sets it for you, not when the user who
invoked your command had manually set GIT_DIR.  So, there is a little
bit of a pickle here...

```

## phillip.wood123@gmail.com, 2024-11-05 15:24

Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Message-ID: <743043bf-60b7-4ed7-8cf2-4f3f972968a6@gmail.com>
URL: https://gitlist.dev/e/743043bf-60b7-4ed7-8cf2-4f3f972968a6%40gmail.com
In-Reply-To: <CAPig+cQmGXxDshTovdAYaZn5UMr3nvXHyH0q2HvAbaT_fhhiLQ@mail.gmail.com>

```
On 16/10/2024 22:12, Eric Sunshine wrote:
> On Wed, Oct 16, 2024 at 5:15 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>> On 15/10/2024 21:01, Eric Sunshine wrote:
>>>> Den tis 15 okt. 2024 kl 09:11 skrev Eric Sunshine <sunshine@sunshineco.com>:
>>>>> This looks like unintentional behavior; probably a bug. It seems to be
>>>>> triggered by `git rebase -i` setting GIT_DIR. [...]
>>>>>       % git -C dir rev-parse --show-toplevel
>>>>>       /.../bar
>>>>>       % GIT_DIR=../../foo/.git/worktrees/bar \
>>>>>           git -C dir rev-parse --show-toplevel
>>>>>       /.../bar/dir
>>>>>
>>>>> The `git rev-parse --show-toplevel` invocation with GIT_DIR set is
>>>>> incorrectly returning `/.../bar/dir` rather than `/.../bar`.
>>
>> I'm about to go off the list until the 29th so I wont be working on it
>> soon either but I think the problem is that git sets $GIT_DIR when it is
>> run from a linked worktree. I've reproduced the commit message from
>> ff5b7913f0a (sequencer, stash: fix running from worktree subdir,
>> 2022-01-26) below which I think explains the problem we're seeing here.
>> Unfortunately the approach of setting $GIT_WORK_TREE used in that commit
>> won't work for exec commands as they may be run in a different worktree.
> 
> Maybe. Maybe not. exec'ing a command in a worktree other than the
> current worktree may be a "don't do it if it hurts" situation. The
> same shortcoming you describe would crop up when exec'ing a command in
> a foreign repository from the one in which `git rebase -i` is being
> run. If we look at it that way ("don't do it if it hurts"), then
> perhaps a documentation update is warranted; something along the lines
> [1] which gives explains that GIT_* environment variables should be
> cleared by a Git hook if it needs to peek into a foreign repository or
> other worktree. It's not a perfectly satisfactory answer, but would at
> least (somewhat?) allay your concern about `git rebase -i` setting
> GIT_WORK_TREE automatically.

That's certainly the simplest way forward. Until 434e0636db1 (sequencer: 
do not export GIT_DIR and GIT_WORK_TREE for 'exec', 2021-12-04)
we set both GIT_DIR and GIT_WORK_TREE when running an exec command. As 
the message for that commit explains that behavior did not match the 
scripted rebase which was the motivation to stop doing it. Unfortunately 
that left us in the situation where git will sometimes set GIT_DIR when 
the user has not. Ideally I'd like to understand why we're setting 
GIT_DIR in setup_git_dir() when, at least at fist sight, it seems to be 
unnecessary. I think that would take some time so maybe we should just 
go back to setting GIT_DIR and GIT_WORK_TREE in the sequencer when 
running "exec" commands.

Best Wishes

Phillip

> [1]: https://lore.kernel.org/git/pull.1457.git.1673171924727.gitgitgadget@gmail.com/


```
