Introduce BaseBranchHelper around candidate resolution

GUI call sites that need a base branch all share the same logic: ask
GetBaseBranchCandidates, take candidates[0] as the config-order
tiebreak, and surface the candidate list when the user needs to
disambiguate. Extracting that into a helper keeps the upcoming prompt
and rebase wiring focused on UX concerns. Not yet routed through —
subsequent commits replace the direct GetBaseBranchCandidates calls
with ResolveBaseBranch.
This commit is contained in:
Stefan Haller 2026-05-21 21:04:49 +02:00
parent 84bd2b6fe1
commit 672a37031d
3 changed files with 40 additions and 0 deletions

View file

@ -129,6 +129,7 @@ func (gui *Gui) resetHelpersAndControllers() {
Search: searchHelper,
Worktree: worktreeHelper,
SubCommits: helpers.NewSubCommitsHelper(helperCommon, refreshHelper),
BaseBranch: helpers.NewBaseBranchHelper(helperCommon),
}
gui.CustomCommandsClient = custom_commands.NewClient(

View file

@ -0,0 +1,37 @@
package helpers
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
)
// BaseBranchHelper resolves the base branch for a given branch. The
// closeness rule (smallest ahead value) usually picks a single answer
// but can leave a tie when the branch's fork point is reachable from
// more than one main branch — in that case the helper surfaces the
// candidates so the caller can disambiguate.
type BaseBranchHelper struct {
c *HelperCommon
}
func NewBaseBranchHelper(c *HelperCommon) *BaseBranchHelper {
return &BaseBranchHelper{c: c}
}
// ResolveBaseBranch returns the base branch for the given branch, the
// full set of tied candidates (for any disambiguation UI), and whether
// the answer is genuinely ambiguous (more than one candidate tied at
// the closest position).
//
// An empty baseRef (with no error) means no configured main branch
// contains the branch — not an error condition.
func (self *BaseBranchHelper) ResolveBaseBranch(branch *models.Branch) (baseRef string, ambiguous bool, candidates []string, err error) {
mainBranches := self.c.Model().MainBranches
candidates, err = self.c.Git().Loaders.BranchLoader.GetBaseBranchCandidates(branch, mainBranches)
if err != nil {
return "", false, nil, err
}
if len(candidates) == 0 {
return "", false, nil, nil
}
return candidates[0], len(candidates) > 1, candidates, nil
}

View file

@ -53,6 +53,7 @@ type Helpers struct {
Search *SearchHelper
Worktree *WorktreeHelper
SubCommits *SubCommitsHelper
BaseBranch *BaseBranchHelper
}
func NewStubHelpers() *Helpers {
@ -90,5 +91,6 @@ func NewStubHelpers() *Helpers {
Search: &SearchHelper{},
Worktree: &WorktreeHelper{},
SubCommits: &SubCommitsHelper{},
BaseBranch: &BaseBranchHelper{},
}
}