From 99c755ad1d4c821be8929c672dbc0613e88113c5 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 24 Apr 2015 15:12:45 -0700 Subject: [PATCH] Use git url fragment to specify reference and dir context. Signed-off-by: David Calavera --- urlutil/git.go | 9 +++++++-- urlutil/git_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/urlutil/git.go b/urlutil/git.go index ba88ddf..dc4d666 100644 --- a/urlutil/git.go +++ b/urlutil/git.go @@ -1,6 +1,9 @@ package urlutil -import "strings" +import ( + "regexp" + "strings" +) var ( validPrefixes = []string{ @@ -8,11 +11,13 @@ var ( "github.com/", "git@", } + + urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$") ) // IsGitURL returns true if the provided str is a git repository URL. func IsGitURL(str string) bool { - if IsURL(str) && strings.HasSuffix(str, ".git") { + if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) { return true } for _, prefix := range validPrefixes { diff --git a/urlutil/git_test.go b/urlutil/git_test.go index 01dcea7..bb89d8b 100644 --- a/urlutil/git_test.go +++ b/urlutil/git_test.go @@ -9,10 +9,15 @@ var ( "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", } + invalidGitUrls = []string{ + "http://github.com/docker/docker.git:#branch", + } ) 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) } } + for _, url := range incompleteGitUrls { if IsGitURL(url) == false { 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) + } + } }