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

926 lines
27 KiB
Go
Raw Normal View History

package controllers
import (
"fmt"
"github.com/fsmiamoto/git-todo-parser/todo"
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 03:32:27 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
2022-02-05 08:04:10 +02:00
"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"
)
// after selecting the 200th commit, we'll load in all the rest
const COMMIT_THRESHOLD = 200
type (
2022-02-22 11:13:11 +02:00
PullFilesFn func() error
)
type LocalCommitsController struct {
2022-02-05 05:42:56 +02:00
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-02-06 06:54:26 +02:00
2022-02-22 11:13:11 +02:00
pullFiles PullFilesFn
}
var _ types.IController = &LocalCommitsController{}
func NewLocalCommitsController(
2023-03-23 09:47:29 +02:00
common *ControllerCommon,
pullFiles PullFilesFn,
) *LocalCommitsController {
return &LocalCommitsController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: common,
pullFiles: pullFiles,
}
}
2022-02-05 01:31:07 +02:00
func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
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),
Description: self.c.Tr.SquashDown,
},
{
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),
Description: self.c.Tr.FixupCommit,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Commits.RenameCommit),
Handler: self.checkSelected(self.reword),
Description: self.c.Tr.RewordCommit,
},
{
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),
Description: self.c.Tr.RenameCommitEditor,
},
{
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),
Description: self.c.Tr.DeleteCommit,
},
{
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),
Description: self.c.Tr.EditCommit,
},
{
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),
Description: self.c.Tr.PickCommit,
},
{
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),
Description: self.c.Tr.CreateFixupCommitDescription,
},
{
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),
Description: self.c.Tr.SquashAboveCommits,
},
{
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),
Description: self.c.Tr.MoveDownCommit,
},
{
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),
Description: self.c.Tr.MoveUpCommit,
},
2022-01-30 11:03:08 +02:00
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Commits.PasteCommits),
Handler: self.paste,
Description: self.c.Tr.PasteCommits,
2022-01-30 11:03:08 +02:00
},
{
Key: opts.GetKey(opts.Config.Commits.MarkCommitAsBaseForRebase),
Handler: self.checkSelected(self.markAsBaseCommit),
Description: self.c.Tr.MarkAsBaseCommit,
Tooltip: self.c.Tr.MarkAsBaseCommitTooltip,
},
// 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,
Description: self.c.Tr.StartSearch,
Tag: "navigation",
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.GotoBottom),
Handler: self.gotoBottom,
Description: self.c.Tr.GotoBottom,
Tag: "navigation",
},
}
for _, binding := range outsideFilterModeBindings {
2022-02-05 01:31:07 +02:00
binding.Handler = opts.Guards.OutsideFilterMode(binding.Handler)
}
bindings := append(outsideFilterModeBindings, []*types.Binding{
{
Key: opts.GetKey(opts.Config.Commits.AmendToCommit),
Handler: self.checkSelected(self.amendTo),
Description: self.c.Tr.AmendToCommit,
},
{
Key: opts.GetKey(opts.Config.Commits.ResetCommitAuthor),
Handler: self.checkSelected(self.amendAttribute),
Description: self.c.Tr.SetResetCommitAuthor,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Commits.RevertCommit),
Handler: self.checkSelected(self.revert),
Description: self.c.Tr.RevertCommit,
},
{
2023-02-20 09:52:45 +02:00
Key: opts.GetKey(opts.Config.Commits.CreateTag),
2022-02-26 10:39:39 +02:00
Handler: self.checkSelected(self.createTag),
Description: self.c.Tr.TagCommit,
},
{
Key: opts.GetKey(opts.Config.Commits.OpenLogMenu),
Handler: self.handleOpenLogMenu,
Description: self.c.Tr.OpenLogMenu,
OpensMenu: true,
},
}...)
2022-02-05 01:31:07 +02:00
return bindings
}
func (self *LocalCommitsController) 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
commit := self.context().GetSelected()
if commit == nil {
task = types.NewRenderStringTask(self.c.Tr.NoCommitsThisBranch)
} else if commit.Action == todo.UpdateRef {
task = types.NewRenderStringTask(
utils.ResolvePlaceholderString(
self.c.Tr.UpdateRefHere,
map[string]string{
"ref": commit.Name,
}))
} else {
cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.GetAppState().IgnoreWhitespaceInDiffView)
task = types.NewRunPtyTask(cmdObj.GetCmd())
}
return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: "Patch",
SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
},
Secondary: secondaryPatchPanelUpdateOpts(self.c),
})
})
}
}
2023-03-23 09:47:29 +02:00
func secondaryPatchPanelUpdateOpts(c *ControllerCommon) *types.ViewUpdateOpts {
if c.Git().Patch.PatchBuilder.Active() {
patch := c.Git().Patch.PatchBuilder.RenderAggregatedPatch(false)
return &types.ViewUpdateOpts{
Task: types.NewRenderStringWithoutScrollTask(patch),
Title: c.Tr.CustomPatch,
}
}
return nil
}
2022-02-13 08:24:37 +02:00
func (self *LocalCommitsController) squashDown(commit *models.Commit) error {
2023-03-23 04:04:57 +02:00
if self.context().GetSelectedLineIdx() >= len(self.c.Model().Commits)-1 {
return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit)
}
applied, err := self.handleMidRebaseCommand(todo.Squash, commit)
if err != nil {
return err
}
if applied {
return nil
}
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.Squash,
Prompt: self.c.Tr.SureSquashThisCommit,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.SquashCommitDown)
return self.interactiveRebase(todo.Squash)
})
},
})
}
2022-02-27 01:18:03 +02:00
func (self *LocalCommitsController) fixup(commit *models.Commit) error {
2023-03-23 04:04:57 +02:00
if self.context().GetSelectedLineIdx() >= len(self.c.Model().Commits)-1 {
return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit)
}
applied, err := self.handleMidRebaseCommand(todo.Fixup, commit)
if err != nil {
return err
}
if applied {
return nil
}
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.Fixup,
Prompt: self.c.Tr.SureFixupThisCommit,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.FixingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.FixupCommit)
return self.interactiveRebase(todo.Fixup)
})
},
})
}
func (self *LocalCommitsController) reword(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand(todo.Reword, commit)
if err != nil {
return err
}
if applied {
return nil
}
commitMessage, err := self.c.Git().Commit.GetCommitMessage(commit.Sha)
if err != nil {
return self.c.Error(err)
}
return self.c.Helpers().Commits.OpenCommitMessagePanel(
&helpers.OpenCommitMessagePanelOpts{
CommitIndex: self.context().GetSelectedLineIdx(),
InitialMessage: commitMessage,
SummaryTitle: self.c.Tr.Actions.RewordCommit,
DescriptionTitle: self.c.Tr.CommitDescriptionTitle,
PreserveMessage: false,
OnConfirm: self.handleReword,
OnSwitchToEditor: self.switchFromCommitMessagePanelToEditor,
},
)
}
func (self *LocalCommitsController) switchFromCommitMessagePanelToEditor(filepath string) error {
if self.isHeadCommit() {
return self.c.RunSubprocessAndRefresh(
self.c.Git().Commit.RewordLastCommitInEditorWithMessageFileCmdObj(filepath))
}
err := self.c.Git().Rebase.BeginInteractiveRebaseForCommit(self.c.Model().Commits, self.context().GetSelectedLineIdx(), false)
if err != nil {
return err
}
// now the selected commit should be our head so we'll amend it with the new message
err = self.c.RunSubprocessAndRefresh(
self.c.Git().Commit.RewordLastCommitInEditorWithMessageFileCmdObj(filepath))
if err != nil {
return err
}
err = self.c.Git().Rebase.ContinueRebase()
if err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (self *LocalCommitsController) handleReword(summary string, description string) error {
err := self.c.Git().Rebase.RewordCommit(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), summary, description)
if err != nil {
return self.c.Error(err)
}
self.c.Helpers().Commits.OnCommitSuccess()
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (self *LocalCommitsController) doRewordEditor() error {
self.c.LogAction(self.c.Tr.Actions.RewordCommit)
if self.isHeadCommit() {
return self.c.RunSubprocessAndRefresh(self.c.Git().Commit.RewordLastCommitInEditorCmdObj())
}
2023-03-23 04:04:57 +02:00
subProcess, err := self.c.Git().Rebase.RewordCommitInEditor(
self.c.Model().Commits, self.context().GetSelectedLineIdx(),
)
if err != nil {
return self.c.Error(err)
}
if subProcess != nil {
return self.c.RunSubprocessAndRefresh(subProcess)
}
return nil
}
2022-02-13 08:24:37 +02:00
func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error {
midRebase, err := self.handleMidRebaseCommand(todo.Reword, commit)
if err != nil {
return err
}
if midRebase {
return nil
}
if self.c.UserConfig.Gui.SkipRewordInEditorWarning {
return self.doRewordEditor()
} else {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.RewordInEditorTitle,
Prompt: self.c.Tr.RewordInEditorPrompt,
HandleConfirm: self.doRewordEditor,
})
}
}
2022-02-13 08:24:37 +02:00
func (self *LocalCommitsController) drop(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand(todo.Drop, commit)
if err != nil {
return err
}
if applied {
return nil
}
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DeleteCommitTitle,
Prompt: self.c.Tr.DeleteCommitPrompt,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.DeletingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.DropCommit)
return self.interactiveRebase(todo.Drop)
})
},
})
}
2022-02-13 08:24:37 +02:00
func (self *LocalCommitsController) edit(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand(todo.Edit, commit)
if err != nil {
return err
}
if applied {
return nil
}
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.EditCommit)
err := self.c.Git().Rebase.EditRebase(commit.Sha)
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebaseWithRefreshOptions(
err,
types.RefreshOptions{Mode: types.BLOCK_UI, Then: func() {
_, index, ok := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool {
return c.Sha == commit.Sha
})
if ok {
self.context().SetSelectedLineIdx(index)
}
}})
})
}
2022-02-13 08:24:37 +02:00
func (self *LocalCommitsController) pick(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand(todo.Pick, commit)
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 todo.TodoCommand) error {
2023-03-23 04:04:57 +02:00
err := self.c.Git().Rebase.InteractiveRebase(self.c.Model().Commits, self.context().GetSelectedLineIdx(), action)
2023-03-23 09:47:29 +02:00
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
}
// 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
func (self *LocalCommitsController) handleMidRebaseCommand(action todo.TodoCommand, commit *models.Commit) (bool, error) {
if commit.Action == models.ActionConflict {
return true, self.c.ErrorMsg(self.c.Tr.ChangingThisActionIsNotAllowed)
}
if !commit.IsTODO() {
2023-03-23 04:04:57 +02:00
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
// If we are in a rebase, the only action that is allowed for
// non-todo commits is rewording the current head commit
if !(action == todo.Reword && self.isHeadCommit()) {
return true, self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
}
}
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 == todo.Reword {
return true, self.c.ErrorMsg(self.c.Tr.RewordNotSupported)
}
if allowed := isChangeOfRebaseTodoAllowed(action); !allowed {
return true, self.c.ErrorMsg(self.c.Tr.ChangingThisActionIsNotAllowed)
}
self.c.LogAction("Update rebase TODO")
msg := utils.ResolvePlaceholderString(
self.c.Tr.Log.HandleMidRebaseCommand,
map[string]string{
"shortSha": commit.ShortSha(),
"action": action.String(),
},
)
self.c.LogCommand(msg, false)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Rebase.EditRebaseTodo(commit, action); 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()
2023-03-23 04:04:57 +02:00
commits := self.c.Model().Commits
// can't move past the initial commit
if index >= len(commits)-1 {
return nil
}
if commit.IsTODO() {
if !commits[index+1].IsTODO() || commits[index+1].Action == models.ActionConflict {
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)
msg := utils.ResolvePlaceholderString(
self.c.Tr.Log.MovingCommitDown,
map[string]string{
"shortSha": commit.ShortSha(),
},
)
self.c.LogCommand(msg, false)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Rebase.MoveTodoDown(commit); err != nil {
return self.c.Error(err)
}
2022-02-06 06:54:26 +02:00
self.context().MoveSelectedLine(1)
return self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
})
}
2023-03-23 04:04:57 +02:00
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
return self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
}
return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.MoveCommitDown)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Rebase.MoveCommitDown(self.c.Model().Commits, index)
if err == nil {
2022-02-06 06:54:26 +02:00
self.context().MoveSelectedLine(1)
}
2023-03-23 09:47:29 +02:00
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
})
}
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()
if index == 0 {
return nil
}
if commit.IsTODO() {
// logging directly here because MoveTodoDown doesn't have enough information
// to provide a useful log
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp)
msg := utils.ResolvePlaceholderString(
self.c.Tr.Log.MovingCommitUp,
map[string]string{
"shortSha": commit.ShortSha(),
},
)
self.c.LogCommand(msg, false)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Rebase.MoveTodoUp(self.c.Model().Commits[index]); err != nil {
return self.c.Error(err)
}
2022-02-06 06:54:26 +02:00
self.context().MoveSelectedLine(-1)
return self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
})
}
2023-03-23 04:04:57 +02:00
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
return self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
}
return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp)
err := self.c.Git().Rebase.MoveCommitUp(self.c.Model().Commits, index)
if err == nil {
2022-02-06 06:54:26 +02:00
self.context().MoveSelectedLine(-1)
}
2023-03-23 09:47:29 +02:00
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
})
}
2022-02-26 10:39:39 +02:00
func (self *LocalCommitsController) amendTo(commit *models.Commit) error {
if self.isHeadCommit() {
2023-03-23 09:47:29 +02:00
if err := self.c.Helpers().AmendHelper.AmendHead(); err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
2023-03-23 04:04:57 +02:00
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
return self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
}
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.AmendCommitTitle,
Prompt: self.c.Tr.AmendCommitPrompt,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.AmendCommit)
err := self.c.Git().Rebase.AmendTo(self.c.Model().Commits, self.context().GetView().SelectedLineIdx())
2023-03-23 09:47:29 +02:00
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
})
},
})
}
func (self *LocalCommitsController) amendAttribute(commit *models.Commit) error {
2023-03-23 09:47:29 +02:00
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE && !self.isHeadCommit() {
return self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
}
return self.c.Menu(types.CreateMenuOptions{
Title: "Amend commit attribute",
Items: []*types.MenuItem{
{
Label: self.c.Tr.ResetAuthor,
OnPress: self.resetAuthor,
Key: 'a',
Tooltip: "Reset the commit's author to the currently configured user. This will also renew the author timestamp",
},
{
Label: self.c.Tr.SetAuthor,
OnPress: self.setAuthor,
Key: 'A',
Tooltip: "Set the author based on a prompt",
},
},
})
}
func (self *LocalCommitsController) resetAuthor() error {
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.ResetCommitAuthor)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Rebase.ResetCommitAuthor(self.c.Model().Commits, self.context().GetSelectedLineIdx()); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
})
}
func (self *LocalCommitsController) setAuthor() error {
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.SetAuthorPromptTitle,
2023-03-23 09:47:29 +02:00
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetAuthorsSuggestionsFunc(),
HandleConfirm: func(value string) error {
return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.SetCommitAuthor)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Rebase.SetCommitAuthor(self.c.Model().Commits, self.context().GetSelectedLineIdx(), value); 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 {
if commit.IsMerge() {
return self.createRevertMergeCommitMenu(commit)
} else {
return self.c.Confirm(types.ConfirmOpts{
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)
2023-03-23 04:04:57 +02:00
if err := self.c.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))
for i, parentSha := range commit.Parents {
i := i
2023-03-23 04:04:57 +02:00
message, err := self.c.Git().Commit.GetCommitMessageFirstLine(parentSha)
if err != nil {
return self.c.Error(err)
}
2022-01-29 10:09:20 +02:00
menuItems[i] = &types.MenuItem{
Label: fmt.Sprintf("%s: %s", utils.SafeTruncate(parentSha, 8), message),
OnPress: func() error {
parentNumber := i + 1
self.c.LogAction(self.c.Tr.Actions.RevertCommit)
2023-03-23 04:04:57 +02:00
if err := self.c.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})
}
func (self *LocalCommitsController) afterRevertCommit() error {
2022-02-06 06:54:26 +02:00
self.context().MoveSelectedLine(1)
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 {
prompt := utils.ResolvePlaceholderString(
self.c.Tr.SureCreateFixupCommit,
map[string]string{
"commit": commit.Sha,
},
)
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.CreateFixupCommit,
Prompt: prompt,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.CreateFixupCommit)
2023-03-23 04:04:57 +02:00
if err := self.c.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 {
prompt := utils.ResolvePlaceholderString(
self.c.Tr.SureSquashAboveCommits,
2022-03-26 05:44:30 +02:00
map[string]string{"commit": commit.Sha},
)
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.SquashAboveCommits,
Prompt: prompt,
HandleConfirm: func() error {
return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.SquashAllAboveFixupCommits)
2023-03-23 04:04:57 +02:00
err := self.c.Git().Rebase.SquashAllAboveFixupCommits(commit)
2023-03-23 09:47:29 +02:00
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
})
},
})
}
2022-02-26 10:39:39 +02:00
func (self *LocalCommitsController) createTag(commit *models.Commit) error {
return self.c.Helpers().Tags.OpenCreateTagPrompt(commit.Sha, func() {})
}
2022-01-31 13:11:34 +02:00
func (self *LocalCommitsController) openSearch() error {
// 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)
if err := self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
return err
}
}
return self.c.Helpers().Search.OpenSearchPrompt(self.context())
}
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)
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)
return nil
}
func (self *LocalCommitsController) handleOpenLogMenu() error {
2022-01-29 10:09:20 +02:00
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.LogMenuTitle,
2022-01-29 10:09:20 +02:00
Items: []*types.MenuItem{
{
Label: self.c.Tr.ToggleShowGitGraphAll,
OnPress: func() error {
2022-02-06 06:54:26 +02:00
self.context().SetShowWholeGitGraph(!self.context().GetShowWholeGitGraph())
2022-02-06 06:54:26 +02:00
if self.context().GetShowWholeGitGraph() {
self.context().SetLimitCommits(false)
}
return self.c.WithWaitingStatus(self.c.Tr.LoadingCommits, func(gocui.Task) error {
2022-03-26 05:44:30 +02:00
return self.c.Refresh(
types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.COMMITS}},
)
})
},
},
{
Label: self.c.Tr.ShowGitGraph,
OpensMenu: true,
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{
Title: self.c.Tr.LogMenuTitle,
2022-01-29 10:09:20 +02:00
Items: []*types.MenuItem{
{
Label: "always",
OnPress: onPress("always"),
},
{
Label: "never",
OnPress: onPress("never"),
},
{
Label: "when maximised",
OnPress: onPress("when-maximised"),
},
},
})
},
},
{
Label: self.c.Tr.SortCommits,
OpensMenu: true,
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.LoadingCommits, func(gocui.Task) 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-29 10:09:20 +02:00
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.LogMenuTitle,
2022-01-29 10:09:20 +02:00
Items: []*types.MenuItem{
{
Label: "topological (topo-order)",
OnPress: onPress("topo-order"),
},
{
Label: "date-order",
OnPress: onPress("date-order"),
},
{
Label: "author-date-order",
OnPress: onPress("author-date-order"),
},
},
})
},
},
},
})
}
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()
if commit == nil {
return nil
}
return callback(commit)
}
}
func (self *LocalCommitsController) GetOnFocus() func(types.OnFocusOpts) error {
return func(types.OnFocusOpts) error {
context := self.context()
if context.GetSelectedLineIdx() > COMMIT_THRESHOLD && context.GetLimitCommits() {
context.SetLimitCommits(false)
self.c.OnWorker(func(_ gocui.Task) {
if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
_ = self.c.Error(err)
}
})
}
return nil
}
}
func (self *LocalCommitsController) Context() types.Context {
2022-02-06 06:54:26 +02:00
return self.context()
}
func (self *LocalCommitsController) context() *context.LocalCommitsContext {
2023-03-23 04:04:57 +02:00
return self.c.Contexts().LocalCommits
}
2022-01-30 11:03:08 +02:00
func (self *LocalCommitsController) paste() error {
2023-03-23 09:47:29 +02:00
return self.c.Helpers().CherryPick.Paste()
2022-01-30 11:03:08 +02:00
}
func (self *LocalCommitsController) markAsBaseCommit(commit *models.Commit) error {
if commit.Sha == self.c.Modes().MarkedBaseCommit.GetSha() {
// Reset when invoking it again on the marked commit
self.c.Modes().MarkedBaseCommit.SetSha("")
} else {
self.c.Modes().MarkedBaseCommit.SetSha(commit.Sha)
}
return self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits)
}
func (self *LocalCommitsController) isHeadCommit() bool {
2023-03-23 04:04:57 +02:00
return models.IsHeadCommit(self.c.Model().Commits, self.context().GetSelectedLineIdx())
}
func isChangeOfRebaseTodoAllowed(action todo.TodoCommand) bool {
allowedActions := []todo.TodoCommand{
todo.Pick,
todo.Drop,
todo.Edit,
todo.Fixup,
todo.Squash,
todo.Reword,
}
return lo.Contains(allowedActions, action)
}