* git rebase exec make -C in worktree confuses repo root dir @ 2024-10-14 20:46 David Moberg 2024-10-14 21:19 ` Taylor Blau 0 siblings, 1 reply; 12+ messages in thread From: David Moberg @ 2024-10-14 20:46 UTC (permalink / raw) To: git@vger.kernel.org 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] ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 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 0 siblings, 2 replies; 12+ messages in thread From: Taylor Blau @ 2024-10-14 21:19 UTC (permalink / raw) To: David Moberg; +Cc: git@vger.kernel.org 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-14 21:19 ` Taylor Blau @ 2024-10-14 21:34 ` David Moberg 2024-10-15 7:11 ` Eric Sunshine 1 sibling, 0 replies; 12+ messages in thread From: David Moberg @ 2024-10-14 21:34 UTC (permalink / raw) To: Taylor Blau; +Cc: git@vger.kernel.org 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 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 1 sibling, 1 reply; 12+ messages in thread From: Eric Sunshine @ 2024-10-15 7:11 UTC (permalink / raw) To: Taylor Blau; +Cc: David Moberg, git@vger.kernel.org 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`. ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-15 7:11 ` Eric Sunshine @ 2024-10-15 18:55 ` David Moberg 2024-10-15 20:01 ` Eric Sunshine 0 siblings, 1 reply; 12+ messages in thread From: David Moberg @ 2024-10-15 18:55 UTC (permalink / raw) To: Eric Sunshine; +Cc: Taylor Blau, git@vger.kernel.org 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`. ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-15 18:55 ` David Moberg @ 2024-10-15 20:01 ` Eric Sunshine 2024-10-16 9:15 ` Phillip Wood 0 siblings, 1 reply; 12+ messages in thread From: Eric Sunshine @ 2024-10-15 20:01 UTC (permalink / raw) To: David Moberg; +Cc: Taylor Blau, git@vger.kernel.org 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.) ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-15 20:01 ` Eric Sunshine @ 2024-10-16 9:15 ` Phillip Wood 2024-10-16 20:36 ` Taylor Blau 2024-10-16 21:12 ` Eric Sunshine 0 siblings, 2 replies; 12+ messages in thread From: Phillip Wood @ 2024-10-16 9:15 UTC (permalink / raw) To: Eric Sunshine, David Moberg; +Cc: Taylor Blau, git@vger.kernel.org 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.) > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-16 9:15 ` Phillip Wood @ 2024-10-16 20:36 ` Taylor Blau 2024-10-17 21:16 ` Elijah Newren 2024-10-16 21:12 ` Eric Sunshine 1 sibling, 1 reply; 12+ messages in thread From: Taylor Blau @ 2024-10-16 20:36 UTC (permalink / raw) To: phillip.wood Cc: Eric Sunshine, David Moberg, Elijah Newren, git@vger.kernel.org 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-16 20:36 ` Taylor Blau @ 2024-10-17 21:16 ` Elijah Newren 0 siblings, 0 replies; 12+ messages in thread From: Elijah Newren @ 2024-10-17 21:16 UTC (permalink / raw) To: Taylor Blau Cc: phillip.wood, Eric Sunshine, David Moberg, git@vger.kernel.org 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... ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-16 9:15 ` Phillip Wood 2024-10-16 20:36 ` Taylor Blau @ 2024-10-16 21:12 ` Eric Sunshine 2024-11-05 15:24 ` phillip.wood123 1 sibling, 1 reply; 12+ messages in thread From: Eric Sunshine @ 2024-10-16 21:12 UTC (permalink / raw) To: phillip.wood Cc: David Moberg, Taylor Blau, git@vger.kernel.org, Elijah Newren 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/ ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: git rebase exec make -C in worktree confuses repo root dir 2024-10-16 21:12 ` Eric Sunshine @ 2024-11-05 15:24 ` phillip.wood123 0 siblings, 0 replies; 12+ messages in thread From: phillip.wood123 @ 2024-11-05 15:24 UTC (permalink / raw) To: Eric Sunshine, phillip.wood Cc: David Moberg, Taylor Blau, git@vger.kernel.org, Elijah Newren, David Moberg 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/ ^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <1730787532-3757-mlmmj-5e7be4fc@vger.kernel.org>]
[parent not found: <CAL2+Mivva3AFR4of0-2d48YDDMbHiNVsUmCzhezHfe+h9faEvQ@mail.gmail.com>]
[parent not found: <CAPig+cTVfNW4AFJKyGbRcy0_YJEJGcNYNx57USyp4zz1g9fSeQ@mail.gmail.com>]
* Re: git rebase exec make -C in worktree confuses repo root dir [not found] ` <CAPig+cTVfNW4AFJKyGbRcy0_YJEJGcNYNx57USyp4zz1g9fSeQ@mail.gmail.com> @ 2024-11-28 15:07 ` Phillip Wood 0 siblings, 0 replies; 12+ messages in thread From: Phillip Wood @ 2024-11-28 15:07 UTC (permalink / raw) To: Eric Sunshine, David Moberg Cc: Elijah Newren, Taylor Blau, phillip.wood, david.moberg, Git Mailing List On 28/11/2024 07:36, Eric Sunshine wrote: > On Wed, Nov 27, 2024 at 11:04 PM David Moberg <kaddkaka@gmail.com> wrote: >>> To: Elijah Newren <newren@gmail.com> >>>> 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... >>> >>> So, what is the way forward? Is the option of setting GIT_WORK_TREE more robust? >> >> Hi, sorry for posting this on the side but I only have Gmail on android which doesn't allow text only emails (I think). >> >> Is there any work ongoing on this item? I think I lost the conversation and/or fell out of it. > > The conversation ended at [1]. As far as I know, nobody is working on it. > > I think Phillip did a good job in [1] of summarizing the two paths > toward a possible fix. Unfortunately, both paths will probably require > a good deal of spelunking through the code and the history to > understand why the logic is the way it is, and (importantly) how to > craft a solution which won't break existing legitimate use-cases. I think the solution of always setting GIT_WORK_TREE and GIT_DIR is probably the most practical. Even if we find a way to not set GIT_DIR when git is run from a linked worktree I think we will still run into problems when core.worktree is set as that also appears to result in GIT_DIR being set without GIT_WORK_TREE. > So, it's not necessarily a small task, and whoever digs into it (if > anyone volunteers) will likely need to devote a good deal of time and > effort to it. (My Git time is severely limited these days, so that > person is unlikely to be me.) In principle I'm interested in fixing this but in practice I'm unlikely to have time to work on it until the end of Outreachy in March. If someone else wants to take a look at it in the meantime I'll definitely try and make time to answer questions and review patches Best Wishes Phillip > [1]: https://lore.kernel.org/git/743043bf-60b7-4ed7-8cf2-4f3f972968a6@gmail.com/ ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2024-11-28 15:07 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
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).