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.
This commit is contained in:
Bartlomiej Komendarczuk 2024-11-02 17:14:45 +01:00
parent 095eb130e9
commit 9b22ccf93a
2 changed files with 16 additions and 1 deletions

View file

@ -76,6 +76,16 @@ func (self *RemoteCommands) CheckRemoteBranchExists(branchName string) bool {
return err == nil return err == nil
} }
// Returns remote name for branch
func (self *RemoteCommands) GetRemoteName(branchName string) (string, error) {
cmdArgs := NewGitCmd("config").
Arg(fmt.Sprintf("branch.%s.remote", branchName)).
ToArgv()
remote, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
return strings.TrimSpace(remote), err
}
// Resolve what might be a aliased URL into a full URL // Resolve what might be a aliased URL into a full URL
// SEE: `man -P 'less +/--get-url +n' git-ls-remote` // SEE: `man -P 'less +/--get-url +n' git-ls-remote`
func (self *RemoteCommands) GetRemoteURL(remoteName string) (string, error) { func (self *RemoteCommands) GetRemoteURL(remoteName string) (string, error) {

View file

@ -42,7 +42,12 @@ func (self *HostHelper) GetCommitURL(commitHash string) (string, error) {
// getting this on every request rather than storing it in state in case our remoteURL changes // getting this on every request rather than storing it in state in case our remoteURL changes
// from one invocation to the next. // from one invocation to the next.
func (self *HostHelper) getHostingServiceMgr() (*hosting_service.HostingServiceMgr, error) { func (self *HostHelper) getHostingServiceMgr() (*hosting_service.HostingServiceMgr, error) {
remoteUrl, err := self.c.Git().Remote.GetRemoteURL("origin") 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 { if err != nil {
return nil, err return nil, err
} }