worktree

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package worktree orchestrates git worktree lifecycle operations.

Index

Constants

This section is empty.

Variables

View Source
var ErrWorktreeExists = errors.New("worktree already exists")

ErrWorktreeExists is returned when a worktree directory already exists.

Functions

func CreateSymlinks(mainRepo, wtPath string, links []string) error

CreateSymlinks creates symlinks in the worktree pointing back to files/dirs in the main repo. Skips entries that don't exist in the main repo.

func Delete

func Delete(ctx context.Context, r exec.Runner, opts DeleteOpts) error

Delete removes a worktree and prunes stale references. If the worktree directory is already gone, it prunes the stale entry instead. Branch deletion is handled by the CLI layer (may require user prompt).

func DirName added in v1.0.3

func DirName(name string) string

DirName returns the directory name used for a worktree, replacing slashes with dashes so branch-style names like "fix/foo" become "fix-foo".

func GenerateName

func GenerateName() string

GenerateName returns a random adjective-verbing-noun triple like "calm-spinning-oak".

func MainPath added in v1.0.4

func MainPath(wts []Worktree) string

MainPath returns the path of the main (first) worktree from a pre-fetched list. Git always lists the main worktree first.

func Rename added in v1.4.0

func Rename(ctx context.Context, r exec.Runner, opts RenameOpts) error

Rename renames the branch and moves the worktree directory. The move is skipped when NewPath is empty or already equal to OldPath, which renames the branch alone.

A failed move rolls the branch name back, so the worktree is left as it was found. The caller must not be inside the worktree being moved: git resolves the move against the process working directory.

The tmux session is not touched; that is handled by the CLI layer, as with Delete and branch deletion.

Types

type CreateOpts

type CreateOpts struct {
	Name         string
	Base         string
	SkipSetup    bool
	SkipSymlinks bool
	Progress     io.Writer
}

CreateOpts holds options for creating a worktree.

type DeleteOpts

type DeleteOpts struct {
	Path     string
	Progress io.Writer
}

DeleteOpts holds options for deleting a worktree.

type RenameOpts added in v1.4.0

type RenameOpts struct {
	OldBranch string
	NewBranch string
	OldPath   string
	NewPath   string
	Progress  io.Writer
}

RenameOpts holds options for renaming a worktree.

type Worktree

type Worktree struct {
	Path   string
	Branch string
	HEAD   string
	Bare   bool
}

Worktree represents a single git worktree entry.

func Create

func Create(ctx context.Context, r exec.Runner, cfg *config.Config, opts CreateOpts) (*Worktree, error)

Create creates a new worktree with optional symlinks and setup command.

func FindByName added in v1.0.4

func FindByName(wts []Worktree, name string) *Worktree

FindByName returns the worktree whose directory basename matches the given name. Returns nil if no match is found.

func FindByPath added in v1.1.0

func FindByPath(wts []Worktree, path string) *Worktree

FindByPath returns the worktree that contains the given absolute path. A path is "inside" a worktree if it equals or is a subdirectory of the worktree root. Returns nil if no match is found.

func List

func List(ctx context.Context, r exec.Runner) ([]Worktree, error)

List returns all worktrees by parsing `git worktree list --porcelain`.

Jump to

Keyboard shortcuts

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