Skip to content

Commit c3081ef

Browse files
authored
Make '>' first jump to the beginning of the branch, and only then to the first commit (#4544)
- **PR Description** In longer branches there's often the need to jump to the beginning of the branch, e.g. in order to re-review all commits from the beginning. There's no easy way to do this in lazygit. In this PR I overload the "go to bottom" key (`>`) to jump to the first commit of the current branch if the selection is above it, and only then jump to the very bottom. I like that we don't need to introduce a new key binding for this.
2 parents a27db87 + 636b94c commit c3081ef

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

pkg/gui/context/list_context_trait.go

+4
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,7 @@ func (self *ListContextTrait) TotalContentHeight() int {
149149
}
150150
return result
151151
}
152+
153+
func (self *ListContextTrait) IndexForGotoBottom() int {
154+
return self.list.Len() - 1
155+
}

pkg/gui/context/local_commits_context.go

+17
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,20 @@ func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPosi
289289
strings.Contains(normalize(commit.ExtraInfo), searchStr) // allow searching for tags
290290
})
291291
}
292+
293+
func (self *LocalCommitsContext) IndexForGotoBottom() int {
294+
commits := self.GetCommits()
295+
selectedIdx := self.GetSelectedLineIdx()
296+
if selectedIdx >= 0 && selectedIdx < len(commits)-1 {
297+
if commits[selectedIdx+1].Status != models.StatusMerged {
298+
_, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool {
299+
return c.Status == models.StatusMerged
300+
})
301+
if found {
302+
return idx - 1
303+
}
304+
}
305+
}
306+
307+
return self.list.Len() - 1
308+
}

pkg/gui/context/sub_commits_context.go

+17
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,20 @@ func (self *SubCommitsContext) RefForAdjustingLineNumberInDiff() string {
227227
func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
228228
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
229229
}
230+
231+
func (self *SubCommitsContext) IndexForGotoBottom() int {
232+
commits := self.GetCommits()
233+
selectedIdx := self.GetSelectedLineIdx()
234+
if selectedIdx >= 0 && selectedIdx < len(commits)-1 {
235+
if commits[selectedIdx+1].Status != models.StatusMerged {
236+
_, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool {
237+
return c.Status == models.StatusMerged
238+
})
239+
if found {
240+
return idx - 1
241+
}
242+
}
243+
}
244+
245+
return self.list.Len() - 1
246+
}

pkg/gui/controllers/list_controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ func (self *ListController) HandleGotoTop() error {
138138
}
139139

140140
func (self *ListController) HandleGotoBottom() error {
141-
return self.handleLineChange(self.context.GetList().Len())
141+
bottomIdx := self.context.IndexForGotoBottom()
142+
change := bottomIdx - self.context.GetList().GetSelectedLineIdx()
143+
return self.handleLineChange(change)
142144
}
143145

144146
func (self *ListController) HandleToggleRangeSelect() error {

pkg/gui/controllers/local_commits_controller.go

-22
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,6 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [
207207
Description: self.c.Tr.MarkAsBaseCommit,
208208
Tooltip: self.c.Tr.MarkAsBaseCommitTooltip,
209209
},
210-
// overriding this navigation keybinding because we might need to load
211-
// more commits on demand
212-
{
213-
Key: opts.GetKey(opts.Config.Universal.GotoBottom),
214-
Handler: self.gotoBottom,
215-
Description: self.c.Tr.GotoBottom,
216-
Tag: "navigation",
217-
},
218210
}
219211

220212
for _, binding := range outsideFilterModeBindings {
@@ -1154,20 +1146,6 @@ func (self *LocalCommitsController) openSearch() error {
11541146
return self.c.Helpers().Search.OpenSearchPrompt(self.context())
11551147
}
11561148

1157-
func (self *LocalCommitsController) gotoBottom() error {
1158-
// we usually lazyload these commits but now that we're jumping to the bottom we need to load them now
1159-
if self.context().GetLimitCommits() {
1160-
self.context().SetLimitCommits(false)
1161-
if err := self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
1162-
return err
1163-
}
1164-
}
1165-
1166-
self.context().SetSelectedLineIdx(self.context().Len() - 1)
1167-
1168-
return nil
1169-
}
1170-
11711149
func (self *LocalCommitsController) handleOpenLogMenu() error {
11721150
return self.c.Menu(types.CreateMenuOptions{
11731151
Title: self.c.Tr.LogMenuTitle,

pkg/gui/types/context.go

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ type IListContext interface {
180180
IsListContext() // used for type switch
181181
RangeSelectEnabled() bool
182182
RenderOnlyVisibleLines() bool
183+
184+
IndexForGotoBottom() int
183185
}
184186

185187
type IPatchExplorerContext interface {

0 commit comments

Comments
 (0)