From 7426d7f934ea90aa3f3ddf5bbe6d8399d9809f8c Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 8 Nov 2026 12:41:57 +0100 Subject: [PATCH] Don't let "go to bottom" in the commits panel go up The command went to the commit above the first merged one whenever the commit below the selection wasn't merged. It did this even if the selection was below that commit already. Collect the places where the command can stop, and go to the closest one below the selection, or to the bottom if there is none. This also makes it easy to add more places to stop at. Co-Authored-By: Claude Opus 5.5 --- pkg/gui/context/commit_list_goto_bottom.go | 37 +++++++++++++------ .../context/commit_list_goto_bottom_test.go | 3 -- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/pkg/gui/context/commit_list_goto_bottom.go b/pkg/gui/context/commit_list_goto_bottom.go index f05e9dffc..5fc011347 100644 --- a/pkg/gui/context/commit_list_goto_bottom.go +++ b/pkg/gui/context/commit_list_goto_bottom.go @@ -6,18 +6,33 @@ import ( ) // Returns the index of the commit that the "go to bottom" command selects in a -// list of commits +// list of commits. If the list has several sections, the command first goes to +// the beginning of the section that the selection is in, which is its oldest +// commit. From there it goes to the beginning of the next section, and from the +// last section to the bottom of the list. +// +// The sections are: +// - the commits of the branch that aren't merged into a main branch yet func indexForGotoBottomInCommits(commits []*models.Commit, selectedIdx int) int { - if selectedIdx >= 0 && selectedIdx < len(commits)-1 { - if commits[selectedIdx+1].Status != models.StatusMerged { - _, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool { - return c.Status == models.StatusMerged - }) - if found { - return idx - 1 - } - } + stops := []int{} + + if _, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool { + return c.Status == models.StatusMerged + }); found { + stops = append(stops, idx-1) } - return len(commits) - 1 + return closestStopBelow(stops, selectedIdx, len(commits)-1) +} + +// Returns the smallest of the given stops that is greater than the selected +// index, or the last index if there is none +func closestStopBelow(stops []int, selectedIdx int, lastIdx int) int { + result := lastIdx + for _, stop := range stops { + if stop > selectedIdx && stop < result { + result = stop + } + } + return result } diff --git a/pkg/gui/context/commit_list_goto_bottom_test.go b/pkg/gui/context/commit_list_goto_bottom_test.go index 4bc2eac0a..4ed686064 100644 --- a/pkg/gui/context/commit_list_goto_bottom_test.go +++ b/pkg/gui/context/commit_list_goto_bottom_test.go @@ -75,10 +75,7 @@ func TestIndexForGotoBottomInCommits(t *testing.T) { commit("c1", models.StatusUnpushed), commit("m1", models.StatusMerged), }, - /* EXPECTED: expected: 4, - ACTUAL: */ - expected: 0, }, }