Skip to content

Commit 16e41ba

Browse files
authored
Merge pull request #1710 from jedevc/use-buildkit-gitutil-parsegitref
Use buildkit's gitutil package to detect remote git files
2 parents 6535f16 + 87a120e commit 16e41ba

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

bake/bake.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/docker/buildx/util/buildflags"
1919
"github.com/docker/buildx/util/platformutil"
2020
"github.com/docker/cli/cli/config"
21-
"github.com/docker/docker/builder/remotecontext/urlutil"
2221
hcl "github.com/hashicorp/hcl/v2"
2322
"github.com/moby/buildkit/client/llb"
2423
"github.com/moby/buildkit/session/auth/authprovider"
@@ -790,7 +789,7 @@ func updateContext(t *build.Inputs, inp *Input) {
790789
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
791790
continue
792791
}
793-
if IsRemoteURL(v.Path) {
792+
if build.IsRemoteURL(v.Path) {
794793
continue
795794
}
796795
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
@@ -804,7 +803,7 @@ func updateContext(t *build.Inputs, inp *Input) {
804803
if strings.HasPrefix(t.ContextPath, "cwd://") {
805804
return
806805
}
807-
if IsRemoteURL(t.ContextPath) {
806+
if build.IsRemoteURL(t.ContextPath) {
808807
return
809808
}
810809
st := llb.Scratch().File(llb.Copy(*inp.State, t.ContextPath, "/"), llb.WithCustomNamef("set context to %s", t.ContextPath))
@@ -840,7 +839,7 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
840839
}
841840

842841
func checkPath(p string) error {
843-
if IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
842+
if build.IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
844843
return nil
845844
}
846845
p, err := filepath.EvalSymlinks(p)
@@ -876,15 +875,15 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
876875
if t.Context != nil {
877876
contextPath = *t.Context
878877
}
879-
if !strings.HasPrefix(contextPath, "cwd://") && !IsRemoteURL(contextPath) {
878+
if !strings.HasPrefix(contextPath, "cwd://") && !build.IsRemoteURL(contextPath) {
880879
contextPath = path.Clean(contextPath)
881880
}
882881
dockerfilePath := "Dockerfile"
883882
if t.Dockerfile != nil {
884883
dockerfilePath = *t.Dockerfile
885884
}
886885

887-
if !isRemoteResource(contextPath) && !path.IsAbs(dockerfilePath) {
886+
if !build.IsRemoteURL(contextPath) && !path.IsAbs(dockerfilePath) {
888887
dockerfilePath = path.Join(contextPath, dockerfilePath)
889888
}
890889

@@ -1040,10 +1039,6 @@ func removeDupes(s []string) []string {
10401039
return s[:i]
10411040
}
10421041

1043-
func isRemoteResource(str string) bool {
1044-
return urlutil.IsGitURL(str) || urlutil.IsURL(str)
1045-
}
1046-
10471042
func parseOutputType(str string) string {
10481043
csvReader := csv.NewReader(strings.NewReader(str))
10491044
fields, err := csvReader.Read()

bake/remote.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ func ReadRemoteFiles(ctx context.Context, nodes []builder.Node, url string, name
8383
return files, inp, nil
8484
}
8585

86-
func IsRemoteURL(url string) bool {
87-
if _, _, ok := detectHTTPContext(url); ok {
88-
return true
89-
}
90-
if _, ok := detectGitContext(url); ok {
91-
return true
92-
}
93-
return false
94-
}
95-
9686
func detectHTTPContext(url string) (*llb.State, string, bool) {
9787
if httpPrefix.MatchString(url) {
9888
httpContext := llb.HTTP(url, llb.Filename("context"), llb.WithCustomName("[internal] load remote build context"))

build/build.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,6 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
12781278
target.LocalDirs["context"] = inp.ContextPath
12791279
}
12801280
}
1281-
12821281
case isLocalDir(inp.ContextPath):
12831282
target.LocalDirs["context"] = inp.ContextPath
12841283
switch inp.DockerfilePath {
@@ -1290,8 +1289,7 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
12901289
dockerfileDir = filepath.Dir(inp.DockerfilePath)
12911290
dockerfileName = filepath.Base(inp.DockerfilePath)
12921291
}
1293-
1294-
case urlutil.IsGitURL(inp.ContextPath), urlutil.IsURL(inp.ContextPath):
1292+
case IsRemoteURL(inp.ContextPath):
12951293
if inp.DockerfilePath == "-" {
12961294
dockerfileReader = inp.InStream
12971295
}
@@ -1346,7 +1344,7 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
13461344
continue
13471345
}
13481346

1349-
if urlutil.IsGitURL(v.Path) || urlutil.IsURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") {
1347+
if IsRemoteURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") {
13501348
target.FrontendAttrs["context:"+k] = v.Path
13511349
continue
13521350
}

build/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99

1010
"github.com/docker/cli/opts"
11+
"github.com/docker/docker/builder/remotecontext/urlutil"
12+
"github.com/moby/buildkit/util/gitutil"
1113
"github.com/pkg/errors"
1214
)
1315

@@ -20,6 +22,16 @@ const (
2022
mobyHostGatewayName = "host-gateway"
2123
)
2224

25+
func IsRemoteURL(c string) bool {
26+
if urlutil.IsURL(c) {
27+
return true
28+
}
29+
if _, err := gitutil.ParseGitRef(c); err == nil {
30+
return true
31+
}
32+
return false
33+
}
34+
2335
func isLocalDir(c string) bool {
2436
st, err := os.Stat(c)
2537
return err == nil && st.IsDir()

commands/bake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
4747
cmdContext := "cwd://"
4848

4949
if len(targets) > 0 {
50-
if bake.IsRemoteURL(targets[0]) {
50+
if build.IsRemoteURL(targets[0]) {
5151
url = targets[0]
5252
targets = targets[1:]
5353
if len(targets) > 0 {
54-
if bake.IsRemoteURL(targets[0]) {
54+
if build.IsRemoteURL(targets[0]) {
5555
cmdContext = targets[0]
5656
targets = targets[1:]
5757
}

commands/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/containerd/console"
17+
"github.com/docker/buildx/build"
1718
"github.com/docker/buildx/controller"
1819
cbuild "github.com/docker/buildx/controller/build"
1920
"github.com/docker/buildx/controller/control"
@@ -29,7 +30,6 @@ import (
2930
"github.com/docker/cli/cli"
3031
"github.com/docker/cli/cli/command"
3132
dockeropts "github.com/docker/cli/opts"
32-
"github.com/docker/docker/builder/remotecontext/urlutil"
3333
"github.com/docker/docker/pkg/ioutils"
3434
"github.com/moby/buildkit/client"
3535
"github.com/moby/buildkit/exporter/containerimage/exptypes"
@@ -670,7 +670,7 @@ func dockerUlimitToControllerUlimit(u *dockeropts.UlimitOpt) *controllerapi.Ulim
670670
// and replaces them to absolute paths.
671671
func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOptions, err error) {
672672
if options.ContextPath != "" && options.ContextPath != "-" {
673-
if !urlutil.IsGitURL(options.ContextPath) && !urlutil.IsURL(options.ContextPath) {
673+
if !build.IsRemoteURL(options.ContextPath) {
674674
options.ContextPath, err = filepath.Abs(options.ContextPath)
675675
if err != nil {
676676
return nil, err
@@ -685,7 +685,7 @@ func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOp
685685
}
686686
var contexts map[string]string
687687
for k, v := range options.NamedContexts {
688-
if urlutil.IsGitURL(v) || urlutil.IsURL(v) || strings.HasPrefix(v, "docker-image://") {
688+
if build.IsRemoteURL(v) || strings.HasPrefix(v, "docker-image://") {
689689
// url prefix, this is a remote path
690690
} else if strings.HasPrefix(v, "oci-layout://") {
691691
// oci layout prefix, this is a local path

0 commit comments

Comments
 (0)