1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/submodules_controller.go

280 lines
8.8 KiB
Go
Raw Normal View History

2022-01-28 11:44:36 +02:00
package controllers
import (
"fmt"
"path/filepath"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
2022-02-05 08:04:10 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubmodulesController struct {
2022-02-05 05:42:56 +02:00
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-01-28 11:44:36 +02:00
}
var _ types.IController = &SubmodulesController{}
2022-01-28 11:44:36 +02:00
func NewSubmodulesController(
2023-03-23 09:47:29 +02:00
controllerCommon *ControllerCommon,
2022-01-28 11:44:36 +02:00
) *SubmodulesController {
return &SubmodulesController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: controllerCommon,
2022-01-28 11:44:36 +02:00
}
}
2022-02-05 01:31:07 +02:00
func (self *SubmodulesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
return []*types.Binding{
2022-01-28 11:44:36 +02:00
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcEnterSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcRemoveSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.Update),
Handler: self.checkSelected(self.update),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcSubmoduleUpdate,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.New),
2022-01-28 11:44:36 +02:00
Handler: self.add,
Description: self.c.Tr.LcAddSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.editURL),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcEditSubmoduleUrl,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.Init),
Handler: self.checkSelected(self.init),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcInitSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.BulkMenu),
2022-01-28 11:44:36 +02:00
Handler: self.openBulkActionsMenu,
Description: self.c.Tr.LcViewBulkSubmoduleOptions,
OpensMenu: true,
},
2022-12-30 02:34:01 +02:00
{
Key: nil,
Handler: self.easterEgg,
Description: self.c.Tr.EasterEgg,
},
2022-01-28 11:44:36 +02:00
}
}
2022-02-27 02:42:22 +02:00
func (self *SubmodulesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
func (self *SubmodulesController) GetOnRenderToMain() func() error {
return func() error {
2023-03-23 09:47:29 +02:00
return self.c.Helpers().Diff.WithDiffModeCheck(func() error {
var task types.UpdateTask
submodule := self.context().GetSelected()
if submodule == nil {
task = types.NewRenderStringTask("No submodules")
} else {
prefix := fmt.Sprintf(
"Name: %s\nPath: %s\nUrl: %s\n\n",
style.FgGreen.Sprint(submodule.Name),
style.FgYellow.Sprint(submodule.Path),
style.FgCyan.Sprint(submodule.Url),
)
2023-03-23 09:47:29 +02:00
file := self.c.Helpers().WorkingTree.FileForSubmodule(submodule)
if file == nil {
task = types.NewRenderStringTask(prefix)
} else {
2023-03-23 04:04:57 +02:00
cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(file, false, !file.HasUnstagedChanges && file.HasStagedChanges, self.c.State().GetIgnoreWhitespaceInDiffView())
task = types.NewRunCommandTaskWithPrefix(cmdObj.GetCmd(), prefix)
}
}
return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: "Submodule",
Task: task,
},
})
})
}
}
2022-01-28 11:44:36 +02:00
func (self *SubmodulesController) enter(submodule *models.SubmoduleConfig) error {
2023-03-23 09:47:29 +02:00
return self.c.Helpers().Repos.EnterSubmodule(submodule)
2022-01-28 11:44:36 +02:00
}
func (self *SubmodulesController) add() error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmoduleUrl,
HandleConfirm: func(submoduleUrl string) error {
nameSuggestion := filepath.Base(strings.TrimSuffix(submoduleUrl, filepath.Ext(submoduleUrl)))
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmoduleName,
InitialContent: nameSuggestion,
HandleConfirm: func(submoduleName string) error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmodulePath,
InitialContent: submoduleName,
HandleConfirm: func(submodulePath string) error {
return self.c.WithWaitingStatus(self.c.Tr.LcAddingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.AddSubmodule)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Submodule.Add(submoduleName, submodulePath, submoduleUrl)
2022-01-28 11:44:36 +02:00
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
})
},
})
},
})
}
func (self *SubmodulesController) editURL(submodule *models.SubmoduleConfig) error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: fmt.Sprintf(self.c.Tr.LcUpdateSubmoduleUrl, submodule.Name),
InitialContent: submodule.Url,
HandleConfirm: func(newUrl string) error {
return self.c.WithWaitingStatus(self.c.Tr.LcUpdatingSubmoduleUrlStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.UpdateSubmoduleUrl)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Submodule.UpdateUrl(submodule.Name, submodule.Path, newUrl)
2022-01-28 11:44:36 +02:00
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
})
}
func (self *SubmodulesController) init(submodule *models.SubmoduleConfig) error {
return self.c.WithWaitingStatus(self.c.Tr.LcInitializingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.InitialiseSubmodule)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Submodule.Init(submodule.Path)
2022-01-28 11:44:36 +02:00
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
}
func (self *SubmodulesController) openBulkActionsMenu() error {
2022-01-29 10:09:20 +02:00
return self.c.Menu(types.CreateMenuOptions{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcBulkSubmoduleOptions,
2022-01-29 10:09:20 +02:00
Items: []*types.MenuItem{
2022-01-28 11:44:36 +02:00
{
2023-03-23 04:04:57 +02:00
LabelColumns: []string{self.c.Tr.LcBulkInitSubmodules, style.FgGreen.Sprint(self.c.Git().Submodule.BulkInitCmdObj().ToString())},
2022-01-28 11:44:36 +02:00
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkInitialiseSubmodules)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Submodule.BulkInitCmdObj().Run()
2022-01-28 11:44:36 +02:00
if err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'i',
2022-01-28 11:44:36 +02:00
},
{
2023-03-23 04:04:57 +02:00
LabelColumns: []string{self.c.Tr.LcBulkUpdateSubmodules, style.FgYellow.Sprint(self.c.Git().Submodule.BulkUpdateCmdObj().ToString())},
2022-01-28 11:44:36 +02:00
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkUpdateSubmodules)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Submodule.BulkUpdateCmdObj().Run(); err != nil {
2022-01-28 11:44:36 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'u',
2022-01-28 11:44:36 +02:00
},
{
2023-03-23 04:04:57 +02:00
LabelColumns: []string{self.c.Tr.LcBulkDeinitSubmodules, style.FgRed.Sprint(self.c.Git().Submodule.BulkDeinitCmdObj().ToString())},
2022-01-28 11:44:36 +02:00
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkDeinitialiseSubmodules)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Submodule.BulkDeinitCmdObj().Run(); err != nil {
2022-01-28 11:44:36 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'd',
2022-01-28 11:44:36 +02:00
},
},
})
}
func (self *SubmodulesController) update(submodule *models.SubmoduleConfig) error {
return self.c.WithWaitingStatus(self.c.Tr.LcUpdatingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.UpdateSubmodule)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Submodule.Update(submodule.Path)
2022-01-28 11:44:36 +02:00
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
}
func (self *SubmodulesController) remove(submodule *models.SubmoduleConfig) error {
return self.c.Confirm(types.ConfirmOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.RemoveSubmodule,
Prompt: fmt.Sprintf(self.c.Tr.RemoveSubmodulePrompt, submodule.Name),
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.RemoveSubmodule)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Submodule.Delete(submodule); err != nil {
2022-01-28 11:44:36 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES, types.FILES}})
},
})
}
2022-12-30 02:34:01 +02:00
func (self *SubmodulesController) easterEgg() error {
2023-03-23 04:04:57 +02:00
return self.c.PushContext(self.c.Contexts().Snake)
2022-12-30 02:34:01 +02:00
}
func (self *SubmodulesController) checkSelected(callback func(*models.SubmoduleConfig) error) func() error {
2022-01-28 11:44:36 +02:00
return func() error {
2022-02-06 06:54:26 +02:00
submodule := self.context().GetSelected()
2022-01-28 11:44:36 +02:00
if submodule == nil {
return nil
}
return callback(submodule)
}
}
func (self *SubmodulesController) Context() types.Context {
2022-02-06 06:54:26 +02:00
return self.context()
}
func (self *SubmodulesController) context() *context.SubmodulesContext {
2023-03-23 04:04:57 +02:00
return self.c.Contexts().Submodules
}