2018-08-14 11:05:26 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-01-16 05:46:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
|
2022-01-28 11:44:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2018-08-14 11:05:26 +02:00
|
|
|
)
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
// list panel functions
|
|
|
|
|
2020-09-29 10:46:45 +02:00
|
|
|
func (gui *Gui) getSelectedStashEntry() *models.StashEntry {
|
2020-08-20 00:53:10 +02:00
|
|
|
selectedLine := gui.State.Panels.Stash.SelectedLineIdx
|
2018-12-06 13:18:17 +02:00
|
|
|
if selectedLine == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:11:34 +02:00
|
|
|
return gui.State.Model.StashEntries[selectedLine]
|
2018-12-06 13:18:17 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
func (gui *Gui) stashRenderToMain() error {
|
2020-08-18 14:23:36 +02:00
|
|
|
var task updateTask
|
2020-03-29 05:34:17 +02:00
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
2018-12-06 13:18:17 +02:00
|
|
|
if stashEntry == nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
task = NewRenderStringTask(gui.c.Tr.NoStashEntries)
|
2020-08-18 14:23:36 +02:00
|
|
|
} else {
|
2022-01-16 05:46:53 +02:00
|
|
|
task = NewRunPtyTask(gui.git.Stash.ShowStashEntryCmdObj(stashEntry.Index).GetCmd())
|
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:23:36 +02:00
|
|
|
main: &viewUpdateOpts{
|
|
|
|
title: "Stash",
|
|
|
|
task: task,
|
|
|
|
},
|
|
|
|
})
|
2018-12-06 13:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// specific functions
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleStashApply() error {
|
2022-01-02 01:34:33 +02:00
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
|
|
|
if stashEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
skipStashWarning := gui.c.UserConfig.Gui.SkipStashWarning
|
2020-04-20 10:53:40 +02:00
|
|
|
|
|
|
|
apply := func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.LogAction(gui.c.Tr.Actions.Stash)
|
|
|
|
err := gui.git.Stash.Apply(stashEntry.Index)
|
2022-01-24 10:03:29 +02:00
|
|
|
_ = gui.postStashRefresh()
|
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Error(err)
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
2022-01-24 10:03:29 +02:00
|
|
|
return nil
|
2020-04-20 10:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipStashWarning {
|
|
|
|
return apply()
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.StashApply,
|
|
|
|
Prompt: gui.c.Tr.SureApplyStashEntry,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2020-08-15 08:36:39 +02:00
|
|
|
return apply()
|
|
|
|
},
|
|
|
|
})
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleStashPop() error {
|
2022-01-02 01:34:33 +02:00
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
|
|
|
if stashEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
skipStashWarning := gui.c.UserConfig.Gui.SkipStashWarning
|
2020-04-20 10:53:40 +02:00
|
|
|
|
|
|
|
pop := func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.LogAction(gui.c.Tr.Actions.Stash)
|
|
|
|
err := gui.git.Stash.Pop(stashEntry.Index)
|
2022-01-24 10:03:29 +02:00
|
|
|
_ = gui.postStashRefresh()
|
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Error(err)
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
2022-01-24 10:03:29 +02:00
|
|
|
return nil
|
2020-04-20 10:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipStashWarning {
|
|
|
|
return pop()
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.StashPop,
|
|
|
|
Prompt: gui.c.Tr.SurePopStashEntry,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2020-08-15 08:36:39 +02:00
|
|
|
return pop()
|
|
|
|
},
|
|
|
|
})
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleStashDrop() error {
|
2022-01-02 01:34:33 +02:00
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
|
|
|
if stashEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.StashDrop,
|
|
|
|
Prompt: gui.c.Tr.SureDropStashEntry,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.LogAction(gui.c.Tr.Actions.Stash)
|
|
|
|
err := gui.git.Stash.Drop(stashEntry.Index)
|
|
|
|
_ = gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
|
2022-01-24 10:03:29 +02:00
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Error(err)
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
2022-01-24 10:03:29 +02:00
|
|
|
return nil
|
2020-08-15 08:36:39 +02:00
|
|
|
},
|
|
|
|
})
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (gui *Gui) postStashRefresh() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
2020-08-21 11:53:45 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleViewStashFiles() error {
|
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
|
|
|
if stashEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{
|
|
|
|
RefName: stashEntry.RefName(),
|
|
|
|
CanRebase: false,
|
|
|
|
Context: gui.State.Contexts.Stash,
|
|
|
|
WindowName: "stash",
|
|
|
|
})
|
2020-08-21 11:53:45 +02:00
|
|
|
}
|
2022-01-30 11:03:08 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleNewBranchOffStashEntry() error {
|
|
|
|
stashEntry := gui.getSelectedStashEntry()
|
|
|
|
if stashEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:11:34 +02:00
|
|
|
return gui.helpers.Refs.NewBranch(stashEntry.RefName(), stashEntry.Description(), "")
|
2022-01-30 11:03:08 +02:00
|
|
|
}
|