From 4d288579ee0e433f15dcf637883f5c72363c786e Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 13 Oct 2026 09:05:13 +0200 Subject: [PATCH] Don't panic when listing the branches fails If the repo that lazygit has open is deleted while lazygit is running, the next full refresh panics. The same happens for a linked worktree that is deleted. In both cases `git for-each-ref` fails because its working directory is gone, and BranchLoader panics when that happens. Return the error instead. The branches refresh logs it, like it does with the other errors it gets from loading the branches. Co-Authored-By: Claude Opus 5.5 (1M context) --- pkg/commands/git_commands/branch_loader.go | 11 +++++++---- pkg/commands/git_commands/branch_loader_test.go | 3 --- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index 3053cd6fa..6cb61754f 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -74,7 +74,10 @@ func (self *BranchLoader) Load(reflogCommits []*models.Commit, onWorker func(func() error), renderFunc func(), ) ([]*models.Branch, error) { - branches, tips := self.obtainBranches() + branches, tips, err := self.obtainBranches() + if err != nil { + return nil, err + } switch self.UserConfig().Git.LocalBranchSortOrder { case "date": @@ -326,10 +329,10 @@ func (self *BranchLoader) GetBaseBranch(branch *models.Branch, mainBranches *Mai // Returns the branches, along with the tip of each of them, keyed by full ref // name -func (self *BranchLoader) obtainBranches() ([]*models.Branch, map[string]refTip) { +func (self *BranchLoader) obtainBranches() ([]*models.Branch, map[string]refTip, error) { output, err := self.getRawBranches() if err != nil { - panic(err) + return nil, nil, err } trimmedOutput := strings.TrimSpace(output) @@ -355,7 +358,7 @@ func (self *BranchLoader) obtainBranches() ([]*models.Branch, map[string]refTip) return branch, true }) - return branches, tips + return branches, tips, nil } func (self *BranchLoader) getRawBranches() (string, error) { diff --git a/pkg/commands/git_commands/branch_loader_test.go b/pkg/commands/git_commands/branch_loader_test.go index eee277ca3..ad9c0b48f 100644 --- a/pkg/commands/git_commands/branch_loader_test.go +++ b/pkg/commands/git_commands/branch_loader_test.go @@ -179,11 +179,8 @@ func TestLoadWhenListingBranchesFails(t *testing.T) { cmd: gitCommon.cmd, } - /* EXPECTED: _, err := loader.Load(nil, nil, nil, false, nil, nil) assert.EqualError(t, err, "chdir /path/to/repo: no such file or directory") - ACTUAL: */ - assert.PanicsWithError(t, "chdir /path/to/repo: no such file or directory", func() { _, _ = loader.Load(nil, nil, nil, false, nil, nil) }) runner.CheckForMissingCalls() }