diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index a7eb35919..c9906a922 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -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( diff --git a/pkg/gui/controllers/helpers/base_branch_helper.go b/pkg/gui/controllers/helpers/base_branch_helper.go new file mode 100644 index 000000000..e921ce93d --- /dev/null +++ b/pkg/gui/controllers/helpers/base_branch_helper.go @@ -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 +} diff --git a/pkg/gui/controllers/helpers/helpers.go b/pkg/gui/controllers/helpers/helpers.go index 4c9c79f3d..a72c89263 100644 --- a/pkg/gui/controllers/helpers/helpers.go +++ b/pkg/gui/controllers/helpers/helpers.go @@ -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{}, } }