1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/gui/controllers/helpers/cherry_pick_helper.go

144 lines
4.0 KiB
Go
Raw Normal View History

2022-02-06 06:54:26 +02:00
package helpers
2022-01-30 11:03:08 +02:00
import (
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"
2022-01-30 11:03:08 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
2022-01-30 11:03:08 +02:00
)
type CherryPickHelper struct {
c *HelperCommon
2022-01-30 11:03:08 +02:00
2022-02-06 06:54:26 +02:00
rebaseHelper *MergeAndRebaseHelper
2022-01-30 11:03:08 +02:00
}
// I'm using the analogy of copy+paste in the terminology here because it's intuitively what's going on,
// even if in truth we're running git cherry-pick
func NewCherryPickHelper(
c *HelperCommon,
2022-02-06 06:54:26 +02:00
rebaseHelper *MergeAndRebaseHelper,
2022-01-30 11:03:08 +02:00
) *CherryPickHelper {
return &CherryPickHelper{
c: c,
rebaseHelper: rebaseHelper,
}
}
func (self *CherryPickHelper) getData() *cherrypicking.CherryPicking {
return self.c.Modes().CherryPicking
}
2022-01-30 11:03:08 +02:00
func (self *CherryPickHelper) Copy(commit *models.Commit, commitsList []*models.Commit, context types.Context) error {
if err := self.resetIfNecessary(context); err != nil {
return err
}
// we will un-copy it if it's already copied
if self.getData().SelectedShaSet().Includes(commit.Sha) {
self.getData().Remove(commit, commitsList)
} else {
self.getData().Add(commit, commitsList)
2022-01-30 11:03:08 +02:00
}
return self.rerender()
}
func (self *CherryPickHelper) CopyRange(commitsList []*models.Commit, context types.IListContext) error {
startIdx, endIdx := context.GetList().GetSelectionRange()
2022-01-30 11:03:08 +02:00
if err := self.resetIfNecessary(context); err != nil {
return err
}
commitSet := self.getData().SelectedShaSet()
2022-01-30 11:03:08 +02:00
allCommitsCopied := lo.EveryBy(commitsList[startIdx:endIdx+1], func(commit *models.Commit) bool {
return commitSet.Includes(commit.Sha)
})
2022-01-30 11:03:08 +02:00
// if all selected commits are already copied, we'll uncopy them
if allCommitsCopied {
for index := startIdx; index <= endIdx; index++ {
commit := commitsList[index]
self.getData().Remove(commit, commitsList)
}
} else {
for index := startIdx; index <= endIdx; index++ {
commit := commitsList[index]
self.getData().Add(commit, commitsList)
}
2022-01-30 11:03:08 +02:00
}
return self.rerender()
}
// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied.
// Only to be called from the branch commits controller
func (self *CherryPickHelper) Paste() error {
return self.c.Confirm(types.ConfirmOpts{
2022-01-30 11:03:08 +02:00
Title: self.c.Tr.CherryPick,
Prompt: self.c.Tr.SureCherryPick,
HandleConfirm: func() error {
isInRebase, err := self.c.Git().Status.IsInInteractiveRebase()
if err != nil {
return err
}
if isInRebase {
if err := self.c.Git().Rebase.CherryPickCommitsDuringRebase(self.getData().CherryPickedCommits); err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
})
}
return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func(gocui.Task) error {
2022-01-30 11:03:08 +02:00
self.c.LogAction(self.c.Tr.Actions.CherryPick)
err := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
2022-01-30 11:03:08 +02:00
return self.rebaseHelper.CheckMergeOrRebase(err)
})
},
})
}
func (self *CherryPickHelper) CanPaste() bool {
return self.getData().Active()
}
2022-01-30 11:03:08 +02:00
func (self *CherryPickHelper) Reset() error {
self.getData().ContextKey = ""
self.getData().CherryPickedCommits = nil
return self.rerender()
}
// you can only copy from one context at a time, because the order and position of commits matter
func (self *CherryPickHelper) resetIfNecessary(context types.Context) error {
oldContextKey := types.ContextKey(self.getData().ContextKey)
if oldContextKey != context.GetKey() {
// need to reset the cherry picking mode
self.getData().ContextKey = string(context.GetKey())
self.getData().CherryPickedCommits = make([]*models.Commit, 0)
}
return nil
}
func (self *CherryPickHelper) rerender() error {
for _, context := range []types.Context{
2023-03-23 03:53:18 +02:00
self.c.Contexts().LocalCommits,
self.c.Contexts().ReflogCommits,
self.c.Contexts().SubCommits,
2022-01-30 11:03:08 +02:00
} {
if err := self.c.PostRefreshUpdate(context); err != nil {
return err
}
}
return nil
}