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

Code before:
	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 {
					err = self.Reset()
				}
				return err
			}

			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 {
					// 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 && !isInRebase {
						err = self.Reset()
					}
				}
				return 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 (refactor):
Same as above:
```go
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
}

if !isInRebase {
	return self.Reset()
}

return nil
```

I know it's more verbose to do it this way but it's less nested and I find it easier to reason about go code when I can skim over `if err != nil` blocks compared to `if err == nil`

Code after:
	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
				}
				if !isInRebase {
					return self.Reset()
				}
				return nil
			})
		},
	})
}

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