105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
// Package publication checks retained contracts against authoritative release tags.
|
|
// It is separate from offline schema-distribution validation.
|
|
package publication
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"forge.maximumdirect.net/eric/schemas/internal/validation"
|
|
)
|
|
|
|
var calver = regexp.MustCompile(`^[0-9]{4}\.(0[1-9]|1[0-2])\.(0|[1-9][0-9]*)$`)
|
|
|
|
func git(repo string, args ...string) ([]byte, error) {
|
|
out, err := exec.Command("git", append([]string{"-C", repo}, args...)...).CombinedOutput()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, out)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// CheckRemote reads the authoritative tag list after the caller has fetched it.
|
|
// It neither fetches nor changes any refs and never checks CI or hosting.
|
|
func CheckRemote(repo, remote string) error {
|
|
out, err := git(repo, "ls-remote", "--tags", "--refs", remote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) != 2 {
|
|
continue
|
|
}
|
|
tag := strings.TrimPrefix(fields[1], "refs/tags/")
|
|
if !calver.MatchString(tag) {
|
|
continue
|
|
}
|
|
// Verify the fetched tag is the authoritative object, not a same-named local ref.
|
|
local, err := git(repo, "rev-parse", "refs/tags/"+tag)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(string(local)) != fields[0] {
|
|
return fmt.Errorf("tag %s differs from %s; fetch authoritative tags before checking", tag, remote)
|
|
}
|
|
if err := CheckTag(repo, tag); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckTag compares catalog records and every file in each published directory
|
|
// with the committed candidate at HEAD. New version directories remain allowed.
|
|
func CheckTag(repo, tag string) error {
|
|
readCatalog := func(revision string) (validation.Catalog, error) {
|
|
data, err := git(repo, "show", revision+":catalog.json")
|
|
var catalog validation.Catalog
|
|
if err != nil {
|
|
return catalog, err
|
|
}
|
|
if err := json.Unmarshal(data, &catalog); err != nil {
|
|
return catalog, fmt.Errorf("%s catalog: %w", revision, err)
|
|
}
|
|
if catalog.FormatVersion != 1 {
|
|
return catalog, fmt.Errorf("%s: unsupported catalog format %d", revision, catalog.FormatVersion)
|
|
}
|
|
return catalog, nil
|
|
}
|
|
previous, err := readCatalog(tag)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
current, err := readCatalog("HEAD")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
records := make(map[string]validation.Record)
|
|
for _, record := range current.Schemas {
|
|
records[record.Name+"@"+record.Version] = record
|
|
}
|
|
for _, record := range previous.Schemas {
|
|
key := record.Name + "@" + record.Version
|
|
if records[key] != record {
|
|
return fmt.Errorf("%s: published inventory record %s changed or disappeared", tag, key)
|
|
}
|
|
directory := strings.TrimSuffix(record.Path, "/schema.json") + "/"
|
|
before, err := git(repo, "ls-tree", "-r", "--format=%(objectname) %(path)", tag, "--", directory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
after, err := git(repo, "ls-tree", "-r", "--format=%(objectname) %(path)", "HEAD", "--", directory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(before) == 0 || string(before) != string(after) {
|
|
return fmt.Errorf("%s: published directory %s changed or disappeared", tag, directory)
|
|
}
|
|
}
|
|
return nil
|
|
}
|