2022-01-16 05:46:53 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-02-05 08:04:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
2022-01-16 05:46:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-02-22 11:13:11 +02:00
|
|
|
PullFilesFn func() error
|
2022-01-16 05:46:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LocalCommitsController struct {
|
2022-02-05 05:42:56 +02:00
|
|
|
baseController
|
2022-02-06 06:54:26 +02:00
|
|
|
*controllerCommon
|
|
|
|
|
2022-02-22 11:13:11 +02:00
|
|
|
pullFiles PullFilesFn
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IController = &LocalCommitsController{}
|
|
|
|
|
|
|
|
func NewLocalCommitsController(
|
2022-02-06 06:54:26 +02:00
|
|
|
common *controllerCommon,
|
2022-01-16 05:46:53 +02:00
|
|
|
pullFiles PullFilesFn,
|
|
|
|
) *LocalCommitsController {
|
|
|
|
return &LocalCommitsController{
|
2022-02-22 11:13:11 +02:00
|
|
|
baseController: baseController{},
|
|
|
|
controllerCommon: common,
|
|
|
|
pullFiles: pullFiles,
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
2022-01-16 05:46:53 +02:00
|
|
|
outsideFilterModeBindings := []*types.Binding{
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.SquashDown),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.squashDown),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcSquashDown,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.MarkCommitAsFixup),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.fixup),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcFixupCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.RenameCommit),
|
2022-01-16 05:46:53 +02:00
|
|
|
Handler: self.checkSelected(self.reword),
|
|
|
|
Description: self.c.Tr.LcRewordCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.RenameCommitWithEditor),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.rewordEditor),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcRenameCommitEditor,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.Remove),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.drop),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcDeleteCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.Edit),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.edit),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcEditCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.PickCommit),
|
2022-02-13 08:24:37 +02:00
|
|
|
Handler: self.checkSelected(self.pick),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcPickCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.CreateFixupCommit),
|
2022-02-26 10:39:39 +02:00
|
|
|
Handler: self.checkSelected(self.createFixupCommit),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcCreateFixupCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.SquashAboveCommits),
|
2022-02-26 10:39:39 +02:00
|
|
|
Handler: self.checkSelected(self.squashAllAboveFixupCommits),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcSquashAboveCommits,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.MoveDownCommit),
|
2022-02-26 10:39:39 +02:00
|
|
|
Handler: self.checkSelected(self.moveDown),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcMoveDownCommit,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.MoveUpCommit),
|
2022-02-26 10:39:39 +02:00
|
|
|
Handler: self.checkSelected(self.moveUp),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcMoveUpCommit,
|
|
|
|
},
|
2022-01-30 11:03:08 +02:00
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.PasteCommits),
|
|
|
|
Handler: opts.Guards.OutsideFilterMode(self.paste),
|
2022-01-30 11:03:08 +02:00
|
|
|
Description: self.c.Tr.LcPasteCommits,
|
|
|
|
},
|
2022-01-16 05:46:53 +02:00
|
|
|
// overriding these navigation keybindings because we might need to load
|
|
|
|
// more commits on demand
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.StartSearch),
|
2022-01-31 13:11:34 +02:00
|
|
|
Handler: self.openSearch,
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcStartSearch,
|
|
|
|
Tag: "navigation",
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.GotoBottom),
|
2022-01-16 05:46:53 +02:00
|
|
|
Handler: self.gotoBottom,
|
|
|
|
Description: self.c.Tr.LcGotoBottom,
|
|
|
|
Tag: "navigation",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, binding := range outsideFilterModeBindings {
|
2022-02-05 01:31:07 +02:00
|
|
|
binding.Handler = opts.Guards.OutsideFilterMode(binding.Handler)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bindings := append(outsideFilterModeBindings, []*types.Binding{
|
|
|
|
{
|
2022-03-27 09:48:37 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.AmendToCommit),
|
|
|
|
Handler: self.checkSelected(self.amendTo),
|
|
|
|
Description: self.c.Tr.LcAmendToCommit,
|
|
|
|
},
|
2022-04-22 16:01:30 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Commits.ResetCommitAuthor),
|
|
|
|
Handler: self.checkSelected(self.resetAuthor),
|
|
|
|
Description: self.c.Tr.LcResetCommitAuthor,
|
|
|
|
},
|
2022-03-27 09:48:37 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Commits.RevertCommit),
|
|
|
|
Handler: self.checkSelected(self.revert),
|
|
|
|
Description: self.c.Tr.LcRevertCommit,
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Commits.TagCommit),
|
2022-02-26 10:39:39 +02:00
|
|
|
Handler: self.checkSelected(self.createTag),
|
2022-01-16 05:46:53 +02:00
|
|
|
Description: self.c.Tr.LcTagCommit,
|
|
|
|
},
|
2022-03-27 09:48:37 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Commits.OpenLogMenu),
|
|
|
|
Handler: self.handleOpenLogMenu,
|
|
|
|
Description: self.c.Tr.LcOpenLogMenu,
|
|
|
|
OpensMenu: true,
|
|
|
|
},
|
2022-01-16 05:46:53 +02:00
|
|
|
}...)
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
return bindings
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) squashDown(commit *models.Commit) error {
|
2022-01-31 13:20:28 +02:00
|
|
|
if len(self.model.Commits) <= 1 {
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.ErrorMsg(self.c.Tr.YouNoCommitsToSquash)
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
applied, err := self.handleMidRebaseCommand("squash", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.Squash,
|
|
|
|
Prompt: self.c.Tr.SureSquashThisCommit,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.SquashCommitDown)
|
|
|
|
return self.interactiveRebase("squash")
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 01:18:03 +02:00
|
|
|
func (self *LocalCommitsController) fixup(commit *models.Commit) error {
|
2022-01-31 13:20:28 +02:00
|
|
|
if len(self.model.Commits) <= 1 {
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.ErrorMsg(self.c.Tr.YouNoCommitsToSquash)
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
applied, err := self.handleMidRebaseCommand("fixup", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.Fixup,
|
|
|
|
Prompt: self.c.Tr.SureFixupThisCommit,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.FixingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.FixupCommit)
|
|
|
|
return self.interactiveRebase("fixup")
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) reword(commit *models.Commit) error {
|
2022-02-13 08:24:37 +02:00
|
|
|
applied, err := self.handleMidRebaseCommand("reword", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := self.git.Commit.GetCommitMessage(commit.Sha)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use the commit message panel here
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Prompt(types.PromptOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LcRewordCommit,
|
|
|
|
InitialContent: message,
|
|
|
|
HandleConfirm: func(response string) error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.RewordCommit)
|
2022-02-06 06:54:26 +02:00
|
|
|
if err := self.git.Rebase.RewordCommit(self.model.Commits, self.context().GetSelectedLineIdx(), response); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error {
|
|
|
|
applied, err := self.handleMidRebaseCommand("reword", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-03-23 14:37:15 +02:00
|
|
|
Title: self.c.Tr.RewordInEditorTitle,
|
|
|
|
Prompt: self.c.Tr.RewordInEditorPrompt,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.RewordCommit)
|
|
|
|
subProcess, err := self.git.Rebase.RewordCommitInEditor(
|
|
|
|
self.model.Commits, self.context().GetSelectedLineIdx(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
if subProcess != nil {
|
|
|
|
return self.c.RunSubprocessAndRefresh(subProcess)
|
|
|
|
}
|
2022-01-16 05:46:53 +02:00
|
|
|
|
2022-03-23 14:37:15 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) drop(commit *models.Commit) error {
|
|
|
|
applied, err := self.handleMidRebaseCommand("drop", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.DeleteCommitTitle,
|
|
|
|
Prompt: self.c.Tr.DeleteCommitPrompt,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.DeletingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.DropCommit)
|
|
|
|
return self.interactiveRebase("drop")
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) edit(commit *models.Commit) error {
|
|
|
|
applied, err := self.handleMidRebaseCommand("edit", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.EditCommit)
|
|
|
|
return self.interactiveRebase("edit")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) pick(commit *models.Commit) error {
|
|
|
|
applied, err := self.handleMidRebaseCommand("pick", commit)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point we aren't actually rebasing so we will interpret this as an
|
|
|
|
// attempt to pull. We might revoke this later after enabling configurable keybindings
|
|
|
|
return self.pullFiles()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) interactiveRebase(action string) error {
|
2022-02-06 06:54:26 +02:00
|
|
|
err := self.git.Rebase.InteractiveRebase(self.model.Commits, self.context().GetSelectedLineIdx(), action)
|
|
|
|
return self.helpers.MergeAndRebase.CheckMergeOrRebase(err)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleMidRebaseCommand sees if the selected commit is in fact a rebasing
|
|
|
|
// commit meaning you are trying to edit the todo file rather than actually
|
|
|
|
// begin a rebase. It then updates the todo file with that action
|
2022-02-13 08:24:37 +02:00
|
|
|
func (self *LocalCommitsController) handleMidRebaseCommand(action string, commit *models.Commit) (bool, error) {
|
|
|
|
if commit.Status != "rebasing" {
|
2022-01-16 05:46:53 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now we do not support setting 'reword' because it requires an editor
|
|
|
|
// and that means we either unconditionally wait around for the subprocess to ask for
|
|
|
|
// our input or we set a lazygit client as the EDITOR env variable and have it
|
|
|
|
// request us to edit the commit message when prompted.
|
|
|
|
if action == "reword" {
|
|
|
|
return true, self.c.ErrorMsg(self.c.Tr.LcRewordNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.c.LogAction("Update rebase TODO")
|
|
|
|
self.c.LogCommand(
|
2022-02-13 08:24:37 +02:00
|
|
|
fmt.Sprintf("Updating rebase action of commit %s to '%s'", commit.ShortSha(), action),
|
2022-01-16 05:46:53 +02:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := self.git.Rebase.EditRebaseTodo(
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().GetSelectedLineIdx(), action,
|
2022-01-16 05:46:53 +02:00
|
|
|
); err != nil {
|
|
|
|
return false, self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, self.c.Refresh(types.RefreshOptions{
|
|
|
|
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) moveDown(commit *models.Commit) error {
|
2022-02-06 06:54:26 +02:00
|
|
|
index := self.context().GetSelectedLineIdx()
|
2022-01-31 13:20:28 +02:00
|
|
|
commits := self.model.Commits
|
2022-02-13 08:24:37 +02:00
|
|
|
if commit.Status == "rebasing" {
|
2022-01-16 05:46:53 +02:00
|
|
|
if commits[index+1].Status != "rebasing" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// logging directly here because MoveTodoDown doesn't have enough information
|
|
|
|
// to provide a useful log
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.MoveCommitDown)
|
2022-02-13 08:24:37 +02:00
|
|
|
self.c.LogCommand(fmt.Sprintf("Moving commit %s down", commit.ShortSha()), false)
|
2022-01-16 05:46:53 +02:00
|
|
|
|
|
|
|
if err := self.git.Rebase.MoveTodoDown(index); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().MoveSelectedLine(1)
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.Refresh(types.RefreshOptions{
|
|
|
|
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.MoveCommitDown)
|
2022-01-31 13:20:28 +02:00
|
|
|
err := self.git.Rebase.MoveCommitDown(self.model.Commits, index)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err == nil {
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().MoveSelectedLine(1)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.MergeAndRebase.CheckMergeOrRebase(err)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) moveUp(commit *models.Commit) error {
|
2022-02-06 06:54:26 +02:00
|
|
|
index := self.context().GetSelectedLineIdx()
|
2022-01-16 05:46:53 +02:00
|
|
|
if index == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:24:37 +02:00
|
|
|
if commit.Status == "rebasing" {
|
2022-01-16 05:46:53 +02:00
|
|
|
// logging directly here because MoveTodoDown doesn't have enough information
|
|
|
|
// to provide a useful log
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp)
|
|
|
|
self.c.LogCommand(
|
2022-02-13 08:24:37 +02:00
|
|
|
fmt.Sprintf("Moving commit %s up", commit.ShortSha()),
|
2022-01-16 05:46:53 +02:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := self.git.Rebase.MoveTodoDown(index - 1); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().MoveSelectedLine(-1)
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.Refresh(types.RefreshOptions{
|
|
|
|
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp)
|
2022-01-31 13:20:28 +02:00
|
|
|
err := self.git.Rebase.MoveCommitDown(self.model.Commits, index-1)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err == nil {
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().MoveSelectedLine(-1)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.MergeAndRebase.CheckMergeOrRebase(err)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) amendTo(commit *models.Commit) error {
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.AmendCommitTitle,
|
|
|
|
Prompt: self.c.Tr.AmendCommitPrompt,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.AmendCommit)
|
2022-02-13 08:24:37 +02:00
|
|
|
err := self.git.Rebase.AmendTo(commit.Sha)
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.MergeAndRebase.CheckMergeOrRebase(err)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:01:30 +02:00
|
|
|
func (self *LocalCommitsController) resetAuthor(commit *models.Commit) error {
|
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
|
|
|
Title: self.c.Tr.LcResetCommitAuthor,
|
|
|
|
Prompt: self.c.Tr.SureResetCommitAuthor,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.ResetCommitAuthor)
|
|
|
|
if err := self.git.Rebase.ResetCommitAuthor(self.model.Commits, self.context().GetSelectedLineIdx()); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) revert(commit *models.Commit) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
if commit.IsMerge() {
|
|
|
|
return self.createRevertMergeCommitMenu(commit)
|
|
|
|
} else {
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.Actions.RevertCommit,
|
|
|
|
Prompt: utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.ConfirmRevertCommit,
|
|
|
|
map[string]string{
|
|
|
|
"selectedCommit": commit.ShortSha(),
|
|
|
|
}),
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.RevertCommit)
|
|
|
|
if err := self.git.Commit.Revert(commit.Sha); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
return self.afterRevertCommit()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) createRevertMergeCommitMenu(commit *models.Commit) error {
|
2022-01-29 10:09:20 +02:00
|
|
|
menuItems := make([]*types.MenuItem, len(commit.Parents))
|
2022-01-16 05:46:53 +02:00
|
|
|
for i, parentSha := range commit.Parents {
|
|
|
|
i := i
|
|
|
|
message, err := self.git.Commit.GetCommitMessageFirstLine(parentSha)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
menuItems[i] = &types.MenuItem{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: fmt.Sprintf("%s: %s", utils.SafeTruncate(parentSha, 8), message),
|
2022-01-16 05:46:53 +02:00
|
|
|
OnPress: func() error {
|
|
|
|
parentNumber := i + 1
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.RevertCommit)
|
|
|
|
if err := self.git.Commit.RevertMerge(commit.Sha, parentNumber); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
return self.afterRevertCommit()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Menu(types.CreateMenuOptions{Title: self.c.Tr.SelectParentCommitForMerge, Items: menuItems})
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) afterRevertCommit() error {
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().MoveSelectedLine(1)
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.Refresh(types.RefreshOptions{
|
|
|
|
Mode: types.BLOCK_UI, Scope: []types.RefreshableView{types.COMMITS, types.BRANCHES},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 01:18:03 +02:00
|
|
|
func (self *LocalCommitsController) createFixupCommit(commit *models.Commit) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
prompt := utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.SureCreateFixupCommit,
|
|
|
|
map[string]string{
|
|
|
|
"commit": commit.Sha,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.CreateFixupCommit,
|
|
|
|
Prompt: prompt,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.CreateFixupCommit)
|
|
|
|
if err := self.git.Commit.CreateFixupCommit(commit.Sha); err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) squashAllAboveFixupCommits(commit *models.Commit) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
prompt := utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.SureSquashAboveCommits,
|
2022-03-26 05:44:30 +02:00
|
|
|
map[string]string{"commit": commit.Sha},
|
2022-01-16 05:46:53 +02:00
|
|
|
)
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.SquashAboveCommits,
|
|
|
|
Prompt: prompt,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.SquashAllAboveFixupCommits)
|
|
|
|
err := self.git.Rebase.SquashAllAboveFixupCommits(commit.Sha)
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.MergeAndRebase.CheckMergeOrRebase(err)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:39:39 +02:00
|
|
|
func (self *LocalCommitsController) createTag(commit *models.Commit) error {
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.Tags.CreateTagMenu(commit.Sha, func() {})
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:11:34 +02:00
|
|
|
func (self *LocalCommitsController) openSearch() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
// we usually lazyload these commits but now that we're searching we need to load them now
|
2022-02-06 06:54:26 +02:00
|
|
|
if self.context().GetLimitCommits() {
|
|
|
|
self.context().SetLimitCommits(false)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err := self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:11:34 +02:00
|
|
|
self.c.OpenSearch()
|
|
|
|
|
|
|
|
return nil
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) gotoBottom() error {
|
|
|
|
// we usually lazyload these commits but now that we're jumping to the bottom we need to load them now
|
2022-02-06 06:54:26 +02:00
|
|
|
if self.context().GetLimitCommits() {
|
|
|
|
self.context().SetLimitCommits(false)
|
2022-01-16 05:46:53 +02:00
|
|
|
if err := self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:31:52 +02:00
|
|
|
self.context().SetSelectedLineIdx(self.context().Len() - 1)
|
2022-01-16 05:46:53 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) handleOpenLogMenu() error {
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Menu(types.CreateMenuOptions{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LogMenuTitle,
|
2022-01-29 10:09:20 +02:00
|
|
|
Items: []*types.MenuItem{
|
2022-01-16 05:46:53 +02:00
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: self.c.Tr.ToggleShowGitGraphAll,
|
2022-01-16 05:46:53 +02:00
|
|
|
OnPress: func() error {
|
2022-02-06 06:54:26 +02:00
|
|
|
self.context().SetShowWholeGitGraph(!self.context().GetShowWholeGitGraph())
|
2022-01-16 05:46:53 +02:00
|
|
|
|
2022-02-06 06:54:26 +02:00
|
|
|
if self.context().GetShowWholeGitGraph() {
|
|
|
|
self.context().SetLimitCommits(false)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.LcLoadingCommits, func() error {
|
2022-03-26 05:44:30 +02:00
|
|
|
return self.c.Refresh(
|
|
|
|
types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}},
|
|
|
|
)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: self.c.Tr.ShowGitGraph,
|
|
|
|
OpensMenu: true,
|
2022-01-16 05:46:53 +02:00
|
|
|
OnPress: func() error {
|
|
|
|
onPress := func(value string) func() error {
|
|
|
|
return func() error {
|
|
|
|
self.c.UserConfig.Git.Log.ShowGraph = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Menu(types.CreateMenuOptions{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LogMenuTitle,
|
2022-01-29 10:09:20 +02:00
|
|
|
Items: []*types.MenuItem{
|
2022-01-16 05:46:53 +02:00
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "always",
|
|
|
|
OnPress: onPress("always"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "never",
|
|
|
|
OnPress: onPress("never"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "when maximised",
|
|
|
|
OnPress: onPress("when-maximised"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: self.c.Tr.SortCommits,
|
|
|
|
OpensMenu: true,
|
2022-01-16 05:46:53 +02:00
|
|
|
OnPress: func() error {
|
|
|
|
onPress := func(value string) func() error {
|
|
|
|
return func() error {
|
|
|
|
self.c.UserConfig.Git.Log.Order = value
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.LcLoadingCommits, func() error {
|
2022-03-26 05:44:30 +02:00
|
|
|
return self.c.Refresh(
|
|
|
|
types.RefreshOptions{
|
|
|
|
Mode: types.SYNC,
|
|
|
|
Scope: []types.RefreshableView{types.COMMITS},
|
|
|
|
},
|
|
|
|
)
|
2022-01-16 05:46:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Menu(types.CreateMenuOptions{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LogMenuTitle,
|
2022-01-29 10:09:20 +02:00
|
|
|
Items: []*types.MenuItem{
|
2022-01-16 05:46:53 +02:00
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "topological (topo-order)",
|
|
|
|
OnPress: onPress("topo-order"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "date-order",
|
|
|
|
OnPress: onPress("date-order"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
{
|
2022-05-08 06:23:32 +02:00
|
|
|
Label: "author-date-order",
|
|
|
|
OnPress: onPress("author-date-order"),
|
2022-01-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) checkSelected(callback func(*models.Commit) error) func() error {
|
|
|
|
return func() error {
|
2022-02-06 06:54:26 +02:00
|
|
|
commit := self.context().GetSelected()
|
2022-01-16 05:46:53 +02:00
|
|
|
if commit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(commit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) Context() types.Context {
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsController) context() *context.LocalCommitsContext {
|
2022-02-13 08:01:53 +02:00
|
|
|
return self.contexts.LocalCommits
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
2022-01-30 11:03:08 +02:00
|
|
|
|
|
|
|
func (self *LocalCommitsController) paste() error {
|
2022-02-06 06:54:26 +02:00
|
|
|
return self.helpers.CherryPick.Paste()
|
2022-01-30 11:03:08 +02:00
|
|
|
}
|