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

Code before:
func (self *BasicCommitsController) openInBrowser(commit *models.Commit) error {
	url, err := self.c.Helpers().Host.GetCommitURL(commit.Hash)
	if err != nil {
		return err
	}

	self.c.LogAction(self.c.Tr.Actions.OpenCommitInBrowser)
	if err := self.c.OS().OpenLink(url); err != nil {
		return err
	}

	return nil
}

func (self *BasicCommitsController) newBranch(commit *models.Commit) error {
	return self.c.Helpers().Refs.NewBranch(commit.RefName(), commit.Description(), "")
}

func (self *BasicCommitsController) createResetMenu(commit *models.Commit) error {
	return self.c.Helpers().Refs.CreateGitResetMenu(commit.Hash)
}

func (self *BasicCommitsController) checkout(commit *models.Commit) error {
	commitBranches := lo.Filter(self.c.Model().Branches, func(branch *models.Branch, _ int) bool {
		return commit.Hash == branch.CommitHash && branch.Name != self.c.Model().CheckedOutBranch
	})

	if len(commitBranches) > 0 {
		return self.c.Helpers().Refs.CreateCheckoutMenu(commit.Hash, commitBranches)
	}

	self.c.Confirm(types.ConfirmOpts{
		Title:  self.c.Tr.CheckoutCommit,
		Prompt: self.c.Tr.SureCheckoutThisCommit,
		HandleConfirm: func() error {
			self.c.LogAction(self.c.Tr.Actions.CheckoutCommit)
			return self.c.Helpers().Refs.CheckoutRef(commit.Hash, types.CheckoutRefOptions{})
		},
	})
	return nil
}

func (self *BasicCommitsController) copyRange(*models.Commit) error {
	return self.c.Helpers().CherryPick.CopyRange(self.context.GetCommits(), self.context)
}

func (self *BasicCommitsController) canCopyCommits(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
	for _, commit := range selectedCommits {
		if commit.Hash == "" {
			return &types.DisabledReason{Text: self.c.Tr.CannotCherryPickNonCommit, ShowErrorInPanel: true}
		}

Review comment (refactor):
Forgot to mention this last time: I'd move this code to the `CreateCheckoutMenu` method as well, there's no reason to do this here and pass it in. The general rule of thumb is to have as little logic as possible in controllers.

Code after:
	url, err := self.c.Helpers().Host.GetCommitURL(commit.Hash)
	if err != nil {
		return err
	}

	self.c.LogAction(self.c.Tr.Actions.OpenCommitInBrowser)
	if err := self.c.OS().OpenLink(url); err != nil {
		return err
	}

	return nil
}

func (self *BasicCommitsController) newBranch(commit *models.Commit) error {
	return self.c.Helpers().Refs.NewBranch(commit.RefName(), commit.Description(), "")
}

func (self *BasicCommitsController) createResetMenu(commit *models.Commit) error {
	return self.c.Helpers().Refs.CreateGitResetMenu(commit.Hash)
}

func (self *BasicCommitsController) checkout(commit *models.Commit) error {
	return self.c.Helpers().Refs.CreateCheckoutMenu(commit)
}

func (self *BasicCommitsController) copyRange(*models.Commit) error {
	return self.c.Helpers().CherryPick.CopyRange(self.context.GetCommits(), self.context)
}

func (self *BasicCommitsController) canCopyCommits(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
	for _, commit := range selectedCommits {
		if commit.Hash == "" {
			return &types.DisabledReason{Text: self.c.Tr.CannotCherryPickNonCommit, ShowErrorInPanel: true}
		}

		if commit.IsMerge() {
			return &types.DisabledReason{Text: self.c.Tr.CannotCherryPickMergeCommit, ShowErrorInPanel: true}
		}
	}

	return nil
}

func (self *BasicCommitsController) handleOldCherryPickKey() error {
	msg := utils.ResolvePlaceholderString(self.c.Tr.OldCherryPickKeyWarning,
		map[string]string{
			"copy":  keybindings.Label(self.c.UserConfig().Keybinding.Commits.CherryPickCopy),
			"paste": keybindings.Label(self.c.UserConfig().Keybinding.Commits.PasteCommits),
		})

	return errors.New(msg)