diff --git a/url.go b/url.go index 4d87fa64..46e9ae88 100644 --- a/url.go +++ b/url.go @@ -12,8 +12,11 @@ import ( // Convert SCP-like URL to SSH URL(e.g. [user@]host.xz:path/to/repo.git/) // ref. http://git-scm.com/docs/git-fetch#_git_urls // (golang hasn't supported Perl-like negative look-behind match) -var hasSchemePattern = regexp.MustCompile("^[^:]+://") -var scpLikeUrlPattern = regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$") +var ( + hasSchemePattern = regexp.MustCompile("^[^:]+://") + scpLikeUrlPattern = regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$") + looksLikeAuthorityPattern = regexp.MustCompile(`[A-Za-z0-9]\.[A-Za-z]+(?::\d{1,5})?`) +) func NewURL(ref string) (*url.URL, error) { if !hasSchemePattern.MatchString(ref) && scpLikeUrlPattern.MatchString(ref) { @@ -36,6 +39,12 @@ func NewURL(ref string) (*url.URL, error) { if err != nil { return url, err } + } else if url.Host == "" { + // If ref is like "github.com/motemen/ghq" consider it as "https://github.com/motemen/ghq" + paths := strings.Split(ref, "/") + if looksLikeAuthorityPattern.MatchString(paths[0]) { + return url.Parse("https://" + ref) + } } url.Scheme = "https" url.Host = "github.com" diff --git a/url_test.go b/url_test.go index 37317d80..4d61ebdf 100644 --- a/url_test.go +++ b/url_test.go @@ -1,10 +1,11 @@ package main import ( - . "github.com/onsi/gomega" "net/url" "os" "testing" + + . "github.com/onsi/gomega" ) func TestNewURL(t *testing.T) { @@ -37,6 +38,16 @@ func TestNewURL(t *testing.T) { Expect(differentNameRepository.Host).To(Equal("github.com")) Expect(err).To(BeNil()) + withAuthorityRepository, err := NewURL("github.com/motemen/gore") + Expect(withAuthorityRepository.String()).To(Equal("https://github.com/motemen/gore")) + Expect(withAuthorityRepository.Host).To(Equal("github.com")) + Expect(err).To(BeNil()) + + withAuthorityRepository2, err := NewURL("golang.org/x/crypto") + Expect(withAuthorityRepository2.String()).To(Equal("https://golang.org/x/crypto")) + Expect(withAuthorityRepository2.Host).To(Equal("golang.org")) + Expect(err).To(BeNil()) + os.Setenv("GITHUB_USER", "ghq-test") sameNameRepository, err := NewURL("same-name-ghq") Expect(sameNameRepository.String()).To(Equal("https://github.com/ghq-test/same-name-ghq"))