From b33ab4f466c9857fa374d35a37dcd23ecb45f973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 22 Jan 2016 01:26:55 +1100 Subject: [PATCH] Handle all sorts of weird git remote URLs - git+ssh://github.com/... - ssh://github.com:22/... - git@github.com:/... Fixes #876 --- git/url.go | 14 ++++++++++- github/remote_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/git/url.go b/git/url.go index 03432354..f3f4adf9 100644 --- a/git/url.go +++ b/git/url.go @@ -8,7 +8,7 @@ import ( var ( cachedSSHConfig SSHConfig - protocolRe = regexp.MustCompile("^[a-zA-Z_-]+://") + protocolRe = regexp.MustCompile("^[a-zA-Z_+-]+://") ) type URLParser struct { @@ -28,10 +28,22 @@ func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) { return } + if u.Scheme == "git+ssh" { + u.Scheme = "ssh" + } + if u.Scheme != "ssh" { return } + if strings.HasPrefix(u.Path, "//") { + u.Path = strings.TrimPrefix(u.Path, "/") + } + + if idx := strings.Index(u.Host, ":"); idx >= 0 { + u.Host = u.Host[0:idx] + } + sshHost := p.SSHConfig[u.Host] // ignore replacing host that fixes for limited network // https://help.github.com/articles/using-ssh-over-the-https-port diff --git a/github/remote_test.go b/github/remote_test.go index 3924ee0a..9220b21a 100644 --- a/github/remote_test.go +++ b/github/remote_test.go @@ -7,7 +7,7 @@ import ( "github.com/github/hub/fixtures" ) -func TestGithubRepo_Remotes(t *testing.T) { +func TestGithubRemote_NoPush(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() @@ -24,3 +24,57 @@ func TestGithubRepo_Remotes(t *testing.T) { assert.Equal(t, remotes[1].Name, "origin") assert.Equal(t, remotes[1].URL.Path, repo.Remote) } + +func TestGithubRemote_GitPlusSsh(t *testing.T) { + repo := fixtures.SetupTestRepo() + defer repo.TearDown() + + remoteName := "upstream" + repo.AddRemote(remoteName, "git+ssh://git@github.com/frozencemetery/python-gssapi", "") + + remotes, err := Remotes() + assert.Equal(t, nil, err) + assert.Equal(t, len(remotes), 2) + assert.Equal(t, remotes[0].Name, remoteName) + assert.Equal(t, remotes[0].URL.Scheme, "ssh") + assert.Equal(t, remotes[0].URL.Host, "github.com") + assert.Equal(t, remotes[0].URL.Path, "/frozencemetery/python-gssapi") + assert.Equal(t, remotes[1].Name, "origin") + assert.Equal(t, remotes[1].URL.Path, repo.Remote) +} + +func TestGithubRemote_SshPort(t *testing.T) { + repo := fixtures.SetupTestRepo() + defer repo.TearDown() + + remoteName := "upstream" + repo.AddRemote(remoteName, "ssh://git@github.com:22/hakatashi/dotfiles.git", "") + + remotes, err := Remotes() + assert.Equal(t, nil, err) + assert.Equal(t, len(remotes), 2) + assert.Equal(t, remotes[0].Name, remoteName) + assert.Equal(t, remotes[0].URL.Scheme, "ssh") + assert.Equal(t, remotes[0].URL.Host, "github.com") + assert.Equal(t, remotes[0].URL.Path, "/hakatashi/dotfiles.git") + assert.Equal(t, remotes[1].Name, "origin") + assert.Equal(t, remotes[1].URL.Path, repo.Remote) +} + +func TestGithubRemote_ColonSlash(t *testing.T) { + repo := fixtures.SetupTestRepo() + defer repo.TearDown() + + remoteName := "upstream" + repo.AddRemote(remoteName, "git@github.com:/fatso83/my-project.git", "") + + remotes, err := Remotes() + assert.Equal(t, nil, err) + assert.Equal(t, len(remotes), 2) + assert.Equal(t, remotes[0].Name, remoteName) + assert.Equal(t, remotes[0].URL.Scheme, "ssh") + assert.Equal(t, remotes[0].URL.Host, "github.com") + assert.Equal(t, remotes[0].URL.Path, "/fatso83/my-project.git") + assert.Equal(t, remotes[1].Name, "origin") + assert.Equal(t, remotes[1].URL.Path, repo.Remote) +} -- GitLab