提交 6f65d70c 编写于 作者: D David Calavera

Remove the scheme from the project host before creating the project's git url.

When the host is absolute, like https://github.enterprise.com, the creation fail because the git url is malformed, like:

git@https://github.enterprise.com:foo/bar
上级 0c0dbcaa
...@@ -63,31 +63,38 @@ func (p *Project) GitURL(name, owner string, isSSH bool) (url string) { ...@@ -63,31 +63,38 @@ func (p *Project) GitURL(name, owner string, isSSH bool) (url string) {
owner = p.Owner owner = p.Owner
} }
host := rawHost(p.Host)
if useHttpProtocol() { if useHttpProtocol() {
url = fmt.Sprintf("https://%s/%s/%s.git", p.Host, owner, name) url = fmt.Sprintf("https://%s/%s/%s.git", host, owner, name)
} else if isSSH { } else if isSSH {
url = fmt.Sprintf("git@%s:%s/%s.git", p.Host, owner, name) url = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name)
} else { } else {
url = fmt.Sprintf("git://%s/%s/%s.git", p.Host, owner, name) url = fmt.Sprintf("git://%s/%s/%s.git", host, owner, name)
} }
return url return url
} }
func useHttpProtocol() bool { // Remove the scheme from host when the credential url is absolute.
https := os.Getenv("GH_PROTOCOL") func rawHost(host string) string {
if https == "https" { u, err := url.Parse(host)
return true utils.Check(err)
} else if https != "" {
return false if u.IsAbs() {
return u.Host
} else {
return u.Path
} }
}
https, _ = git.Config("gh.protocol") func useHttpProtocol() bool {
if https == "https" { https := os.Getenv("GH_PROTOCOL")
return true if https == "" {
https, _ = git.Config("gh.protocol")
} }
return false return https == "https"
} }
// TODO: remove it // TODO: remove it
......
...@@ -57,7 +57,7 @@ func TestWebURL(t *testing.T) { ...@@ -57,7 +57,7 @@ func TestWebURL(t *testing.T) {
assert.Equal(t, "https://github.com/defunkt/hub/wiki/_pages", url) assert.Equal(t, "https://github.com/defunkt/hub/wiki/_pages", url)
} }
func TestGitURL(t *testing.T) { func TestGitURLGitHub(t *testing.T) {
os.Setenv("GH_PROTOCOL", "https") os.Setenv("GH_PROTOCOL", "https")
project := Project{Name: "foo", Owner: "bar", Host: "github.com"} project := Project{Name: "foo", Owner: "bar", Host: "github.com"}
...@@ -72,6 +72,21 @@ func TestGitURL(t *testing.T) { ...@@ -72,6 +72,21 @@ func TestGitURL(t *testing.T) {
assert.Equal(t, "git@github.com:jingweno/gh.git", url) assert.Equal(t, "git@github.com:jingweno/gh.git", url)
} }
func TestGitURLEnterprise(t *testing.T) {
project := Project{Name: "foo", Owner: "bar", Host: "https://github.corporate.com"}
os.Setenv("GH_PROTOCOL", "https")
url := project.GitURL("gh", "jingweno", false)
assert.Equal(t, "https://github.corporate.com/jingweno/gh.git", url)
os.Setenv("GH_PROTOCOL", "git")
url = project.GitURL("gh", "jingweno", false)
assert.Equal(t, "git://github.corporate.com/jingweno/gh.git", url)
url = project.GitURL("gh", "jingweno", true)
assert.Equal(t, "git@github.corporate.com:jingweno/gh.git", url)
}
func TestParseOwnerAndName(t *testing.T) { func TestParseOwnerAndName(t *testing.T) {
owner, name := parseOwnerAndName("git://github.com/jingweno/gh.git") owner, name := parseOwnerAndName("git://github.com/jingweno/gh.git")
assert.Equal(t, "gh", name) assert.Equal(t, "gh", name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册