Repository: jesseduffield/lazygit
File: pkg/gui/controllers/helpers/cherry_pick_helper.go (Go)

Code before:
	} else {
		for index := startIdx; index <= endIdx; index++ {
			commit := commitsList[index]
			self.getData().Add(commit, commitsList)
		}
	}

	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{
		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
				}
				_ = self.Reset()
				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 {
				self.c.LogAction(self.c.Tr.Actions.CherryPick)
				err := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
				_ = self.Reset()
				return self.rebaseHelper.CheckMergeOrRebase(err)
			})
		},
	})
}

func (self *CherryPickHelper) CanPaste() bool {
	return self.getData().Active()
}

func (self *CherryPickHelper) Reset() error {
	self.getData().ContextKey = ""
	self.getData().CherryPickedCommits = nil

	return self.rerender()
}

Review comment (suggestion):
The lack of error handling makes me slightly nervous. If a function can return an error, it should be handled; if the function never returns an error, or it's not important to handle, then it shouldn't have a return type of "error".

One way to solve this could be to remove the Reset from here, turn the two `return`s below into `err = ...` (the second would have to be in an `else` block then), and at the very end of the function do
```
if err == nil {
  err = Reset()
}
return err
```

Code after:
	} else {
		for index := startIdx; index <= endIdx; index++ {
			commit := commitsList[index]
			self.getData().Add(commit, commitsList)
		}
	}

	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{
		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
				}
				err = self.c.Refresh(types.RefreshOptions{
					Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
				})
				if err != nil {
					return err
				}

				return self.Reset()
			}

			return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func(gocui.Task) error {
				self.c.LogAction(self.c.Tr.Actions.CherryPick)
				err := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
				err = self.rebaseHelper.CheckMergeOrRebase(err)
				if err != nil {
					return err
				}

				// If we're in an interactive rebase at this point, it must
				// be because there were conflicts. Don't clear the copied
				// commits in this case, since we might want to abort and
				// try pasting them again.
				isInRebase, err = self.c.Git().Status.IsInInteractiveRebase()
				if err != nil {
					return err
				}