[PATCH v4 1/1] git-gui: handle missing worktree and separated gitdir
- From
- Shroom Moo <egg_mushroomcow@foxmail.com>
- Date
- May 1, 2026, 10:54 UTC
- Message-ID
- <tencent_277823B7C5D69914E168E5679A907C655606@qq.com>
- In-Reply-To
- <tencent_8A236D9D4A8D8CCA7DAA083157AA8543700A@qq.com>
When git-gui is started from a directory that Git recognizes as a valid repository but the working tree is not accessible (e.g., a separated gitdir created by `git clone --separate-git-dir`, a bare repository, or a case where the worktree directory was removed), it previously called `rev-parse --show-toplevel` without error handling, causing a fatal Tcl error ("this operation must be run in a work tree").
Wrap the call in a `catch` and handle the failure as follows:
- For bare repositories, keep `_gitworktree` empty so that the existing `is_bare` check shows "Cannot use bare repository" and exits. No behavioral change.
- If we are inside the gitdir (i.e., `--is-inside-git-dir` is `true`), refuse to start with a clear message: "Cannot start git-gui from inside the Git directory. Please run git-gui from a working tree." This prevents dangerous operations in a separated gitdir or when starting from a .git subdirectory, where ordinary Git commands like `git status` also refuse to run.
- Otherwise, leave `_gitworktree` empty so that the existing error paths (e.g., missing worktree) can display their own diagnostics.
This fix intentionally drops the previous ability to start git-gui from within a regular repository's .git subdirectory. That behavior was never reliable (Git itself forbids operations inside .git) and broke completely in the multi‑worktree case where the parent directory is not guaranteed to be the intended worktree. The error message guides the user to start git-gui from a worktree instead.
Signed-off-by: Shroom Moo <egg_mushroomcow@foxmail.com> --- git-gui/git-gui.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 23fe76e498..9e848aa26b 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -1169,7 +1169,17 @@ if {![file isdirectory $_gitdir]} { load_config 0 apply_config -set _gitworktree [git rev-parse --show-toplevel] +if {[catch {set _gitworktree [git rev-parse --show-toplevel]}]} { + if {![catch {set bare [git rev-parse --is-bare-repository]}] && $bare eq {true}} { + set _gitworktree {} + } elseif {![catch {set inside [git rev-parse --is-inside-git-dir]}] && $inside eq {true}} { + catch {wm withdraw .} + error_popup [mc "Cannot start git-gui from inside the Git directory\nPlease run git-gui from a working tree"] + exit 1 + } else { + set _gitworktree {} + } +} if {$_prefix ne {}} { if {$_gitworktree eq {}} {
-- 2.52.0.windows.1