summaryrefslogtreecommitdiffstats
path: root/pkg/gui/stash_panel.go
blob: e51fcc054c8bf49b3100e6a1770097236c116390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gui

import (
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/gui/controllers"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

// list panel functions

func (gui *Gui) getSelectedStashEntry() *models.StashEntry {
	return gui.State.Contexts.Stash.GetSelected()
}

func (gui *Gui) stashRenderToMain() error {
	var task updateTask
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		task = NewRenderStringTask(gui.c.Tr.NoStashEntries)
	} else {
		task = NewRunPtyTask(gui.git.Stash.ShowStashEntryCmdObj(stashEntry.Index).GetCmd())
	}

	return gui.refreshMainViews(refreshMainOpts{
		main: &viewUpdateOpts{
			title: "Stash",
			task:  task,
		},
	})
}

// specific functions

func (gui *Gui) handleStashApply() error {
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		return nil
	}

	skipStashWarning := gui.c.UserConfig.Gui.SkipStashWarning

	apply := func() error {
		gui.c.LogAction(gui.c.Tr.Actions.Stash)
		err := gui.git.Stash.Apply(stashEntry.Index)
		_ = gui.postStashRefresh()
		if err != nil {
			return gui.c.Error(err)
		}
		return nil
	}

	if skipStashWarning {
		return apply()
	}

	return gui.c.Ask(types.AskOpts{
		Title:  gui.c.Tr.StashApply,
		Prompt: gui.c.Tr.SureApplyStashEntry,
		HandleConfirm: func() error {
			return apply()
		},
	})
}

func (gui *Gui) handleStashPop() error {
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		return nil
	}

	skipStashWarning := gui.c.UserConfig.Gui.SkipStashWarning

	pop := func() error {
		gui.c.LogAction(gui.c.Tr.Actions.Stash)
		err := gui.git.Stash.Pop(stashEntry.Index)
		_ = gui.postStashRefresh()
		if err != nil {
			return gui.c.Error(err)
		}
		return nil
	}

	if skipStashWarning {
		return pop()
	}

	return gui.c.Ask(types.AskOpts{
		Title:  gui.c.Tr.StashPop,
		Prompt: gui.c.Tr.SurePopStashEntry,
		HandleConfirm: func() error {
			return pop()
		},
	})
}

func (gui *Gui) handleStashDrop() error {
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		return nil
	}

	return gui.c.Ask(types.AskOpts{
		Title:  gui.c.Tr.StashDrop,
		Prompt: gui.c.Tr.SureDropStashEntry,
		HandleConfirm: func() error {
			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}})
			if err != nil {
				return gui.c.Error(err)
			}
			return nil
		},
	})
}

func (gui *Gui) postStashRefresh() error {
	return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
}

func (gui *Gui) handleViewStashFiles() error {
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		return nil
	}

	return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{
		RefName:    stashEntry.RefName(),
		CanRebase:  false,
		Context:    gui.State.Contexts.Stash,
		WindowName: "stash",
	})
}

func (gui *Gui) handleNewBranchOffStashEntry() error {
	stashEntry := gui.getSelectedStashEntry()
	if stashEntry == nil {
		return nil
	}

	return gui.helpers.Refs.NewBranch(stashEntry.RefName(), stashEntry.Description(), "")
}