Skip to content

Commit 7940a96

Browse files
authored
Merge pull request #3646 from crazy-max/urlutil
history: don't import build package
2 parents 9bfd2a4 + 7899695 commit 7940a96

File tree

8 files changed

+156
-35
lines changed

8 files changed

+156
-35
lines changed

bake/bake.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/buildx/util/buildflags"
2525
"github.com/docker/buildx/util/platformutil"
2626
"github.com/docker/buildx/util/progress"
27+
"github.com/docker/buildx/util/urlutil"
2728
dockeropts "github.com/docker/cli/opts"
2829
hcl "github.com/hashicorp/hcl/v2"
2930
"github.com/moby/buildkit/client"
@@ -1302,7 +1303,7 @@ func updateContext(t *build.Inputs, inp *Input) {
13021303
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
13031304
continue
13041305
}
1305-
if build.IsRemoteURL(v.Path) {
1306+
if urlutil.IsRemoteURL(v.Path) {
13061307
continue
13071308
}
13081309
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
@@ -1316,7 +1317,7 @@ func updateContext(t *build.Inputs, inp *Input) {
13161317
if strings.HasPrefix(t.ContextPath, "cwd://") {
13171318
return
13181319
}
1319-
if build.IsRemoteURL(t.ContextPath) {
1320+
if urlutil.IsRemoteURL(t.ContextPath) {
13201321
return
13211322
}
13221323
st := llb.Scratch().File(
@@ -1330,10 +1331,10 @@ func updateContext(t *build.Inputs, inp *Input) {
13301331
}
13311332

13321333
func isRemoteContext(t build.Inputs, inp *Input) bool {
1333-
if build.IsRemoteURL(t.ContextPath) {
1334+
if urlutil.IsRemoteURL(t.ContextPath) {
13341335
return true
13351336
}
1336-
if inp != nil && build.IsRemoteURL(inp.URL) && !strings.HasPrefix(t.ContextPath, "cwd://") {
1337+
if inp != nil && urlutil.IsRemoteURL(inp.URL) && !strings.HasPrefix(t.ContextPath, "cwd://") {
13371338
return true
13381339
}
13391340
return false
@@ -1363,7 +1364,7 @@ func collectLocalPaths(t build.Inputs) []string {
13631364
}
13641365

13651366
func isLocalPath(p string) (string, bool) {
1366-
if build.IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
1367+
if urlutil.IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
13671368
return "", false
13681369
}
13691370
return strings.TrimPrefix(p, "cwd://"), true
@@ -1381,7 +1382,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
13811382
if t.Context != nil {
13821383
contextPath = *t.Context
13831384
}
1384-
if !strings.HasPrefix(contextPath, "cwd://") && !build.IsRemoteURL(contextPath) {
1385+
if !strings.HasPrefix(contextPath, "cwd://") && !urlutil.IsRemoteURL(contextPath) {
13851386
contextPath = path.Clean(contextPath)
13861387
}
13871388
dockerfilePath := "Dockerfile"
@@ -1411,7 +1412,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
14111412
if err != nil {
14121413
return nil, err
14131414
}
1414-
} else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) {
1415+
} else if !urlutil.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && urlutil.IsRemoteURL(inp.URL)) {
14151416
// We don't currently support reading a remote Dockerfile with a local
14161417
// context when doing a remote invocation because we automatically
14171418
// derive the dockerfile from the context atm:
@@ -1433,7 +1434,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
14331434
if v, ok := strings.CutPrefix(bi.ContextPath, "cwd://"); ok {
14341435
bi.ContextPath = path.Clean(v)
14351436
}
1436-
if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !filepath.IsAbs(bi.DockerfilePath) {
1437+
if !urlutil.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !filepath.IsAbs(bi.DockerfilePath) {
14371438
bi.DockerfilePath = filepath.Join(bi.ContextPath, bi.DockerfilePath)
14381439
}
14391440
for k, v := range bi.NamedContexts {

build/localstate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/docker/buildx/builder"
77
"github.com/docker/buildx/localstate"
88
"github.com/docker/buildx/util/confutil"
9+
"github.com/docker/buildx/util/urlutil"
910
"github.com/moby/buildkit/client"
1011
)
1112

@@ -16,13 +17,13 @@ func saveLocalState(so *client.SolveOpt, target string, opts Options, node build
1617
}
1718
lp := opts.Inputs.ContextPath
1819
dp := opts.Inputs.DockerfilePath
19-
if dp != "" && !IsRemoteURL(lp) && lp != "-" && dp != "-" {
20+
if dp != "" && !urlutil.IsRemoteURL(lp) && lp != "-" && dp != "-" {
2021
dp, err = filepath.Abs(dp)
2122
if err != nil {
2223
return err
2324
}
2425
}
25-
if lp != "" && !IsRemoteURL(lp) && lp != "-" {
26+
if lp != "" && !urlutil.IsRemoteURL(lp) && lp != "-" {
2627
lp, err = filepath.Abs(lp)
2728
if err != nil {
2829
return err

build/opt.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/docker/buildx/util/dockerutil"
3232
"github.com/docker/buildx/util/osutil"
3333
"github.com/docker/buildx/util/progress"
34+
"github.com/docker/buildx/util/urlutil"
3435
"github.com/moby/buildkit/client"
3536
"github.com/moby/buildkit/client/llb"
3637
"github.com/moby/buildkit/client/ociindex"
@@ -710,7 +711,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro
710711
dockerfileDir = filepath.Dir(inp.DockerfilePath)
711712
dockerfileName = filepath.Base(inp.DockerfilePath)
712713
}
713-
case IsRemoteURL(inp.ContextPath):
714+
case urlutil.IsRemoteURL(inp.ContextPath):
714715
if inp.DockerfilePath == "-" {
715716
dockerfileReader = inp.InStream.NewReadCloser()
716717
} else if filepath.IsAbs(inp.DockerfilePath) {
@@ -751,7 +752,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro
751752
dockerfileName = "Dockerfile"
752753
target.FrontendAttrs["dockerfilekey"] = "dockerfile"
753754
}
754-
if isHTTPURL(inp.DockerfilePath) {
755+
if urlutil.IsHTTPURL(inp.DockerfilePath) {
755756
dockerfileDir, err = createTempDockerfileFromURL(ctx, d, inp.DockerfilePath, pw)
756757
if err != nil {
757758
return nil, err
@@ -824,7 +825,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro
824825
continue
825826
}
826827

827-
if IsRemoteURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") {
828+
if urlutil.IsRemoteURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") {
828829
target.FrontendAttrs["context:"+k] = v.Path
829830
processGitURL(v.Path, "context:"+k, target, caps)
830831
continue

build/utils.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/docker/buildx/driver"
1313
"github.com/docker/cli/opts"
14-
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
1514
"github.com/pkg/errors"
1615
"github.com/sirupsen/logrus"
1716
)
@@ -25,23 +24,6 @@ const (
2524
mobyHostGatewayName = "host-gateway"
2625
)
2726

28-
// isHTTPURL returns true if the provided str is an HTTP(S) URL by checking if it
29-
// has a http:// or https:// scheme. No validation is performed to verify if the
30-
// URL is well-formed.
31-
func isHTTPURL(str string) bool {
32-
return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://")
33-
}
34-
35-
func IsRemoteURL(c string) bool {
36-
if isHTTPURL(c) {
37-
return true
38-
}
39-
if _, ok, _ := dfgitutil.ParseGitRef(c); ok {
40-
return true
41-
}
42-
return false
43-
}
44-
4527
func isArchive(header []byte) bool {
4628
for _, m := range [][]byte{
4729
{0x42, 0x5A, 0x68}, // bzip2

commands/bake.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/docker/buildx/util/osutil"
3434
"github.com/docker/buildx/util/progress"
3535
"github.com/docker/buildx/util/tracing"
36+
"github.com/docker/buildx/util/urlutil"
3637
"github.com/docker/cli/cli/command"
3738
"github.com/moby/buildkit/identity"
3839
"github.com/moby/buildkit/session/auth/authprovider"
@@ -613,11 +614,11 @@ func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string
613614
// from the command line arguments.
614615
func bakeArgs(args []string) (url, cmdContext string, targets []string) {
615616
cmdContext, targets = "cwd://", args
616-
if len(targets) == 0 || !build.IsRemoteURL(targets[0]) {
617+
if len(targets) == 0 || !urlutil.IsRemoteURL(targets[0]) {
617618
return url, cmdContext, targets
618619
}
619620
url, targets = targets[0], targets[1:]
620-
if len(targets) == 0 || !build.IsRemoteURL(targets[0]) {
621+
if len(targets) == 0 || !urlutil.IsRemoteURL(targets[0]) {
621622
return url, cmdContext, targets
622623
}
623624
cmdContext, targets = targets[0], targets[1:]

commands/history/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"sync"
1414
"time"
1515

16-
"github.com/docker/buildx/build"
1716
"github.com/docker/buildx/builder"
1817
"github.com/docker/buildx/localstate"
18+
"github.com/docker/buildx/util/urlutil"
1919
"github.com/docker/cli/cli/command"
2020
controlapi "github.com/moby/buildkit/api/services/control"
2121
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
@@ -56,7 +56,7 @@ func BuildName(fattrs map[string]string, ls *localstate.State) string {
5656
}
5757

5858
var localPath string
59-
if ls != nil && !build.IsRemoteURL(ls.LocalPath) {
59+
if ls != nil && !urlutil.IsRemoteURL(ls.LocalPath) {
6060
if ls.LocalPath != "" && ls.LocalPath != "-" {
6161
localPath = filepath.ToSlash(ls.LocalPath)
6262
}

util/urlutil/urlutil.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package urlutil
2+
3+
import (
4+
"strings"
5+
6+
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
7+
)
8+
9+
// IsHTTPURL returns true if the provided str is an HTTP(S) URL by checking if
10+
// it has a http:// or https:// scheme. No validation is performed to verify if
11+
// the URL is well-formed.
12+
func IsHTTPURL(str string) bool {
13+
return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://")
14+
}
15+
16+
// IsRemoteURL returns true for HTTP(S) URLs and Git references.
17+
func IsRemoteURL(c string) bool {
18+
if IsHTTPURL(c) {
19+
return true
20+
}
21+
if _, ok, _ := dfgitutil.ParseGitRef(c); ok {
22+
return true
23+
}
24+
return false
25+
}

util/urlutil/urlutil_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package urlutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIsHTTPURL(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
want bool
14+
}{
15+
{
16+
name: "https url",
17+
input: "https://example.com/repo.git",
18+
want: true,
19+
},
20+
{
21+
name: "http url",
22+
input: "http://example.com/repo.git",
23+
want: true,
24+
},
25+
{
26+
name: "http prefix only",
27+
input: "http://",
28+
want: true,
29+
},
30+
{
31+
name: "non-http protocol",
32+
input: "git://example.com/repo.git",
33+
want: false,
34+
},
35+
{
36+
name: "no protocol",
37+
input: "example.com/repo.git",
38+
want: false,
39+
},
40+
{
41+
name: "uppercase protocol is not matched",
42+
input: "HTTPS://example.com/repo.git",
43+
want: false,
44+
},
45+
{
46+
name: "leading whitespace does not match",
47+
input: " https://example.com/repo.git",
48+
want: false,
49+
},
50+
}
51+
for _, tc := range tests {
52+
t.Run(tc.name, func(t *testing.T) {
53+
require.Equal(t, tc.want, IsHTTPURL(tc.input))
54+
})
55+
}
56+
}
57+
58+
func TestIsRemoteURL(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
input string
62+
want bool
63+
}{
64+
{
65+
name: "https url is remote",
66+
input: "https://example.com/not-a-git-url",
67+
want: true,
68+
},
69+
{
70+
name: "http url is remote",
71+
input: "http://example.com/path",
72+
want: true,
73+
},
74+
{
75+
name: "scp style git remote",
76+
input: "git@github.com:moby/buildkit.git",
77+
want: true,
78+
},
79+
{
80+
name: "github shorthand git remote",
81+
input: "github.com/moby/buildkit",
82+
want: true,
83+
},
84+
{
85+
name: "relative local path is not remote",
86+
input: "./hack",
87+
want: false,
88+
},
89+
{
90+
name: "plain local path is not remote",
91+
input: "hack/dockerfiles",
92+
want: false,
93+
},
94+
{
95+
name: "unknown protocol is not remote",
96+
input: "docker-image://alpine",
97+
want: false,
98+
},
99+
{
100+
name: "empty is not remote",
101+
input: "",
102+
want: false,
103+
},
104+
}
105+
for _, tc := range tests {
106+
t.Run(tc.name, func(t *testing.T) {
107+
require.Equal(t, tc.want, IsRemoteURL(tc.input))
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)