Move IsGitTransport() to gitutils

This function was only used inside gitutils,
and is written specifically for the requirements
there.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2017-06-26 10:07:04 -07:00
parent 913eb99fdc
commit d3d1aabcc6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 34 additions and 21 deletions

View file

@ -57,7 +57,7 @@ func Clone(remoteURL string) (string, error) {
func parseRemoteURL(remoteURL string) (gitRepo, error) {
repo := gitRepo{}
if !urlutil.IsGitTransport(remoteURL) {
if !isGitTransport(remoteURL) {
remoteURL = "https://" + remoteURL
}
@ -151,3 +151,9 @@ func gitWithinDir(dir string, args ...string) ([]byte, error) {
func git(args ...string) ([]byte, error) {
return exec.Command("git", args...).CombinedOutput()
}
// isGitTransport returns true if the provided str is a git transport by inspecting
// the prefix of the string for known protocols used in git.
func isGitTransport(str string) bool {
return urlutil.IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
}

View file

@ -209,3 +209,30 @@ func TestCheckoutGit(t *testing.T) {
assert.Equal(t, c.exp, string(b))
}
}
func TestValidGitTransport(t *testing.T) {
gitUrls := []string{
"git://github.com/docker/docker",
"git@github.com:docker/docker.git",
"git@bitbucket.org:atlassianlabs/atlassian-docker.git",
"https://github.com/docker/docker.git",
"http://github.com/docker/docker.git",
"http://github.com/docker/docker.git#branch",
"http://github.com/docker/docker.git#:dir",
}
incompleteGitUrls := []string{
"github.com/docker/docker",
}
for _, url := range gitUrls {
if !isGitTransport(url) {
t.Fatalf("%q should be detected as valid Git prefix", url)
}
}
for _, url := range incompleteGitUrls {
if isGitTransport(url) {
t.Fatalf("%q should not be detected as valid Git prefix", url)
}
}
}

View file

@ -29,12 +29,6 @@ func IsGitURL(str string) bool {
return checkURL(str, "git")
}
// IsGitTransport returns true if the provided str is a git transport by inspecting
// the prefix of the string for known protocols used in git.
func IsGitTransport(str string) bool {
return IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
}
// IsTransportURL returns true if the provided str is a transport (tcp, tcp+tls, udp, unix) URL.
func IsTransportURL(str string) bool {
return checkURL(str, "transport")

View file

@ -27,20 +27,6 @@ var (
}
)
func TestValidGitTransport(t *testing.T) {
for _, url := range gitUrls {
if !IsGitTransport(url) {
t.Fatalf("%q should be detected as valid Git prefix", url)
}
}
for _, url := range incompleteGitUrls {
if IsGitTransport(url) {
t.Fatalf("%q should not be detected as valid Git prefix", url)
}
}
}
func TestIsGIT(t *testing.T) {
for _, url := range gitUrls {
if !IsGitURL(url) {