Repository: jesseduffield/lazygit (language: go)

Issue:
Refreshing custom patch view when adding commit files to custom patch
**Describe the bug**
When adding a commit file to a custom patch, the bottom part of the right half displays title "Diff" and is empty. The view updates after hitting an arrow key to navigate to another file. Something is not refreshing properly when files are added to the patch builder.

**To Reproduce**
Steps to reproduce the behavior:

1. Navigate to the commits context
2. Hit enter to view files in a commit
3. Hit space to add file to custom patch

**Expected behavior**
Immediately after adding a commit file to the patch builder, the view should update as in the "post-refresh" screenshot below.

**Screenshots**
If applicable, add screenshots to help explain your problem.
Pre-refresh:
<img width="1098" alt="pre-refresh" src="https://github.com/jesseduffield/lazygit/assets/26228688/e9799b11-53e1-426d-8a61-99eb036a4c90">

Post-refresh:
<img width="1098" alt="post-refresh" src="https://github.com/jesseduffield/lazygit/assets/26228688/05919591-5d6d-46b0-81ff-c03744f469f3">


**Version info:**
This happens on current master (05b97c8c), but does not happen on 0.40.2. I'm able to duplicate on macOS, Windows 11, and Ubuntu 22.04 (WSL).

**Additional context**
Doing a git bisect, it looks like this behavior started after 240948b8. This change on my fork (https://github.com/afhoffman/lazygit/commit/d1f1ed0c28e686f24e655aaf4d2837972a8b2517) seems to fix it but I'm not totally sure why that fix works, and it doesn't feel right.


<!--
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:
Ah, when I made the 240948b88 commit I did feel uncomfortable about it and wondered what would break. I was actually a bit surprised that nothing broke (apparently)... Thanks for the very clear bug report, and for bisecting it.

BTW, we have a similar problem in the Files view, but it's a bit harder to reproduce. The only reproduction recipe I could come up with so far is this:
- set `git.autoRefresh` to false to make it more deterministic
- modify two files in the same directory (don't stage them)
- select the containing directory in the Files view
- in a different terminal, stage one of the files with the command line
- go back to the lazygit terminal: it refreshes, and shows the unstaged file in the "Unstaged changes" pane, but the "Staged changes" pane is empty. Refreshing manually fixes it.

This also broke with 240948b, so it's likely the same root cause. I'll look into it.

Fix patch:
diff --git a/pkg/gui/controllers/helpers/inline_status_helper.go b/pkg/gui/controllers/helpers/inline_status_helper.go
index 7368986bde4..2476a57cfea 100644
--- a/pkg/gui/controllers/helpers/inline_status_helper.go
+++ b/pkg/gui/controllers/helpers/inline_status_helper.go
@@ -66,7 +66,7 @@ func (self inlineStatusHelperTask) Continue() {
 func (self *InlineStatusHelper) WithInlineStatus(opts InlineStatusOpts, f func(gocui.Task) error) {
 	context := self.c.ContextForKey(opts.ContextKey).(types.IListContext)
 	view := context.GetView()
-	visible := view.Visible && self.windowHelper.TopViewInWindow(context.GetWindowName()) == view
+	visible := view.Visible && self.windowHelper.TopViewInWindow(context.GetWindowName(), false) == view
 	if visible && context.IsItemVisible(opts.Item) {
 		self.c.OnWorker(func(task gocui.Task) {
 			self.start(opts)
diff --git a/pkg/gui/controllers/helpers/window_helper.go b/pkg/gui/controllers/helpers/window_helper.go
index 4bdd7a88972..c0bdc2ab633 100644
--- a/pkg/gui/controllers/helpers/window_helper.go
+++ b/pkg/gui/controllers/helpers/window_helper.go
@@ -90,7 +90,7 @@ func (self *WindowHelper) MoveToTopOfWindow(context types.Context) {
 
 	window := context.GetWindowName()
 
-	topView := self.TopViewInWindow(window)
+	topView := self.TopViewInWindow(window, true)
 
 	if topView != nil && view.Name() != topView.Name() {
 		if err := self.c.GocuiGui().SetViewOnTopOf(view.Name(), topView.Name()); err != nil {
@@ -99,14 +99,14 @@ func (self *WindowHelper) MoveToTopOfWindow(context types.Context) {
 	}
 }
 
-func (self *WindowHelper) TopViewInWindow(windowName string) *gocui.View {
+func (self *WindowHelper) TopViewInWindow(windowName string, includeInvisibleViews bool) *gocui.View {
 	// now I need to find all views in that same window, via contexts. And I guess then I need to find the index of the highest view in that list.
 	viewNamesInWindow := self.viewNamesInWindow(windowName)
 
 	// The views list is ordered highest-last, so we're grabbing the last view of the window
 	var topView *gocui.View
 	for _, currentView := range self.c.GocuiGui().Views() {
-		if lo.Contains(viewNamesInWindow, currentView.Name()) && currentView.Visible {
+		if lo.Contains(viewNamesInWindow, currentView.Name()) && (currentView.Visible || includeInvisibleViews) {
 			topView = currentView
 		}
 	}
diff --git a/pkg/gui/main_panels.go b/pkg/gui/main_panels.go
index 3dee86f1b0e..bf30331cd92 100644
--- a/pkg/gui/main_panels.go
+++ b/pkg/gui/main_panels.go
@@ -38,7 +38,7 @@ func (gui *Gui) moveMainContextToTop(context types.Context) {
 
 	view := context.GetView()
 
-	topView := gui.helpers.Window.TopViewInWindow(context.GetWindowName())
+	topView := gui.helpers.Window.TopViewInWindow(context.GetWindowName(), true)
 
 	if topView != nil && topView != view {
 		// We need to copy the content to avoid a flicker effect: If we're flicking


Test patch:
diff --git a/pkg/integration/tests/patch_building/select_all_files.go b/pkg/integration/tests/patch_building/select_all_files.go
index 711d31c29a1..5665cef50f5 100644
--- a/pkg/integration/tests/patch_building/select_all_files.go
+++ b/pkg/integration/tests/patch_building/select_all_files.go
@@ -6,7 +6,7 @@ import (
 )
 
 var SelectAllFiles = NewIntegrationTest(NewIntegrationTestArgs{
-	Description:  "All all files of a commit to a custom patch with the 'a' keybinding",
+	Description:  "Add all files of a commit to a custom patch with the 'a' keybinding",
 	ExtraCmdArgs: []string{},
 	Skip:         false,
 	SetupConfig:  func(config *config.AppConfig) {},