Documentation
¶
Overview ¶
Package worktrees finds the linked git worktrees under the configured roots and tells a finished one (its pull request merged or closed) from one that still holds work. A finished worktree that holds nothing but build output is removed whole; one that also holds uncommitted changes or unpushed commits keeps them and loses only its build output, as does an unfinished one left untouched for the grace. A live session's worktree and a locked one are never touched. A worktree whose gitfile names a repository that no longer exists is an orphan (Dangling).
Index ¶
- Constants
- Variables
- func Allocated(fi fs.FileInfo) int64
- func Containing(dir string) (string, error)
- func Dangling(dir string) bool
- func Find(roots []string) []string
- func FindHarness(places []string) []string
- func Linked(dir string) bool
- func MainCheckout(ctx context.Context, git Git, cwd string) (string, error)
- func NestedCheckout(dir string) (bool, error)
- func NestedSessionPath(p string) bool
- func NestedSessions(projects string) []string
- func RemoteStatus(ctx context.Context, git Git, t Tree, hadPull bool) (gone, published bool, err error)
- func RemoveAll(path string) error
- func RepoOf(url string) string
- func SessionPath(root, repo, branch string) (string, error)
- func SessionRemovable(ctx context.Context, git Git, dir string) (bool, error)
- func Size(path string) int64
- func Writable(path string) error
- type Git
- type Tree
- type Verdict
- func Automatic(t Tree, pull *github.HeadPull, gone, harness, live, pushed bool, now time.Time) Verdict
- func AutomaticPressure(t Tree, pull *github.HeadPull, gone, harness, live, pushed bool, now time.Time, ...) Verdict
- func Decide(t Tree, pull *github.HeadPull, live, pushed bool, now time.Time) Verdict
Constants ¶
const ( Keep = "keep" Remove = "remove" // Clean removes only the build output of a finished worktree whose // real work stays. Clean = "clean" )
The actions on a worktree.
const ( KeptLive = "live" KeptLocked = "locked" KeptNoOrigin = "no GitHub origin" KeptDetached = "detached HEAD" KeptNoPull = "no pull request" KeptOpen = "pull request open" KeptClosed = "closed within the grace" KeptTouched = "touched within the grace" KeptWork = "holding only work" )
The kinds of kept worktrees.
const DetachedIdle = 72 * time.Hour
DetachedIdle is how long a worktree on a detached HEAD (a release's or a pull request's proof checkout) must be untouched before it counts as finished: no pull request says when its work ended.
const Grace = 6 * time.Hour
Grace is how long a pull request must be closed, and a worktree's HEAD and index untouched, before the worktree counts as finished: its worker may still prove the merge from it.
const SessionIdle = 7 * 24 * time.Hour
SessionIdle is how long an unarchived desktop session with no process in its worktree keeps that worktree live after its last activity.
Variables ¶
var Regenerable = []string{
"node_modules", "dist", "dist-types", "bin", "build", "out", "target",
".turbo", ".next", ".cache", ".yarn/cache", "coverage",
"__pycache__", ".pytest_cache", ".venv",
}
Regenerable are the directories a build, an install or a test run writes and the next one writes again, by name or, with a slash, by their last path elements: untracked or ignored, one of these is build output wherever it lies.
Functions ¶
func Containing ¶
Containing returns the linked worktree dir lies in, at or above it: the nearest .git decides, so a checkout inside a worktree is not that worktree. It is "" for none; an error says that could not be told, and a caller protecting work treats that as a worktree.
func Dangling ¶
Dangling reports whether dir is a linked worktree whose gitfile names a gitdir that no longer exists: its repository went, or pruned it, and git cannot read it.
func Find ¶
Find lists the linked worktrees under roots, sorted; a root that does not exist has none. It does not look inside a worktree for more.
func FindHarness ¶ added in v0.171.0
FindHarness finds harness worktrees in checkouts under the supplied places, without descending into build output or repository metadata.
func Linked ¶ added in v0.171.0
Linked reports whether a directory is a linked checkout, including a dangling one; a main checkout is not eligible for automatic removal.
func MainCheckout ¶
MainCheckout resolves the common git directory even when cwd is a linked worktree.
func NestedCheckout ¶ added in v0.171.0
NestedCheckout protects checkouts inside a worktree, including harness worktrees and repositories under ignored build directories.
func NestedSessionPath ¶
NestedSessionPath reports targets under a checkout's harness worktree area.
func NestedSessions ¶
NestedSessions finds linked worktrees inside clones, without walking their copies.
func RemoteStatus ¶ added in v0.171.0
func RemoteStatus(ctx context.Context, git Git, t Tree, hadPull bool) (gone, published bool, err error)
RemoteStatus verifies the branch and publication against current remote heads. Local tracking refs and closed pull requests are not publication evidence: they can outlive an unmerged branch deleted from the remote.
func RemoveAll ¶ added in v0.156.0
RemoveAll removes path and everything under it, read-only directories included (Writable).
func SessionPath ¶
SessionPath places a branch beside other sessions of the main checkout. Resolve existing parents to prevent a symlink from hiding a nested target.
func SessionRemovable ¶
SessionRemovable requires a clean, unlocked tree whose complete change is already integrated in the default branch, including a squash merge. Failed or conflicting merge simulations conservatively retain the tree.
func Size ¶
Size is the disk space in bytes the files under path take, as du counts it (the apparent size where the platform says no blocks).
func Writable ¶ added in v0.156.0
Writable gives the owner write and search permission on every directory under path, path included, as go clean -modcache does: a read-only one (a Go module cache copy, envtest's binaries under go/bin/k8s) refuses the unlink of what it holds, while a read-only file in a writable directory unlinks. Symbolic links are not followed, path itself included, and no change reaches outside path.
Types ¶
type Tree ¶
type Tree struct {
Dir string
// Repo is the GitHub repository of its origin remote (owner/repo), ""
// for none; Branch the branch checked out, "" when HEAD is detached;
// Pushed the branch on origin it tracks, "" for none.
Repo, Branch, Pushed string
Head string
// Locked says git worktree lock protects it.
Locked bool
// Work are the uncommitted changes that are real work: tracked
// changes and untracked paths that are no build output.
Work []string
// Build are the untracked or ignored paths (git status's units,
// directories end in /) that are build output: Regenerable ones.
Build []string
// Unpushed are the commits on HEAD no remote-tracking branch has.
Unpushed int
// Touched is when its HEAD, reflog or index last changed.
Touched time.Time
}
Tree is one linked worktree as git reports it.
func Inspect ¶
Inspect reads dir's state through git. git status runs without its optional index refresh, so the look changes nothing Touched reads.
func InspectBuildDirs ¶ added in v0.171.0
InspectBuildDirs includes additional explicitly configured build directories.
func (Tree) HeadBranch ¶
HeadBranch is the branch a pull request of t names as its head: the branch on origin it tracks, unless that is the default branch it was cut from, else the branch checked out.
type Verdict ¶
Verdict is what happens to a worktree, and why; Kept is the kind of a kept one.
func Automatic ¶ added in v0.171.0
func Automatic(t Tree, pull *github.HeadPull, gone, harness, live, pushed bool, now time.Time) Verdict
Automatic judges a worktree for the disk step. A harness worktree whose complete work is pushed can be removed without a pull request. Other worktrees need a merged pull request or a confirmed deleted remote branch. Unlike the legacy doctor trim, unfinished tasks keep their build output.
func AutomaticPressure ¶
func AutomaticPressure(t Tree, pull *github.HeadPull, gone, harness, live, pushed bool, now time.Time, pressure bool) Verdict
AutomaticPressure expires retention grace only for tasks confirmed ended. Live processes, locks, open tasks and unpublished work retain their protections.
func Decide ¶
Decide judges t: live says a session or a process works in it, pull is the newest pull request its branch opened (nil for none), pushed says its unpushed commits are the pull request's own (its head, or before it).
A worktree kept for any other reason than a live session or a lock, and untouched Grace, still loses its build output (Clean, Kept naming why it stays): the next build writes it again, and git state, branch and commits stay.