From fdfe0c97e7731180664269e23227894340c8dcd9 Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Wed, 8 Oct 2014 04:09:08 +0000 Subject: [PATCH] Add HasValidGITPrefix to utils/utils.go This will allow us to use a common Git prefix check for both api/clients/commands.go and builder/job.go. Previous prefix check in build from Git (in builder/jobs.go) ignored valid prefixes such as "git@", "http://" or "https://". Signed-off-by: Lakshan Perera Upstream-commit: d3ac9ea98e872fee808693c736bc5a465d6426e2 Component: engine --- components/engine/api/client/commands.go | 2 +- components/engine/builder/job.go | 3 +-- components/engine/utils/utils.go | 4 ++++ components/engine/utils/utils_test.go | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/components/engine/api/client/commands.go b/components/engine/api/client/commands.go index 6c4e5c55fe..ab8ac96f03 100644 --- a/components/engine/api/client/commands.go +++ b/components/engine/api/client/commands.go @@ -116,7 +116,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { root := cmd.Arg(0) if utils.IsGIT(root) { remoteURL := cmd.Arg(0) - if !strings.HasPrefix(remoteURL, "git://") && !strings.HasPrefix(remoteURL, "git@") && !utils.IsURL(remoteURL) { + if !utils.ValidGitTransport(remoteURL) { remoteURL = "https://" + remoteURL } diff --git a/components/engine/builder/job.go b/components/engine/builder/job.go index 555232c9ae..4ce8cbe020 100644 --- a/components/engine/builder/job.go +++ b/components/engine/builder/job.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "os/exec" - "strings" "github.com/docker/docker/daemon" "github.com/docker/docker/engine" @@ -59,7 +58,7 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status { if remoteURL == "" { context = ioutil.NopCloser(job.Stdin) } else if utils.IsGIT(remoteURL) { - if !strings.HasPrefix(remoteURL, "git://") { + if !utils.ValidGitTransport(remoteURL) { remoteURL = "https://" + remoteURL } root, err := ioutil.TempDir("", "docker-build-git") diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index 792b80bd51..70ab420791 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -304,6 +304,10 @@ func IsGIT(str string) bool { return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@github.com:") || (strings.HasSuffix(str, ".git") && IsURL(str)) } +func ValidGitTransport(str string) bool { + return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@") || IsURL(str) +} + var ( localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`) ) diff --git a/components/engine/utils/utils_test.go b/components/engine/utils/utils_test.go index ce304482b8..6e2de7e041 100644 --- a/components/engine/utils/utils_test.go +++ b/components/engine/utils/utils_test.go @@ -97,3 +97,24 @@ func TestReadSymlinkedDirectoryToFile(t *testing.T) { t.Errorf("failed to remove symlink: %s", err) } } + +func TestValidGitTransport(t *testing.T) { + for _, url := range []string{ + "git://github.com/docker/docker", + "git@github.com:docker/docker.git", + "https://github.com/docker/docker.git", + "http://github.com/docker/docker.git", + } { + if ValidGitTransport(url) == false { + t.Fatalf("%q should be detected as valid Git prefix", url) + } + } + + for _, url := range []string{ + "github.com/docker/docker", + } { + if ValidGitTransport(url) == true { + t.Fatalf("%q should not be detected as valid Git prefix", url) + } + } +}