diff options
-rw-r--r-- | pkg/gui/cherry_picking.go | 92 | ||||
-rw-r--r-- | pkg/gui/commits_panel.go | 89 |
2 files changed, 92 insertions, 89 deletions
diff --git a/pkg/gui/cherry_picking.go b/pkg/gui/cherry_picking.go new file mode 100644 index 000000000..b1cbf84e5 --- /dev/null +++ b/pkg/gui/cherry_picking.go @@ -0,0 +1,92 @@ +package gui + +import ( + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands" +) + +func (gui *Gui) handleCopyCommit(g *gocui.Gui, v *gocui.View) error { + if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { + return err + } + + // get currently selected commit, add the sha to state. + commit := gui.State.Commits[gui.State.Panels.Commits.SelectedLineIdx] + + // we will un-copy it if it's already copied + for index, cherryPickedCommit := range gui.State.CherryPickedCommits { + if commit.Sha == cherryPickedCommit.Sha { + gui.State.CherryPickedCommits = append(gui.State.CherryPickedCommits[0:index], gui.State.CherryPickedCommits[index+1:]...) + return gui.Contexts.BranchCommits.Context.HandleRender() + } + } + + gui.addCommitToCherryPickedCommits(gui.State.Panels.Commits.SelectedLineIdx) + return gui.Contexts.BranchCommits.Context.HandleRender() +} + +func (gui *Gui) cherryPickedCommitShaMap() map[string]bool { + commitShaMap := map[string]bool{} + for _, commit := range gui.State.CherryPickedCommits { + commitShaMap[commit.Sha] = true + } + return commitShaMap +} + +func (gui *Gui) addCommitToCherryPickedCommits(index int) { + commitShaMap := gui.cherryPickedCommitShaMap() + commitShaMap[gui.State.Commits[index].Sha] = true + + newCommits := []*commands.Commit{} + for _, commit := range gui.State.Commits { + if commitShaMap[commit.Sha] { + // duplicating just the things we need to put in the rebase TODO list + newCommits = append(newCommits, &commands.Commit{Name: commit.Name, Sha: commit.Sha}) + } + } + + gui.State.CherryPickedCommits = newCommits +} + +func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error { + if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { + return err + } + + commitShaMap := gui.cherryPickedCommitShaMap() + + // find the last commit that is copied that's above our position + // if there are none, startIndex = 0 + startIndex := 0 + for index, commit := range gui.State.Commits[0:gui.State.Panels.Commits.SelectedLineIdx] { + if commitShaMap[commit.Sha] { + startIndex = index + } + } + + for index := startIndex; index <= gui.State.Panels.Commits.SelectedLineIdx; index++ { + gui.addCommitToCherryPickedCommits(index) + } + + return gui.Contexts.BranchCommits.Context.HandleRender() +} + +// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied +func (gui *Gui) HandlePasteCommits(g *gocui.Gui, v *gocui.View) error { + if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { + return err + } + + return gui.ask(askOpts{ + returnToView: v, + returnFocusOnClose: true, + title: gui.Tr.SLocalize("CherryPick"), + prompt: gui.Tr.SLocalize("SureCherryPick"), + handleConfirm: func() error { + return gui.WithWaitingStatus(gui.Tr.SLocalize("CherryPickingStatus"), func() error { + err := gui.GitCommand.CherryPickCommits(gui.State.CherryPickedCommits) + return gui.handleGenericMergeCommandResult(err) + }) + }, + }) +} diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 513d3f29a..6db2d71d6 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -1,7 +1,6 @@ package gui import ( - "strconv" "sync" "github.com/jesseduffield/gocui" @@ -415,94 +414,6 @@ func (gui *Gui) handleCommitRevert(g *gocui.Gui, v *gocui.View) error { return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI, scope: []int{COMMITS, BRANCHES}}) } -func (gui *Gui) handleCopyCommit(g *gocui.Gui, v *gocui.View) error { - if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { - return err - } - - // get currently selected commit, add the sha to state. - commit := gui.State.Commits[gui.State.Panels.Commits.SelectedLineIdx] - - // we will un-copy it if it's already copied - for index, cherryPickedCommit := range gui.State.CherryPickedCommits { - if commit.Sha == cherryPickedCommit.Sha { - gui.State.CherryPickedCommits = append(gui.State.CherryPickedCommits[0:index], gui.State.CherryPickedCommits[index+1:]...) - return gui.Contexts.BranchCommits.Context.HandleRender() - } - } - - gui.addCommitToCherryPickedCommits(gui.State.Panels.Commits.SelectedLineIdx) - return gui.Contexts.BranchCommits.Context.HandleRender() -} - -func (gui *Gui) cherryPickedCommitShaMap() map[string]bool { - commitShaMap := map[string]bool{} - for _, commit := range gui.State.CherryPickedCommits { - commitShaMap[commit.Sha] = true - } - return commitShaMap -} - -func (gui *Gui) addCommitToCherryPickedCommits(index int) { - commitShaMap := gui.cherryPickedCommitShaMap() - commitShaMap[gui.State.Commits[index].Sha] = true - - newCommits := []*commands.Commit{} - for _, commit := range gui.State.Commits { - if commitShaMap[commit.Sha] { - // duplicating just the things we need to put in the rebase TODO list - newCommits = append(newCommits, &commands.Commit{Name: commit.Name, Sha: commit.Sha}) - } - } - - gui.State.CherryPickedCommits = newCommits -} - -func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error { - if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { - return err - } - - commitShaMap := gui.cherryPickedCommitShaMap() - - // find the last commit that is copied that's above our position - // if there are none, startIndex = 0 - startIndex := 0 - for index, commit := range gui.State.Commits[0:gui.State.Panels.Commits.SelectedLineIdx] { - if commitShaMap[commit.Sha] { - startIndex = index - } - } - - gui.Log.Info("commit copy start index: " + strconv.Itoa(startIndex)) - - for index := startIndex; index <= gui.State.Panels.Commits.SelectedLineIdx; index++ { - gui.addCommitToCherryPickedCommits(index) - } - - return gui.Contexts.BranchCommits.Context.HandleRender() -} - -// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied -func (gui *Gui) HandlePasteCommits(g *gocui.Gui, v *gocui.View) error { - if ok, err := gui.validateNotInFilterMode(); err != nil || !ok { - return err - } - - return gui.ask(askOpts{ - returnToView: v, - returnFocusOnClose: true, - title: gui.Tr.SLocalize("CherryPick"), - prompt: gui.Tr.SLocalize("SureCherryPick"), - handleConfirm: func() error { - return gui.WithWaitingStatus(gui.Tr.SLocalize("CherryPickingStatus"), func() error { - err := gui.GitCommand.CherryPickCommits(gui.State.CherryPickedCommits) - return gui.handleGenericMergeCommandResult(err) - }) - }, - }) -} - func (gui *Gui) handleViewCommitFiles() error { commit := gui.getSelectedLocalCommit() if commit == nil { |