From f2b49791c206b8efb3e807d4a22d3c16afcf26d9 Mon Sep 17 00:00:00 2001 From: Songmu Date: Sun, 28 Apr 2019 16:07:05 +0900 Subject: [PATCH 1/2] fix NewURL --- url.go | 28 +++++++++++++++------------- url_test.go | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/url.go b/url.go index 9d53aa6e..479998db 100644 --- a/url.go +++ b/url.go @@ -14,18 +14,26 @@ import ( // (golang hasn't supported Perl-like negative look-behind match) var ( hasSchemePattern = regexp.MustCompile("^[^:]+://") - scpLikeUrlPattern = 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) { - matched := scpLikeUrlPattern.FindStringSubmatch(ref) - user := matched[1] - host := matched[2] - path := matched[3] + if !hasSchemePattern.MatchString(ref) { + if scpLikeUrlPattern.MatchString(ref) { + matched := scpLikeUrlPattern.FindStringSubmatch(ref) + user := matched[1] + host := matched[2] + path := matched[3] - ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path) + ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path) + } else { + // If ref is like "github.com/motemen/ghq" convert to "https://github.com/motemen/ghq" + paths := strings.Split(ref, "/") + if len(paths) > 1 && looksLikeAuthorityPattern.MatchString(paths[0]) { + ref = "https://" + ref + } + } } url, err := url.Parse(ref) @@ -39,12 +47,6 @@ 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 4d61ebdf..17e362d1 100644 --- a/url_test.go +++ b/url_test.go @@ -24,7 +24,7 @@ func TestNewURL(t *testing.T) { Expect(err).To(BeNil()) scpUrlWithRoot, err := NewURL("git@github.com:/motemen/pusheen-explorer.git") - Expect(scpUrlWithRoot.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer.git")) + Expect(scpUrlWithRoot.String()).To(Equal("ssh://git@github.com//motemen/pusheen-explorer.git")) Expect(scpUrlWithRoot.Host).To(Equal("github.com")) Expect(err).To(BeNil()) From 9819635bbda428cfad2f7ff0d45a78b1b794c026 Mon Sep 17 00:00:00 2001 From: Songmu Date: Sun, 28 Apr 2019 16:15:12 +0900 Subject: [PATCH 2/2] fix import --- commands_test.go | 6 +----- local_repository_test.go | 7 ++----- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/commands_test.go b/commands_test.go index a3fb0022..39db1d68 100644 --- a/commands_test.go +++ b/commands_test.go @@ -6,14 +6,10 @@ import ( "net/url" "os" "path/filepath" - - "github.com/urfave/cli" -) - -import ( "testing" . "github.com/onsi/gomega" + "github.com/urfave/cli" ) func flagSet(name string, flags []cli.Flag) *flag.FlagSet { diff --git a/local_repository_test.go b/local_repository_test.go index 1bc5597e..bc22dcf3 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -2,17 +2,14 @@ package main import ( "io/ioutil" + "net/url" + "os" "path/filepath" "testing" . "github.com/onsi/gomega" ) -import ( - "net/url" - "os" -) - func TestNewLocalRepository(t *testing.T) { RegisterTestingT(t)