1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/modes/cherrypicking/cherry_picking.go
Jesse Duffield e33fe37a99 Standardise on using lo for slice functions
We've been sometimes using lo and sometimes using my slices package, and we need to pick one
for consistency. Lo is more extensive and better maintained so we're going with that.

My slices package was a superset of go's own slices package so in some places I've just used
the official one (the methods were just wrappers anyway).

I've also moved the remaining methods into the utils package.
2023-07-30 18:51:23 +10:00

57 lines
1.6 KiB
Go

package cherrypicking
import (
"github.com/jesseduffield/generics/set"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/samber/lo"
)
type CherryPicking struct {
CherryPickedCommits []*models.Commit
// we only allow cherry picking from one context at a time, so you can't copy a commit from the local commits context and then also copy a commit in the reflog context
ContextKey string
}
func New() *CherryPicking {
return &CherryPicking{
CherryPickedCommits: make([]*models.Commit, 0),
ContextKey: "",
}
}
func (self *CherryPicking) Active() bool {
return len(self.CherryPickedCommits) > 0
}
func (self *CherryPicking) SelectedShaSet() *set.Set[string] {
shas := lo.Map(self.CherryPickedCommits, func(commit *models.Commit, _ int) string {
return commit.Sha
})
return set.NewFromSlice(shas)
}
func (self *CherryPicking) Add(selectedCommit *models.Commit, commitsList []*models.Commit) {
commitSet := self.SelectedShaSet()
commitSet.Add(selectedCommit.Sha)
self.update(commitSet, commitsList)
}
func (self *CherryPicking) Remove(selectedCommit *models.Commit, commitsList []*models.Commit) {
commitSet := self.SelectedShaSet()
commitSet.Remove(selectedCommit.Sha)
self.update(commitSet, commitsList)
}
func (self *CherryPicking) update(selectedShaSet *set.Set[string], commitsList []*models.Commit) {
cherryPickedCommits := lo.Filter(commitsList, func(commit *models.Commit, _ int) bool {
return selectedShaSet.Includes(commit.Sha)
})
self.CherryPickedCommits = lo.Map(cherryPickedCommits, func(commit *models.Commit, _ int) *models.Commit {
return &models.Commit{Name: commit.Name, Sha: commit.Sha}
})
}