run

package
v2.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 18 Imported by: 1 module, 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

Overview

Package run orchestrates a preen run: survey the tree, group it into a plan, and apply that plan under a conservation guarantee.

The engine owns every mechanical step and every guardrail. Grouping is the only judgment it delegates, so a run behaves the same whether a model was involved or not.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNothingToDo reports a clean tree with no commits to redo.
	ErrNothingToDo = errors.New("nothing to preen")
	// ErrContentChanged reports that the content tree moved during a run,
	// which means work was lost or invented. The run is rolled back.
	ErrContentChanged = errors.New("content changed during the run")
	// ErrGateFailed reports that the configured gate command failed after a
	// commit.
	ErrGateFailed = errors.New("gate command failed")
	// ErrHunkMissing reports a planned hunk that no longer appears in the
	// regenerated diff, so the plan no longer matches the tree.
	ErrHunkMissing = errors.New("planned hunk not found in the current diff")
	// ErrPushedRewrite reports an attempt to redo published commits without
	// the explicit consent that requires.
	ErrPushedRewrite = errors.New("range contains pushed commits")
	// ErrProtectedBranch reports a rewrite refused because the branch is shared
	// by name, like main.
	ErrProtectedBranch = errors.New("refusing to rewrite a protected branch")
	// ErrNeedBase reports a rewrite that cannot work out which commits to redo.
	ErrNeedBase = errors.New("a rewrite needs an explicit base")
	// ErrFixupTarget reports a change no unpushed commit introduced, so there is
	// nothing to fold it into.
	ErrFixupTarget = errors.New("no unpushed commit introduced these lines")
	// ErrAllGenerated reports a tree whose every change is generated output, so
	// there is nothing worth committing.
	ErrAllGenerated = errors.New("every change is generated output")
)

Sentinel errors for a preen run.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	// Repo is the repository being preened.
	Repo *repo.Repo
	// Grouper decides how changes cluster into commits.
	Grouper group.Grouper
	// Gate runs the configured check after each commit.
	Gate Gate
	// Now supplies the current time, injectable so runs are reproducible in
	// tests.
	Now func() time.Time
	// Out receives progress output.
	Out io.Writer
}

Engine runs preen against one repository.

func New

func New(r *repo.Repo) *Engine

New returns an Engine with the deterministic grouper and real clock, the configuration that needs no model and no network.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, p *plan.Plan, opts Options) (*Result, error)

Apply carries out a plan and verifies that content was conserved. On any divergence it restores from the backup ref and reports what differed.

The plan describes the tree as it was when Plan surveyed it. A change that arrives after that survey is not in the plan and stays uncommitted: planned hunks are matched by body rather than position, so an edit that removes one fails the run rather than committing the wrong lines.

func (*Engine) ApplyFixup

func (e *Engine) ApplyFixup(ctx context.Context, fp *FixupPlan, opts Options) (*Result, error)

ApplyFixup stages each routed change, records it as a fixup commit, and then squashes the lot with an autosquash rebase.

The same conservation guarantee applies: the content going in must equal the content coming out, and a rebase that conflicts is aborted and rolled back rather than left half done.

func (*Engine) Plan

func (e *Engine) Plan(ctx context.Context, opts Options) (*plan.Plan, error)

Plan surveys the repository and builds a plan without changing anything.

func (*Engine) PlanFixup

func (e *Engine) PlanFixup(ctx context.Context, opts Options) (*FixupPlan, error)

PlanFixup routes each dirty change to the unpushed commit that last touched it, instead of building new commits.

Targeting is per file rather than per line. A file-level answer is one git can state exactly, where attributing a hunk to a commit means guessing at which of several commits owns a region, and a wrong guess here silently rewrites the wrong commit.

func (*Engine) Push

func (e *Engine) Push(ctx context.Context) error

Push republishes a rewritten branch. It is separate from Apply so the push happens only after the commits are recorded and verified.

func (*Engine) Request

func (e *Engine) Request(ctx context.Context, opts Options) (group.Request, error)

Request returns what a grouper would be asked to solve for this run, without grouping or changing anything. An agent that made the changes can answer it from its own knowledge of why it made them.

type Fixup

type Fixup struct {
	// Part is the change being folded in.
	Part plan.Part
	// Target is the unpushed commit the change belongs to.
	Target repo.Commit
}

Fixup is one change routed to the commit that introduced it.

type FixupPlan

type FixupPlan struct {
	// Plan is the renderable, validatable plan.
	Plan *plan.Plan
	// Fixups are the routed changes.
	Fixups []Fixup
}

FixupPlan is a fixup run's intent: the plan to render and approve, plus the routing that says which commit each change folds into.

type Gate

type Gate interface {
	// Check runs the gate in dir and returns its combined output.
	Check(ctx context.Context, dir, command string) ([]byte, error)
}

Gate runs a check after a commit and reports failure.

type GateFunc

type GateFunc func(ctx context.Context, dir, command string) ([]byte, error)

GateFunc adapts a function to the Gate interface.

func (GateFunc) Check

func (f GateFunc) Check(ctx context.Context, dir, command string) ([]byte, error)

Check calls f.

type Options

type Options struct {
	// Scope limits the run to paths matching these pathspecs. Everything else
	// stays uncommitted and untouched.
	Scope []string
	// Leave names changes to leave uncommitted, as a drop at the approval
	// prompt does. Unlike Scope, each one is still accounted for in the plan.
	Leave []string
	// Gate is a command run after each commit. A failure stops the run.
	Gate string
	// Absorb brings unpushed commits back into the tree to be redone.
	Absorb bool
	// NoVerify skips commit hooks, which requires standing consent from the
	// caller and is never preen's own decision.
	NoVerify bool
	// AllowHookRewrites accepts content differences confined to paths the run
	// committed, which is what a reformatting commit hook produces.
	AllowHookRewrites bool
	// Style is the commit message convention. It is applied while planning, so
	// the messages shown for approval are the ones that get recorded.
	Style style.Style
	// Pushed grants the explicit consent that rewriting published commits
	// requires. It cannot come from a config file, only from the invocation.
	Pushed bool
	// PushedBase names the commit just before the range to redo. Without one,
	// the merge base with the default branch is used.
	PushedBase string
	// AllowProtected permits a rewrite on a protected branch, the second
	// consent needed when the branch is shared by name.
	AllowProtected bool
	// Protected are extra branch patterns the repository declares off limits.
	Protected []string
	// Fixup folds each change into the unpushed commit that introduced it
	// instead of building new commits.
	Fixup bool
	// IncludeFiles lists the touched paths in each commit body.
	IncludeFiles bool
	// IncludeLineNumbers cites each file's changed line ranges in the body,
	// read from the hunk headers of the real diff.
	IncludeLineNumbers bool
	// Sweep scans the added lines for debris and reports what it finds. It
	// never removes anything.
	Sweep bool
	// AllowGenerated plans generated output like any other change instead of
	// holding it back, which is the consent a repository that vendors such a
	// directory deliberately has to give.
	AllowGenerated bool
	// GeneratedPatterns are extra never-commit patterns the repository declares.
	GeneratedPatterns []string
	// GeneratedAllow are patterns exempted from the never-commit rules, for
	// paths the repository commits on purpose.
	GeneratedAllow []string
}

Options control a single run.

type Recorded

type Recorded struct {
	// Hash is the new commit.
	Hash string
	// Subject is its subject line.
	Subject string
	// Paths are the files it touched.
	Paths []string
}

Recorded is a commit the run created.

type Result

type Result struct {
	// Backup is the recovery ref covering the whole run.
	Backup string
	// Commits are the commits created, in order.
	Commits []Recorded
	// TreeStart is the content hash before the run.
	TreeStart string
	// TreeEnd is the content hash after the run.
	TreeEnd string
	// Reformatted lists paths a commit hook rewrote, accepted only when the
	// caller allowed hook rewrites.
	Reformatted []string
}

Result reports what a run did.

Jump to

Keyboard shortcuts

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