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 <lakshan@laktek.com>
| ... | ... |
@@ -116,7 +116,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
| 116 | 116 |
root := cmd.Arg(0) |
| 117 | 117 |
if utils.IsGIT(root) {
|
| 118 | 118 |
remoteURL := cmd.Arg(0) |
| 119 |
- if !strings.HasPrefix(remoteURL, "git://") && !strings.HasPrefix(remoteURL, "git@") && !utils.IsURL(remoteURL) {
|
|
| 119 |
+ if !utils.ValidGitTransport(remoteURL) {
|
|
| 120 | 120 |
remoteURL = "https://" + remoteURL |
| 121 | 121 |
} |
| 122 | 122 |
|
| ... | ... |
@@ -5,7 +5,6 @@ import ( |
| 5 | 5 |
"io/ioutil" |
| 6 | 6 |
"os" |
| 7 | 7 |
"os/exec" |
| 8 |
- "strings" |
|
| 9 | 8 |
|
| 10 | 9 |
"github.com/docker/docker/daemon" |
| 11 | 10 |
"github.com/docker/docker/engine" |
| ... | ... |
@@ -59,7 +58,7 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status {
|
| 59 | 59 |
if remoteURL == "" {
|
| 60 | 60 |
context = ioutil.NopCloser(job.Stdin) |
| 61 | 61 |
} else if utils.IsGIT(remoteURL) {
|
| 62 |
- if !strings.HasPrefix(remoteURL, "git://") {
|
|
| 62 |
+ if !utils.ValidGitTransport(remoteURL) {
|
|
| 63 | 63 |
remoteURL = "https://" + remoteURL |
| 64 | 64 |
} |
| 65 | 65 |
root, err := ioutil.TempDir("", "docker-build-git")
|
| ... | ... |
@@ -304,6 +304,10 @@ func IsGIT(str string) bool {
|
| 304 | 304 |
return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@github.com:") || (strings.HasSuffix(str, ".git") && IsURL(str)) |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 |
+func ValidGitTransport(str string) bool {
|
|
| 308 |
+ return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@") || IsURL(str) |
|
| 309 |
+} |
|
| 310 |
+ |
|
| 307 | 311 |
var ( |
| 308 | 312 |
localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`) |
| 309 | 313 |
) |
| ... | ... |
@@ -97,3 +97,24 @@ func TestReadSymlinkedDirectoryToFile(t *testing.T) {
|
| 97 | 97 |
t.Errorf("failed to remove symlink: %s", err)
|
| 98 | 98 |
} |
| 99 | 99 |
} |
| 100 |
+ |
|
| 101 |
+func TestValidGitTransport(t *testing.T) {
|
|
| 102 |
+ for _, url := range []string{
|
|
| 103 |
+ "git://github.com/docker/docker", |
|
| 104 |
+ "git@github.com:docker/docker.git", |
|
| 105 |
+ "https://github.com/docker/docker.git", |
|
| 106 |
+ "http://github.com/docker/docker.git", |
|
| 107 |
+ } {
|
|
| 108 |
+ if ValidGitTransport(url) == false {
|
|
| 109 |
+ t.Fatalf("%q should be detected as valid Git prefix", url)
|
|
| 110 |
+ } |
|
| 111 |
+ } |
|
| 112 |
+ |
|
| 113 |
+ for _, url := range []string{
|
|
| 114 |
+ "github.com/docker/docker", |
|
| 115 |
+ } {
|
|
| 116 |
+ if ValidGitTransport(url) == true {
|
|
| 117 |
+ t.Fatalf("%q should not be detected as valid Git prefix", url)
|
|
| 118 |
+ } |
|
| 119 |
+ } |
|
| 120 |
+} |