2022-02-22 12:16:00 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2023-01-21 13:38:14 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
|
2023-03-21 11:57:52 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
2022-02-22 12:16:00 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommitMessageController struct {
|
|
|
|
baseController
|
2023-03-23 09:47:29 +02:00
|
|
|
c *ControllerCommon
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IController = &CommitMessageController{}
|
|
|
|
|
|
|
|
func NewCommitMessageController(
|
2023-03-23 09:47:29 +02:00
|
|
|
common *ControllerCommon,
|
2022-02-22 12:16:00 +02:00
|
|
|
) *CommitMessageController {
|
|
|
|
return &CommitMessageController{
|
2023-03-23 09:47:29 +02:00
|
|
|
baseController: baseController{},
|
|
|
|
c: common,
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
bindings := []*types.Binding{
|
|
|
|
{
|
2023-03-21 11:57:52 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.SubmitEditorText),
|
|
|
|
Handler: self.confirm,
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.Confirm,
|
2022-02-22 12:16:00 +02:00
|
|
|
},
|
|
|
|
{
|
2023-03-21 11:57:52 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.Return),
|
|
|
|
Handler: self.close,
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.Close,
|
2022-02-22 12:16:00 +02:00
|
|
|
},
|
2023-01-21 13:38:14 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.PrevItem),
|
|
|
|
Handler: self.handlePreviousCommit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.NextItem),
|
|
|
|
Handler: self.handleNextCommit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.TogglePanel),
|
|
|
|
Handler: self.switchToCommitDescription,
|
|
|
|
},
|
2023-05-18 19:15:23 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.CommitMessage.SwitchToEditor),
|
|
|
|
Handler: self.switchToEditor,
|
|
|
|
},
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:57:52 +02:00
|
|
|
func (self *CommitMessageController) GetOnFocusLost() func(types.OnFocusLostOpts) error {
|
|
|
|
return func(types.OnFocusLostOpts) error {
|
|
|
|
self.context().RenderCommitLength()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:16:00 +02:00
|
|
|
func (self *CommitMessageController) Context() types.Context {
|
|
|
|
return self.context()
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:57:52 +02:00
|
|
|
func (self *CommitMessageController) context() *context.CommitMessageContext {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Contexts().CommitMessage
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (self *CommitMessageController) handlePreviousCommit() error {
|
|
|
|
return self.handleCommitIndexChange(1)
|
|
|
|
}
|
2022-02-22 12:16:00 +02:00
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (self *CommitMessageController) handleNextCommit() error {
|
|
|
|
if self.context().GetSelectedIndex() == context.NoCommitIndex {
|
|
|
|
return nil
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
2023-01-21 13:38:14 +02:00
|
|
|
return self.handleCommitIndexChange(-1)
|
|
|
|
}
|
2022-02-22 12:16:00 +02:00
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (self *CommitMessageController) switchToCommitDescription() error {
|
|
|
|
if err := self.c.PushContext(self.c.Contexts().CommitDescription); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-22 12:16:00 +02:00
|
|
|
|
2023-05-18 19:15:23 +02:00
|
|
|
func (self *CommitMessageController) switchToEditor() error {
|
|
|
|
return self.c.Helpers().Commits.SwitchToEditor()
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (self *CommitMessageController) handleCommitIndexChange(value int) error {
|
|
|
|
currentIndex := self.context().GetSelectedIndex()
|
|
|
|
newIndex := currentIndex + value
|
|
|
|
if newIndex == context.NoCommitIndex {
|
|
|
|
self.context().SetSelectedIndex(newIndex)
|
2023-04-29 12:04:43 +02:00
|
|
|
self.c.Helpers().Commits.SetMessageAndDescriptionInView(self.context().GetHistoryMessage())
|
2022-02-22 12:16:00 +02:00
|
|
|
return nil
|
2023-04-29 12:04:43 +02:00
|
|
|
} else if currentIndex == context.NoCommitIndex {
|
|
|
|
self.context().SetHistoryMessage(self.c.Helpers().Commits.JoinCommitMessageAndDescription())
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
validCommit, err := self.setCommitMessageAtIndex(newIndex)
|
|
|
|
if validCommit {
|
|
|
|
self.context().SetSelectedIndex(newIndex)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if the given index is for a valid commit
|
|
|
|
func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, error) {
|
|
|
|
commitMessage, err := self.c.Git().Commit.GetCommitMessageFromHistory(index)
|
|
|
|
if err != nil {
|
|
|
|
if err == git_commands.ErrInvalidCommitIndex {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr)
|
|
|
|
}
|
|
|
|
self.c.Helpers().Commits.UpdateCommitPanelView(commitMessage)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageController) confirm() error {
|
|
|
|
return self.c.Helpers().Commits.HandleCommitConfirm()
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitMessageController) close() error {
|
2023-01-21 13:38:14 +02:00
|
|
|
return self.c.Helpers().Commits.CloseCommitMessagePanel()
|
2022-02-22 12:16:00 +02:00
|
|
|
}
|