Avoid crash when the git command to obtain branches fails
- PR Description
In #4546 a user reported a crash in refresh when the repository is corrupt. It would be good not to crash, but to handle this gracefully. This PR does that.
However, this is probably not a good enough solution, because we only log the error (in RefreshHelper.refreshBranches) and don't communicate it otherwise to users. This is true for all errors that happen during refresh, though.
It is unclear how we could report refresh errors to users; showing them in a popup doesn't seem right. As long as we don't have a solution for this, it might actually be better to crash, so that users can file an issue and we tell them how to sort it out. Leaving as draft for the time being.
Coverage summary from Codacy
See diff coverage on Codacy
| Coverage variation | Diff coverage |
|---|---|
| Report missing for e6bd9d0ae6dd30d04dfe77d2cac15ac54fa18ff6[^1] | :white_check_mark: 62.50% |
Coverage variation details
| Coverable lines | Covered lines | Coverage | |
|---|---|---|---|
| Common ancestor commit (e6bd9d0ae6dd30d04dfe77d2cac15ac54fa18ff6) | Report Missing | Report Missing | Report Missing |
| Head commit (c5a2a56700d6fb7235f58e8b3f81eb5bc875d1bf) | 56008 | 48624 | 86.82% |
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>
Diff coverage details
| Coverable lines | Covered lines | Diff coverage | |
|---|---|---|---|
| Pull request (#4547) | 8 | 5 | 62.50% |
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%
See your quality gate settings Change summary preferences
[^1]: Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
I hit a similar error with lazygit 0.55.1, although the error message is a bit different:
panic: fatal: unknown error occurred while reading the configuration files
goroutine 500 [running]:
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*BranchLoader).obtainBranches(0xc0000a4140)
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/branch_loader.go:238 +0x1c9
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*BranchLoader).Load(0xc0000a4140, {0xc00005ea08, 0x462, 0x49f}, 0xc00016e640, {0xc00190e180, 0x3, 0x68?}, 0x1, 0xc001b49ef0, ...)
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/branch_loader.go:75 +0x65
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*RefreshHelper).refreshBranches(0xc0000a5640, 0x0, 0x0, 0x1)
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/refresh_helper.go:456 +0x246
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*RefreshHelper).Refresh.func2.3()
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/refresh_helper.go:131 +0x25
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*RefreshHelper).Refresh.func2.1.1({0x4110c0?, 0xc000247dc0?})
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/refresh_helper.go:106 +0x13
github.com/jesseduffield/gocui.(*Gui).onWorkerAux(0xc0000ac000, 0x44c772?, {0x110c380?, 0xc001530740?})
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:721 +0x65
github.com/jesseduffield/gocui.(*Gui).OnWorker.func1()
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:708 +0x2b
created by github.com/jesseduffield/gocui.(*Gui).OnWorker in goroutine 492
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:707 +0x90
It would be helpful to know which configuration file it tried to read when the error occured. I successfully created and pushed a feature branch a few hours ago and wanted to update my local repo after the PR was merged when this happened. After the crash the remotes were gone -- git remote -v would return nothing. I tried git fsck, but it just reported a bunch of dangling objects (which I'd assume is harmless):
$ git fsck
Checking ref database: 100% (1/1), done.
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1616/1616), done.
Checking connectivity: 4773, done.
dangling blob 2dc0bfc9c14300345e0a2ff4b5de569e3985689e
dangling commit a180346e0bab1b81113a44cf36febe2276f49fda
:
@efussi This is an error from git, it is talking about your repository's config file (.git/config). If you can't figure out what's wrong with it by looking at it in a text editor, post it here and we can help figure it out.
Thanks for looking at this, @stefanhaller! Something must have messed up my .git/config and I assume lazygit tripped over the missing remotes in there. I have no indication lazygit is to blame for the corruption, and it works fine after I fixed the config file. So please ignore my previous message, it's just a similar stack trace that led me here.