jesseduffield / lazygit

simple terminal UI for git commands
MIT License
52.99k stars 1.85k forks source link

Moving the selection to the commit list before they've loaded causes a crash #3332

Closed Un1q32 closed 8 months ago

Un1q32 commented 8 months ago

Describe the bug Title

To Reproduce Steps to reproduce the behavior:

  1. Open lazygit on a repo large enough that the commit list takes a moment to load
  2. Press left arrow twice quickly to move the selection to the commit list
  3. Crash

Expected behavior To select the commits as normal once they are done loading

Screenshots

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListViewModel[...]).GetSelected(0x19dd468)
    github.com/jesseduffield/lazygit/pkg/gui/context/list_view_model.go:29 +0x6c
github.com/jesseduffield/lazygit/pkg/gui/context.NewLocalCommitsContext.func2(0xffffffffffffffff?, 0x12c?)
    github.com/jesseduffield/lazygit/pkg/gui/context/local_commits_context.go:34 +0xa5
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).refreshViewport(0xc00013b680)
    github.com/jesseduffield/lazygit/pkg/gui/context/list_context_trait.go:51 +0x47
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).FocusLine(0xc00013b680)
    github.com/jesseduffield/lazygit/pkg/gui/context/list_context_trait.go:45 +0x87
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).HandleFocus(0xc00013b680, {{0x0?, 0xc0000106a8?}, 0xc00011a930?})
    github.com/jesseduffield/lazygit/pkg/gui/context/list_context_trait.go:65 +0x36
github.com/jesseduffield/lazygit/pkg/gui.(*ContextMgr).ActivateContext(0xc00013bac0, {0x19f5548, 0xc0000106a8}, {{0x0?, 0x1?}, 0xc00072f2f0?})
    github.com/jesseduffield/lazygit/pkg/gui/context.go:250 +0x20e
github.com/jesseduffield/lazygit/pkg/gui.(*ContextMgr).Push(0xc000725510?, {0x19f5548, 0xc0000106a8}, {0x0?, 0x18aef8d?, 0x7?})
    github.com/jesseduffield/lazygit/pkg/gui/context.go:86 +0x1c7
github.com/jesseduffield/lazygit/pkg/gui.(*guiCommon).PushContext(0xc00008e6f0?, {0x19f5548?, 0xc0000106a8?}, {0x0?, 0xc000092078?, 0xc00036e000?})
    github.com/jesseduffield/lazygit/pkg/gui/gui_common.go:44 +0x33
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*SideWindowController).nextSideWindow(0xc000011128)
    github.com/jesseduffield/lazygit/pkg/gui/controllers/side_window_controller.go:95 +0x1e3
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).SetKeybinding.func2(0x1366c63?, 0xc00072f5cc?)
    github.com/jesseduffield/lazygit/pkg/gui/keybindings.go:373 +0x1b
github.com/jesseduffield/gocui.(*Gui).execKeybinding(0xc00072f4e8?, 0x100d7aa?, 0xc00081b610?)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:1424 +0x59
github.com/jesseduffield/gocui.(*Gui).execKeybindings(0xc000102340, 0xc000412f00, 0xc00072f648)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:1392 +0x451
github.com/jesseduffield/gocui.(*Gui).onKey(0xc00009e088?, 0xc00072f568?)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:1249 +0xf7
github.com/jesseduffield/gocui.(*Gui).handleEvent(0xc00009e060?, 0xc00072f5c8?)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:798 +0x4a
github.com/jesseduffield/gocui.(*Gui).processEvent(0xc000102340)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:752 +0x20b
github.com/jesseduffield/gocui.(*Gui).MainLoop(0xc000102340)
    github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:739 +0xa5
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).Run(0xc000164000, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
    github.com/jesseduffield/lazygit/pkg/gui/gui.go:669 +0x5e5
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError.func1()
    github.com/jesseduffield/lazygit/pkg/gui/gui.go:675 +0x4c
github.com/jesseduffield/lazygit/pkg/utils.SafeWithError(0x0?)
    github.com/jesseduffield/lazygit/pkg/utils/utils.go:108 +0x67
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError(0xc000164000, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
    github.com/jesseduffield/lazygit/pkg/gui/gui.go:674 +0xd4
github.com/jesseduffield/lazygit/pkg/app.(*App).Run(...)
    github.com/jesseduffield/lazygit/pkg/app/app.go:263
github.com/jesseduffield/lazygit/pkg/app.Run({0x19eff30?, 0xc00031efa0?}, 0xc0003520f0, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
    github.com/jesseduffield/lazygit/pkg/app/app.go:48 +0xe9
github.com/jesseduffield/lazygit/pkg/app.Start(0xc000257f30, {0x0, 0x0})
    github.com/jesseduffield/lazygit/pkg/app/entry_point.go:150 +0xd46
main.main()
    github.com/jesseduffield/lazygit/main.go:23 +0x9e

Version info:

commit=, build date=, build source=homebrew, version=0.40.2, os=darwin, arch=amd64, git version=2.39.3 (Apple Git-145)
git version 2.39.3 (Apple Git-145)

Additional context If you select the commits too fast it won't crash, I'm not sure what the exact timing is, but it's easy enough that I've reproduced it 5 times in a few minutes, about the same time as the commits are loaded maybe

stefanhaller commented 8 months ago

Very likely the same as #3321.