worktrees

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 36 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 worktrees is the git worktrees sessions work in apart from the user's checkout: made under .worktrees of a repository on a branch of their own, locked for the process or the session using them, archived to a ref before an idle one holding work is removed, restored from that ref, and reclaimed along with the branches merged upstream. Features that work in trees add to their upkeep through hooks: why they still need a tree, what they let go of before one is removed, the repositories they keep and what they reclaim after

Index

Constants

View Source
const (
	KeepUnarchivable   = "keep-unarchivable"
	KeepWorktreeRefs   = "keep-worktree-refs"
	KeepLargeUntracked = "keep-large-untracked"
	KeepSubmoduleWork  = "keep-submodule-work"
)

How a removal blocker keeps a tree

View Source
const (
	ExitKeep    = "keep"
	ExitArchive = "archive"
	ExitDiscard = "discard"
)

What the user may do with a tree holding work as they are done with it

View Source
const ArchivePrefix = "refs/icebath/archive/"

Ref namespace holding the archives of removed trees

View Source
const FilterDiscoveryFailed = "The repository's git filters could not be determined."

Errors of a git run refused because the repository's filter drivers could not be found with certainty

View Source
const SubmoduleReason = "the submodule "

Start of every reason a submodule keeps a tree

Variables

This section is empty.

Functions

func Claimed

func Claimed(branch string) bool

Whether the branch lies where the trees icebath makes for sessions have theirs, those of subagents among them, or is one the sweep deletes once no tree has it checked out, such as one of a pull request: a name another feature must not give a branch of its own

func Dirty

func Dirty(tree, repo string, timeout time.Duration) bool

Whether the tree holds staged, unstaged or untracked changes, or edits git was told to overlook. true when git can't tell. Links back to the main checkout that .worktreeinclude made are no change

func Expire

func Expire(repo string, retentionDays float64, dryRun bool) ([]string, error)

Deletes the archives older than the retention, none for zero, or only names them for a dry run: their refs

func Live

func Live(paths []string, exclude ...int) map[string][]int

Processes working in each of the paths, by the path as given: their folder, or on Linux a file they hold open, lies in it. The excluded processes are left out

func PruneMissing

func PruneMissing(repo string, dryRun bool) ([]string, error)

Drops the registrations of worktrees whose folder is gone, metadata only, or names them for a dry run

func ReclaimBranches

func ReclaimBranches(repo string, dryRun bool, records []BranchRecord) ([]string, error)

Deletes every branch of an audit whose verdict deletes it, or names them for a dry run

func RefSafe

func RefSafe(name string) string

A tree name made one valid part of a ref

func Shallow

func Shallow(ctx context.Context, repo string, timeout time.Duration) bool

Whether the repository is a shallow clone, whose push state can't be told. false when git can't tell

func SizeMB

func SizeMB(ctx context.Context, path string, timeout time.Duration) *int

Megabytes a folder takes, through du where there is one, otherwise by walking it. nil when it could not be measured in time or before the context ended

func StagedState

func StagedState(tree string) (string, error)

Digest of what the tree has staged, unchanged by a refresh of file times and moved by any add or remove

func State

func State(tree Tree) string

How a tree stands as its session ends: dirty, unpushed, or empty for one clean and pushed, or gone

func StillInUse

func StillInUse(tree string, grace time.Duration, exclude ...int) bool

Whether a process other than this one still works in the tree once the grace passed

func SubmoduleWork

func SubmoduleWork(ctx context.Context, tree string, gitlinks bool) (string, error)

Why a submodule keeps the tree: a repository of submodules that removing the tree deletes, or a submodule whose work only its commit reference holds, any gitlink at all when gitlinks is set. Empty when none does. Its runs only read, and stop once the context ends

func Summary

func Summary(repo string, sizeBelow int) (int, *int)

How many trees .worktrees holds and the megabytes they take, not measured once they are at least sizeBelow

func Tier

func Tier(name string, hours float64) float64

Idle hours before a tree may be reclaimed: the base tier for trees icebath named, three times it for the user's

func Unpushed

func Unpushed(tree string, timeout time.Duration) bool

Unpushed reports whether the tree's branch has commits that are missing from every remote branch, or from the trunk when there are no remotes. It returns true when git can't tell

Types

type Archive

type Archive struct {
	Ref     string
	Name    string
	Sha     string
	Created time.Time
	Meta    ArchiveMeta
	// Digest of the staged entries of the index it captured, set as it is made
	Staged string
}

One archive of a removed tree

func Archives

func Archives(ctx context.Context, repo string) []Archive

Every archive of the repository, newest first. None when git can't list them or the context ended first

func Find

func Find(repo, name string) *Archive

Archive of the name or ref, otherwise the newest archive of a tree of that name. nil when there is none

func Snapshot

func Snapshot(repo, tree, reason string, maxMB float64) (*Archive, error)

Archives everything of the tree, tracked, staged and untracked, through a temporary index, never touching its own index or files: a commit of its files whose parents are its HEAD and a commit of its index, under a ref of its name

type ArchiveError

type ArchiveError struct{ Message string }

Failure to snapshot or restore a tree, after which it must be kept

func (*ArchiveError) Error

func (e *ArchiveError) Error() string

type ArchiveMeta

type ArchiveMeta struct {
	Version  int    `json:"version"`
	Name     string `json:"name"`
	Path     string `json:"path"`
	RepoRoot string `json:"repo_root"`
	// Branch it had checked out, empty when detached
	Branch      string    `json:"branch"`
	Head        string    `json:"head"`
	IndexCommit string    `json:"index_commit"`
	Created     int64     `json:"created"`
	Reason      string    `json:"reason"`
	Skipped     []Skipped `json:"skipped_untracked"`
}

What an archive's commit message keeps of the tree

type ArchiveRemoval

type ArchiveRemoval struct {
	Ref string
	// Branch kept with the tree gone, empty for none
	KeptBranch string
	Removed    bool
	// Why the tree was kept after its archive
	WhyKept string
}

What archiving then removing one tree came to

type ArchivedTree

type ArchivedTree struct {
	Ref     string `json:"ref"`
	Removed bool   `json:"removed"`
	WhyKept string `json:"why_kept,omitempty"`
}

What the sweep did with one tree it archived

type BranchRecord

type BranchRecord struct {
	Name    string `json:"name"`
	Verdict string `json:"verdict"`
	Reason  string `json:"reason"`
}

One local branch with its verdict, delete or keep

type Decision

type Decision struct {
	Name     string  `json:"name"`
	Path     string  `json:"path"`
	Action   string  `json:"action"`
	Reason   string  `json:"reason"`
	AgeHours float64 `json:"age_hours"`
	Mtime    float64 `json:"mtime"`
	Force    bool    `json:"force"`
	Verdict  string  `json:"verdict"`
	Lock     string  `json:"lock_state,omitempty"`
	SizeMB   *int    `json:"size_mb"`
	Tier     float64 `json:"tier_hours"`
	Planned  float64 `json:"planned_at"`
	// contains filtered or unexported fields
}

One planned action of the sweep for a tree, with what it was decided on

type ExternalTree

type ExternalTree struct {
	Path    string `json:"path"`
	Branch  string `json:"branch"`
	Locked  bool   `json:"locked"`
	Missing bool   `json:"missing"`
}

A worktree registered outside .worktrees, shown and never touched

func External

func External(ctx context.Context, repo string) ([]ExternalTree, error)

External returns the repository's worktrees registered outside .worktrees, with the missing ones flagged.

type Hooks

type Hooks struct {
	// Why the feature still needs the tree, such as a scheduled job that runs in it. Empty when it doesn't
	Holds func(ctx context.Context, tree string) string
	// Lets go of what the feature keeps rooted in the tree, such as language servers, before the tree is removed
	Release func(tree string)
	// Hears of a repository a tree was made or restored in, such as to keep its trees pruned on a schedule
	Recorded func(ctx context.Context, repo string)
	// Reclaims what the feature keeps for checkouts that are gone, once trees were pruned
	Reclaim func(ctx context.Context)
}

What a feature that works in trees adds to their upkeep, each part left nil when it has none

type Leftover

type Leftover struct {
	Path    string `json:"path"`
	Branch  string `json:"branch"`
	Commits int    `json:"commits"`
	Dirty   bool   `json:"dirty"`
	Pruned  bool   `json:"pruned"`
	Archive string `json:"archive,omitempty"`
	// What git could not tell, so the zero counts are no proof the subagent did nothing
	Failed bool   `json:"inspection_failed,omitempty"`
	Note   string `json:"note,omitempty"`
}

What a subagent left in its tree once it finished: the commits it made, whether the tree holds uncommitted work, and whether the tree was removed, archived first when it held work

type Profiles

type Profiles struct {
	Worktrees *Worktrees
}

Trees as profiles are renamed: the record a profile keeps of its trees moves with its folder, so the trees of repositories inside the folder are recorded, and linked by git, at where they lie now

func (Profiles) Delete

func (Profiles) Delete(string) error

The record lies in the profile's folder, which goes with it

func (Profiles) Move

func (p Profiles) Move(old, renamed string) ([]string, error)

Records the trees that lay in the old folder at their places in the new one, and has git link each repository and its trees again there

type Registrant

type Registrant interface {
	Hooks() Hooks
}

type Registry

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

func (*Registry) Hooks

func (r *Registry) Hooks() Hooks

Hooks of every registrant, run in turn

func (*Registry) Host

func (r *Registry) Host() *Worktrees

Host the registry is nested in, through which plugins make, archive and reclaim trees

func (*Registry) Register

func (r *Registry) Register(registrant Registrant)

type RemovalBlock

type RemovalBlock struct {
	Action string
	Reason string
}

Why a tree may not be removed and how it is kept

func Blocked

func Blocked(tree string) *RemovalBlock

Why removing a clean tree would lose something no branch holds, nil when it is safe

func Blocker

func Blocker(ctx context.Context, tree string, skipped []Skipped, gitlinks bool) *RemovalBlock

Blocker returns why removing the tree would lose something that no branch or archive has: a submodule's work, a nested repository, its own ref, or untracked files too large for its archive. It is nil when removal is safe. Anything that can't be checked keeps the tree, including checks stopped when the context ends.

func Preflight

func Preflight(tree string, maxMB float64) *RemovalBlock

Why archiving and removing the tree would lose something, nil when it is safe

type Request

type Request struct {
	Name   string
	Owner  int
	Launch bool
}

Asks for a tree: its name, none for a generated one, the process it is locked for, and whether it is a launch's own tree, kept for the session working in it once the launch ends and recorded so a resume makes it again

type Restored

type Restored struct {
	Path string
	// Branch it is on, or that moved on or is checked out elsewhere when it is detached
	Branch   string
	Detached bool
	Head     string
	Ref      string
	Skipped  []Skipped
}

What a restore made

type Settings

type Settings struct {
	// Idle trees holding work are archived and removed
	ArchiveDirty bool
	// Largest untracked file in MB an archive takes, any for zero
	ArchiveMaxFileMB float64
	// Days an archive is kept, for good for zero, and that length in words as a user is told it: as the settings write it when they name
	// a variable, so no log shows the variable's value
	ArchiveRetentionDays float64
	ArchiveRetention     string
	// GB every tree together may take, any for zero
	MaxTotalGB float64
}

Settings of the profile the archive and the sweep follow

func (Settings) Classic

func (s Settings) Classic() bool

Whether the sweep acts as the pruner before archives: nothing is archived and no size is kept

type Skipped

type Skipped struct {
	Path  string `json:"path"`
	Bytes int64  `json:"bytes"`
}

Untracked file left out of an archive for being larger than it takes

type Tree

type Tree struct {
	Path   string `json:"path"`
	Branch string `json:"branch"`
	Repo   string `json:"repo_root"`
	// What it was branched from
	Base string `json:"-"`
}

A tree made for a session to work in

func (Tree) SubagentNote

func (t Tree) SubagentNote() string

What the subagent is told of the tree it works in

type TreeRecord

type TreeRecord struct {
	Name    string  `json:"name"`
	Path    string  `json:"path"`
	Branch  string  `json:"branch"`
	AgeDays float64 `json:"age_days"`
	SizeMB  *int    `json:"size_mb"`
	Verdict string  `json:"verdict"`
	Reason  string  `json:"reason"`
	// Untracked files archived before the tree goes
	Untracked []string `json:"untracked"`
}

One tree under .worktrees with its reclaim verdict

type Worktrees

type Worktrees struct {
	Registry
	// contains filtered or unexported fields
}

func New

func New(cfg *config.Config, stores *sessions.Sessions, host *terminal.Terminal) *Worktrees

func (*Worktrees) ArchiveThenRemove

func (w *Worktrees) ArchiveThenRemove(cfg *config.Config, repo, tree, reason string, maxMB float64, unlock bool, heads func() (map[string]string, bool), keepBranch bool, exclude ...int) (*ArchiveRemoval, error)

Archives the tree, then removes it unless it changed meanwhile, is in use, or holds what the archive lacks. Its branch goes too unless it is kept, protected or published. heads gives origin's branches, read in the profile's environment when nil

func (*Worktrees) Audit

func (w *Worktrees) Audit(ctx context.Context, cfg *config.Config, repo string, sizes bool, olderThanDays *float64,
	acting bool) ([]TreeRecord, error)

Classifies every tree under .worktrees without changing anything, several trees side by side; olderThanDays, when given, only keeps reapable trees younger than it. Acting for a prune, a shallow clone is deepened first and origin asked for its branches again. A read leaves the clone as it is, a shallow one noted on the verdicts that keep work not found upstream, and takes origin's branches as a read begun within headsFresh found them. Verdicts reached on a shallow clone's history are not kept. Its runs of git stop once the context ends, which it then answers

func (*Worktrees) Branches

func (w *Worktrees) Branches(ctx context.Context, cfg *config.Config, repo string, acting bool) ([]BranchRecord, error)

Classifies every local branch by what of it upstream holds: deleted when merged or every commit is the same change as one upstream, and not checked out anywhere. Kept when git can't tell. Acting for a prune, a shallow clone is deepened first in the profile's environment; a read leaves the clone as it is, a shallow one noted on the verdicts that keep commits not found upstream. Its runs of git stop once the context ends

func (*Worktrees) Create

func (w *Worktrees) Create(ctx context.Context, cfg *config.Config, repo string, request Request, sync bool, say func(string)) *Tree

Create makes an isolated repository tree under .worktrees on its own icebath/<name> branch, from the newest base when the settings fetch first, with its .worktreeinclude entries copied in and locked for the owner. It returns the tree, or nil when it could not be created, and reports each step through say.

func (*Worktrees) Execute

func (w *Worktrees) Execute(cfg *config.Config, repo string, plan []*Decision) ([]string, map[string]bool, []ArchivedTree)

Carries out a plan, checking each tree again right before it is archived or removed: the trees kept for their work, the branches kept, and what was archived

func (*Worktrees) FinishSubagent

func (w *Worktrees) FinishSubagent(cfg *config.Config, tree Tree) Leftover

Inspects a finished subagent's tree: an empty clean one is removed with its branch, one holding work archived and removed with its branch kept, as the profile's settings allow

func (*Worktrees) Leave

func (w *Worktrees) Leave(cfg *config.Config, tree Tree, choice string, say func(string), exclude ...int) bool

What becomes of a launch's own tree as the launch ends, after what the user chose: settled when no session waits to work in it, no other process works in it and nothing else serves it, otherwise kept for the session that worked in it last, saying so. Whether it is gone

func (*Worktrees) Plan

func (w *Worktrees) Plan(cfg *config.Config, repo string, hours float64, olderThanDays *float64) []*Decision

What the sweep would do with every tree under .worktrees, changing nothing: olderThanDays, when given, only holds back removals

func (*Worktrees) PruneStale

func (w *Worktrees) PruneStale(ctx context.Context, cfg *config.Config, repo string, hours float64) error

PruneStale removes the repository's trees that were idle past their tier and the branches without a tree, archiving trees with work first unless the settings ask for the pruner's behavior from before archives. Then it reclaims the data that features saved for removed checkouts

func (*Worktrees) Reclaim

func (w *Worktrees) Reclaim(cfg *config.Config, repo string, dryRun bool, records []TreeRecord) []string

Removes every tree of an audit whose verdict reaps it, its untracked scratch archived first, or names them for a dry run

func (*Worktrees) Record

func (w *Worktrees) Record(cfg *config.Config, tree Tree)

Remembers a tree a launch made or a session moved into, never failing: the trees of repositories gone are dropped

func (*Worktrees) Recreate

func (w *Worktrees) Recreate(cfg *config.Config, cwd, session string) string

Makes again the recorded tree a resumed session's missing folder names, on its branch when that is still there, and keeps it for the session, the newest session working in it when none is named: the tree, empty when it can't

func (*Worktrees) Register

func (w *Worktrees) Register(registrant Registrant)

Adds a registrant. A registry nested in the host reaches it through Host

func (*Worktrees) Restore

func (w *Worktrees) Restore(ctx context.Context, repo, name, target string) (*Restored, error)

Makes the archived tree again at its first path or the target, with its exact files and index

func (*Worktrees) Retain

func (w *Worktrees) Retain(repo, tree, session string) bool

Locks the tree again for the session working in it, once the process that made it ends

func (*Worktrees) Settings

func (w *Worktrees) Settings(cfg *config.Config) Settings

Settings of the profile, none below zero as their bounds keep them

func (*Worktrees) Settle

func (w *Worktrees) Settle(cfg *config.Config, tree Tree, choice string, say func(string), exclude ...int)

Carries out what the user chose for a tree they are done with, otherwise removes it with its branch when it is clean and pushed and nothing of it would be lost, saying why one is kept. The excluded processes don't count as working in it

func (*Worktrees) Subagent

func (w *Worktrees) Subagent(ctx context.Context, cwd, id string) *Tree

Tree a subagent works in apart from the checkout of the agent that delegated to it: .worktrees/subagent-<id> of the checkout's repository on a branch icebath-subagent/subagent-<id> off its HEAD. nil outside git or when it could not be made

func (*Worktrees) Sweep

func (w *Worktrees) Sweep(cfg *config.Config, repo string, hours float64) error

Sweep plans and runs one repository's sweep, then deletes archives past their retention and branches without a tree

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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