1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/reflog_panel.go

119 lines
3.2 KiB
Go
Raw Normal View History

2020-01-09 12:34:17 +02:00
package gui
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
2020-01-09 12:34:17 +02:00
)
// list panel functions
2020-09-29 10:36:54 +02:00
func (gui *Gui) getSelectedReflogCommit() *models.Commit {
2020-08-20 00:53:10 +02:00
selectedLine := gui.State.Panels.ReflogCommits.SelectedLineIdx
reflogComits := gui.State.FilteredReflogCommits
2020-03-29 01:11:15 +02:00
if selectedLine == -1 || len(reflogComits) == 0 {
2020-01-09 12:34:17 +02:00
return nil
}
2020-03-29 01:11:15 +02:00
return reflogComits[selectedLine]
2020-01-09 12:34:17 +02:00
}
func (gui *Gui) reflogCommitsRenderToMain() error {
2020-01-09 12:34:17 +02:00
commit := gui.getSelectedReflogCommit()
2020-08-18 14:02:35 +02:00
var task updateTask
2020-01-09 12:34:17 +02:00
if commit == nil {
2021-04-04 15:51:59 +02:00
task = NewRenderStringTask("No reflog history")
} else {
2021-12-07 12:59:36 +02:00
cmdObj := gui.GitCommand.ShowCmdObj(commit.Sha, gui.State.Modes.Filtering.GetPath())
2021-12-07 12:59:36 +02:00
task = NewRunPtyTask(cmdObj.GetCmd())
2020-01-09 12:34:17 +02:00
}
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-18 14:02:35 +02:00
main: &viewUpdateOpts{
title: "Reflog Entry",
task: task,
},
})
2020-01-09 12:34:17 +02:00
}
2020-03-29 01:35:12 +02:00
// the reflogs panel is the only panel where we cache data, in that we only
// load entries that have been created since we last ran the call. This means
// we need to be more careful with how we use this, and to ensure we're emptying
// the reflogs array when changing contexts.
// This method also manages two things: ReflogCommits and FilteredReflogCommits.
// FilteredReflogCommits are rendered in the reflogs panel, and ReflogCommits
// are used by the branches panel to obtain recency values for sorting.
func (gui *Gui) refreshReflogCommits() error {
2020-03-29 01:35:12 +02:00
// pulling state into its own variable incase it gets swapped out for another state
// and we get an out of bounds exception
state := gui.State
2020-09-29 10:36:54 +02:00
var lastReflogCommit *models.Commit
2020-03-29 01:35:12 +02:00
if len(state.ReflogCommits) > 0 {
lastReflogCommit = state.ReflogCommits[0]
2020-03-26 12:12:40 +02:00
}
2020-09-29 10:36:54 +02:00
refresh := func(stateCommits *[]*models.Commit, filterPath string) error {
commits, onlyObtainedNewReflogCommits, err := gui.GitCommand.GetReflogCommits(lastReflogCommit, filterPath)
if err != nil {
return gui.surfaceError(err)
}
if onlyObtainedNewReflogCommits {
*stateCommits = append(commits, *stateCommits...)
} else {
*stateCommits = commits
}
return nil
2020-01-09 12:34:17 +02:00
}
if err := refresh(&state.ReflogCommits, ""); err != nil {
return err
}
if gui.State.Modes.Filtering.Active() {
2021-04-03 02:32:14 +02:00
if err := refresh(&state.FilteredReflogCommits, state.Modes.Filtering.GetPath()); err != nil {
return err
}
} else {
state.FilteredReflogCommits = state.ReflogCommits
}
2020-01-09 12:34:17 +02:00
2021-04-03 06:56:11 +02:00
return gui.postRefreshUpdate(gui.State.Contexts.ReflogCommits)
2020-01-09 12:34:17 +02:00
}
func (gui *Gui) handleCheckoutReflogCommit() error {
2020-01-09 13:08:28 +02:00
commit := gui.getSelectedReflogCommit()
if commit == nil {
return nil
}
2020-08-15 08:38:16 +02:00
err := gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.LcCheckoutCommit,
prompt: gui.Tr.SureCheckoutThisCommit,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2021-04-11 11:35:42 +02:00
return gui.handleCheckoutRef(commit.Sha, handleCheckoutRefOptions{span: gui.Tr.Spans.CheckoutReflogCommit})
2020-08-15 08:36:39 +02:00
},
})
2020-01-09 13:08:28 +02:00
if err != nil {
return err
}
2020-08-20 00:53:10 +02:00
gui.State.Panels.ReflogCommits.SelectedLineIdx = 0
2020-01-09 13:08:28 +02:00
return nil
}
2020-02-16 00:32:51 +02:00
func (gui *Gui) handleCreateReflogResetMenu() error {
2020-02-16 00:32:51 +02:00
commit := gui.getSelectedReflogCommit()
return gui.createResetMenu(commit.Sha)
}
func (gui *Gui) handleViewReflogCommitFiles() error {
commit := gui.getSelectedReflogCommit()
if commit == nil {
return nil
}
2021-04-03 06:56:11 +02:00
return gui.switchToCommitFilesContext(commit.Sha, false, gui.State.Contexts.ReflogCommits, "commits")
}