generate

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: GPL-3.0 Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeSMP

func ComputeSMP(oldContent, newContent []byte) []byte

ComputeSMP computes a strategic merge patch: the minimal YAML document that, when applied through the expand+merge2 pipeline in pkg/action/patch.go, reproduces newContent from oldContent.

  • Maps are recursively diffed; only changed/added keys are included.
  • Scalar lists emit only added items (expandScalarLists prepends old values on apply).
  • Map-lists are matched by an inferred merge key (e.g. "name"); only changed/added items are included (merge2 matches by key on apply).

Returns nil if YAML cannot be parsed or if there are no changes.

func Parameterize

func Parameterize(content string, params map[string]string) string

Parameterize replaces literal occurrences of param values in content with Go template expressions ({{ .key }}). Replacements are applied longest-value-first to avoid partial-match issues.

func ParameterizePath

func ParameterizePath(path string, params map[string]string) string

ParameterizePath replaces literal occurrences of param values in a file path with Go template expressions ({{ .key }}). Replacements are applied longest-value-first.

func Run

func Run(ctx context.Context, opts Options, logger *slog.Logger) error

Run generates a loom module from one or more sources.

Types

type ChangeSet added in v0.4.0

type ChangeSet struct {
	Title      string
	Body       string
	BaseBranch string
	HeadBranch string
	RepoURL    string
	Provider   string // "github" or "gitlab"
	Files      []FileChange
}

ChangeSet holds metadata about a PR/MR.

func Compose added in v0.4.0

func Compose(sets []*ChangeSet, logger *slog.Logger) (*ChangeSet, error)

Compose squashes changesets, applied in order, into a single net ChangeSet. Per file, the old content comes from the first changeset that touched it and the new content from the last, so the result transforms the original base state directly into the final desired state.

type ChangeSource added in v0.4.0

type ChangeSource interface {
	Fetch(ctx context.Context, ref string, token string, logger *slog.Logger) (*ChangeSet, error)
}

ChangeSource fetches file changes from a source (PR/MR, commit range, or local snapshot).

type ChangeType

type ChangeType int

ChangeType classifies what happened to a file in a PR/MR.

const (
	ChangeAdded ChangeType = iota
	ChangeModified
	ChangeDeleted
	ChangeRenamed
)

func (ChangeType) String

func (c ChangeType) String() string

type CommitSource added in v0.4.0

type CommitSource struct {
	// RepoURL is the remote repository URL. Empty when LocalPath is set.
	RepoURL string
	// LocalPath points at an existing local checkout to read from instead of
	// cloning.
	LocalPath string
	// BaseRev is the base of the range. Empty means HeadRev's first parent
	// (single-commit form).
	BaseRev string
	// HeadRev is the commit whose state the module should reproduce.
	HeadRev string
	// Provider is a hint inferred from the repo host ("github", "gitlab", or "").
	Provider string
}

CommitSource fetches the diff of a commit or commit range using git only — no provider API. It works against any git host and authenticates with the user's existing git credentials.

func (*CommitSource) Fetch added in v0.4.0

func (s *CommitSource) Fetch(ctx context.Context, _ string, _ string, logger *slog.Logger) (*ChangeSet, error)

Fetch implements ChangeSource.

type FileChange

type FileChange struct {
	Type       ChangeType
	Path       string // file path relative to repo root
	OldPath    string // previous path (only for renames)
	NewContent []byte // full file content after the change (for added/modified)
	OldContent []byte // full file content before the change (for modified)
}

FileChange represents a single file change from a PR/MR.

type GitHubSource added in v0.4.0

type GitHubSource struct{}

GitHubSource fetches PR diffs from GitHub.

func (*GitHubSource) Fetch added in v0.4.0

func (p *GitHubSource) Fetch(ctx context.Context, ref, token string, logger *slog.Logger) (*ChangeSet, error)

type GitLabSource added in v0.4.0

type GitLabSource struct{}

GitLabSource fetches MR diffs from GitLab.

func (*GitLabSource) Fetch added in v0.4.0

func (p *GitLabSource) Fetch(ctx context.Context, ref, token string, logger *slog.Logger) (*ChangeSet, error)

type Options

type Options struct {
	// Refs are the source references (PR/MR, commit, or local path), applied
	// in the order given (oldest first).
	Refs []string
	// Params maps param names to concrete values to parameterize.
	Params map[string]string
	// OutputDir is the directory to write the generated module.
	OutputDir string
	// ModuleName overrides the auto-derived module name.
	ModuleName string
	// TokenEnv is the name of the environment variable that holds the
	// GitHub personal access token or GitLab private token used to
	// authenticate API requests when fetching PR/MR data. Commit and
	// snapshot sources are git-native and do not use it.
	TokenEnv string
	// Include, Exclude, and Base configure snapshot (local path) sources.
	Include []string
	Exclude []string
	Base    string
}

Options configures module generation.

type SnapshotOptions added in v0.4.0

type SnapshotOptions struct {
	Include []string
	Exclude []string
	Base    string
}

SnapshotOptions configures snapshot (local file) sources. The options are shared by all snapshot refs of a single generate invocation.

type SnapshotSource added in v0.4.0

type SnapshotSource struct {
	Path    string // local checkout; mutually exclusive with RepoURL
	RepoURL string // remote repository URL
	Ref     string // committed ref to capture; empty = worktree (local) or default branch (remote)
	Include []string
	Exclude []string
	Base    string
}

SnapshotSource captures the state of files in a repository. The source is either a local checkout (Path) or a remote repository (RepoURL, cloned bare with lazy blobs).

Without Ref, a local snapshot captures the working tree — including uncommitted and untracked files. With Ref (always for remotes, where no working tree exists), it captures the committed tree at that ref.

Without Base every matched file becomes an added file (a stamp-out module); with Base the captured state is diffed against that git ref (a transform module), so modified YAML still produces SMP patches.

func (*SnapshotSource) Fetch added in v0.4.0

func (s *SnapshotSource) Fetch(ctx context.Context, _ string, _ string, logger *slog.Logger) (*ChangeSet, error)

Fetch implements ChangeSource.

type Source added in v0.4.0

type Source struct {
	Kind SourceKind
	// Provider is "github" or "gitlab" when known, "" otherwise. Only PR
	// sources require a provider; commit and snapshot sources are git-native
	// and use it solely to populate the generated pr operation.
	Provider     string
	ChangeSource ChangeSource
}

Source is a parsed source reference.

func ParseSourceRef added in v0.4.0

func ParseSourceRef(ref string, snap SnapshotOptions, logger *slog.Logger) (*Source, error)

ParseSourceRef parses a source reference and returns the parsed Source.

PR/MR references (see also the generate spec):

  • github:owner/repo#123 / gitlab:group/repo!123
  • https://<host>/owner/repo/pull/123
  • https://<host>/group/repo/-/merge_requests/123

Commit references (git-native, any host):

  • <repo>@<sha> or <repo>@<base>...<head>, where <repo> is a short-form repo (github:o/r), any git URL, or a local checkout path
  • commit / compare URLs (sugar, normalized to <repo>@<range>)

Snapshot references (current state of local files):

  • a local path: ./checkout, /abs/path, or file:<path>

type SourceKind added in v0.4.0

type SourceKind int

SourceKind classifies what kind of source a reference points at.

const (
	// KindPR is a GitHub pull request or GitLab merge request.
	KindPR SourceKind = iota
	// KindCommit is a single commit or commit range, fetched via git.
	KindCommit
	// KindSnapshot is the current state of files in a local checkout.
	KindSnapshot
)

func (SourceKind) String added in v0.4.0

func (k SourceKind) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL