Skip to content

Commit

Permalink
Add HasValidGITPrefix to utils/utils.go
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
laktek committed Oct 26, 2014
1 parent 6ce4f82 commit d3ac9ea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions builder/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/docker/docker/daemon"
"github.com/docker/docker/engine"
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ func IsGIT(str string) bool {
return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "[email protected]:") || (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*`)
)
Expand Down
21 changes: 21 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"[email protected]: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)
}
}
}

0 comments on commit d3ac9ea

Please sign in to comment.