Merge pull request #12769 from calavera/git_build_fragment

Use git url fragment to specify build reference and dir context.
This commit is contained in:
David Calavera 2015-05-04 12:48:24 -07:00
commit 6c37313b87
2 changed files with 19 additions and 2 deletions

View file

@ -1,6 +1,9 @@
package urlutil package urlutil
import "strings" import (
"regexp"
"strings"
)
var ( var (
validPrefixes = []string{ validPrefixes = []string{
@ -8,11 +11,13 @@ var (
"github.com/", "github.com/",
"git@", "git@",
} }
urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")
) )
// IsGitURL returns true if the provided str is a git repository URL. // IsGitURL returns true if the provided str is a git repository URL.
func IsGitURL(str string) bool { func IsGitURL(str string) bool {
if IsURL(str) && strings.HasSuffix(str, ".git") { if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
return true return true
} }
for _, prefix := range validPrefixes { for _, prefix := range validPrefixes {

View file

@ -9,10 +9,15 @@ var (
"git@bitbucket.org:atlassianlabs/atlassian-docker.git", "git@bitbucket.org:atlassianlabs/atlassian-docker.git",
"https://github.com/docker/docker.git", "https://github.com/docker/docker.git",
"http://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{ incompleteGitUrls = []string{
"github.com/docker/docker", "github.com/docker/docker",
} }
invalidGitUrls = []string{
"http://github.com/docker/docker.git:#branch",
}
) )
func TestValidGitTransport(t *testing.T) { func TestValidGitTransport(t *testing.T) {
@ -35,9 +40,16 @@ func TestIsGIT(t *testing.T) {
t.Fatalf("%q should be detected as valid Git url", url) t.Fatalf("%q should be detected as valid Git url", url)
} }
} }
for _, url := range incompleteGitUrls { for _, url := range incompleteGitUrls {
if IsGitURL(url) == false { if IsGitURL(url) == false {
t.Fatalf("%q should be detected as valid Git url", url) t.Fatalf("%q should be detected as valid Git url", url)
} }
} }
for _, url := range invalidGitUrls {
if IsGitURL(url) == true {
t.Fatalf("%q should not be detected as valid Git prefix", url)
}
}
} }