Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support refs which start with URL Authority in ghq get #119

Merged
merged 1 commit into from Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions url.go
Expand Up @@ -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) {
Expand All @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion 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) {
Expand Down Expand Up @@ -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"))
Expand Down