cleanup

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 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

Overview

Package cleanup decides, for each linked worktree of a repository, whether it can be removed, and removes it when asked to.

A worktree is removed only on positive proof from a forge that its HEAD commit is preserved there — contained in a merged or closed pull or merge request, or in a project's default branch — and only when it holds nothing local that removal would lose. Anything short of that proof, including a forge error, keeps the worktree.

Index

Constants

View Source
const (
	// ActionKeep leaves the worktree in place; the reason says why.
	ActionKeep = "keep"
	// ActionWouldRemove is a removable worktree in a dry run.
	ActionWouldRemove = "would-remove"
	// ActionRemoved means the worktree was removed.
	ActionRemoved = "removed"
	// ActionError means deciding or removing failed; the worktree is kept
	// and the reason holds the error.
	ActionError = "error"
)

Row actions.

Variables

View Source
var DefaultDisposable = []string{
	"_output/", ".cache/", "__pycache__/", "*.pyc", "*.test", "*.out",
	"node_modules/", "bin/", "dist/", ".build*/", "target/",
}

DefaultDisposable is the conservative default list of ignored paths a worktree may hold and still be removed: build output and caches only.

Functions

func ValidateDisposable

func ValidateDisposable(raw []string) error

ValidateDisposable returns the error New would return for the disposable patterns raw, or nil when every one is well formed.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine decides and removes worktrees, many at once. It is safe for concurrent use when its Git and Forge are, and asks the forge each question about a commit once, however many worktrees and repositories need the answer.

func New

func New(git Git, f Forge, projects Projects, opts Options) (*Engine, error)

New returns an Engine, or an error when a disposable pattern is malformed.

func (*Engine) Repo

func (e *Engine) Repo(ctx context.Context, repo string, emit func(Row)) error

Repo processes the linked worktrees of repo, deciding as many at once as Options.Concurrency allows and calling emit, from any goroutine, with each worktree's row as soon as it is decided or acted on. Removals within repo run one at a time. Repo returns an error when the repository's worktrees or remotes cannot be listed, and ctx.Err() when ctx is canceled; it starts no decision after the cancellation, and a removal already under way finishes first, its row emitted whatever the outcome.

func (*Engine) Scan added in v0.2.0

func (e *Engine) Scan(ctx context.Context, repos []string, emit func(Row)) error

Scan processes repos as Repo does, all of them at once within the one Options.Concurrency bound, starting them in order. A repository whose worktrees or remotes cannot be listed is reported as an error row. Scan returns ctx.Err() when ctx is canceled, once every removal under way has finished and been reported.

type Forge

type Forge interface {
	Exists(ctx context.Context, p forge.Project) error
	Changes(ctx context.Context, p forge.Project, sha string) ([]forge.Change, error)
	ClosedChanges(ctx context.Context, projects []forge.Project, sha string) (map[forge.Project][]forge.Change, error)
	Contains(ctx context.Context, p forge.Project, ancestor, descendant string) (bool, error)
	DefaultBranch(ctx context.Context, p forge.Project) (string, error)
}

Forge answers, for one project, whether it exists, which changes contain a commit, and whether one commit contains another.

Exists returns an error wrapping forge.ErrProjectNotFound for a project that is gone or invisible. Changes and Contains return an error wrapping forge.ErrCommitUnknown when the project does not hold a commit they were asked about; Contains reports commits sharing no history as (false, nil).

ClosedChanges returns, by project, the changes containing a commit that Changes may omit: on GitHub, pull requests closed without merging. It is the costliest question, asked once for several projects together.

type Git

type Git interface {
	Worktrees(ctx context.Context, repo string) ([]gitrepo.Worktree, error)
	Remotes(ctx context.Context, repo string) ([]gitrepo.Remote, error)
	Inspect(ctx context.Context, path string) (gitrepo.LocalState, error)
	BranchTip(ctx context.Context, repo, branch string) (string, error)
	RemoveWorktree(ctx context.Context, repo, path string) error
	DeleteBranch(ctx context.Context, repo, branch, expectSHA string) error
}

Git reads a repository's worktrees and removes them and their branches. gitrepo.Git implements it.

type Options

type Options struct {
	// Prune removes removable worktrees; without it the run is dry and
	// changes nothing.
	Prune bool
	// MinIdle keeps a worktree whose git directory changed more recently.
	MinIdle time.Duration
	// Disposable lists the gitignore-like patterns an ignored path must
	// match for its worktree to be removable. An empty list makes every
	// ignored path block removal.
	Disposable []string
	// WorkDir is the process's working directory, absolute and
	// symlink-resolved as git reports worktree paths; the worktree holding
	// it is kept. Empty keeps none for this reason.
	WorkDir string
	// Concurrency is the most worktree decisions, counting each
	// repository's listing as one, in flight at once across every
	// repository the engine processes. Below 1 means 1.
	Concurrency int
	// Now returns the current time; nil means time.Now.
	Now func() time.Time
}

Options configures an Engine.

type Projects

type Projects func(remotes []gitrepo.Remote) []forge.Project

Projects returns the forge projects a repository's remotes name, skipping remotes on hosts that are not a known forge.

type Row

type Row struct {
	Action string `json:"action"`
	Reason string `json:"reason"`
	Repo   string `json:"repo"`
	Path   string `json:"path"`
	Branch string `json:"branch"`
	Head   string `json:"head"`
	// ChangeURL is the pull or merge request that kept or removed the
	// worktree.
	ChangeURL string `json:"change_url,omitempty"`
	// Blocking lists the paths that kept the worktree.
	Blocking []string `json:"blocking,omitempty"`
}

Row reports the outcome for one worktree.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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