211 lines
7.1 KiB
Go
211 lines
7.1 KiB
Go
|
|
package releaseidentity
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SourceSpec describes explicit source, Go, publication, and version choices.
|
||
|
|
// Nil SourceSpec selects the historical Go-module shorthand.
|
||
|
|
type SourceSpec struct {
|
||
|
|
Dir string
|
||
|
|
GoEnabled bool
|
||
|
|
Publication string
|
||
|
|
VersionFormat string
|
||
|
|
VersionPrefix *string
|
||
|
|
TagPrefix *string
|
||
|
|
}
|
||
|
|
|
||
|
|
// SourceSelection contains the normalized configuration, independent of the
|
||
|
|
// working or committed module declaration.
|
||
|
|
type SourceSelection struct {
|
||
|
|
Dir string
|
||
|
|
GoEnabled bool
|
||
|
|
Publication string
|
||
|
|
VersionFormat string
|
||
|
|
VersionPrefix string
|
||
|
|
TagPrefix *string
|
||
|
|
}
|
||
|
|
|
||
|
|
// ResolvedSource joins the configuration selection with its physical scope.
|
||
|
|
type ResolvedSource struct {
|
||
|
|
SourceSelection
|
||
|
|
Selection
|
||
|
|
ModulePath string
|
||
|
|
}
|
||
|
|
|
||
|
|
// ResolveWorkingSource resolves the selected working-tree directory without
|
||
|
|
// requiring a module declaration.
|
||
|
|
func ResolveWorkingSource(repositoryDir, moduleDir string, source *SourceSpec) (ResolvedSource, error) {
|
||
|
|
config, err := NormalizeSource(moduleDir, source)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{}, err
|
||
|
|
}
|
||
|
|
directory, err := ResolveSelection(repositoryDir, sourceDirectory(moduleDir, source))
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{SourceSelection: config}, err
|
||
|
|
}
|
||
|
|
if source != nil && source.Dir != "" && moduleDir != "" {
|
||
|
|
module, err := ResolveSelection(repositoryDir, moduleDir)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, err
|
||
|
|
}
|
||
|
|
if module.ModuleDir != directory.ModuleDir {
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, selectionConflict(source.Dir, moduleDir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
info, err := os.Stat(directory.ModuleDir)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, fmt.Errorf("inspect source directory: %w", err)
|
||
|
|
}
|
||
|
|
if !info.IsDir() {
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, fmt.Errorf("selected source %q is not a directory", config.Dir)
|
||
|
|
}
|
||
|
|
config.Dir = directory.ModuleSubdir
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ResolveCommittedSource resolves the selected directory in a captured commit
|
||
|
|
// without reading a module declaration or relying on the working tree.
|
||
|
|
func ResolveCommittedSource(ctx context.Context, repositoryDir, moduleDir, commit string, source *SourceSpec) (ResolvedSource, error) {
|
||
|
|
config, err := NormalizeSource(moduleDir, source)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{}, err
|
||
|
|
}
|
||
|
|
resolver, err := committedPaths(repositoryDir, commit)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{SourceSelection: config}, err
|
||
|
|
}
|
||
|
|
directory, err := resolveCommittedSourceDirectory(ctx, resolver, moduleDir, source)
|
||
|
|
if err == nil {
|
||
|
|
config.Dir = directory.ModuleSubdir
|
||
|
|
}
|
||
|
|
return ResolvedSource{SourceSelection: config, Selection: directory}, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoadWorkingSource resolves the source scope and reads its module declaration
|
||
|
|
// only when Go capability is selected.
|
||
|
|
func LoadWorkingSource(ctx context.Context, repositoryDir, moduleDir string, source *SourceSpec) (ResolvedSource, error) {
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return ResolvedSource{}, err
|
||
|
|
}
|
||
|
|
resolved, err := ResolveWorkingSource(repositoryDir, moduleDir, source)
|
||
|
|
if err != nil || !resolved.GoEnabled {
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
declarationPath := filepath.Join(resolved.ModuleDir, "go.mod")
|
||
|
|
declaration, err := os.ReadFile(declarationPath)
|
||
|
|
if err != nil {
|
||
|
|
return resolved, fmt.Errorf("read selected module declaration %q: %w", declarationPath, err)
|
||
|
|
}
|
||
|
|
resolved.ModulePath, err = modulePathFromDeclaration(declaration)
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoadCommittedSource reads the selected module declaration from the captured
|
||
|
|
// commit, without requiring that declaration in the working tree.
|
||
|
|
func LoadCommittedSource(ctx context.Context, repositoryDir, moduleDir, commit string, source *SourceSpec) (ResolvedSource, error) {
|
||
|
|
config, err := NormalizeSource(moduleDir, source)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{}, err
|
||
|
|
}
|
||
|
|
resolver, err := committedPaths(repositoryDir, commit)
|
||
|
|
if err != nil {
|
||
|
|
return ResolvedSource{SourceSelection: config}, err
|
||
|
|
}
|
||
|
|
directory, err := resolveCommittedSourceDirectory(ctx, resolver, moduleDir, source)
|
||
|
|
resolved := ResolvedSource{SourceSelection: config, Selection: directory}
|
||
|
|
if err != nil {
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
resolved.Dir = directory.ModuleSubdir
|
||
|
|
if !resolved.GoEnabled {
|
||
|
|
return resolved, nil
|
||
|
|
}
|
||
|
|
declaration, err := readCommittedDeclaration(ctx, directory, commit, resolver)
|
||
|
|
if err != nil {
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
resolved.ModulePath, err = modulePathFromDeclaration(declaration)
|
||
|
|
return resolved, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func resolveCommittedSourceDirectory(ctx context.Context, resolver git.PathResolver, moduleDir string, source *SourceSpec) (Selection, error) {
|
||
|
|
directory, err := selectCommittedDirectory(ctx, resolver, sourceDirectory(moduleDir, source))
|
||
|
|
if err != nil {
|
||
|
|
return directory, err
|
||
|
|
}
|
||
|
|
if source != nil && source.Dir != "" && moduleDir != "" {
|
||
|
|
module, err := selectCommittedDirectory(ctx, resolver, moduleDir)
|
||
|
|
if err != nil {
|
||
|
|
return directory, err
|
||
|
|
}
|
||
|
|
if module.ModuleSubdir != directory.ModuleSubdir {
|
||
|
|
return directory, selectionConflict(source.Dir, moduleDir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return directory, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func selectionConflict(sourceDir, moduleDir string) error {
|
||
|
|
return fmt.Errorf("source.dir %q conflicts with module_dir %q", sourceDir, moduleDir)
|
||
|
|
}
|
||
|
|
|
||
|
|
func sourceDirectory(moduleDir string, source *SourceSpec) string {
|
||
|
|
if source != nil && source.Dir != "" {
|
||
|
|
return source.Dir
|
||
|
|
}
|
||
|
|
return moduleDir
|
||
|
|
}
|
||
|
|
|
||
|
|
// NormalizeSource applies explicit defaults and preserves the legacy shorthand.
|
||
|
|
func NormalizeSource(moduleDir string, source *SourceSpec) (SourceSelection, error) {
|
||
|
|
if source == nil {
|
||
|
|
source = &SourceSpec{GoEnabled: true, Publication: "go-module"}
|
||
|
|
}
|
||
|
|
dir := source.Dir
|
||
|
|
if dir == "" {
|
||
|
|
dir = moduleDir
|
||
|
|
}
|
||
|
|
if dir == "" {
|
||
|
|
dir = "."
|
||
|
|
}
|
||
|
|
if filepath.IsAbs(dir) {
|
||
|
|
return SourceSelection{}, fmt.Errorf("source directory must be repository-relative: %q", dir)
|
||
|
|
}
|
||
|
|
result := SourceSelection{Dir: filepath.ToSlash(filepath.Clean(dir)), GoEnabled: source.GoEnabled, Publication: source.Publication, VersionFormat: source.VersionFormat, TagPrefix: source.TagPrefix}
|
||
|
|
if result.Publication == "" {
|
||
|
|
result.Publication = "source"
|
||
|
|
}
|
||
|
|
if result.Publication != "source" && result.Publication != "go-module" {
|
||
|
|
return SourceSelection{}, fmt.Errorf("invalid publication mode %q", result.Publication)
|
||
|
|
}
|
||
|
|
if result.Publication == "go-module" && !result.GoEnabled {
|
||
|
|
return SourceSelection{}, fmt.Errorf("go-module publication requires Go capability")
|
||
|
|
}
|
||
|
|
if result.VersionFormat == "" {
|
||
|
|
result.VersionFormat = "semver"
|
||
|
|
}
|
||
|
|
switch result.VersionFormat {
|
||
|
|
case "semver":
|
||
|
|
result.VersionPrefix = "v"
|
||
|
|
case "date", "year-month":
|
||
|
|
default:
|
||
|
|
return SourceSelection{}, fmt.Errorf("invalid version format %q", result.VersionFormat)
|
||
|
|
}
|
||
|
|
if source.VersionPrefix != nil {
|
||
|
|
result.VersionPrefix = *source.VersionPrefix
|
||
|
|
}
|
||
|
|
if result.VersionPrefix != "" && result.VersionPrefix != "v" {
|
||
|
|
return SourceSelection{}, fmt.Errorf("version prefix must be \"v\" or empty")
|
||
|
|
}
|
||
|
|
return result, nil
|
||
|
|
}
|