提交 b33ab4f4 编写于 作者: M Mislav Marohnić

Handle all sorts of weird git remote URLs

- git+ssh://github.com/...
- ssh://github.com:22/...
- git@github.com:/...

Fixes #876
上级 3e69b8ec
......@@ -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
......
......@@ -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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册