1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-20 05:19:24 +02:00

move workspace reset menu into controller

This commit is contained in:
Jesse Duffield 2022-02-26 19:26:39 +11:00
parent ee1337b931
commit 8fd6338527
6 changed files with 296 additions and 285 deletions

180
pkg/gui/controllers.go Normal file
View File

@ -0,0 +1,180 @@
package gui
import (
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/services/custom_commands"
)
func (gui *Gui) resetControllers() {
helperCommon := gui.c
osCommand := gui.os
model := gui.State.Model
refsHelper := helpers.NewRefsHelper(
helperCommon,
gui.git,
gui.State.Contexts,
model,
)
rebaseHelper := helpers.NewMergeAndRebaseHelper(helperCommon, gui.State.Contexts, gui.git, gui.takeOverMergeConflictScrolling, refsHelper)
gui.helpers = &helpers.Helpers{
Refs: refsHelper,
PatchBuilding: helpers.NewPatchBuildingHelper(helperCommon, gui.git),
Bisect: helpers.NewBisectHelper(helperCommon, gui.git),
Suggestions: helpers.NewSuggestionsHelper(helperCommon, model, gui.refreshSuggestions),
Files: helpers.NewFilesHelper(helperCommon, gui.git, osCommand),
WorkingTree: helpers.NewWorkingTreeHelper(model),
Tags: helpers.NewTagsHelper(helperCommon, gui.git),
GPG: helpers.NewGpgHelper(helperCommon, gui.os, gui.git),
MergeAndRebase: rebaseHelper,
CherryPick: helpers.NewCherryPickHelper(
helperCommon,
gui.git,
gui.State.Contexts,
func() *cherrypicking.CherryPicking { return gui.State.Modes.CherryPicking },
rebaseHelper,
),
}
gui.CustomCommandsClient = custom_commands.NewClient(
helperCommon,
gui.os,
gui.git,
gui.State.Contexts,
gui.helpers,
gui.getKey,
)
common := controllers.NewControllerCommon(
helperCommon,
osCommand,
gui.git,
gui.helpers,
model,
gui.State.Contexts,
gui.State.Modes,
)
syncController := controllers.NewSyncController(
common,
gui.getSuggestedRemote,
)
submodulesController := controllers.NewSubmodulesController(
common,
gui.enterSubmodule,
)
bisectController := controllers.NewBisectController(common)
reflogController := controllers.NewReflogController(common)
subCommitsController := controllers.NewSubCommitsController(common)
getSavedCommitMessage := func() string {
return gui.State.savedCommitMessage
}
getCommitMessage := func() string {
return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent())
}
setCommitMessage := gui.getSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage })
onCommitAttempt := func(message string) {
gui.State.savedCommitMessage = message
gui.Views.CommitMessage.ClearTextArea()
}
onCommitSuccess := func() {
gui.State.savedCommitMessage = ""
}
commitMessageController := controllers.NewCommitMessageController(
common,
getCommitMessage,
onCommitAttempt,
onCommitSuccess,
)
remoteBranchesController := controllers.NewRemoteBranchesController(common)
gui.Controllers = Controllers{
Submodules: submodulesController,
Global: controllers.NewGlobalController(common),
Files: controllers.NewFilesController(
common,
gui.enterSubmodule,
setCommitMessage,
getSavedCommitMessage,
gui.switchToMerge,
),
Tags: controllers.NewTagsController(common),
LocalCommits: controllers.NewLocalCommitsController(common, syncController.HandlePull),
Remotes: controllers.NewRemotesController(
common,
func(branches []*models.RemoteBranch) { gui.State.Model.RemoteBranches = branches },
),
Menu: controllers.NewMenuController(common),
Undo: controllers.NewUndoController(common),
Sync: syncController,
}
branchesController := controllers.NewBranchesController(common)
gitFlowController := controllers.NewGitFlowController(common)
filesRemoveController := controllers.NewFilesRemoveController(common)
stashController := controllers.NewStashController(common)
commitFilesController := controllers.NewCommitFilesController(common)
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
common,
func(commits []*models.Commit) { gui.State.Model.SubCommits = commits },
)
for _, context := range []controllers.ContextWithRefName{
gui.State.Contexts.Branches,
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.Tags,
} {
controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context))
}
commitishControllerFactory := controllers.NewCommitishControllerFactory(
common,
gui.SwitchToCommitFilesContext,
)
for _, context := range []controllers.Commitish{
gui.State.Contexts.LocalCommits,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
gui.State.Contexts.Stash,
} {
controllers.AttachControllers(context, commitishControllerFactory.Create(context))
}
controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController)
controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files, filesRemoveController)
controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags)
controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules)
controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController)
controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController)
controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController)
controllers.AttachControllers(gui.State.Contexts.CommitFiles, commitFilesController)
controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes)
controllers.AttachControllers(gui.State.Contexts.Stash, stashController)
controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu)
controllers.AttachControllers(gui.State.Contexts.CommitMessage, commitMessageController)
controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController)
controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global)
listControllerFactory := controllers.NewListControllerFactory(gui.c)
for _, context := range gui.getListContexts() {
controllers.AttachControllers(context, listControllerFactory.Create(context))
}
}

View File

@ -122,11 +122,16 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
},
{
Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
Handler: self.createResetMenu,
Handler: self.createResetToUpstreamMenu,
Description: self.c.Tr.LcViewResetToUpstreamOptions,
OpensMenu: true,
},
// here
{
Key: opts.GetKey(opts.Config.Files.ViewResetOptions),
Handler: self.createResetMenu,
Description: self.c.Tr.LcViewResetOptions,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Files.ToggleTreeView),
Handler: self.toggleTreeView,
@ -571,7 +576,7 @@ func (self *FilesController) stash() error {
return self.handleStashSave(self.git.Stash.Save)
}
func (self *FilesController) createResetMenu() error {
func (self *FilesController) createResetToUpstreamMenu() error {
return self.helpers.Refs.CreateGitResetMenu("@{upstream}")
}

View File

@ -0,0 +1,108 @@
package controllers
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
// this is in its own file given that the workspace controller file is already quite long
func (self *FilesController) createResetMenu() error {
red := style.FgRed
nukeStr := "reset --hard HEAD && git clean -fd"
if len(self.model.Submodules) > 0 {
nukeStr = fmt.Sprintf("%s (%s)", nukeStr, self.c.Tr.LcAndResetSubmodules)
}
menuItems := []*types.MenuItem{
{
DisplayStrings: []string{
self.c.Tr.LcDiscardAllChangesToAllFiles,
red.Sprint(nukeStr),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.NukeWorkingTree)
if err := self.git.WorkingTree.ResetAndClean(); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
self.c.Tr.LcDiscardAnyUnstagedChanges,
red.Sprint("git checkout -- ."),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.DiscardUnstagedFileChanges)
if err := self.git.WorkingTree.DiscardAnyUnstagedFileChanges(); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
self.c.Tr.LcDiscardUntrackedFiles,
red.Sprint("git clean -fd"),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.RemoveUntrackedFiles)
if err := self.git.WorkingTree.RemoveUntrackedFiles(); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
self.c.Tr.LcSoftReset,
red.Sprint("git reset --soft HEAD"),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.SoftReset)
if err := self.git.WorkingTree.ResetSoft("HEAD"); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
"mixed reset",
red.Sprint("git reset --mixed HEAD"),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.MixedReset)
if err := self.git.WorkingTree.ResetMixed("HEAD"); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
self.c.Tr.LcHardReset,
red.Sprint("git reset --hard HEAD"),
},
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.HardReset)
if err := self.git.WorkingTree.ResetHard("HEAD"); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
}
return self.c.Menu(types.CreateMenuOptions{Title: "", Items: menuItems})
}

View File

@ -498,174 +498,6 @@ func NewGui(
return gui, nil
}
func (gui *Gui) resetControllers() {
helperCommon := gui.c
osCommand := gui.os
model := gui.State.Model
refsHelper := helpers.NewRefsHelper(
helperCommon,
gui.git,
gui.State.Contexts,
model,
)
rebaseHelper := helpers.NewMergeAndRebaseHelper(helperCommon, gui.State.Contexts, gui.git, gui.takeOverMergeConflictScrolling, refsHelper)
gui.helpers = &helpers.Helpers{
Refs: refsHelper,
PatchBuilding: helpers.NewPatchBuildingHelper(helperCommon, gui.git),
Bisect: helpers.NewBisectHelper(helperCommon, gui.git),
Suggestions: helpers.NewSuggestionsHelper(helperCommon, model, gui.refreshSuggestions),
Files: helpers.NewFilesHelper(helperCommon, gui.git, osCommand),
WorkingTree: helpers.NewWorkingTreeHelper(model),
Tags: helpers.NewTagsHelper(helperCommon, gui.git),
GPG: helpers.NewGpgHelper(helperCommon, gui.os, gui.git),
MergeAndRebase: rebaseHelper,
CherryPick: helpers.NewCherryPickHelper(
helperCommon,
gui.git,
gui.State.Contexts,
func() *cherrypicking.CherryPicking { return gui.State.Modes.CherryPicking },
rebaseHelper,
),
}
gui.CustomCommandsClient = custom_commands.NewClient(
helperCommon,
gui.os,
gui.git,
gui.State.Contexts,
gui.helpers,
gui.getKey,
)
common := controllers.NewControllerCommon(
helperCommon,
osCommand,
gui.git,
gui.helpers,
model,
gui.State.Contexts,
gui.State.Modes,
)
syncController := controllers.NewSyncController(
common,
gui.getSuggestedRemote,
)
submodulesController := controllers.NewSubmodulesController(
common,
gui.enterSubmodule,
)
bisectController := controllers.NewBisectController(common)
reflogController := controllers.NewReflogController(common)
subCommitsController := controllers.NewSubCommitsController(common)
getSavedCommitMessage := func() string {
return gui.State.savedCommitMessage
}
getCommitMessage := func() string {
return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent())
}
setCommitMessage := gui.getSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage })
onCommitAttempt := func(message string) {
gui.State.savedCommitMessage = message
gui.Views.CommitMessage.ClearTextArea()
}
onCommitSuccess := func() {
gui.State.savedCommitMessage = ""
}
commitMessageController := controllers.NewCommitMessageController(
common,
getCommitMessage,
onCommitAttempt,
onCommitSuccess,
)
remoteBranchesController := controllers.NewRemoteBranchesController(common)
gui.Controllers = Controllers{
Submodules: submodulesController,
Global: controllers.NewGlobalController(common),
Files: controllers.NewFilesController(
common,
gui.enterSubmodule,
setCommitMessage,
getSavedCommitMessage,
gui.switchToMerge,
),
Tags: controllers.NewTagsController(common),
LocalCommits: controllers.NewLocalCommitsController(common, syncController.HandlePull),
Remotes: controllers.NewRemotesController(
common,
func(branches []*models.RemoteBranch) { gui.State.Model.RemoteBranches = branches },
),
Menu: controllers.NewMenuController(common),
Undo: controllers.NewUndoController(common),
Sync: syncController,
}
branchesController := controllers.NewBranchesController(common)
gitFlowController := controllers.NewGitFlowController(common)
filesRemoveController := controllers.NewFilesRemoveController(common)
stashController := controllers.NewStashController(common)
commitFilesController := controllers.NewCommitFilesController(common)
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
common,
func(commits []*models.Commit) { gui.State.Model.SubCommits = commits },
)
for _, context := range []controllers.ContextWithRefName{
gui.State.Contexts.Branches,
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.Tags,
} {
controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context))
}
commitishControllerFactory := controllers.NewCommitishControllerFactory(
common,
gui.SwitchToCommitFilesContext,
)
for _, context := range []controllers.Commitish{
gui.State.Contexts.LocalCommits,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
gui.State.Contexts.Stash,
} {
controllers.AttachControllers(context, commitishControllerFactory.Create(context))
}
controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController)
controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files, filesRemoveController)
controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags)
controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules)
controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController)
controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController)
controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController)
controllers.AttachControllers(gui.State.Contexts.CommitFiles, commitFilesController)
controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes)
controllers.AttachControllers(gui.State.Contexts.Stash, stashController)
controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu)
controllers.AttachControllers(gui.State.Contexts.CommitMessage, commitMessageController)
controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController)
controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global)
listControllerFactory := controllers.NewListControllerFactory(gui.c)
for _, context := range gui.getListContexts() {
controllers.AttachControllers(context, listControllerFactory.Create(context))
}
}
var RuneReplacements = map[rune]string{
// for the commit graph
graph.MergeSymbol: "M",

View File

@ -365,14 +365,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
Handler: self.handleShowAllBranchLogs,
Description: self.c.Tr.LcAllBranchesLogGraph,
},
{
ViewName: "files",
Contexts: []string{string(context.FILES_CONTEXT_KEY)},
Key: opts.GetKey(opts.Config.Files.ViewResetOptions),
Handler: self.handleCreateResetMenu,
Description: self.c.Tr.LcViewResetOptions,
OpensMenu: true,
},
{
ViewName: "files",
Contexts: []string{string(context.FILES_CONTEXT_KEY)},

View File

@ -1,106 +0,0 @@
package gui
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
func (gui *Gui) handleCreateResetMenu() error {
red := style.FgRed
nukeStr := "reset --hard HEAD && git clean -fd"
if len(gui.State.Model.Submodules) > 0 {
nukeStr = fmt.Sprintf("%s (%s)", nukeStr, gui.c.Tr.LcAndResetSubmodules)
}
menuItems := []*types.MenuItem{
{
DisplayStrings: []string{
gui.c.Tr.LcDiscardAllChangesToAllFiles,
red.Sprint(nukeStr),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.NukeWorkingTree)
if err := gui.git.WorkingTree.ResetAndClean(); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
gui.c.Tr.LcDiscardAnyUnstagedChanges,
red.Sprint("git checkout -- ."),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.DiscardUnstagedFileChanges)
if err := gui.git.WorkingTree.DiscardAnyUnstagedFileChanges(); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
gui.c.Tr.LcDiscardUntrackedFiles,
red.Sprint("git clean -fd"),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.RemoveUntrackedFiles)
if err := gui.git.WorkingTree.RemoveUntrackedFiles(); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
gui.c.Tr.LcSoftReset,
red.Sprint("git reset --soft HEAD"),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.SoftReset)
if err := gui.git.WorkingTree.ResetSoft("HEAD"); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
"mixed reset",
red.Sprint("git reset --mixed HEAD"),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.MixedReset)
if err := gui.git.WorkingTree.ResetMixed("HEAD"); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
{
DisplayStrings: []string{
gui.c.Tr.LcHardReset,
red.Sprint("git reset --hard HEAD"),
},
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.HardReset)
if err := gui.git.WorkingTree.ResetHard("HEAD"); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
},
},
}
return gui.c.Menu(types.CreateMenuOptions{Title: "", Items: menuItems})
}