2023-01-21 13:38:14 +02:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ICommitsHelper interface {
|
|
|
|
UpdateCommitPanelView(message string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommitsHelper struct {
|
|
|
|
c *HelperCommon
|
|
|
|
|
|
|
|
getCommitSummary func() string
|
|
|
|
setCommitSummary func(string)
|
|
|
|
getCommitDescription func() string
|
|
|
|
setCommitDescription func(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ICommitsHelper = &CommitsHelper{}
|
|
|
|
|
|
|
|
func NewCommitsHelper(
|
|
|
|
c *HelperCommon,
|
|
|
|
getCommitSummary func() string,
|
|
|
|
setCommitSummary func(string),
|
|
|
|
getCommitDescription func() string,
|
|
|
|
setCommitDescription func(string),
|
|
|
|
) *CommitsHelper {
|
|
|
|
return &CommitsHelper{
|
|
|
|
c: c,
|
|
|
|
getCommitSummary: getCommitSummary,
|
|
|
|
setCommitSummary: setCommitSummary,
|
|
|
|
getCommitDescription: getCommitDescription,
|
|
|
|
setCommitDescription: setCommitDescription,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) SplitCommitMessageAndDescription(message string) (string, string) {
|
|
|
|
for _, separator := range []string{"\n\n", "\n\r\n\r", "\n", "\n\r"} {
|
|
|
|
msg, description, found := strings.Cut(message, separator)
|
|
|
|
if found {
|
|
|
|
return msg, description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {
|
|
|
|
summary, description := self.SplitCommitMessageAndDescription(message)
|
|
|
|
|
|
|
|
self.setCommitSummary(summary)
|
|
|
|
self.setCommitDescription(description)
|
|
|
|
self.c.Contexts().CommitMessage.RenderCommitLength()
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:04:43 +02:00
|
|
|
func (self *CommitsHelper) JoinCommitMessageAndDescription() string {
|
2023-01-21 13:38:14 +02:00
|
|
|
if len(self.getCommitDescription()) == 0 {
|
|
|
|
return self.getCommitSummary()
|
|
|
|
}
|
|
|
|
return self.getCommitSummary() + "\n" + self.getCommitDescription()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) UpdateCommitPanelView(message string) {
|
|
|
|
if message != "" {
|
|
|
|
self.SetMessageAndDescriptionInView(message)
|
|
|
|
return
|
|
|
|
}
|
2023-07-22 06:05:42 +02:00
|
|
|
|
|
|
|
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
|
|
|
|
preservedMessage := self.c.Contexts().CommitMessage.GetPreservedMessage()
|
|
|
|
self.SetMessageAndDescriptionInView(preservedMessage)
|
|
|
|
return
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
2023-07-22 06:05:42 +02:00
|
|
|
|
|
|
|
self.SetMessageAndDescriptionInView("")
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OpenCommitMessagePanelOpts struct {
|
2023-07-22 06:05:42 +02:00
|
|
|
CommitIndex int
|
|
|
|
SummaryTitle string
|
|
|
|
DescriptionTitle string
|
|
|
|
PreserveMessage bool
|
|
|
|
OnConfirm func(summary string, description string) error
|
|
|
|
InitialMessage string
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOpts) error {
|
2023-07-22 06:05:42 +02:00
|
|
|
onConfirm := func(summary string, description string) error {
|
|
|
|
if err := self.CloseCommitMessagePanel(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts.OnConfirm(summary, description)
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
self.c.Contexts().CommitMessage.SetPanelState(
|
|
|
|
opts.CommitIndex,
|
2023-07-22 06:05:42 +02:00
|
|
|
opts.SummaryTitle,
|
|
|
|
opts.DescriptionTitle,
|
2023-01-21 13:38:14 +02:00
|
|
|
opts.PreserveMessage,
|
2023-07-22 06:05:42 +02:00
|
|
|
onConfirm,
|
2023-01-21 13:38:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.UpdateCommitPanelView(opts.InitialMessage)
|
|
|
|
|
|
|
|
return self.pushCommitMessageContexts()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) OnCommitSuccess() {
|
|
|
|
// if we have a preserved message we want to clear it on success
|
|
|
|
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
|
|
|
|
self.c.Contexts().CommitMessage.SetPreservedMessage("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) HandleCommitConfirm() error {
|
2023-07-22 06:05:42 +02:00
|
|
|
summary, description := self.getCommitSummary(), self.getCommitDescription()
|
2023-01-21 13:38:14 +02:00
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
if summary == "" {
|
2023-01-21 13:38:14 +02:00
|
|
|
return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr)
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
err := self.c.Contexts().CommitMessage.OnConfirm(summary, description)
|
2023-01-21 13:38:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) CloseCommitMessagePanel() error {
|
|
|
|
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
|
2023-04-29 12:04:43 +02:00
|
|
|
message := self.JoinCommitMessageAndDescription()
|
2023-01-21 13:38:14 +02:00
|
|
|
|
|
|
|
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
|
|
|
|
} else {
|
|
|
|
self.SetMessageAndDescriptionInView("")
|
|
|
|
}
|
2023-04-29 12:04:43 +02:00
|
|
|
|
|
|
|
self.c.Contexts().CommitMessage.SetHistoryMessage("")
|
|
|
|
|
|
|
|
return self.PopCommitMessageContexts()
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 12:04:43 +02:00
|
|
|
func (self *CommitsHelper) PopCommitMessageContexts() error {
|
2023-01-21 13:38:14 +02:00
|
|
|
return self.c.RemoveContexts(self.commitMessageContexts())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) pushCommitMessageContexts() error {
|
|
|
|
for _, context := range self.commitMessageContexts() {
|
|
|
|
if err := self.c.PushContext(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitsHelper) commitMessageContexts() []types.Context {
|
|
|
|
return []types.Context{
|
|
|
|
self.c.Contexts().CommitDescription,
|
|
|
|
self.c.Contexts().CommitMessage,
|
|
|
|
}
|
|
|
|
}
|