From 021241c5f8efed0219e9f41d0c44e2352a1f32e5 Mon Sep 17 00:00:00 2001 From: sergiochan Date: Tue, 10 Mar 2026 18:20:47 -0700 Subject: [PATCH] fix(host): prefer selected remote when building commit URLs --- pkg/gui/controllers/helpers/host_helper.go | 24 ++++++++++- .../controllers/helpers/host_helper_test.go | 42 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 pkg/gui/controllers/helpers/host_helper_test.go diff --git a/pkg/gui/controllers/helpers/host_helper.go b/pkg/gui/controllers/helpers/host_helper.go index 42115e86f..3e58e120c 100644 --- a/pkg/gui/controllers/helpers/host_helper.go +++ b/pkg/gui/controllers/helpers/host_helper.go @@ -2,6 +2,7 @@ package helpers import ( "github.com/jesseduffield/lazygit/pkg/commands/hosting_service" + "github.com/jesseduffield/lazygit/pkg/commands/models" ) // this helper just wraps our hosting_service package @@ -37,10 +38,31 @@ 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") + remoteName := getPreferredRemoteName( + self.c.Contexts().RemoteBranches.GetSelected(), + self.c.Contexts().Remotes.GetSelected(), + ) + + remoteUrl, err := self.c.Git().Remote.GetRemoteURL(remoteName) + if err != nil && remoteName != "origin" { + remoteUrl, err = self.c.Git().Remote.GetRemoteURL("origin") + } if err != nil { return nil, err } + configServices := self.c.UserConfig().Services return hosting_service.NewHostingServiceMgr(self.c.Log, self.c.Tr, remoteUrl, configServices), nil } + +func getPreferredRemoteName(selectedRemoteBranch *models.RemoteBranch, selectedRemote *models.Remote) string { + if selectedRemoteBranch != nil && selectedRemoteBranch.RemoteName != "" { + return selectedRemoteBranch.RemoteName + } + + if selectedRemote != nil && selectedRemote.Name != "" { + return selectedRemote.Name + } + + return "origin" +} diff --git a/pkg/gui/controllers/helpers/host_helper_test.go b/pkg/gui/controllers/helpers/host_helper_test.go new file mode 100644 index 000000000..e7ffc893e --- /dev/null +++ b/pkg/gui/controllers/helpers/host_helper_test.go @@ -0,0 +1,42 @@ +package helpers + +import ( + "testing" + + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/stretchr/testify/assert" +) + +func TestGetPreferredRemoteName(t *testing.T) { + tests := []struct { + name string + selectedRemoteBranch *models.RemoteBranch + selectedRemote *models.Remote + expected string + }{ + { + name: "uses selected remote branch when available", + selectedRemoteBranch: &models.RemoteBranch{RemoteName: "upstream"}, + selectedRemote: &models.Remote{Name: "origin"}, + expected: "upstream", + }, + { + name: "falls back to selected remote", + selectedRemoteBranch: nil, + selectedRemote: &models.Remote{Name: "mirror"}, + expected: "mirror", + }, + { + name: "defaults to origin", + selectedRemoteBranch: nil, + selectedRemote: nil, + expected: "origin", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.expected, getPreferredRemoteName(test.selectedRemoteBranch, test.selectedRemote)) + }) + } +}