From 9b22ccf93a4e6586de2896a134ccee7bef33d50b Mon Sep 17 00:00:00 2001 From: Bartlomiej Komendarczuk Date: Sat, 2 Nov 2024 17:14:45 +0100 Subject: [PATCH] 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. --- pkg/commands/git_commands/remote.go | 10 ++++++++++ pkg/gui/controllers/helpers/host_helper.go | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/commands/git_commands/remote.go b/pkg/commands/git_commands/remote.go index e2b3c6086..8371c76ea 100644 --- a/pkg/commands/git_commands/remote.go +++ b/pkg/commands/git_commands/remote.go @@ -76,6 +76,16 @@ func (self *RemoteCommands) CheckRemoteBranchExists(branchName string) bool { 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 // SEE: `man -P 'less +/--get-url +n' git-ls-remote` func (self *RemoteCommands) GetRemoteURL(remoteName string) (string, error) { diff --git a/pkg/gui/controllers/helpers/host_helper.go b/pkg/gui/controllers/helpers/host_helper.go index ab8631d36..d3ca05a6b 100644 --- a/pkg/gui/controllers/helpers/host_helper.go +++ b/pkg/gui/controllers/helpers/host_helper.go @@ -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 // from one invocation to the next. 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 { return nil, err }