git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Eric Sunshine <sunshine@sunshineco.com>,
	David Moberg <kaddkaka@gmail.com>
Cc: Taylor Blau <me@ttaylorr.com>,
	"git@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: git rebase exec make -C in worktree confuses repo root dir
Date: Wed, 16 Oct 2024 10:15:49 +0100	[thread overview]
Message-ID: <1dc91aa7-04da-4023-bbd5-5b12539477ba@gmail.com> (raw)
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.)
> 


  reply	other threads:[~2024-10-16  9:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14 20:46 git rebase exec make -C in worktree confuses repo root dir David Moberg
2024-10-14 21:19 ` Taylor Blau
2024-10-14 21:34   ` David Moberg
2024-10-15  7:11   ` Eric Sunshine
2024-10-15 18:55     ` David Moberg
2024-10-15 20:01       ` Eric Sunshine
2024-10-16  9:15         ` Phillip Wood [this message]
2024-10-16 20:36           ` Taylor Blau
2024-10-17 21:16             ` Elijah Newren
2024-10-16 21:12           ` Eric Sunshine
2024-11-05 15:24             ` phillip.wood123
     [not found] <1730787532-3757-mlmmj-5e7be4fc@vger.kernel.org>
     [not found] ` <CAL2+Mivva3AFR4of0-2d48YDDMbHiNVsUmCzhezHfe+h9faEvQ@mail.gmail.com>
     [not found]   ` <CAPig+cTVfNW4AFJKyGbRcy0_YJEJGcNYNx57USyp4zz1g9fSeQ@mail.gmail.com>
2024-11-28 15:07     ` Phillip Wood

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=1dc91aa7-04da-4023-bbd5-5b12539477ba@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=kaddkaka@gmail.com \
    --cc=me@ttaylorr.com \
    --cc=phillip.wood@dunelm.org.uk \
    --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).