workspace

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 13 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 workspace prepares the directory an agent works in: a git worktree on the task's branch (<branch_prefix><task>) outside the repository (mode "worktree"), a detached worktree at the commit a review run inspects, or the project itself ("same"). Nothing is written into the project's working tree; `git worktree add` only adds metadata under the repository's .git directory (and a base on a remote is fetched into its remote-tracking ref).

Index

Constants

View Source
const (
	Worktree = "worktree"
	Same     = "same"
)

Modes.

View Source
const (
	Removed = "removed"
	Gone    = "gone" // deleted before (pruned from git's list), or an empty directory left behind
	// Replaced: the path is now another linked worktree of the repository, on another
	// branch. It is not the task's any more and is left alone.
	Replaced = "replaced"
)

Outcomes of Remove.

View Source
const DefaultBranchPrefix = "feature/"

DefaultBranchPrefix is the branch prefix of a project that sets none.

Variables

View Source
var PushBlock = [][2]string{
	{"url.otag-blocked://.pushInsteadOf", ""},
	{"protocol.otag-blocked.allow", "never"},
}

PushBlock is the git config, given through the environment, that makes every push of a run fail inside git itself (internal/run sets it on every run): an empty pushInsteadOf prefix rewrites every push URL to the otag-blocked scheme, which may never be used. Push removes it again for the one push otag makes itself.

View Source
var RemoteHeadArgs = []string{"for-each-ref", "--format=%(refname:short) %(symref:short)", "refs/remotes/*/HEAD"}

RemoteHeadArgs is the git command (after -C dir) whose output ParseRemoteHeads reads.

Functions

func BranchExists

func BranchExists(ctx context.Context, projectPath, branch string) bool

BranchExists reports whether the local branch exists in the repository at projectPath.

func BranchFor

func BranchFor(prefix, taskID string) string

BranchFor returns the branch of a new task worktree: prefix + task id.

func BranchSHA added in v0.1.2

func BranchSHA(ctx context.Context, projectPath, branch string) (string, error)

BranchSHA returns the commit the local branch points at.

func Changes

func Changes(ctx context.Context, dir string) (string, error)

Changes returns the uncommitted changes of the work tree at dir (`git status --porcelain`; "" when clean).

func CheckBranchPrefix added in v0.1.2

func CheckBranchPrefix(prefix string) error

CheckBranchPrefix reports whether prefix makes valid branch names: prefix followed by a task id must pass `git check-ref-format --branch` ("" and "feature/" do, "feature//", "-x", "a..b/" and "x.lock/" do not).

func CheckRef added in v0.1.2

func CheckRef(ref string) error

CheckRef reports whether ref is a plausible ref or commit to hand to git as a base: a ref name `git check-ref-format` accepts (one level allowed: "main", "origin/dev", "refs/remotes/origin/dev"), a commit id, or HEAD; never something git would read as an option.

func CommitsSince

func CommitsSince(ctx context.Context, dir, base string) (int, error)

CommitsSince counts commits on HEAD of dir that are not in base.

func DefaultBranch

func DefaultBranch(ctx context.Context, projectPath string) string

DefaultBranch returns the branch work is merged into: the local branch origin/HEAD names, else the branch the project checkout is on ("" when detached).

func DeleteBranch

func DeleteBranch(ctx context.Context, projectPath, branch, target string) (int, error)

DeleteBranch deletes branch if target contains all of its commits (the semantics of `git branch -d`, against target instead of the checkout's HEAD). It returns the number of unmerged commits; with any, nothing is deleted. Both commits are read once: the deletion is one ref transaction that verifies target and deletes branch only if both still point at them, so a branch or target that moved meanwhile keeps the branch. A symbolic branch ref (it would delete what it points at) and a branch checked out in a worktree (the main checkout included) are refused.

func ForceDeleteBranch added in v0.1.2

func ForceDeleteBranch(ctx context.Context, projectPath, branch string) error

ForceDeleteBranch deletes branch whether or not any branch contains it (`git branch -D`): a squash or rebase merge never makes a task branch an ancestor of the target. It is still one ref transaction on the commit read first, and a symbolic ref or a branch checked out in a worktree is refused.

func HasRemote added in v0.1.2

func HasRemote(ctx context.Context, projectPath, name string) bool

HasRemote reports whether name is a configured remote of the repository.

func Head(ctx context.Context, dir string) (sha string, dirty bool, err error)

Head returns the commit at HEAD of dir and whether the work tree has uncommitted changes.

func MergeBase

func MergeBase(ctx context.Context, projectPath, branch string) string

MergeBase returns the merge base of branch and the project's HEAD: the best guess of a task's base when none was recorded.

func ParseRemoteHeads added in v0.1.2

func ParseRemoteHeads(out string) string

ParseRemoteHeads picks the remote default branch from RemoteHeadArgs' output: origin's, else the first one ("" = none).

func RemoteBranch added in v0.1.2

func RemoteBranch(ctx context.Context, projectPath, remote, branch string) (string, error)

RemoteBranch returns the commit the remote's branch is at ("" when it has no such branch); it asks the remote (`git ls-remote`).

func RemoteHead added in v0.1.2

func RemoteHead(ctx context.Context, projectPath string) string

RemoteHead returns the remote default branch of the repository as a remote-tracking ref (origin/main from refs/remotes/origin/HEAD; origin first, else the first remote that has one), "" when no remote names one.

func RemoteTracking added in v0.1.2

func RemoteTracking(ctx context.Context, projectPath, base string) (remote, branch string, ok bool)

RemoteTracking splits base into a configured remote and its branch when base is a remote-tracking ref (<remote>/<branch> or refs/remotes/<remote>/<branch>). The longest configured remote that prefixes base wins.

func Remove

func Remove(ctx context.Context, s RemoveSpec) (string, error)

Remove removes the worktree of a task (`git worktree remove`) and returns what it found: Removed, Gone or Replaced. Uncommitted changes are a *DirtyError unless Force, which discards them. A path that no longer exists is only pruned from git's list; an empty directory (what an interrupted removal leaves) is deleted first, as it has nothing to lose. A path outside Root, the main checkout, anything that is not a linked worktree of the repository, and a worktree with a detached HEAD where a branch was expected (s.Branch set) are refused. The branch stays.

func ResolveBase added in v0.1.2

func ResolveBase(ctx context.Context, projectPath, base, lockDir string) (ref, sha string, err error)

ResolveBase returns the ref and commit a new task branch starts from. An empty base is the project's HEAD. A remote-tracking ref (<remote>/<branch> or refs/remotes/<remote>/<branch>, where <remote> is a configured remote) is fetched first (`git fetch <remote> <branch>` into its remote-tracking ref); a failed fetch is an error, never a silent fallback to a stale ref. Any other ref or commit is used as it is and must resolve to a commit. The fetch and the read of the fetched commit run under the repository's lock in lockDir (lockRepo), and a fetch that fails is tried once more (another git, outside otag, may have moved the ref meanwhile).

func Uncommitted

func Uncommitted(ctx context.Context, s RemoveSpec) (string, error)

Uncommitted returns the uncommitted changes of the task worktree s ("" when it is clean, gone or not the task's any more); errors are Remove's refusals.

func Unmerged

func Unmerged(ctx context.Context, projectPath, branch, target string) (int, error)

Unmerged counts the commits of branch that target does not contain (0 = merged).

Types

type DirtyError

type DirtyError struct {
	Path   string
	Status string // `git status --porcelain`
}

DirtyError means a worktree has uncommitted changes (tracked or untracked files).

func (*DirtyError) Error

func (e *DirtyError) Error() string

type PushRejected added in v0.1.2

type PushRejected struct{ Output string }

PushRejected means the remote refused the update: not a fast-forward, or a lease that no longer holds. Nothing changed on the remote.

func (*PushRejected) Error added in v0.1.2

func (e *PushRejected) Error() string

type PushResult added in v0.1.2

type PushResult struct {
	SHA    string `json:"sha"`    // the commit the remote branch now has
	Output string `json:"output"` // git's porcelain report
}

PushResult is a successful push.

func Push added in v0.1.2

func Push(ctx context.Context, s PushSpec) (PushResult, error)

Push pushes the commit the local branch is at to refs/heads/<branch> of the remote (`git push <remote> <sha>:refs/heads/<branch>`): never another ref, no tags, no submodules. The commit is read first, so the pushed commit is exactly the one returned even when the branch moves meanwhile. The project's hooks run; otag's push block (PushBlock) is left out of git's environment.

type PushSpec added in v0.1.2

type PushSpec struct {
	ProjectPath string
	Remote      string
	Branch      string // the local branch; pushed to the remote branch of the same name
	// Lease, when set, forces the push with --force-with-lease: the remote branch must
	// still be at *Lease ("" = it must not exist). Nil is a plain (fast-forward) push.
	Lease *string
}

PushSpec is one push of a task branch.

type RemoveSpec

type RemoveSpec struct {
	ProjectPath string
	Path        string // the recorded worktree path
	// Branch is the task's branch: the worktree must still be on it. "" is a detached
	// review checkout: the worktree must still be detached.
	Branch string
	// Root is otag's worktree root of the project (<data>/worktrees/<project>): Path,
	// with symlinks resolved, must be inside it.
	Root  string
	Force bool // discard uncommitted changes
}

RemoveSpec names the worktree of a task that is to be removed.

type Result

type Result struct {
	Mode    string `json:"mode"`
	Path    string `json:"path"`
	Branch  string `json:"branch,omitempty"`
	BaseRef string `json:"base_ref,omitempty"`
	BaseSHA string `json:"base_sha,omitempty"`
	Reused  bool   `json:"reused"` // an existing worktree of the task was reused
	// BranchExisted: the branch predates this call (reused worktree, or recreated from a
	// surviving branch); BaseSHA is then not the task's base and the caller keeps the
	// recorded one (or MergeBase).
	BranchExisted bool `json:"branch_existed"`
}

Result is a prepared workspace.

func Prepare

func Prepare(ctx context.Context, s Spec) (Result, error)

Prepare creates (or reuses) the workspace.

type Spec

type Spec struct {
	Mode        string
	ProjectPath string
	Path        string // worktree path (mode worktree)
	Branch      string // worktree branch (mode worktree); "" with Detach
	// Base is the ref a new branch starts from ("" = the project's HEAD); see
	// ResolveBase. Only used when the worktree and its branch are created.
	Base string
	// Known: the task recorded Branch as its own before (tasks.branch). A branch that
	// exists although the task never had it is someone else's: Prepare refuses it
	// instead of taking it over.
	Known bool
	// Detach, when set, is the commit a detached worktree is created at (a review run
	// inspects the reviewed task's head); no branch is created.
	Detach string
	// LockDir is where the lock that serializes base fetches of one repository across
	// processes lives (<data>/locks); "" takes no lock.
	LockDir string
}

Spec describes the workspace of a task.

Jump to

Keyboard shortcuts

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