Fix crash when opening lazygit at the root of a bare repository#5594
Open
pedrampdd wants to merge 1 commit into
Open
Fix crash when opening lazygit at the root of a bare repository#5594pedrampdd wants to merge 1 commit into
pedrampdd wants to merge 1 commit into
Conversation
When lazygit was started from the root of a bare repository (a folder
that uses worktrees, where .git points at a sibling .bare directory),
GetRepoPathsForDir would call
git rev-parse --show-toplevel --absolute-git-dir --git-common-dir \
--is-bare-repository --show-superproject-working-tree
which exits with "fatal: this operation must be run in a work tree"
because --show-toplevel cannot run without a working tree. lazygit then
crashed before its existing bare-repo prompt could ever fire.
Catch that failure and retry without --show-toplevel. If the fallback
query reports the repository is bare, synthesise the worktree path
from the parent of the git dir (which is the directory the user
invoked lazygit from). The non-bare fast path is unchanged. Errors
from causes other than a missing working tree are surfaced with the
original message, so genuine "not a git repository" failures still
look the same.
After the fix, opening lazygit at the bare-repo root reaches the
existing "Lazygit does not yet support bare repos. Open most recent
repo?" prompt instead of dying with a confusing rev-parse error.
Adds two unit tests exercising the new fallback: one for the
bare-repo-without-working-tree case (success), and one verifying that
non-bare rev-parse failures still surface their original error.
Fixes jesseduffield#5469
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5469.
The bug
When the user runs
lazygitfrom the root of a bare repository (a folder where.gitpoints at a sibling.baredirectory and worktrees live alongside it), lazygit crashes immediately with:Reproduction (from the issue):
Root cause
GetRepoPathsForDircallsgit rev-parse --show-toplevel --absolute-git-dir --git-common-dir --is-bare-repository --show-superproject-working-tree.--show-toplevelexits non-zero withfatal: this operation must be run in a work treewhen there is no working tree, so the entire query fails before the--is-bare-repositoryflag is ever inspected.Lazygit already has explicit handling for bare repos (
pkg/gui/recent_repos_panel.gochecksgit.Status.IsBareRepo()and surfaces a "Lazygit does not yet support bare repos. Open most recent repo?" prompt) but that path was unreachable because path discovery crashed first.The fix
If the full rev-parse fails, retry without
--show-toplevel. If the fallback reportsis-bare-repository=true, build theRepoPathsvalue with the parent of the git dir as the worktree path (which is the directory the user invoked lazygit from). The non-bare fast path is unchanged: it's still a single rev-parse call.If the fallback also fails, or if it succeeds but reports the repo is not bare, the original error is surfaced unchanged so genuine "not a git repository" failures still look the same to users.
Verification
bare repo without working tree(new fallback succeeds) andrev-parse fails with non-bare cause(fallback runs but original error is preserved) pass alongside the existing scenarios.pkg/commands/...tests pass.This PR is intentionally a minimum-impact fix for the crash. Extending lazygit to actually open a worktree of a bare repo (which is the deeper feature the reporter asks for) is a separate larger change.