Documentation
¶
Overview ¶
Package task holds what the daemon, the bridge and `stagent task` share about tasks (タスク): the canonical path of a source checkout (元のチェ ックアウト), which paths lie in a worktree, and running git.
Index ¶
- Constants
- func Canonical(ctx context.Context, env []string, dir string) (string, error)
- func CanonicalPath(p string) string
- func Changes(ctx context.Context, env []string, worktree string, links []string) ([]string, error)
- func CheckBranch(ctx context.Context, env []string, repo, name string) error
- func CopyFiles(ctx context.Context, j CopyJob)
- func CurrentBranch(ctx context.Context, env []string, dir string) (string, error)
- func DefaultBranch(ctx context.Context, env []string, repo string) (string, error)
- func GH(ctx context.Context, env []string, dir string, args ...string) ([]byte, error)
- func Git(ctx context.Context, env []string, dir string, args ...string) ([]byte, error)
- func HasRef(ctx context.Context, env []string, repo, ref string) bool
- func IsDir(p string) bool
- func IssueBranch(number int, title string) string
- func LinkDir(target, link string) error
- func Listed(ctx context.Context, env []string, repo, worktree string) (bool, error)
- func Main(args []string) int
- func NewID() string
- func PosixPath(p string) string
- func Prepare(ctx context.Context, p Prep) error
- func Preview(ctx context.Context, env []string, repo string, patterns []string) (*wire.TaskPreviewResult, error)
- func PullRequestRefs(ctx context.Context, env []string, repo string, number int) (head, base string, err error)
- func Remove(ctx context.Context, r Removal) (keptBranch string, err error)
- func RunScript(ctx context.Context, s Script) error
- func SameKey(t *wire.Task, repo, kind string, number int, name string) bool
- func SamePath(a, b string) bool
- func SavedPatterns(configPath, repo string) []string
- func SavedWorktreeFolder(configPath string) string
- func ShareLinks(repo, worktree string) []string
- func Unpushed(ctx context.Context, env []string, repo, branch string) (int, error)
- func ValidName(ctx context.Context, env []string, repo, name string) error
- func ValidPattern(p string) error
- func ValidRepoKey(key string) error
- func Within(p, dir string) bool
- func Worktrees(ctx context.Context, env []string, repo string) ([]string, error)
- type CopyJob
- type CopyPlan
- type CopySpec
- type GitError
- type Orca
- type OrcaError
- type Prep
- type Removal
- type Script
- type ScriptPlan
- type StageError
- type Worktree
Constants ¶
const ( ExitFailure = 1 ExitUsage = 2 ExitNotFound = 127 )
Exit codes of `stagent task`.
const ( // MaxCopyBytes and MaxCopyEntries bound what one worktree gets; an // entry that would pass either is skipped (limit). MaxCopyBytes = 2 << 30 MaxCopyEntries = 50_000 )
const IncludeFile = ".worktreeinclude"
IncludeFile is Orca's list of paths to copy, at a checkout's root.
const OrcaFile = "orca.yaml"
OrcaFile is Orca's per-repository settings file, at a checkout's root. stagent only reads it.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶
Canonical returns the canonical path (正の形) of the source checkout that dir lies in: `git rev-parse --show-toplevel`, symlinks resolved (CanonicalPath). A linked worktree is a checkout of its own: it is not mapped to its main worktree. Task.Repo and task.preview's repo are this value, as is the key of config.json's repos.
func CanonicalPath ¶
CanonicalPath makes p absolute and clean, resolves its symlinks when it exists and, on Windows, upper-cases its drive letter.
func Changes ¶
Changes lists what removing worktree would lose, as `git status --porcelain` lines: tracked files changed (staged or not) and untracked files git does not ignore. Ignored files (a copied .env) are not changes, nor are the shared-directory links named in links (paths relative to worktree, see ShareLinks).
func CheckBranch ¶
CheckBranch fails when name cannot be a branch name.
func CopyFiles ¶
CopyFiles links the shared directories and then copies what is to be copied into j.Dest, leaving every destination that exists as it is (a retry copies only what is missing), and nothing under a link. A shared directory that cannot be linked gets one warning line; what is listed under it is copied as ordinary files instead. Nothing here fails the task: errors are lines in the terminal.
func CurrentBranch ¶
CurrentBranch is the branch checked out in dir, "" when detached.
func DefaultBranch ¶
DefaultBranch is the repository's default branch as the checkout repo knows it: origin/HEAD (set by clone, or `git remote set-head origin --auto`), else origin's main or master.
func GH ¶
GH runs gh in dir with env (nil: this process's), found on env's PATH, and returns its standard output. A failing gh is an error with its last stderr line.
func Git ¶
Git runs git in dir with env (nil: this process's environment) and returns its standard output. git is looked up in env's PATH. On Windows every call gets -c core.longpaths=true. A git that cannot be found is an error wrapping exec.ErrNotFound; one that fails is a *GitError.
func IssueBranch ¶
IssueBranch is an issue's default branch: <number>-<slug>, the slug being its title lower-cased, every run of characters other than a-z and 0-9 one hyphen, trimmed of hyphens, at most 40 characters. When nothing of the title is left the branch is issue-<number>.
func LinkDir ¶
LinkDir makes link a symbolic link to the directory target (a shared directory of a task's worktree).
func Listed ¶
Listed reports whether worktree (canonical) is a worktree of the checkout repo. An error means git could not answer: not found (wrapping exec.ErrNotFound) or failing, e.g. because repo is gone.
func Prepare ¶
Prepare runs the stages of p's task in order — fetch, branch, worktree, copy, setup — stopping at the first that fails (*StageError). A worktree task whose directory is left empty then has it removed (after leaving it when it is the working directory). Every stage can run twice: a task that failed is prepared again with the same task (ADR 0006).
func Preview ¶
func Preview(ctx context.Context, env []string, repo string, patterns []string) (*wire.TaskPreviewResult, error)
Preview is task.preview for the source checkout repo (canonical) with the app's patterns: the plan, at most wire.TaskPreviewRows copies and skips, and the checkout's orca.yaml scripts.setup or why that file is unusable (its sharedDirectories then ignored).
func PullRequestRefs ¶
func PullRequestRefs(ctx context.Context, env []string, repo string, number int) (head, base string, err error)
PullRequestRefs asks gh, in the checkout repo, for a pull request's head branch (a task's branch, what `gh pr checkout` checks out) and base branch.
func Remove ¶
Remove runs the stages of r's removal in order — archive (when asked), worktree (unlink the shared-directory links, then git worktree remove), branch — stopping at the first that fails (*StageError). It returns the task's branch when it is kept: `git branch -d` refused it and gh does not say its pull request was merged at the very commit the branch is on. Remote branches are never deleted.
func RunScript ¶
RunScript runs s (PlanScript for this OS) in s.Dir and waits for it. A script exiting non-zero is an error naming its exit code.
func SameKey ¶ added in v0.14.0
SameKey reports whether t is the task keyed by the source checkout repo (canonical) and, for kind name, name (exactly), else number: a named task never shares its key with an issue's or PR's.
func SamePath ¶
SamePath reports whether two canonical paths name the same place (case-insensitively on Windows).
func SavedPatterns ¶
SavedPatterns reads the app's patterns for the source checkout repo (canonical) from config.json at configPath. Only the daemon writes that file (config.set); a missing or unreadable one has none.
func SavedWorktreeFolder ¶ added in v0.14.0
SavedWorktreeFolder reads the worktree folder (worktree の置き場) from config.json at configPath, as config.set stored it: absolute and clean. "": none set, or a missing or unreadable file.
func ShareLinks ¶
ShareLinks lists the shared directories (worktree.sharedDirectories of the source checkout's orca.yaml) that are links in worktree — symbolic links, or junctions on Windows — as slash-separated paths relative to it. An orca.yaml that cannot be read has none.
func Unpushed ¶
Unpushed counts the commits of the checkout repo's local branch that no remote-tracking branch has (`git rev-list <branch> --not --remotes --count`); 0 when there is no such branch.
func ValidName ¶ added in v0.14.0
ValidName fails when name cannot be a named task's name: it must be 1–64 characters without whitespace, control characters or any of / \ : * ? " < > |, not start with . or -, and be a branch name git takes as it is (`git check-ref-format --branch`, run in the checkout repo, printing it back: @ and @{-N} name other branches). The name is also a directory under the worktree folder and the default branch; the app mirrors all but the git check.
func ValidPattern ¶
ValidPattern reports why p cannot be an app pattern (a git pathspec glob from the checkout's root): empty, a negation (!), absolute, or with a .. or .git component.
func ValidRepoKey ¶
ValidRepoKey reports why key cannot be a key of config.json's repos: it must be an absolute, clean path (no trailing separator). Whether it exists does not matter.
Types ¶
type CopyJob ¶
type CopyJob struct {
CopySpec
Dest string
// Out (the terminal) gets one line per link, copy and skip.
Out io.Writer
// Link makes link point to the directory target; nil: LinkDir.
Link func(target, link string) error
}
CopyJob is the copy stage: spec's links and copies made in Dest, a new worktree of spec.Repo.
type CopyPlan ¶
type CopyPlan struct {
Copies []wire.TaskCopy
Links []string // shared directories, relative with / separators
Skipped []wire.TaskSkip
TotalBytes int64
}
CopyPlan is a resolved CopySpec, every shared directory taken as linked.
type CopySpec ¶
type CopySpec struct {
// Repo is the source checkout (canonical).
Repo string
// Env is git's environment (nil: this process's).
Env []string
// Patterns are the app's (config.json repos.<Repo>.worktree_include).
Patterns []string
Shared []string
}
CopySpec is what a worktree of Repo gets copied and linked.
type Orca ¶
type Orca struct {
Setup string // scripts.setup ("": none)
Archive string // scripts.archive ("": none)
}
Orca is what stagent uses of an orca.yaml. Which checkout's file counts depends on the key: scripts.setup comes from the task's worktree; scripts.archive and worktree.sharedDirectories from the source checkout (元のチェックアウト).
func LoadOrca ¶
LoadOrca reads the orca.yaml in dir. No file is (nil, nil). Like Orca, a file that is larger than 256 KiB, is not valid YAML, repeats a key or is not a mapping is refused whole (*OrcaError); a value of the wrong type, or a string over 64 KiB, is dropped alone and the rest kept. Unknown keys are ignored.
type Prep ¶
type Prep struct {
Task wire.Task
// SwitchBranch (in_place only): switch the checkout to Task.Branch.
SwitchBranch bool
// Setup (in_place only): run scripts.setup. A worktree task always
// runs it.
Setup bool
// WorktreeInclude are the app's patterns of files to copy into a
// worktree task's worktree (config.json repos.<repo>.worktree_include).
WorktreeInclude []string
// Env is the environment of git, gh and the setup script; nil: this
// process's.
Env []string
// Stdin feeds the setup script (nil: none); Out, the terminal, gets
// the stage headers, the commands run and their output.
Stdin io.Reader
Out io.Writer
// Stage is told each stage as it starts (task.progress).
Stage func(stage string)
}
Prep is a task to prepare: what `stagent task run` does in the task's first session before it starts the agent (ADR 0006).
type Removal ¶
type Removal struct {
Task wire.Task
// Archive runs scripts.archive of the source checkout's orca.yaml
// first, in the worktree.
Archive bool
// Force removes a worktree with changes (git worktree remove --force).
Force bool
// Env is the environment of git, gh and the archive script; nil: this
// process's.
Env []string
// Stdin feeds the archive script (nil: none); Out, the terminal (nil:
// nowhere), gets the stage headers, the commands run and their output.
Stdin io.Reader
Out io.Writer
// Stage is told each stage as it starts (task.progress).
Stage func(stage string)
}
Removal is 片付け of a worktree task once its sessions have stopped: what `stagent task remove` runs, or the bridge itself without archive (ADR 0006).
type Script ¶
type Script struct {
Text string
// Dir is the task's worktree: the working directory and
// ORCA_WORKTREE_PATH (ORCA_WORKSPACE_NAME is its base name).
Dir string
// Root is the source checkout (元のチェックアウト): ORCA_ROOT_PATH.
Root string
// Env is the environment before the ORCA_* variables; nil: this
// process's.
Env []string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
Script is an orca.yaml script (scripts.setup, scripts.archive) to run the way Orca runs it.
type ScriptPlan ¶
ScriptPlan is how a Script runs: Program with Args and, when Runner is set, the path of a file holding Runner (suffix RunnerExt) as the last argument; Env is its environment.
func PlanScript ¶
func PlanScript(goos string, s Script, git string) (ScriptPlan, error)
PlanScript decides how s runs on goos. git is git's path, used on Windows only, to find Git for Windows' bash.exe next to it.
- Linux, macOS: the login shell (SHELL of s.Env when it is a POSIX shell, else /bin/sh) as `<shell> -l -c`, with `set -e` first: the first failing command fails the script.
- Windows: cmd.exe (%ComSpec%) runs a .cmd runner, as Orca's: every line is `call`ed and the first one failing (errorlevel) ends it. A script starting with #! runs in Git for Windows' bash.exe instead, with `set -e` and the ORCA_* paths in /c/… form; without that bash it is an error.
type StageError ¶
StageError is the stage that failed and why.
func (*StageError) Error ¶
func (e *StageError) Error() string
func (*StageError) Unwrap ¶
func (e *StageError) Unwrap() error