77 "fmt"
88 "io"
99 "maps"
10+ "net/url"
1011 "os"
1112 "path"
1213 "path/filepath"
@@ -29,6 +30,7 @@ import (
2930 hcl "github.com/hashicorp/hcl/v2"
3031 "github.com/moby/buildkit/client"
3132 "github.com/moby/buildkit/client/llb"
33+ "github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
3234 "github.com/pkg/errors"
3335 "github.com/zclconf/go-cty/cty"
3436 "github.com/zclconf/go-cty/cty/convert"
@@ -1310,7 +1312,7 @@ func updateContext(t *build.Inputs, inp *Input) {
13101312 st := llb .Scratch ().File (llb .Copy (* inp .State , v .Path , "/" , & llb.CopyInfo {
13111313 CopyDirContentsOnly : true ,
13121314 }), llb .WithCustomNamef ("set context %s to %s" , k , v .Path ))
1313- t .NamedContexts [k ] = build.NamedContext {State : & st , Path : inp .URL }
1315+ t .NamedContexts [k ] = build.NamedContext {State : & st , Path : remoteURLWithSubdir ( inp .URL , v . Path ) }
13141316 }
13151317
13161318 if t .ContextPath == "." {
@@ -1330,7 +1332,44 @@ func updateContext(t *build.Inputs, inp *Input) {
13301332 llb .WithCustomNamef ("set context to %s" , t .ContextPath ),
13311333 )
13321334 t .ContextState = & st
1333- t .ContextPath = inp .URL
1335+ t .ContextPath = remoteURLWithSubdir (inp .URL , t .ContextPath )
1336+ }
1337+
1338+ func remoteURLWithSubdir (remoteURL , subdir string ) string {
1339+ subdir = path .Clean (subdir )
1340+ if subdir == "." || remoteURL == "" {
1341+ return remoteURL
1342+ }
1343+
1344+ // only relevant for git urls
1345+ parsed , ok , err := dfgitutil .ParseGitRef (remoteURL )
1346+ if err != nil || ! ok {
1347+ return remoteURL
1348+ }
1349+ if parsed .SubDir != "" {
1350+ subdir = path .Clean (path .Join (parsed .SubDir , subdir ))
1351+ }
1352+
1353+ // keep query string and transport style untouched nad only append/adjust
1354+ // fragment subdir
1355+ if ! strings .Contains (remoteURL , "#" ) && subdir != "" {
1356+ if u , err := url .Parse (remoteURL ); err == nil {
1357+ q := u .Query ()
1358+ if q .Has ("subdir" ) {
1359+ q .Set ("subdir" , subdir )
1360+ u .RawQuery = q .Encode ()
1361+ return u .String ()
1362+ }
1363+ }
1364+ }
1365+
1366+ // otherwise, we adjust the fragment part to add/replace subdir
1367+ base , frag , _ := strings .Cut (remoteURL , "#" )
1368+ ref , _ , _ := strings .Cut (frag , ":" )
1369+ if ref == "" {
1370+ return base + "#:" + subdir
1371+ }
1372+ return base + "#" + ref + ":" + subdir
13341373}
13351374
13361375func collectLocalPaths (t build.Inputs ) []string {
0 commit comments