stack

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 16 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 stack owns everything about a worktree's docker stack: how it is named, which ports it gets, and how it goes up and down.

Index

Constants

View Source
const DefaultStride = 1

Variables

This section is empty.

Functions

func AllocatePort

func AllocatePort(defaultPort, index, stride, projectOffset int) (int, error)

func BranchAt added in v0.23.0

func BranchAt(paths map[string]string, path string) string

BranchAt is the branch recorded at path in paths (branch to path), empty when none is.

func DriftRemedy added in v0.27.0

func DriftRemedy(wt Worktree) string

DriftRemedy says how to give a switched worktree's stack to the branch it holds. wtm switch refuses a worktree wtm created, named after its branch.

func PortsOverride

func PortsOverride(allocations []Allocation, every bool) string

PortsOverride rebases the ports a project wrote as literals, which no environment variable can reach. Compose appends `ports` when merging, so the list is replaced with !override, restating every port of that service.

func ProjectName

func ProjectName(repoName string, index int, branch string) string

ProjectName is the compose project wtc gives a worktree. Mirrors its composeProjectName/sanitize pair, which is the only way to address that stack's containers and volumes from the outside.

func ProjectPrefix added in v0.2.0

func ProjectPrefix(repoName string, index int) string

ProjectPrefix is what every compose project of a worktree at this index starts with, whatever its branch: how leftovers of a removed worktree are spotted before the index is handed to a new one.

func Stride

func Stride(projectDir string) int

Stride reads the project's port stride, from .wtcrc.json when present so an existing setup keeps working untouched.

func StripEnvOverrides added in v0.10.0

func StripEnvOverrides(worktreeDir string) error

StripEnvOverrides takes the port block back out, leaving every other line alone. An adopted worktree survives its stack, and a block naming ports nothing listens on any more is worse than no block at all.

func WorktreePrefix added in v0.9.0

func WorktreePrefix(repoName string) string

WorktreePrefix is what every worktree stack of a repository shares, and what tells its volumes and images from everything else docker holds. It is sanitised as compose does: the raw name found nothing at all for MyApp.

func WorktreesRoot added in v0.4.6

func WorktreesRoot(repoDir string) string

WorktreesRoot is where the worktrees wtm creates itself live. What git lists outside it, `claude -w` worktrees or a manual `git worktree add`, has no index, no provisioned .env and no stack until `wtm adopt` gives it those.

func WriteEnvOverrides

func WriteEnvOverrides(worktreeDir string, allocations []Allocation) error

WriteEnvOverrides rewrites the port block of the worktree's .env in place. docker compose reads that file from the project directory on its own, which is how the rebased ports reach the containers.

Types

type Allocation

type Allocation struct {
	Service   string
	Var       string
	HostIP    string
	Port      int
	Container string
}

func Allocate

func Allocate(services []compose.ServicePort, index, stride, projectOffset int) ([]Allocation, error)

Allocate rebases every published port of the compose file, variable or literal. A literal cannot be overridden through the environment, so it goes through a generated compose file instead; see PortsOverride.

type Client

type Client struct {
	Runner execx.Runner
	Dir    string // project repository root
	Out    io.Writer
	// Managed carries the branches holding a recorded index, which is what
	// makes a worktree outside .worktrees visible to Worktrees. Nothing in such
	// a worktree's path tells it apart from a stranger's.
	Managed map[string]bool
	// Paths is the registry's worktree_paths, branch to path. It names the
	// branch of an adopted worktree on a detached HEAD, a rebase stopped on a
	// conflict for one, which git lists with no branch at all.
	Paths map[string]string
	// contains filtered or unexported fields
}

func (*Client) Abandoned added in v0.13.0

func (c *Client) Abandoned(ctx context.Context) ([]string, error)

Abandoned lists the directories under WorktreesRoot that still carry a worktree's .git pointer file while `git worktree list` no longer names them: what a pruned or hand-deleted administrative directory leaves on disk. Every other command keys off git's listing, so nothing else can see them.

func (*Client) All added in v0.10.0

func (c *Client) All(ctx context.Context) ([]Worktree, error)

All lists every linked worktree of the repository, whether wtm knows it or not. `wtm adopt` is what it exists for: a worktree has to be seen once before it can be brought in, and Worktrees hides exactly the ones worth adopting.

func (*Client) Down

func (c *Client) Down(ctx context.Context, project, worktreeDir string) error

Down takes a worktree's stack away for good, its volumes included. The anonymous ones carry no compose label: left by a down, nothing finds them again, not even the next up, which mounts fresh ones.

func (*Client) FindByBranch

func (c *Client) FindByBranch(ctx context.Context, branch string) (Worktree, error)

func (*Client) Manage added in v0.27.0

func (c *Client) Manage(branch string)

Manage makes branch's worktree visible to the listing before its index is recorded, which an adoption or a switch needs to find the worktree it acts on.

func (*Client) RecordedAt added in v0.27.0

func (c *Client) RecordedAt(wt Worktree) string

RecordedAt is the branch wt's stack is recorded under when wt now holds another branch, "" otherwise. A branch is checked out in one worktree at most, so naming the branch it holds names that stack.

func (*Client) Rekey added in v0.27.0

func (c *Client) Rekey(old, branch string)

Rekey files old's recorded path under branch, as config.RekeyWorktree does in the registry: this copy would otherwise still list the worktree as old's.

func (*Client) Stop added in v0.27.0

func (c *Client) Stop(ctx context.Context, project, worktreeDir string) error

Stop halts a worktree's stack and keeps its containers, so the next up starts them again with the anonymous volumes they had. A down there left those volumes behind on every stop, a batch per stop and start.

func (*Client) Up

func (c *Client) Up(ctx context.Context, project, worktreeDir string, files, env, services []string) error

Up brings a worktree's stack up, env ("API_PORT=20087") interpolating ahead of the worktree's .env. services narrows what starts, empty for all. Naming some is additive on a stack already up, so taking a service away needs a stop first.

func (*Client) Worktrees

func (c *Client) Worktrees(ctx context.Context) ([]Worktree, error)

Worktrees lists the worktrees wtm manages, in git's own order: the ones it created, plus the adopted ones its registry carries an index for. One switched outside wtm is listed under the branch its stack is recorded under.

type Worktree

type Worktree struct {
	Index  int
	Pos    int
	Path   string
	Branch string
	// git refuses to remove a locked worktree even with one --force. A lock can
	// carry no reason, so the flag cannot be inferred from LockReason.
	Locked     bool
	LockReason string
	// Detached says HEAD points straight at Head instead of at a branch. git
	// then names no branch, so Branch is derived from the path: under
	// WorktreesRoot wtm always creates <root>/<branch>, and elsewhere only the
	// registry's recorded path can say which branch stood there.
	Detached bool
	Head     string
	// UnderRoot says the worktree sits where wtm creates its own. An adopted
	// one does not, which is what Remove reads to leave the directory alone.
	UnderRoot bool
	// Holds is the branch checked out there when it is not Branch: the
	// worktree was switched outside wtm, and Branch is the one its stack is
	// still recorded under. Empty otherwise.
	Holds string
}

Worktree carries two very different numbers. Index is the stable one ports and the compose project name derive from, filled by internal/index. Pos is where git listed it, which resorts alphabetically: a hint for that resolver.

func (Worktree) ShortHead added in v0.4.7

func (w Worktree) ShortHead() string

ShortHead abbreviates Head the way git prints it in its own listings.

Jump to

Keyboard shortcuts

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