diff --git a/attest/vcs_test.go b/attest/vcs_test.go index 0ef3db6..88ce6c5 100644 --- a/attest/vcs_test.go +++ b/attest/vcs_test.go @@ -76,9 +76,17 @@ func TestVCS(t *testing.T) { CheckoutTag: "v0.0.2", LoadPath: "podinfo", ExpectManifests: []string{"kustomization.yaml", "deployment.yaml", "hpa.yaml", "service.yaml"}, - ExpectImageTags: []string{"v0.0.2"}, + ExpectImageTags: []string{"v6.7.0"}, ExpectRawTags: []string{"0.0.2", "v0.0.2", "podinfo/v6.7.0"}, }, + { + URL: "https://github.com/errordeveloper/tape-git-testing", + CheckoutHash: "9eeeed9f4ff44812ca23dba1bd0af9f509686d21", // => v0.0.1 + LoadPath: "podinfo", + ExpectManifests: []string{"kustomization.yaml", "deployment.yaml", "hpa.yaml", "service.yaml"}, + ExpectImageTags: []string{"v6.6.3"}, + ExpectRawTags: []string{"0.0.1", "v0.0.1", "podinfo/v6.6.3"}, + }, } repos := &repos{} diff --git a/oci/artefact.go b/oci/artefact.go index 772afa6..d3a5917 100644 --- a/oci/artefact.go +++ b/oci/artefact.go @@ -395,28 +395,51 @@ func SemVerTagsFromAttestations(ctx context.Context, tag name.Tag, sourceAttesta return []name.Tag{} } ref := groupSummary.Git.Reference - if len(ref.Tags) == 0 { + numTags := len(ref.Tags) + if numTags == 0 { return []name.Tag{} } - // TODO: detect tags with groupSummary.Path+"/" as prefix and priorities them - tags := make([]name.Tag, 0, len(ref.Tags)) - set := make(map[string]struct{}, len(ref.Tags)) + tags := newTagtagSet(numTags) + scopedTags := newTagtagSet(numTags) for i := range ref.Tags { t := ref.Tags[i].Name - if !strings.HasPrefix(t, "v") { - t = "v" + t - } - if _, ok := set[t]; !ok { - if semver.IsValid(t) { - tags = append(tags, tag.Context().Tag(t)) - set[t] = struct{}{} - } + // this is accounts only for a simple case where tape is pointed at a dir + // and a tags have prefix that matches it exactly, it won't work for cases + // where tape is pointed at a subdir a parent of which has a scoped tag + if strings.HasPrefix(t, groupSummary.Path+"/") { + scopedTags.add(strings.TrimPrefix(t, groupSummary.Path+"/"), tag) + continue } + tags.add(t, tag) } - if len(tags) == 0 { - return []name.Tag{} + if len(scopedTags.list) > 0 { + return scopedTags.list + } + return tags.list +} + +type tagSet struct { + set map[string]struct{} + list []name.Tag +} + +func newTagtagSet(c int) *tagSet { + return &tagSet{ + set: make(map[string]struct{}, c), + list: make([]name.Tag, 0, c), + } +} + +func (s *tagSet) add(t string, image name.Tag) { + if !strings.HasPrefix(t, "v") { + t = "v" + t + } + if _, ok := s.set[t]; !ok { + if semver.IsValid(t) { + s.list = append(s.list, image.Context().Tag(t)) + s.set[t] = struct{}{} + } } - return tags } func makeDescriptorWithPlatform() Descriptor {