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

adjust NewURL() #132

Merged
merged 2 commits into from
Apr 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions local_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 15 additions & 13 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down