2022-02-06 06:54:26 +02:00
|
|
|
package helpers
|
2022-01-30 11:03:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CherryPickHelper struct {
|
2022-02-06 06:54:26 +02:00
|
|
|
c *types.HelperCommon
|
2022-01-30 11:03:08 +02:00
|
|
|
|
|
|
|
git *commands.GitCommand
|
|
|
|
|
2022-01-31 13:20:28 +02:00
|
|
|
contexts *context.ContextTree
|
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(
|
2022-02-06 06:54:26 +02:00
|
|
|
c *types.HelperCommon,
|
2022-01-30 11:03:08 +02:00
|
|
|
git *commands.GitCommand,
|
2022-01-31 13:20:28 +02:00
|
|
|
contexts *context.ContextTree,
|
2022-02-06 06:54:26 +02:00
|
|
|
rebaseHelper *MergeAndRebaseHelper,
|
2022-01-30 11:03:08 +02:00
|
|
|
) *CherryPickHelper {
|
|
|
|
return &CherryPickHelper{
|
|
|
|
c: c,
|
|
|
|
git: git,
|
2022-01-31 13:20:28 +02:00
|
|
|
contexts: contexts,
|
2022-01-30 11:03:08 +02:00
|
|
|
rebaseHelper: rebaseHelper,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:51:40 +02:00
|
|
|
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
|
2023-03-23 02:51:40 +02:00
|
|
|
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(selectedIndex int, commitsList []*models.Commit, context types.Context) error {
|
|
|
|
if err := self.resetIfNecessary(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:51:40 +02:00
|
|
|
commitSet := self.getData().SelectedShaSet()
|
2022-01-30 11:03:08 +02:00
|
|
|
|
|
|
|
// find the last commit that is copied that's above our position
|
|
|
|
// if there are none, startIndex = 0
|
|
|
|
startIndex := 0
|
|
|
|
for index, commit := range commitsList[0:selectedIndex] {
|
2022-03-19 03:26:30 +02:00
|
|
|
if commitSet.Includes(commit.Sha) {
|
2022-01-30 11:03:08 +02:00
|
|
|
startIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for index := startIndex; index <= selectedIndex; index++ {
|
|
|
|
commit := commitsList[index]
|
2023-03-23 02:51:40 +02:00
|
|
|
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 {
|
2022-03-30 08:48:29 +02:00
|
|
|
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 {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.CherryPick)
|
|
|
|
err := self.git.Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
|
|
|
|
return self.rebaseHelper.CheckMergeOrRebase(err)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2022-02-13 08:01:53 +02:00
|
|
|
self.contexts.LocalCommits,
|
2022-01-31 13:20:28 +02:00
|
|
|
self.contexts.ReflogCommits,
|
|
|
|
self.contexts.SubCommits,
|
2022-01-30 11:03:08 +02:00
|
|
|
} {
|
|
|
|
if err := self.c.PostRefreshUpdate(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|