Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/gui/controllers/helpers/host_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
42 changes: 42 additions & 0 deletions pkg/gui/controllers/helpers/host_helper_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}