From 524bbff1acebd72f1b4a9248f0d457a49c5bc4b6 Mon Sep 17 00:00:00 2001 From: Rizky Mirzaviandy Priambodo <142987522+Xavrir@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:16:48 +0700 Subject: [PATCH] fix: use selected remote when opening commit in browser --- .../controllers/basic_commits_controller.go | 23 +++++++++++++++++-- pkg/gui/controllers/helpers/host_helper.go | 12 ++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pkg/gui/controllers/basic_commits_controller.go b/pkg/gui/controllers/basic_commits_controller.go index be856a9e9..2e02e4f57 100644 --- a/pkg/gui/controllers/basic_commits_controller.go +++ b/pkg/gui/controllers/basic_commits_controller.go @@ -6,6 +6,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -235,7 +236,7 @@ func (self *BasicCommitsController) copyCommitHashToClipboard(commit *models.Com } func (self *BasicCommitsController) copyCommitURLToClipboard(commit *models.Commit) error { - url, err := self.c.Helpers().Host.GetCommitURL(commit.Hash()) + url, err := self.getCommitURL(commit) if err != nil { return err } @@ -334,7 +335,7 @@ func (self *BasicCommitsController) copyCommitTagsToClipboard(commit *models.Com } func (self *BasicCommitsController) openInBrowser(commit *models.Commit) error { - url, err := self.c.Helpers().Host.GetCommitURL(commit.Hash()) + url, err := self.getCommitURL(commit) if err != nil { return err } @@ -425,3 +426,21 @@ func (self *BasicCommitsController) selectCommitsOfCurrentBranch() error { self.context.HandleFocus(types.OnFocusOpts{}) return nil } + +func (self *BasicCommitsController) getCommitURL(commit *models.Commit) (string, error) { + remoteName := self.remoteNameFromContext() + return self.c.Helpers().Host.GetCommitURLForRemote(commit.Hash(), remoteName) +} + +// When viewing commits under a specific remote branch, return that remote's +// name so we build the URL from the right remote. Falls back to "origin". +func (self *BasicCommitsController) remoteNameFromContext() string { + if subCommitsCtx, ok := self.context.(*context.SubCommitsContext); ok { + if ref := subCommitsCtx.GetRef(); ref != nil { + if remoteBranch, ok := ref.(*models.RemoteBranch); ok { + return remoteBranch.RemoteName + } + } + } + return "origin" +} diff --git a/pkg/gui/controllers/helpers/host_helper.go b/pkg/gui/controllers/helpers/host_helper.go index 42115e86f..c2bcffbe7 100644 --- a/pkg/gui/controllers/helpers/host_helper.go +++ b/pkg/gui/controllers/helpers/host_helper.go @@ -27,7 +27,11 @@ func (self *HostHelper) GetPullRequestURL(from string, to string) (string, error } func (self *HostHelper) GetCommitURL(commitHash string) (string, error) { - mgr, err := self.getHostingServiceMgr() + return self.GetCommitURLForRemote(commitHash, "origin") +} + +func (self *HostHelper) GetCommitURLForRemote(commitHash string, remoteName string) (string, error) { + mgr, err := self.getHostingServiceMgrForRemote(remoteName) if err != nil { return "", err } @@ -37,7 +41,11 @@ func (self *HostHelper) GetCommitURL(commitHash string) (string, error) { // getting this on every request rather than storing it in state in case our remoteURL changes // from one invocation to the next. func (self *HostHelper) getHostingServiceMgr() (*hosting_service.HostingServiceMgr, error) { - remoteUrl, err := self.c.Git().Remote.GetRemoteURL("origin") + return self.getHostingServiceMgrForRemote("origin") +} + +func (self *HostHelper) getHostingServiceMgrForRemote(remoteName string) (*hosting_service.HostingServiceMgr, error) { + remoteUrl, err := self.c.Git().Remote.GetRemoteURL(remoteName) if err != nil { return nil, err }