Repository: jesseduffield/lazygit (language: go)

Issue:
View upstream during rebasing causing crash
**Describe the bug**
View upstream during rebasing causing crash

**To Reproduce**
Steps to reproduce the behavior:

1. Rebase and the branch state is at `no branch, rebasing xx(e.g. main)`
2. View divergence from upstream on `main`
3. Lazygit crash

**Expected behavior**
Can see divergence

**Screenshots**
Will upload if needed.

**Version info:**
commit=, build date=, build source=homebrew, version=0.44.0, os=darwin, arch=arm64, git version=2.45.2
git version 2.45.2

**Additional context**
<details>
  <summary>Crash stack</summary>

  ```text
  panic: runtime error: index out of range [0] with length 0

  goroutine 1 [running]:
  github.com/jesseduffield/lazygit/pkg/gui/context.(*ListRenderer).insertNonModelItems(0x0?, {0x140009ac260, 0x2, 0x0?}, 0x14, 0x0, {0x105819420?, 0x104bd45d8?, 0x104fb0380?}, {0x0, ...})
	github.com/jesseduffield/lazygit/pkg/gui/context/list_renderer.go:124 +0x31c
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListRenderer).renderLines(0x14000157eb0, 0x0, 0x14)
	github.com/jesseduffield/lazygit/pkg/gui/context/list_renderer.go:96 +0x1d4
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).HandleRender(0x14000157ea0)
	github.com/jesseduffield/lazygit/pkg/gui/context/list_context_trait.go:105 +0xdc
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).postRefreshUpdate(0x14000121208, {0x10513c890, 0x1400017e2a0})
	github.com/jesseduffield/lazygit/pkg/gui/view_helpers.go:135 +0x88
github.com/jesseduffield/lazygit/pkg/gui.(*guiCommon).PostRefreshUpdate(0x14000138078?, {0x10513c890?, 0x1400017e2a0?})
	github.com/jesseduffield/lazygit/pkg/gui/gui_common.go:33 +0x28
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*SubCommitsHelper).ViewSubCommits(0x1400000cd38, {{0x1051303e0, 0x14000e3e0c0}, {0x14000138090, 0x16}, {0x14000138078, 0x12}, {0x10513c078, 0x14000031ae0}, 0x0})
	github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers/sub_commits_helper.go:70 +0x468
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*BranchesController).viewUpstreamOptions.func1()
	github.com/jesseduffield/lazygit/pkg/gui/controllers/branches_controller.go:206 +0x1b8
github.com/jesseduffield/lazygit/pkg/gui/context.(*MenuContext).OnMenuPress(0x14000088820?, 0x140001aa0e0)
	github.com/jesseduffield/lazygit/pkg/gui/context/menu_context.go:206 +0x128
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*MenuController).press(0x140000318e0?, 0x140001aa0e0)
	github.com/jesseduffield/lazygit/pkg/gui/controllers/menu_controller.go:72 +0x40
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*MenuController).GetKeybindings.(*ListControllerTrait[...]).withItem.func4()
	github.com/jesseduffield/lazygit/pkg/gui/controllers/list_controller_trait.go:116 +0x48
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).callKeybindingHandler(0x14000121208, 0x14000cef2c0?)
	github.com/jesseduffield/lazygit/pkg/gui/keybindings.go:465 +0x11c
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).SetKeybinding.func1()
	github.com/jesseduffield/lazygit/pkg/gui/keybindings.go:412 +0x24
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).SetKeybinding.(*Gui).wrappedHandler.func3(0x140013594a8?, 0x10463abcc?)
	github.com/jesseduffield/lazygit/pkg/gui/keybindings.go:406 +0x24
github.com/jesseduffield/gocui.(*Gui).execKeybinding(0x14001359630?, 0x2?, 0x3ae05c700b3?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:1529 +0x6c
github.com/jesseduffield/gocui.(*Gui).execKeybindings(0x14000102c40, 0x140000c6200, 0x140013595b0)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:1497 +0x398
github.com/jesseduffield/gocui.(*Gui).onKey(0x140000a2628?, 0x14001359530?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:1308 +0x170
github.com/jesseduffield/gocui.(*Gui).handleEvent(0x140000a2600?, 0x14001359598?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:842 +0x54
github.com/jesseduffield/gocui.(*Gui).processEvent(0x14000102c40)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:796 +0x180
github.com/jesseduffield/gocui.(*Gui).MainLoop(0x14000102c40)
	github.com/jesseduffield/gocui@v0.3.1-0.20240906064314-bfab49c720d7/gui.go:775 +0xf0
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).Run(0x14000121208, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:846 +0x424
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError.func1()
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:852 +0x40
github.com/jesseduffield/lazygit/pkg/utils.SafeWithError(0x1400012d360?)
	github.com/jesseduffield/lazygit/pkg/utils/utils.go:99 +0x5c
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError(0x14000121208, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:851 +0xac
github.com/jesseduffield/lazygit/pkg/app.(*App).Run(...)
	github.com/jesseduffield/lazygit/pkg/app/app.go:270
github.com/jesseduffield/lazygit/pkg/app.Run({0x105135698?, 0x14000122370?}, 0x1400013d740, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/app/app.go:48 +0x9c
github.com/jesseduffield/lazygit/pkg/app.Start(0x14000045ef8, {0x0, 0x0})
	github.com/jesseduffield/lazygit/pkg/app/entry_point.go:167 +0x98c
main.main()
	github.com/jesseduffield/lazygit/main.go:23 +0x98
  ```
</details>

**Note:** please try updating to the latest version or [manually building](https://github.com/jesseduffield/lazygit/#manual) the latest `master` to see if the issue still occurs.

<!--
If you want to try and debug this issue yourself, you can run `lazygit --debug` in one terminal panel and `lazygit --logs` in another to view the logs.
-->



Discussion / hints:
I have seen this crash too, but wasn't able to reliably reproduce it yet. Trying your reproduction steps doesn't result in a crash for me.

There must be more conditions to making it crash; something about where the current selection in the commits panel is, or how long the divergence log is, or something like that. It would be great if you could narrow it down some more.
Ohh I forgot one thing, when rebasing, there're some files in conflicts. In my case, there is a single file in conflicts.

Fix patch:
diff --git a/pkg/gui/context/list_renderer.go b/pkg/gui/context/list_renderer.go
index b89eccf8b22..06fc69b0c0e 100644
--- a/pkg/gui/context/list_renderer.go
+++ b/pkg/gui/context/list_renderer.go
@@ -121,7 +121,10 @@ func (self *ListRenderer) insertNonModelItems(
 			break
 		}
 		if item.Index+offset >= startIdx {
-			padding := strings.Repeat(" ", columnPositions[item.Column])
+			padding := ""
+			if columnPositions != nil {
+				padding = strings.Repeat(" ", columnPositions[item.Column])
+			}
 			lines = slices.Insert(lines, item.Index+offset-startIdx, padding+item.Content)
 		}
 		offset++


Test patch:
diff --git a/pkg/integration/tests/branch/show_divergence_from_upstream_no_divergence.go b/pkg/integration/tests/branch/show_divergence_from_upstream_no_divergence.go
new file mode 100644
index 00000000000..5b446fcb83b
--- /dev/null
+++ b/pkg/integration/tests/branch/show_divergence_from_upstream_no_divergence.go
@@ -0,0 +1,34 @@
+package branch
+
+import (
+	"github.com/jesseduffield/lazygit/pkg/config"
+	. "github.com/jesseduffield/lazygit/pkg/integration/components"
+)
+
+var ShowDivergenceFromUpstreamNoDivergence = NewIntegrationTest(NewIntegrationTestArgs{
+	Description:  "Show divergence from upstream when the divergence view is empty",
+	ExtraCmdArgs: []string{},
+	Skip:         false,
+	SetupConfig:  func(config *config.AppConfig) {},
+	SetupRepo: func(shell *Shell) {
+		shell.EmptyCommit("commit1")
+		shell.CloneIntoRemote("origin")
+		shell.SetBranchUpstream("master", "origin/master")
+	},
+	Run: func(t *TestDriver, keys config.KeybindingConfig) {
+		t.Views().Branches().
+			Focus().
+			Lines(Contains("master")).
+			Press(keys.Branches.SetUpstream)
+
+		t.ExpectPopup().Menu().Title(Contains("Upstream")).Select(Contains("View divergence from upstream")).Confirm()
+
+		t.Views().SubCommits().
+			IsFocused().
+			Title(Contains("Commits (master <-> origin/master)")).
+			Lines(
+				Contains("--- Remote ---"),
+				Contains("--- Local ---"),
+			)
+	},
+})
diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go
index 301a5a53035..db79247ffea 100644
--- a/pkg/integration/tests/test_list.go
+++ b/pkg/integration/tests/test_list.go
@@ -65,6 +65,7 @@ var tests = []*components.IntegrationTest{
 	branch.SetUpstream,
 	branch.ShowDivergenceFromBaseBranch,
 	branch.ShowDivergenceFromUpstream,
+	branch.ShowDivergenceFromUpstreamNoDivergence,
 	branch.SortLocalBranches,
 	branch.SortRemoteBranches,
 	branch.SquashMerge,