lazygit/pkg/gui/controllers/helpers/host_helper.go
Bartlomiej Komendarczuk 9b22ccf93a Prepare remote url for specific branch
Before we always use "origin". Now I modified the operation to check the
remote for a given branch, and based on the remote name, a remote URL is
created.
2024-11-02 17:14:45 +01:00

56 lines
1.4 KiB
Go

package helpers
import (
"github.com/jesseduffield/lazygit/pkg/commands/hosting_service"
)
// this helper just wraps our hosting_service package
type IHostHelper interface {
GetPullRequestURL(from string, to string) (string, error)
GetCommitURL(commitHash string) (string, error)
}
type HostHelper struct {
c *HelperCommon
}
func NewHostHelper(
c *HelperCommon,
) *HostHelper {
return &HostHelper{
c: c,
}
}
func (self *HostHelper) GetPullRequestURL(from string, to string) (string, error) {
mgr, err := self.getHostingServiceMgr()
if err != nil {
return "", err
}
return mgr.GetPullRequestURL(from, to)
}
func (self *HostHelper) GetCommitURL(commitHash string) (string, error) {
mgr, err := self.getHostingServiceMgr()
if err != nil {
return "", err
}
return mgr.GetCommitURL(commitHash)
}
// 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) {
branch := self.c.IGetContexts.Contexts().Branches.GetSelected().Name
remoteName, err := self.c.Git().Remote.GetRemoteName(branch)
if err != nil {
return nil, err
}
remoteUrl, err := self.c.Git().Remote.GetRemoteURL(remoteName)
if err != nil {
return nil, err
}
configServices := self.c.UserConfig().Services
return hosting_service.NewHostingServiceMgr(self.c.Log, self.c.Tr, remoteUrl, configServices), nil
}