mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-31 14:24:25 +03:00
Split commit message panel into commit summary and commit description panel
When we use the one panel for the entire commit message, its tricky to have a keybinding both for adding a newline and submitting. By having two panels: one for the summary line and one for the description, we allow for 'enter' to submit the message when done from the summary panel, and 'enter' to add a newline when done from the description panel. Alt-enter, for those who can use that key combo, also works for submitting the message from the description panel. For those who can't use that key combo, and don't want to remap the keybinding, they can hit tab to go back to the summary panel and then 'enter' to submit the message. We have some awkwardness in that both contexts (i.e. panels) need to appear and disappear in tandem and we don't have a great way of handling that concept, so we just push both contexts one after the other, and likewise remove both contexts when we escape.
This commit is contained in:
60
pkg/gui/controllers/commit_description_controller.go
Normal file
60
pkg/gui/controllers/commit_description_controller.go
Normal file
@ -0,0 +1,60 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
type CommitDescriptionController struct {
|
||||
baseController
|
||||
c *ControllerCommon
|
||||
}
|
||||
|
||||
var _ types.IController = &CommitMessageController{}
|
||||
|
||||
func NewCommitDescriptionController(
|
||||
common *ControllerCommon,
|
||||
) *CommitDescriptionController {
|
||||
return &CommitDescriptionController{
|
||||
baseController: baseController{},
|
||||
c: common,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
||||
bindings := []*types.Binding{
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.TogglePanel),
|
||||
Handler: self.switchToCommitMessage,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.Return),
|
||||
Handler: self.close,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.ConfirmInEditor),
|
||||
Handler: self.confirm,
|
||||
},
|
||||
}
|
||||
|
||||
return bindings
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) Context() types.Context {
|
||||
return self.context()
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) context() types.Context {
|
||||
return self.c.Contexts().CommitMessage
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) switchToCommitMessage() error {
|
||||
return self.c.PushContext(self.c.Contexts().CommitMessage)
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) close() error {
|
||||
return self.c.Helpers().Commits.CloseCommitMessagePanel()
|
||||
}
|
||||
|
||||
func (self *CommitDescriptionController) confirm() error {
|
||||
return self.c.Helpers().Commits.HandleCommitConfirm()
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
@ -8,27 +9,16 @@ import (
|
||||
type CommitMessageController struct {
|
||||
baseController
|
||||
c *ControllerCommon
|
||||
|
||||
getCommitMessage func() string
|
||||
onCommitAttempt func(message string)
|
||||
onCommitSuccess func()
|
||||
}
|
||||
|
||||
var _ types.IController = &CommitMessageController{}
|
||||
|
||||
func NewCommitMessageController(
|
||||
common *ControllerCommon,
|
||||
getCommitMessage func() string,
|
||||
onCommitAttempt func(message string),
|
||||
onCommitSuccess func(),
|
||||
) *CommitMessageController {
|
||||
return &CommitMessageController{
|
||||
baseController: baseController{},
|
||||
c: common,
|
||||
|
||||
getCommitMessage: getCommitMessage,
|
||||
onCommitAttempt: onCommitAttempt,
|
||||
onCommitSuccess: onCommitSuccess,
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,6 +36,18 @@ func (self *CommitMessageController) GetKeybindings(opts types.KeybindingsOpts)
|
||||
Handler: self.close,
|
||||
Description: self.c.Tr.LcClose,
|
||||
},
|
||||
{
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
return bindings
|
||||
@ -62,30 +64,61 @@ func (self *CommitMessageController) Context() types.Context {
|
||||
return self.context()
|
||||
}
|
||||
|
||||
// this method is pointless in this context but I'm keeping it consistent
|
||||
// with other contexts so that when generics arrive it's easier to refactor
|
||||
func (self *CommitMessageController) context() *context.CommitMessageContext {
|
||||
return self.c.Contexts().CommitMessage
|
||||
}
|
||||
|
||||
func (self *CommitMessageController) confirm() error {
|
||||
message := self.getCommitMessage()
|
||||
self.onCommitAttempt(message)
|
||||
func (self *CommitMessageController) handlePreviousCommit() error {
|
||||
return self.handleCommitIndexChange(1)
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr)
|
||||
func (self *CommitMessageController) handleNextCommit() error {
|
||||
if self.context().GetSelectedIndex() == context.NoCommitIndex {
|
||||
return nil
|
||||
}
|
||||
return self.handleCommitIndexChange(-1)
|
||||
}
|
||||
|
||||
func (self *CommitMessageController) switchToCommitDescription() error {
|
||||
if err := self.c.PushContext(self.c.Contexts().CommitDescription); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *CommitMessageController) handleCommitIndexChange(value int) error {
|
||||
currentIndex := self.context().GetSelectedIndex()
|
||||
newIndex := currentIndex + value
|
||||
if newIndex == context.NoCommitIndex {
|
||||
self.context().SetSelectedIndex(newIndex)
|
||||
self.c.Helpers().Commits.SetMessageAndDescriptionInView("")
|
||||
return nil
|
||||
}
|
||||
|
||||
cmdObj := self.c.Git().Commit.CommitCmdObj(message)
|
||||
self.c.LogAction(self.c.Tr.Actions.Commit)
|
||||
validCommit, err := self.setCommitMessageAtIndex(newIndex)
|
||||
if validCommit {
|
||||
self.context().SetSelectedIndex(newIndex)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
_ = self.c.PopContext()
|
||||
return self.c.Helpers().GPG.WithGpgHandling(cmdObj, self.c.Tr.CommittingStatus, func() error {
|
||||
self.onCommitSuccess()
|
||||
return nil
|
||||
})
|
||||
// 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()
|
||||
}
|
||||
|
||||
func (self *CommitMessageController) close() error {
|
||||
return self.c.PopContext()
|
||||
return self.c.Helpers().Commits.CloseCommitMessagePanel()
|
||||
}
|
||||
|
@ -14,22 +14,15 @@ import (
|
||||
type FilesController struct {
|
||||
baseController // nolint: unused
|
||||
c *ControllerCommon
|
||||
|
||||
setCommitMessage func(message string)
|
||||
getSavedCommitMessage func() string
|
||||
}
|
||||
|
||||
var _ types.IController = &FilesController{}
|
||||
|
||||
func NewFilesController(
|
||||
common *ControllerCommon,
|
||||
setCommitMessage func(message string),
|
||||
getSavedCommitMessage func() string,
|
||||
) *FilesController {
|
||||
return &FilesController{
|
||||
c: common,
|
||||
setCommitMessage: setCommitMessage,
|
||||
getSavedCommitMessage: getSavedCommitMessage,
|
||||
c: common,
|
||||
}
|
||||
}
|
||||
|
||||
|
153
pkg/gui/controllers/helpers/commits_helper.go
Normal file
153
pkg/gui/controllers/helpers/commits_helper.go
Normal file
@ -0,0 +1,153 @@
|
||||
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()
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) joinCommitMessageAndDescription() string {
|
||||
if len(self.getCommitDescription()) == 0 {
|
||||
return self.getCommitSummary()
|
||||
}
|
||||
return self.getCommitSummary() + "\n" + self.getCommitDescription()
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) UpdateCommitPanelView(message string) {
|
||||
// first try the passed in message, if not fallback to context -> view in that order
|
||||
if message != "" {
|
||||
self.SetMessageAndDescriptionInView(message)
|
||||
return
|
||||
}
|
||||
message = self.c.Contexts().CommitMessage.GetPreservedMessage()
|
||||
if message != "" {
|
||||
self.SetMessageAndDescriptionInView(message)
|
||||
} else {
|
||||
self.SetMessageAndDescriptionInView(self.getCommitSummary())
|
||||
}
|
||||
}
|
||||
|
||||
type OpenCommitMessagePanelOpts struct {
|
||||
CommitIndex int
|
||||
Title string
|
||||
PreserveMessage bool
|
||||
OnConfirm func(string) error
|
||||
InitialMessage string
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOpts) error {
|
||||
self.c.Contexts().CommitMessage.SetPanelState(
|
||||
opts.CommitIndex,
|
||||
opts.Title,
|
||||
opts.PreserveMessage,
|
||||
opts.OnConfirm,
|
||||
)
|
||||
|
||||
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("")
|
||||
}
|
||||
self.SetMessageAndDescriptionInView("")
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) HandleCommitConfirm() error {
|
||||
fullMessage := self.joinCommitMessageAndDescription()
|
||||
|
||||
if fullMessage == "" {
|
||||
return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr)
|
||||
}
|
||||
|
||||
err := self.c.Contexts().CommitMessage.OnConfirm(fullMessage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) CloseCommitMessagePanel() error {
|
||||
if self.c.Contexts().CommitMessage.GetPreserveMessage() {
|
||||
message := self.joinCommitMessageAndDescription()
|
||||
|
||||
self.c.Contexts().CommitMessage.SetPreservedMessage(message)
|
||||
} else {
|
||||
self.SetMessageAndDescriptionInView("")
|
||||
}
|
||||
return self.EscapeCommitsPanel()
|
||||
}
|
||||
|
||||
func (self *CommitsHelper) EscapeCommitsPanel() error {
|
||||
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,
|
||||
}
|
||||
}
|
@ -75,17 +75,17 @@ func getMessageHeight(wrap bool, message string, width int) int {
|
||||
return lineCount
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) getConfirmationPanelDimensions(wrap bool, prompt string) (int, int, int, int) {
|
||||
panelWidth := self.getConfirmationPanelWidth()
|
||||
func (self *ConfirmationHelper) getPopupPanelDimensions(wrap bool, prompt string) (int, int, int, int) {
|
||||
panelWidth := self.getPopupPanelWidth()
|
||||
panelHeight := getMessageHeight(wrap, prompt, panelWidth)
|
||||
return self.getConfirmationPanelDimensionsAux(panelWidth, panelHeight)
|
||||
return self.getPopupPanelDimensionsAux(panelWidth, panelHeight)
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) getConfirmationPanelDimensionsForContentHeight(panelWidth, contentHeight int) (int, int, int, int) {
|
||||
return self.getConfirmationPanelDimensionsAux(panelWidth, contentHeight)
|
||||
func (self *ConfirmationHelper) getPopupPanelDimensionsForContentHeight(panelWidth, contentHeight int) (int, int, int, int) {
|
||||
return self.getPopupPanelDimensionsAux(panelWidth, contentHeight)
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) getConfirmationPanelDimensionsAux(panelWidth int, panelHeight int) (int, int, int, int) {
|
||||
func (self *ConfirmationHelper) getPopupPanelDimensionsAux(panelWidth int, panelHeight int) (int, int, int, int) {
|
||||
width, height := self.c.GocuiGui().Size()
|
||||
if panelHeight > height*3/4 {
|
||||
panelHeight = height * 3 / 4
|
||||
@ -96,7 +96,7 @@ func (self *ConfirmationHelper) getConfirmationPanelDimensionsAux(panelWidth int
|
||||
height/2 + panelHeight/2
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) getConfirmationPanelWidth() int {
|
||||
func (self *ConfirmationHelper) getPopupPanelWidth() int {
|
||||
width, _ := self.c.GocuiGui().Size()
|
||||
// we want a minimum width up to a point, then we do it based on ratio.
|
||||
panelWidth := 4 * width / 7
|
||||
@ -254,7 +254,7 @@ func (self *ConfirmationHelper) ResizeConfirmationPanel() {
|
||||
if self.c.Views().Suggestions.Visible {
|
||||
suggestionsViewHeight = 11
|
||||
}
|
||||
panelWidth := self.getConfirmationPanelWidth()
|
||||
panelWidth := self.getPopupPanelWidth()
|
||||
prompt := self.c.Views().Confirmation.Buffer()
|
||||
wrap := true
|
||||
if self.c.Views().Confirmation.Editable {
|
||||
@ -262,7 +262,7 @@ func (self *ConfirmationHelper) ResizeConfirmationPanel() {
|
||||
wrap = false
|
||||
}
|
||||
panelHeight := getMessageHeight(wrap, prompt, panelWidth) + suggestionsViewHeight
|
||||
x0, y0, x1, y1 := self.getConfirmationPanelDimensionsAux(panelWidth, panelHeight)
|
||||
x0, y0, x1, y1 := self.getPopupPanelDimensionsAux(panelWidth, panelHeight)
|
||||
confirmationViewBottom := y1 - suggestionsViewHeight
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().Confirmation.Name(), x0, y0, x1, confirmationViewBottom, 0)
|
||||
|
||||
@ -271,24 +271,22 @@ func (self *ConfirmationHelper) ResizeConfirmationPanel() {
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) ResizeCurrentPopupPanel() error {
|
||||
v := self.c.GocuiGui().CurrentView()
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
c := self.c.CurrentContext()
|
||||
|
||||
if v == self.c.Views().Menu {
|
||||
switch c {
|
||||
case self.c.Contexts().Menu:
|
||||
self.resizeMenu()
|
||||
} else if v == self.c.Views().Confirmation || v == self.c.Views().Suggestions {
|
||||
self.ResizeConfirmationPanel()
|
||||
} else if self.IsPopupPanel(v.Name()) {
|
||||
return self.ResizePopupPanel(v, v.Buffer())
|
||||
case self.c.Contexts().Confirmation, self.c.Contexts().Suggestions:
|
||||
self.resizeConfirmationPanel()
|
||||
case self.c.Contexts().CommitMessage, self.c.Contexts().CommitDescription:
|
||||
self.ResizeCommitMessagePanels()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) ResizePopupPanel(v *gocui.View, content string) error {
|
||||
x0, y0, x1, y1 := self.getConfirmationPanelDimensions(v.Wrap, content)
|
||||
x0, y0, x1, y1 := self.getPopupPanelDimensions(v.Wrap, content)
|
||||
_, err := self.c.GocuiGui().SetView(v.Name(), x0, y0, x1, y1, 0)
|
||||
return err
|
||||
}
|
||||
@ -296,8 +294,8 @@ func (self *ConfirmationHelper) ResizePopupPanel(v *gocui.View, content string)
|
||||
func (self *ConfirmationHelper) resizeMenu() {
|
||||
itemCount := self.c.Contexts().Menu.GetList().Len()
|
||||
offset := 3
|
||||
panelWidth := self.getConfirmationPanelWidth()
|
||||
x0, y0, x1, y1 := self.getConfirmationPanelDimensionsForContentHeight(panelWidth, itemCount+offset)
|
||||
panelWidth := self.getPopupPanelWidth()
|
||||
x0, y0, x1, y1 := self.getPopupPanelDimensionsForContentHeight(panelWidth, itemCount+offset)
|
||||
menuBottom := y1 - offset
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().Menu.Name(), x0, y0, x1, menuBottom, 0)
|
||||
|
||||
@ -306,6 +304,42 @@ func (self *ConfirmationHelper) resizeMenu() {
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0)
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) resizeConfirmationPanel() {
|
||||
suggestionsViewHeight := 0
|
||||
if self.c.Views().Suggestions.Visible {
|
||||
suggestionsViewHeight = 11
|
||||
}
|
||||
panelWidth := self.getPopupPanelWidth()
|
||||
prompt := self.c.Views().Confirmation.Buffer()
|
||||
wrap := true
|
||||
if self.c.Views().Confirmation.Editable {
|
||||
prompt = self.c.Views().Confirmation.TextArea.GetContent()
|
||||
wrap = false
|
||||
}
|
||||
panelHeight := getMessageHeight(wrap, prompt, panelWidth) + suggestionsViewHeight
|
||||
x0, y0, x1, y1 := self.getPopupPanelDimensionsAux(panelWidth, panelHeight)
|
||||
confirmationViewBottom := y1 - suggestionsViewHeight
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().Confirmation.Name(), x0, y0, x1, confirmationViewBottom, 0)
|
||||
|
||||
suggestionsViewTop := confirmationViewBottom + 1
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().Suggestions.Name(), x0, suggestionsViewTop, x1, suggestionsViewTop+suggestionsViewHeight, 0)
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) ResizeCommitMessagePanels() {
|
||||
panelWidth := self.getPopupPanelWidth()
|
||||
content := self.c.Views().CommitDescription.TextArea.GetContent()
|
||||
summaryViewHeight := 3
|
||||
panelHeight := getMessageHeight(false, content, panelWidth)
|
||||
minHeight := 7
|
||||
if panelHeight < minHeight {
|
||||
panelHeight = minHeight
|
||||
}
|
||||
x0, y0, x1, y1 := self.getPopupPanelDimensionsAux(panelWidth, panelHeight)
|
||||
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().CommitMessage.Name(), x0, y0, x1, y0+summaryViewHeight-1, 0)
|
||||
_, _ = self.c.GocuiGui().SetView(self.c.Views().CommitDescription.Name(), x0, y0+summaryViewHeight, x1, y1+summaryViewHeight, 0)
|
||||
}
|
||||
|
||||
func (self *ConfirmationHelper) IsPopupPanel(viewName string) bool {
|
||||
return viewName == "commitMessage" || viewName == "confirmation" || viewName == "menu"
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ type Helpers struct {
|
||||
GPG *GpgHelper
|
||||
Upstream *UpstreamHelper
|
||||
AmendHelper *AmendHelper
|
||||
Commits *CommitsHelper
|
||||
Snake *SnakeHelper
|
||||
// lives in context package because our contexts need it to render to main
|
||||
Diff *DiffHelper
|
||||
@ -64,6 +65,7 @@ func NewStubHelpers() *Helpers {
|
||||
GPG: &GpgHelper{},
|
||||
Upstream: &UpstreamHelper{},
|
||||
AmendHelper: &AmendHelper{},
|
||||
Commits: &CommitsHelper{},
|
||||
Snake: &SnakeHelper{},
|
||||
Diff: &DiffHelper{},
|
||||
Repos: &ReposHelper{},
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
@ -18,23 +19,23 @@ type IWorkingTreeHelper interface {
|
||||
}
|
||||
|
||||
type WorkingTreeHelper struct {
|
||||
c *HelperCommon
|
||||
refHelper *RefsHelper
|
||||
setCommitMessage func(message string)
|
||||
getSavedCommitMessage func() string
|
||||
c *HelperCommon
|
||||
refHelper *RefsHelper
|
||||
commitsHelper *CommitsHelper
|
||||
gpgHelper *GpgHelper
|
||||
}
|
||||
|
||||
func NewWorkingTreeHelper(
|
||||
c *HelperCommon,
|
||||
refHelper *RefsHelper,
|
||||
setCommitMessage func(message string),
|
||||
getSavedCommitMessage func() string,
|
||||
commitsHelper *CommitsHelper,
|
||||
gpgHelper *GpgHelper,
|
||||
) *WorkingTreeHelper {
|
||||
return &WorkingTreeHelper{
|
||||
c: c,
|
||||
refHelper: refHelper,
|
||||
setCommitMessage: setCommitMessage,
|
||||
getSavedCommitMessage: getSavedCommitMessage,
|
||||
c: c,
|
||||
refHelper: refHelper,
|
||||
commitsHelper: commitsHelper,
|
||||
gpgHelper: gpgHelper,
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,7 +84,7 @@ func (self *WorkingTreeHelper) OpenMergeTool() error {
|
||||
})
|
||||
}
|
||||
|
||||
func (self *WorkingTreeHelper) HandleCommitPress() error {
|
||||
func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage string) error {
|
||||
if err := self.prepareFilesForCommit(); err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
@ -96,28 +97,25 @@ func (self *WorkingTreeHelper) HandleCommitPress() error {
|
||||
return self.PromptToStageAllAndRetry(self.HandleCommitPress)
|
||||
}
|
||||
|
||||
savedCommitMessage := self.getSavedCommitMessage()
|
||||
if len(savedCommitMessage) > 0 {
|
||||
self.setCommitMessage(savedCommitMessage)
|
||||
} else {
|
||||
commitPrefixConfig := self.commitPrefixConfigForRepo()
|
||||
if commitPrefixConfig != nil {
|
||||
prefixPattern := commitPrefixConfig.Pattern
|
||||
prefixReplace := commitPrefixConfig.Replace
|
||||
rgx, err := regexp.Compile(prefixPattern)
|
||||
if err != nil {
|
||||
return self.c.ErrorMsg(fmt.Sprintf("%s: %s", self.c.Tr.LcCommitPrefixPatternError, err.Error()))
|
||||
}
|
||||
prefix := rgx.ReplaceAllString(self.refHelper.GetCheckedOutRef().Name, prefixReplace)
|
||||
self.setCommitMessage(prefix)
|
||||
}
|
||||
}
|
||||
return self.commitsHelper.OpenCommitMessagePanel(
|
||||
&OpenCommitMessagePanelOpts{
|
||||
CommitIndex: context.NoCommitIndex,
|
||||
InitialMessage: initialMessage,
|
||||
Title: self.c.Tr.CommitSummary,
|
||||
PreserveMessage: true,
|
||||
OnConfirm: self.handleCommit,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if err := self.c.PushContext(self.c.Contexts().CommitMessage); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
func (self *WorkingTreeHelper) handleCommit(message string) error {
|
||||
cmdObj := self.c.Git().Commit.CommitCmdObj(message)
|
||||
self.c.LogAction(self.c.Tr.Actions.Commit)
|
||||
_ = self.commitsHelper.EscapeCommitsPanel()
|
||||
return self.gpgHelper.WithGpgHandling(cmdObj, self.c.Tr.CommittingStatus, func() error {
|
||||
self.commitsHelper.OnCommitSuccess()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// HandleCommitEditorPress - handle when the user wants to commit changes via
|
||||
@ -143,9 +141,27 @@ func (self *WorkingTreeHelper) HandleWIPCommitPress() error {
|
||||
return self.c.ErrorMsg(self.c.Tr.SkipHookPrefixNotConfigured)
|
||||
}
|
||||
|
||||
self.setCommitMessage(skipHookPrefix)
|
||||
return self.HandleCommitPressWithMessage(skipHookPrefix)
|
||||
}
|
||||
|
||||
return self.HandleCommitPress()
|
||||
func (self *WorkingTreeHelper) HandleCommitPress() error {
|
||||
message := self.c.Contexts().CommitMessage.GetPreservedMessage()
|
||||
|
||||
if message != "" {
|
||||
commitPrefixConfig := self.commitPrefixConfigForRepo()
|
||||
if commitPrefixConfig != nil {
|
||||
prefixPattern := commitPrefixConfig.Pattern
|
||||
prefixReplace := commitPrefixConfig.Replace
|
||||
rgx, err := regexp.Compile(prefixPattern)
|
||||
if err != nil {
|
||||
return self.c.ErrorMsg(fmt.Sprintf("%s: %s", self.c.Tr.LcCommitPrefixPatternError, err.Error()))
|
||||
}
|
||||
prefix := rgx.ReplaceAllString(self.refHelper.GetCheckedOutRef().Name, prefixReplace)
|
||||
message = prefix
|
||||
}
|
||||
}
|
||||
|
||||
return self.HandleCommitPressWithMessage(message)
|
||||
}
|
||||
|
||||
func (self *WorkingTreeHelper) PromptToStageAllAndRetry(retry func() error) error {
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
"github.com/samber/lo"
|
||||
@ -256,24 +257,30 @@ func (self *LocalCommitsController) reword(commit *models.Commit) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
message, err := self.c.Git().Commit.GetCommitMessage(commit.Sha)
|
||||
commitMessage, err := self.c.Git().Commit.GetCommitMessage(commit.Sha)
|
||||
if err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
|
||||
// TODO: use the commit message panel here
|
||||
return self.c.Prompt(types.PromptOpts{
|
||||
Title: self.c.Tr.LcRewordCommit,
|
||||
InitialContent: message,
|
||||
HandleConfirm: func(response string) error {
|
||||
self.c.LogAction(self.c.Tr.Actions.RewordCommit)
|
||||
if err := self.c.Git().Rebase.RewordCommit(self.c.Model().Commits, self.context().GetSelectedLineIdx(), response); err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
|
||||
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
||||
return self.c.Helpers().Commits.OpenCommitMessagePanel(
|
||||
&helpers.OpenCommitMessagePanelOpts{
|
||||
CommitIndex: self.context().GetSelectedLineIdx(),
|
||||
InitialMessage: commitMessage,
|
||||
Title: self.c.Tr.Actions.RewordCommit,
|
||||
PreserveMessage: false,
|
||||
OnConfirm: self.handleReword,
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
func (self *LocalCommitsController) handleReword(message string) error {
|
||||
err := self.c.Git().Rebase.RewordCommit(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), message)
|
||||
if err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
self.c.Helpers().Commits.OnCommitSuccess()
|
||||
_ = self.c.Helpers().Commits.EscapeCommitsPanel()
|
||||
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
||||
}
|
||||
|
||||
func (self *LocalCommitsController) doRewordEditor() error {
|
||||
|
Reference in New Issue
Block a user