Skip to content

Fix crash when opening lazygit at the root of a bare repository#5594

Open
pedrampdd wants to merge 1 commit into
jesseduffield:masterfrom
pedrampdd:fix/bare-repo-rev-parse-5469
Open

Fix crash when opening lazygit at the root of a bare repository#5594
pedrampdd wants to merge 1 commit into
jesseduffield:masterfrom
pedrampdd:fix/bare-repo-rev-parse-5469

Conversation

@pedrampdd
Copy link
Copy Markdown

Fixes #5469.

The bug

When the user runs lazygit from the root of a bare repository (a folder where .git points at a sibling .bare directory and worktrees live alongside it), lazygit crashes immediately with:

*errors.errorString Error getting repo paths: 'git -C <dir> rev-parse --path-format=absolute --show-toplevel --absolute-git-dir --git-common-dir --is-bare-repository --show-superproject-working-tree' failed: fatal: this operation must be run in a work tree

Reproduction (from the issue):

mkdir prom && cd prom
git clone --bare https://github.com/octocat/Hello-World.git .bare
echo 'gitdir: ./.bare' > .git
git worktree add foo HEAD
lazygit       # crashes here

Root cause

GetRepoPathsForDir calls git rev-parse --show-toplevel --absolute-git-dir --git-common-dir --is-bare-repository --show-superproject-working-tree. --show-toplevel exits non-zero with fatal: this operation must be run in a work tree when there is no working tree, so the entire query fails before the --is-bare-repository flag is ever inspected.

Lazygit already has explicit handling for bare repos (pkg/gui/recent_repos_panel.go checks git.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 reports is-bare-repository=true, build the RepoPaths value 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

  • New tests bare repo without working tree (new fallback succeeds) and rev-parse fails with non-bare cause (fallback runs but original error is preserved) pass alongside the existing scenarios.
  • All pkg/commands/... tests pass.
  • Manual repro using the reproduction steps above: before the fix lazygit crashes; after the fix it shows the existing "does not yet support bare repos. Open most recent repo?" prompt — strictly better than a crash, and gives the user a recovery option.

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when trying to run lazygit in bare repo

1 participant