Skip to content

Commit

Permalink
Obtain remote URL by calling "ls-remote --get-url" instead of using g…
Browse files Browse the repository at this point in the history
…it config

This has the advantage that it still works when the user has configured aliases
using the insteadOf feature [1].

[1] https://git-scm.com/docs/git-config/2.42.0#Documentation/git-config.txt-urlltbasegtinsteadOf)
  • Loading branch information
stefanhaller committed Dec 21, 2023
1 parent 66b608b commit 499957c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions pkg/commands/git_commands/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git_commands

import (
"fmt"
"strings"

"github.com/jesseduffield/gocui"
)
Expand Down Expand Up @@ -74,3 +75,14 @@ func (self *RemoteCommands) CheckRemoteBranchExists(branchName string) bool {

return err == nil
}

// 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) {
cmdArgs := NewGitCmd("ls-remote").
Arg("--get-url", remoteName).
ToArgv()

url, err := self.cmd.New(cmdArgs).RunWithOutput()
return strings.TrimSpace(url), err
}
24 changes: 17 additions & 7 deletions pkg/gui/controllers/helpers/host_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ func NewHostHelper(
}

func (self *HostHelper) GetPullRequestURL(from string, to string) (string, error) {
return self.getHostingServiceMgr().GetPullRequestURL(from, to)
mgr, err := self.getHostingServiceMgr()
if err != nil {
return "", err
}
return mgr.GetPullRequestURL(from, to)
}

func (self *HostHelper) GetCommitURL(commitSha string) (string, error) {
return self.getHostingServiceMgr().GetCommitURL(commitSha)
mgr, err := self.getHostingServiceMgr()
if err != nil {
return "", err
}
return mgr.GetCommitURL(commitSha)
}

// getting this on every request rather than storing it in state in case our remoteURL changes
// from one invocation to the next. Note however that we're currently caching config
// results so we might want to invalidate the cache here if it becomes a problem.
func (self *HostHelper) getHostingServiceMgr() *hosting_service.HostingServiceMgr {
remoteUrl := self.c.Git().Config.GetRemoteURL()
// from one invocation to the next.
func (self *HostHelper) getHostingServiceMgr() (*hosting_service.HostingServiceMgr, error) {
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)
return hosting_service.NewHostingServiceMgr(self.c.Log, self.c.Tr, remoteUrl, configServices), nil
}

0 comments on commit 499957c

Please sign in to comment.