From 6f65d70cfb06fb7b8c7a0106cbd553a0f2f5060b Mon Sep 17 00:00:00 2001 From: David Calavera Date: Sat, 14 Dec 2013 14:35:14 -0800 Subject: [PATCH] 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 --- github/project.go | 33 ++++++++++++++++++++------------- github/project_test.go | 17 ++++++++++++++++- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/github/project.go b/github/project.go index b9c8452e..44f7d690 100644 --- a/github/project.go +++ b/github/project.go @@ -63,31 +63,38 @@ func (p *Project) GitURL(name, owner string, isSSH bool) (url string) { owner = p.Owner } + host := rawHost(p.Host) + 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 { - url = fmt.Sprintf("git@%s:%s/%s.git", p.Host, owner, name) + url = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name) } 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 } -func useHttpProtocol() bool { - https := os.Getenv("GH_PROTOCOL") - if https == "https" { - return true - } else if https != "" { - return false +// Remove the scheme from host when the credential url is absolute. +func rawHost(host string) string { + u, err := url.Parse(host) + utils.Check(err) + + if u.IsAbs() { + return u.Host + } else { + return u.Path } +} - https, _ = git.Config("gh.protocol") - if https == "https" { - return true +func useHttpProtocol() bool { + https := os.Getenv("GH_PROTOCOL") + if https == "" { + https, _ = git.Config("gh.protocol") } - return false + return https == "https" } // TODO: remove it diff --git a/github/project_test.go b/github/project_test.go index 61f5cfc1..b0694084 100644 --- a/github/project_test.go +++ b/github/project_test.go @@ -57,7 +57,7 @@ func TestWebURL(t *testing.T) { 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") project := Project{Name: "foo", Owner: "bar", Host: "github.com"} @@ -72,6 +72,21 @@ func TestGitURL(t *testing.T) { 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) { owner, name := parseOwnerAndName("git://github.com/jingweno/gh.git") assert.Equal(t, "gh", name) -- GitLab