286 lines
11 KiB
Go
286 lines
11 KiB
Go
// Package releaseidentity resolves source release identities within a repository.
|
|
package releaseidentity
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
"go.maximumdirect.net/releasekit/internal/versioncontract"
|
|
"golang.org/x/mod/modfile"
|
|
"golang.org/x/mod/module"
|
|
)
|
|
|
|
// Selection identifies the normalized repository and selected module
|
|
// directories. ModuleDir is an absolute path; ModuleSubdir is its canonical
|
|
// repository-relative slash path, with "." representing the root.
|
|
type Selection struct {
|
|
RepositoryDir string
|
|
ModuleDir string
|
|
ModuleSubdir string
|
|
}
|
|
|
|
// Identity is the resolved identity of a production or diagnostic release.
|
|
type Identity struct {
|
|
Selection
|
|
Kind string
|
|
SourceDir string
|
|
SourceSubdir string
|
|
GoEnabled bool
|
|
Publication string
|
|
VersionFormat string
|
|
VersionPrefix string
|
|
Commit string
|
|
ModulePath string
|
|
Version string
|
|
Tag string
|
|
NotePath string
|
|
Note string
|
|
}
|
|
|
|
// ResolveSelection normalizes a repository-relative module selection and
|
|
// confirms its final filesystem location remains inside the repository.
|
|
func ResolveSelection(repositoryDir, moduleDir string) (Selection, error) {
|
|
if moduleDir == "" {
|
|
moduleDir = "."
|
|
}
|
|
if filepath.IsAbs(moduleDir) {
|
|
return Selection{}, fmt.Errorf("module directory must be repository-relative: %q", moduleDir)
|
|
}
|
|
repositoryAbs, err := filepath.Abs(repositoryDir)
|
|
if err != nil {
|
|
return Selection{}, fmt.Errorf("resolve repository directory: %w", err)
|
|
}
|
|
repositoryAbs, err = filepath.EvalSymlinks(repositoryAbs)
|
|
if err != nil {
|
|
return Selection{}, fmt.Errorf("resolve repository directory %q: %w", repositoryDir, err)
|
|
}
|
|
// Preserve components until symlinks are followed, so link/.. uses the
|
|
// linked directory's parent just as committed path traversal does.
|
|
moduleAbs, err := filepath.EvalSymlinks(repositoryAbs + string(filepath.Separator) + moduleDir)
|
|
if err != nil {
|
|
return Selection{}, fmt.Errorf("resolve module directory %q: %w", moduleDir, err)
|
|
}
|
|
relative, err := filepath.Rel(repositoryAbs, moduleAbs)
|
|
if err != nil {
|
|
return Selection{}, fmt.Errorf("make module directory relative to repository: %w", err)
|
|
}
|
|
if relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) || filepath.IsAbs(relative) {
|
|
return Selection{}, fmt.Errorf("module directory %q resolves outside repository %q", moduleDir, repositoryDir)
|
|
}
|
|
if relative == "." {
|
|
return Selection{RepositoryDir: repositoryAbs, ModuleDir: moduleAbs, ModuleSubdir: "."}, nil
|
|
}
|
|
return Selection{RepositoryDir: repositoryAbs, ModuleDir: moduleAbs, ModuleSubdir: filepath.ToSlash(relative)}, nil
|
|
}
|
|
|
|
// FromCommitted derives identity from the selected module declaration at a
|
|
// captured commit. The selected module must contain its own go.mod.
|
|
func FromCommitted(ctx context.Context, repositoryDir, moduleDir, version, commit string) (Identity, error) {
|
|
return FromCommittedSource(ctx, repositoryDir, moduleDir, version, commit, nil)
|
|
}
|
|
|
|
// FromCommittedSource resolves an identity from source at the captured commit.
|
|
func FromCommittedSource(ctx context.Context, repositoryDir, moduleDir, version, commit string, source *SourceSpec) (Identity, error) {
|
|
selection, err := LoadCommittedSource(ctx, repositoryDir, moduleDir, commit, source)
|
|
return deriveSelected(ctx, selection, version, commit, err)
|
|
}
|
|
|
|
// ResolveCommittedSelection resolves directory selection without reading the
|
|
// module declaration or requiring the directory to be materialized.
|
|
func ResolveCommittedSelection(ctx context.Context, repositoryDir, moduleDir, commit string) (Selection, error) {
|
|
resolver, err := committedPaths(repositoryDir, commit)
|
|
if err != nil {
|
|
return Selection{}, err
|
|
}
|
|
return selectCommittedDirectory(ctx, resolver, moduleDir)
|
|
}
|
|
|
|
func committedPaths(repositoryDir, commit string) (git.PathResolver, error) {
|
|
repositoryAbs, err := filepath.Abs(repositoryDir)
|
|
if err != nil {
|
|
return git.PathResolver{}, err
|
|
}
|
|
repositoryAbs, err = filepath.EvalSymlinks(repositoryAbs)
|
|
if err != nil {
|
|
return git.PathResolver{}, err
|
|
}
|
|
return git.CommittedPaths(repositoryAbs, commit), nil
|
|
}
|
|
|
|
func selectCommittedDirectory(ctx context.Context, resolver git.PathResolver, moduleDir string) (Selection, error) {
|
|
selection := Selection{RepositoryDir: resolver.RepositoryDir}
|
|
if moduleDir == "" {
|
|
moduleDir = "."
|
|
}
|
|
if filepath.IsAbs(moduleDir) {
|
|
return selection, fmt.Errorf("module directory must be repository-relative: %q", moduleDir)
|
|
}
|
|
directory, err := resolver.ResolveDestination(ctx, "go.mod", filepath.ToSlash(moduleDir))
|
|
if err != nil {
|
|
return selection, fmt.Errorf("resolve committed module directory: %w", err)
|
|
}
|
|
if directory.Kind != git.DestinationDirectory {
|
|
return selection, fmt.Errorf("selected committed module directory %q resolves to %s at %q", moduleDir, directory.Kind, directory.Path)
|
|
}
|
|
selection.ModuleDir = filepath.Join(resolver.RepositoryDir, filepath.FromSlash(directory.Path))
|
|
selection.ModuleSubdir = directory.Path
|
|
return selection, nil
|
|
}
|
|
|
|
func readCommittedDeclaration(ctx context.Context, selection Selection, commit string, resolver git.PathResolver) ([]byte, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, fmt.Errorf("resolve committed release identity: %w", err)
|
|
}
|
|
declarationPath, err := resolver.ResolveDestination(ctx, "go.mod", moduleFilePath(selection.ModuleSubdir))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve selected module declaration at %s: %w", commit, err)
|
|
}
|
|
if declarationPath.Kind != git.DestinationEntry {
|
|
return nil, fmt.Errorf("selected module declaration resolves to %s at %q", declarationPath.Kind, declarationPath.Path)
|
|
}
|
|
entry, _, err := resolver.Lookup(ctx, declarationPath.Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
declaration, err := git.ReadBlob(ctx, selection.RepositoryDir, entry.ObjectID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read selected module declaration at %s: %w", commit, err)
|
|
}
|
|
return declaration, nil
|
|
}
|
|
|
|
// FromWorkingTree derives identity from the selected module declaration in the
|
|
// working tree. The selected module must contain its own go.mod.
|
|
func FromWorkingTree(ctx context.Context, repositoryDir, moduleDir, version string) (Identity, error) {
|
|
return FromWorkingSource(ctx, repositoryDir, moduleDir, version, nil)
|
|
}
|
|
|
|
// FromWorkingSource resolves an identity from working-tree source.
|
|
func FromWorkingSource(ctx context.Context, repositoryDir, moduleDir, version string, source *SourceSpec) (Identity, error) {
|
|
selection, err := LoadWorkingSource(ctx, repositoryDir, moduleDir, source)
|
|
return deriveSelected(ctx, selection, version, "", err)
|
|
}
|
|
|
|
func deriveSelected(ctx context.Context, source ResolvedSource, version, commit string, err error) (Identity, error) {
|
|
identity := Identity{
|
|
Selection: source.Selection, Kind: "production", SourceDir: source.ModuleDir, SourceSubdir: source.ModuleSubdir,
|
|
GoEnabled: source.GoEnabled, Publication: source.Publication, VersionFormat: source.VersionFormat,
|
|
VersionPrefix: source.VersionPrefix, ModulePath: source.ModulePath, Version: version, Commit: commit,
|
|
}
|
|
if !source.GoEnabled {
|
|
identity.ModuleDir, identity.ModuleSubdir = "", ""
|
|
}
|
|
if err != nil {
|
|
return identity, err
|
|
}
|
|
if !versioncontract.ValidIdentifier(version, source.VersionFormat, source.VersionPrefix) {
|
|
return identity, fmt.Errorf("%w: %q is not a valid %s identifier", versioncontract.ErrInvalidIdentifier, version, source.VersionFormat)
|
|
}
|
|
if source.Publication == "source" {
|
|
prefix := source.ModuleSubdir
|
|
if prefix == "." {
|
|
prefix = ""
|
|
}
|
|
if source.TagPrefix != nil {
|
|
prefix = *source.TagPrefix
|
|
}
|
|
identity.Tag = version
|
|
if prefix != "" {
|
|
identity.Tag = prefix + "/" + version
|
|
}
|
|
identity.NotePath = path.Join(source.ModuleSubdir, "docs", "releases", version+".md")
|
|
if err := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
|
return identity, fmt.Errorf("invalid source release tag: %w", err)
|
|
}
|
|
return identity, nil
|
|
}
|
|
if source.VersionFormat != "semver" || source.VersionPrefix != "v" {
|
|
return identity, fmt.Errorf("Go module publication requires prefixed semantic versions and the module-derived tag")
|
|
}
|
|
moduleIdentity, err := deriveModule(source.Selection, source.ModulePath, version)
|
|
if err != nil {
|
|
return identity, err
|
|
}
|
|
identity.Tag, identity.NotePath = moduleIdentity.Tag, moduleIdentity.NotePath
|
|
if source.TagPrefix != nil {
|
|
prefix := strings.TrimSuffix(strings.TrimSuffix(identity.Tag, version), "/")
|
|
if *source.TagPrefix != prefix {
|
|
return identity, fmt.Errorf("source tag prefix %q conflicts with Go module tag prefix %q", *source.TagPrefix, prefix)
|
|
}
|
|
}
|
|
if err := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
|
return identity, fmt.Errorf("invalid module release tag: %w", err)
|
|
}
|
|
return identity, nil
|
|
}
|
|
|
|
// Derive applies Go module path-major rules and derives the standard full tag
|
|
// and module-local release-note path from a declaration and selection.
|
|
func Derive(selection Selection, declaration []byte, version string) (Identity, error) {
|
|
identity := Identity{Selection: selection, Version: version}
|
|
if selection.RepositoryDir == "" || selection.ModuleDir == "" || selection.ModuleSubdir == "" {
|
|
return identity, fmt.Errorf("incomplete module selection")
|
|
}
|
|
modulePath, err := modulePathFromDeclaration(declaration)
|
|
identity.ModulePath = modulePath
|
|
if err != nil {
|
|
return identity, err
|
|
}
|
|
return deriveModule(selection, modulePath, version)
|
|
}
|
|
|
|
func deriveModule(selection Selection, modulePath, version string) (Identity, error) {
|
|
identity := Identity{
|
|
Selection: selection, Kind: "production", SourceDir: selection.ModuleDir, SourceSubdir: selection.ModuleSubdir,
|
|
GoEnabled: true, Publication: "go-module", VersionFormat: "semver", VersionPrefix: "v",
|
|
ModulePath: modulePath, Version: version,
|
|
}
|
|
_, pathMajor, ok := module.SplitPathVersion(modulePath)
|
|
if !ok {
|
|
return identity, fmt.Errorf("module path %q has an invalid major-version suffix", modulePath)
|
|
}
|
|
if !versioncontract.ValidStableVersion(version) {
|
|
return identity, fmt.Errorf("invalid stable release version %q", version)
|
|
}
|
|
if err := module.CheckPathMajor(version, pathMajor); err != nil {
|
|
return identity, fmt.Errorf("version %q is incompatible with module path %q: %w", version, modulePath, err)
|
|
}
|
|
|
|
tagPrefix := selection.ModuleSubdir
|
|
if strings.HasPrefix(pathMajor, "/") && tagPrefix != "." && path.Base(tagPrefix) == strings.TrimPrefix(pathMajor, "/") {
|
|
tagPrefix = path.Dir(tagPrefix)
|
|
}
|
|
tag := version
|
|
if tagPrefix != "." {
|
|
tag = path.Join(tagPrefix, version)
|
|
}
|
|
notePath := path.Join(selection.ModuleSubdir, "docs", "releases", version+".md")
|
|
identity.Tag, identity.NotePath = tag, notePath
|
|
return identity, nil
|
|
}
|
|
|
|
func modulePathFromDeclaration(declaration []byte) (string, error) {
|
|
modulePath := modfile.ModulePath(declaration)
|
|
if modulePath == "" {
|
|
return "", fmt.Errorf("selected module declaration has no valid module directive")
|
|
}
|
|
if err := module.CheckImportPath(modulePath); err != nil {
|
|
return modulePath, fmt.Errorf("invalid module path %q: %w", modulePath, err)
|
|
}
|
|
if _, _, ok := module.SplitPathVersion(modulePath); !ok {
|
|
return modulePath, fmt.Errorf("module path %q has an invalid major-version suffix", modulePath)
|
|
}
|
|
return modulePath, nil
|
|
}
|
|
|
|
func moduleFilePath(moduleSubdir string) string {
|
|
if moduleSubdir == "." {
|
|
return "go.mod"
|
|
}
|
|
return path.Join(moduleSubdir, "go.mod")
|
|
}
|