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

Code before:
}

// PromptToContinueRebase asks the user if they want to continue the rebase/merge that's in progress
func (self *MergeAndRebaseHelper) PromptToContinueRebase() error {
	return self.c.Confirm(types.ConfirmOpts{
		Title:  self.c.Tr.Continue,
		Prompt: self.c.Tr.ConflictsResolved,
		HandleConfirm: func() error {
			return self.genericMergeCommand(REBASE_OPTION_CONTINUE)
		},
	})
}

func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error {
	checkedOutBranch := self.refsHelper.GetCheckedOutRef().Name
	if ref == checkedOutBranch {
		return self.c.ErrorMsg(self.c.Tr.CantRebaseOntoSelf)
	}
	menuItems := []*types.MenuItem{
		{
			Label: self.c.Tr.SimpleRebase,
			Key:   's',
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RebaseBranch)
				baseCommit := self.c.Modes().MarkedBaseCommit.GetSha()
				err := lo.Ternary(baseCommit != "",
					self.c.Git().Rebase.RebaseBranchFromBaseCommit(ref, baseCommit),
					self.c.Git().Rebase.RebaseBranch(ref))
				err = self.CheckMergeOrRebase(err)
				if err == nil {
					self.c.Modes().MarkedBaseCommit.Reset()
				}
				return err
			},
		},
		{
			Label:   self.c.Tr.InteractiveRebase,
			Key:     'i',
			Tooltip: self.c.Tr.InteractiveRebaseTooltip,
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RebaseBranch)
				baseCommit := self.c.Modes().MarkedBaseCommit.GetSha()
				err := lo.Ternary(baseCommit != "",
					self.c.Git().Rebase.EditRebaseFromBaseCommit(ref, baseCommit),
					self.c.Git().Rebase.EditRebase(ref))
				if err = self.CheckMergeOrRebase(err); err != nil {
					return err
				}
				self.c.Modes().MarkedBaseCommit.Reset()
				return self.c.PushContext(self.c.Contexts().LocalCommits)
			},

Review comment (suggestion):
I'm 99% sure this isn't working the way you expect: lo.Ternary evaluates both the if and else expressions before picking which one to return (just cos that's how the language works and lo has no way to get around that). So this will unconditionally call both rebase functions.

I believe lo has lo.TernaryFunc which you can use to only evaluate the expression if it's chosen, but at that point I wonder if it's just easier to use an if/else statement.

lo.Ternary should really only be used when it's dealing expressions that evaluate without side effects and without much time/memory footprint.

In this case I'd use an opts object in `RebaseBranch` and let it internally deal with the baseCommit arg.

Code after:
}

// PromptToContinueRebase asks the user if they want to continue the rebase/merge that's in progress
func (self *MergeAndRebaseHelper) PromptToContinueRebase() error {
	return self.c.Confirm(types.ConfirmOpts{
		Title:  self.c.Tr.Continue,
		Prompt: self.c.Tr.ConflictsResolved,
		HandleConfirm: func() error {
			return self.genericMergeCommand(REBASE_OPTION_CONTINUE)
		},
	})
}

func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error {
	checkedOutBranch := self.refsHelper.GetCheckedOutRef().Name
	if ref == checkedOutBranch {
		return self.c.ErrorMsg(self.c.Tr.CantRebaseOntoSelf)
	}
	menuItems := []*types.MenuItem{
		{
			Label: self.c.Tr.SimpleRebase,
			Key:   's',
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RebaseBranch)
				baseCommit := self.c.Modes().MarkedBaseCommit.GetSha()
				var err error
				if baseCommit != "" {
					err = self.c.Git().Rebase.RebaseBranchFromBaseCommit(ref, baseCommit)
				} else {
					err = self.c.Git().Rebase.RebaseBranch(ref)
				}
				err = self.CheckMergeOrRebase(err)
				if err == nil {
					self.c.Modes().MarkedBaseCommit.Reset()
				}
				return err
			},
		},
		{
			Label:   self.c.Tr.InteractiveRebase,
			Key:     'i',
			Tooltip: self.c.Tr.InteractiveRebaseTooltip,
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RebaseBranch)
				baseCommit := self.c.Modes().MarkedBaseCommit.GetSha()
				var err error
				if baseCommit != "" {
					err = self.c.Git().Rebase.EditRebaseFromBaseCommit(ref, baseCommit)
				} else {
					err = self.c.Git().Rebase.EditRebase(ref)
				}