Skip to content

Fix escape from submodule when repo was opened with --git-dir/--work-tree - #5904

Open
xooooooooox wants to merge 1 commit into
jesseduffield:masterfrom
xooooooooox:fix-1118
Open

Fix escape from submodule when repo was opened with --git-dir/--work-tree#5904
xooooooooox wants to merge 1 commit into
jesseduffield:masterfrom
xooooooooox:fix-1118

Conversation

@xooooooooox

Copy link
Copy Markdown

PR Description

Fixes #1118.

When lazygit is opened against a dotfile-style bare repo via --git-dir/--work-tree (the yadm/vcsh setup), entering a submodule and pressing escape failed to return to the parent repo:

fatal: not a git repository (or any of the parent directories): .git

Root cause: the --git-dir/--work-tree flags only survive as the GIT_DIR/GIT_WORK_TREE env vars. Entering a submodule clears them (correctly — the submodule must not be read through the parent's git dir), but the repo-path stack records only the working directory, so escaping restores the cwd and nothing else. VerifyInGitRepo then runs git rev-parse in a worktree whose git dir cannot be discovered from the path alone. Worse, if there happens to be a repo somewhere above the worktree, escape silently switches to that (wrong) repo instead of erroring.

Fix: the repo-path stack now stores the location the repo was opened with — path plus the two env vars (StringStack generalized to Stack[RepoLocation]) — and switching back restores them. All other switch paths (recent repos, worktrees) keep the existing cleared-env semantics. A failed switch now also restores the previous env vars, so the repo we stay in keeps working.

Normal repos push empty env fields (restore == clear), so behavior is unchanged outside the bare-repo case; nested submodules likewise.

Test: new integration test submodule/enter_dotfile_bare_repo reproduces the bug on master (in the test sandbox it manifests as the silent wrong-repo switch, since the sandbox lives inside an enclosing repo) and passes with the fix.

Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (no new user-facing text)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (n/a)
  • Docs have been updated if necessary (n/a)
  • You've read through your own file changes for silly mistakes etc

Entering a submodule clears the GIT_DIR/GIT_WORK_TREE env vars, but
returning only restored the working directory. For repos opened via
--git-dir/--work-tree (dotfile-style bare repos, e.g. yadm/vcsh), the
parent repo's git dir cannot be rediscovered from the path alone, so
escape failed with 'not a git repository' -- or silently switched to an
enclosing repo when one existed above the worktree.

The repo-path stack now records the env vars alongside the path
(StringStack generalized to Stack[RepoLocation]), and switching back
restores them. Failed switches restore the previous env vars so the
repo we stay in keeps working.

Fixes jesseduffield#1118
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.

lazygit quits unexpectedly with message: Error: must be run inside a git repository

1 participant