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) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-10-15 08:35:00 +02:00
co-authored by Claude Opus 5.5
parent 82207df464
commit 4d288579ee
2 changed files with 7 additions and 7 deletions
+7 -4
View File
@@ -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) {
@@ -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()
}