Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions frontend/dockerfile/dfgitutil/git_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,17 @@ func (gf *GitRef) loadQuery(query url.Values) error {
}
return nil
}

// FragmentFormat returns a simplified git URL in fragment format with only ref.
// If the URL cannot be parsed, the original string is returned with false.
func FragmentFormat(remote string) (string, bool) {
gitRef, _, err := ParseGitRef(remote)
if err != nil || gitRef == nil {
return remote, false
}
u := gitRef.Remote
if gitRef.Ref != "" {
u += "#" + gitRef.Ref
}
return u, true
}
171 changes: 171 additions & 0 deletions frontend/dockerfile/dfgitutil/git_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,174 @@ func TestParseGitRef(t *testing.T) {
})
}
}

func TestFragmentFormat(t *testing.T) {
cases := []struct {
ref string
expected string
ok bool
}{
{
ref: "https://example.com/",
expected: "https://example.com/",
ok: false,
},
{
ref: "https://example.com/foo.git",
expected: "https://example.com/foo.git",
ok: true,
},
{
ref: "https://example.com/foo.git#deadbeef",
expected: "https://example.com/foo.git#deadbeef",
ok: true,
},
{
ref: "https://example.com/foo.git#release/1.2",
expected: "https://example.com/foo.git#release/1.2",
ok: true,
},
{
ref: "https://example.com/foo.git/",
expected: "https://example.com/foo.git/",
ok: false,
},
{
ref: "https://example.com/foo.git.bar",
expected: "https://example.com/foo.git.bar",
ok: false,
},
{
ref: "git://example.com/foo",
expected: "git://example.com/foo",
ok: true,
},
{
ref: "github.com/moby/buildkit",
expected: "github.com/moby/buildkit",
ok: true,
},
{
ref: "github.com/moby/buildkit#master",
expected: "github.com/moby/buildkit#master",
ok: true,
},
{
ref: "custom.xyz/moby/buildkit.git",
expected: "custom.xyz/moby/buildkit.git",
ok: false,
},
{
ref: "https://github.com/moby/buildkit",
expected: "https://github.com/moby/buildkit",
ok: false,
},
{
ref: "https://github.com/moby/buildkit.git",
expected: "https://github.com/moby/buildkit.git",
ok: true,
},
{
ref: "https://foo:[email protected]/moby/buildkit.git",
expected: "https://foo:[email protected]/moby/buildkit.git",
ok: true,
},
{
ref: "[email protected]:moby/buildkit",
expected: "[email protected]:moby/buildkit",
ok: true,
},
{
ref: "[email protected]:moby/buildkit.git",
expected: "[email protected]:moby/buildkit.git",
ok: true,
},
{
ref: "[email protected]:atlassianlabs/atlassian-docker.git",
expected: "[email protected]:atlassianlabs/atlassian-docker.git",
ok: true,
},
{
ref: "https://github.com/foo/bar.git#baz/qux:quux/quuz",
expected: "https://github.com/foo/bar.git#baz/qux",
ok: true,
},
{
ref: "http://github.com/docker/docker.git:#branch",
expected: "http://github.com/docker/docker.git:#branch",
ok: false,
},
{
ref: "https://github.com/docker/docker.git#:myfolder",
expected: "https://github.com/docker/docker.git",
ok: true,
},
{
ref: "./.git",
expected: "./.git",
ok: false,
},
{
ref: ".git",
expected: ".git",
ok: false,
},
{
ref: "https://github.com/docker/docker.git?ref=v1.0.0&subdir=/subdir",
expected: "https://github.com/docker/docker.git#v1.0.0",
ok: true,
},
{
ref: "https://github.com/moby/buildkit.git?subdir=/subdir#v1.0.0",
expected: "https://github.com/moby/buildkit.git#v1.0.0",
ok: true,
},
{
ref: "https://github.com/moby/buildkit.git?tag=v1.0.0",
expected: "https://github.com/moby/buildkit.git#refs/tags/v1.0.0",
ok: true,
},
{
ref: "github.com/moby/buildkit?tag=v1.0.0",
expected: "github.com/moby/buildkit#refs/tags/v1.0.0",
ok: true,
},
{
ref: "https://github.com/moby/buildkit.git?branch=v1.0",
expected: "https://github.com/moby/buildkit.git#refs/heads/v1.0",
ok: true,
},
{
ref: "https://github.com/moby/buildkit.git?ref=v1.0.0#v1.2.3",
expected: "https://github.com/moby/buildkit.git?ref=v1.0.0#v1.2.3",
ok: false,
},
{
ref: "https://github.com/moby/buildkit.git?ref=v1.0.0&tag=v1.2.3",
expected: "https://github.com/moby/buildkit.git?ref=v1.0.0&tag=v1.2.3",
ok: false,
},
{
ref: "https://github.com/moby/buildkit.git?tag=v1.0.0&branch=v1.0",
expected: "https://github.com/moby/buildkit.git?tag=v1.0.0&branch=v1.0",
ok: false,
},
{
ref: "[email protected]:moby/buildkit.git?subdir=/subdir#v1.0.0",
expected: "[email protected]:moby/buildkit.git#v1.0.0",
ok: true,
},
{
ref: "https://github.com/moby/buildkit.git?invalid=123",
expected: "https://github.com/moby/buildkit.git?invalid=123",
ok: false,
},
}
for i, tt := range cases {
t.Run(fmt.Sprintf("case%d", i+1), func(t *testing.T) {
got, ok := FragmentFormat(tt.ref)
require.Equal(t, tt.expected, got)
require.Equal(t, tt.ok, ok)
})
}
}
32 changes: 2 additions & 30 deletions solver/llbsolver/provenance/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/containerd/platforms"
slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
slsa02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2"
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
provenancetypes "github.com/moby/buildkit/solver/llbsolver/provenance/types"
"github.com/moby/buildkit/util/gitutil"
"github.com/moby/buildkit/util/purl"
"github.com/moby/buildkit/util/urlutil"
"github.com/package-url/packageurl-go"
Expand Down Expand Up @@ -70,35 +70,7 @@ func digestSetForCommit(commit string) slsa.DigestSet {
}

func findMaterial(srcs provenancetypes.Sources, uri string) (*slsa.ProvenanceMaterial, bool) {
// Git URLs in querystring format or subdir need to be converted to fragment format with only ref
gitRef, err := gitutil.ParseURL(uri)
if err == nil && gitRef != nil {
u := gitRef.Remote
var ref string
if gitRef.Opts != nil {
ref = gitRef.Opts.Ref
}
if len(gitRef.Query) > 0 {
for k, v := range gitRef.Query {
if len(v) == 0 {
continue
}
switch k {
case "ref":
ref = v[0]
case "branch":
ref = "refs/heads/" + v[0]
case "tag":
ref = "refs/tags/" + v[0]
}
}
}
if ref != "" {
u += "#" + ref
}
uri = u
}

uri, _ = dfgitutil.FragmentFormat(uri)
for _, s := range srcs.Git {
if s.URL == uri {
return &slsa.ProvenanceMaterial{
Expand Down
2 changes: 1 addition & 1 deletion util/gitutil/git_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var supportedProtos = map[string]struct{}{

var protoRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+://`)

// URL is a custom URL type that points to a remote Git repository.
// GitURL is a custom URL type that points to a remote Git repository.
//
// URLs can be parsed from both standard URLs (e.g.
// "https://github.com/moby/buildkit.git"), as well as SCP-like URLs (e.g.
Expand Down