Documentation
¶
Overview ¶
Package stack is the domain model: stacks of branches rooted at a trunk, pure navigation over them, and the ports a backend must implement.
Index ¶
- Constants
- type Branch
- type Direction
- type Error
- type Graph
- func (g *Graph) Branches() []string
- func (g *Graph) Children(name string) []string
- func (g *Graph) IsTrunk(name string) bool
- func (g *Graph) Parent(name string) (string, bool)
- func (g *Graph) StackOf(name string) (*Stack, int, bool)
- func (g *Graph) StacksOn(trunk string) []*Stack
- func (g *Graph) Tracked(name string) bool
- type Kind
- type LinkOptions
- type Metadata
- type NavRequest
- type NavResult
- type PRChecks
- type PRRef
- type Scope
- type Snapshot
- type Stack
- type SubmitOptions
- type SubmitResult
- type Submitter
- type Tracker
Constants ¶
const ( MsgAlreadyTop = "Already at the top of the stack." MsgAlreadyBottom = "Already at the bottom most branch in the stack." )
Graphite's clamp messages.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Branch ¶
type Branch struct {
Name string
// Head is the tip commit id known to the backend (may be empty).
Head string
// Base is the parent's tip at the last restack (may be empty).
Base string
PR *PRRef
// RenamedFrom is the branch's old name when an Update renames it, so
// the backend carries its record (pull request included) over to the
// new name. Leaving it empty while changing Name records a new branch.
RenamedFrom string
}
Branch is one layer of a stack.
type Error ¶
type Error struct {
Kind Kind
// Msg is a one-line description.
Msg string
// NextSteps are concrete actions, rendered as a list.
NextSteps []string
// Files lists conflicted paths for KindConflict.
Files []string
// Branch names the branch involved (e.g. the one being rebased).
Branch string
// Checks lists the pull requests whose checks stopped a merge, for
// KindChecksFailing and KindChecksPending.
Checks []PRChecks
// Detail carries captured diagnostic output (stderr tail); never stdout.
Detail string
// Cause is the underlying error, if any.
Cause error
}
Error is the user-facing error type shared by every port and use case.
func (*Error) Is ¶
Is lets errors.Is match on kind: errors.Is(err, &stack.Error{Kind: stack.KindConflict}).
func (*Error) WithDetail ¶
WithDetail records diagnostic output.
type Graph ¶
type Graph struct {
Stacks []Stack
// Trunks are the distinct trunk branches in first-seen order.
Trunks []string
}
Graph holds every stack in a repository.
func (*Graph) Children ¶
Children returns the branches directly above name. For a stacked branch that is at most one branch today (stacks are linear); for a trunk it is the bottom branch of every stack rooted there.
func (*Graph) Parent ¶
Parent returns the branch below name: the previous branch in its stack, or the trunk for the bottom branch. Trunks and untracked branches have none.
type Kind ¶
type Kind int
Kind classifies an Error so that the CLI can pick an exit code and message style and the MCP server can return a machine-readable code.
const ( KindUnknown Kind = iota // KindNotRepo: the directory is not inside a git repository. KindNotRepo // KindNotInStack: the current branch is not tracked in any stack. KindNotInStack // KindConflict: a rebase stopped on conflicts; Files and Branch are set. KindConflict // KindAPIFailure: the forge API call failed. KindAPIFailure // KindInvalidArgs: the request is invalid for the current state. KindInvalidArgs // KindDisambiguate: the branch belongs to several stacks. KindDisambiguate // KindRebaseActive: a git rebase is already in progress. KindRebaseActive // KindLocked: the backend's stack metadata is locked or stale. KindLocked KindStacksUnavailable // KindModifyRecovery: the backend has an interrupted modify to recover. KindModifyRecovery // KindUnsupported: the operation is not possible with the current backend. KindUnsupported // KindNotInstalled: a required tool is missing. KindNotInstalled // KindAuthRequired: the user must log in. KindAuthRequired // KindNotAtTop: the operation needs the top of the stack. KindNotAtTop // KindInteractionRequired: a prompt, editor or TUI is needed but not available. KindInteractionRequired // KindPartial: the command did everything it could, but some of it // could not be done (the result says what); running it again after // following the next steps finishes the job. KindPartial // KindChecksFailing: a merge was refused because checks failed on // pull requests it would land; Checks says which. KindChecksFailing // KindChecksPending: a merge was refused because checks are still // running on pull requests it would land; Checks says which. KindChecksPending // KindSigningFailed: git could not sign a commit (a locked SSH key, a // gpg agent that can't prompt); nothing was committed. KindSigningFailed )
type LinkOptions ¶ added in v0.3.0
type LinkOptions struct {
// Stack is the remote stack to add the pull requests to; 0 starts a new
// one, which needs at least two pull requests.
Stack int
// Trunk is the base of the bottom pull request of a new stack.
Trunk string
// PRs are pull request numbers, bottom to top.
PRs []int
}
LinkOptions controls Submitter.Link.
type Metadata ¶
type Metadata interface {
Load(ctx context.Context, repo git.Repo) (*Graph, error)
// Update loads the graph fresh under the backend's lock, lets fn edit it
// and writes the result back. fn may edit each stack's Branches (remove
// entries, change Head and Base, rename one by setting Name and
// RenamedFrom) and may also append stacks (each with a
// trunk and at least one branch; they are written to the current
// worktree's file, else the main checkout's, else a new file in the
// common dir) and may set or clear a stack's ID and Number (clearing
// removes them from the record). It must not remove or reorder existing
// stacks; a stack left with no branches is removed. When fn returns an
// error nothing is written and the error is returned.
Update(ctx context.Context, repo git.Repo, fn func(*Graph) error) error
// Snapshot reads the metadata exactly as stored, under the backend's
// lock.
Snapshot(ctx context.Context, repo git.Repo) (Snapshot, error)
// Restore writes a snapshot back byte for byte under the lock, removing
// the files that did not exist when it was taken.
Restore(ctx context.Context, repo git.Repo, s Snapshot) error
}
Metadata reads and edits the stack graph. Load must be fast and offline: shell completion depends on it.
type NavRequest ¶
type NavRequest struct {
Steps int
// selects between several children today only where the graph branches
// (a trunk with several stacks); stacks themselves are linear.
To string
}
NavRequest asks to move from a branch along the stack.
type NavResult ¶
NavResult is the computed destination.
func Navigate ¶
func Navigate(g *Graph, req NavRequest) (NavResult, error)
Navigate computes the destination of req without touching git. Merged branches are skipped as destinations (they are no longer real layers) unless the request starts on one. Moving down from the bottom branch lands on the trunk, as in Graphite.
type PRChecks ¶ added in v0.2.0
type PRChecks struct {
Number int `json:"number"`
Branch string `json:"branch"`
// Failing checks finished without passing.
Failing []string `json:"failing,omitempty"`
// Pending checks are queued or still running.
Pending []string `json:"pending,omitempty"`
}
PRChecks names the checks holding up one pull request.
type Snapshot ¶ added in v0.4.0
type Snapshot struct {
// Files maps each metadata file's path to its contents; an empty value
// records that the file did not exist.
Files map[string][]byte `json:"files"`
}
Snapshot is the backend's metadata exactly as stored, opaque to everything but the backend that made it.
type Stack ¶
type Stack struct {
// ID is the backend's remote identifier, if any.
ID string
// Number is the backend's repo-scoped stack number, if any.
Number int
Trunk string
// Branches run bottom (closest to trunk) to top.
Branches []Branch
// Worktree is the working tree whose metadata holds this stack (gh stack
// keeps stacks per worktree), or empty when unknown. Sync runs there.
Worktree string
}
Stack is an ordered, linear list of branches on top of a trunk.
type SubmitOptions ¶
type SubmitOptions struct {
// Publish creates PRs ready for review instead of drafts.
Publish bool
// Interactive lets the backend open its own editor on the terminal.
Interactive bool
}
SubmitOptions controls Submitter.Submit.
type SubmitResult ¶
type SubmitResult struct {
Output string
// Streamed is true when Output was already written to the user's
// terminal as it was produced, so callers must not print it again.
Streamed bool
}
SubmitResult carries the backend's human-readable output for relaying.
type Submitter ¶
type Submitter interface {
Submit(ctx context.Context, repo git.Repo, o SubmitOptions) (SubmitResult, error)
// Link joins pull requests that already exist into a stack on the
// remote, without pushing any branch.
Link(ctx context.Context, repo git.Repo, o LinkOptions) (SubmitResult, error)
// Unstack dissolves the stack with the given number on the forge,
// leaving its pull requests open and unstacked, so the next Submit
// creates it again from the local order. A stack the forge no longer
// has is not an error. The number must not be tracked locally any
// more, or gh stack would remove the local record too.
Unstack(ctx context.Context, repo git.Repo, number int) error
}
Submitter pushes the current stack and creates or updates its PRs.
type Tracker ¶
type Tracker interface {
// InitStack creates a new stack on trunk from the given branch names.
// Existing branches are adopted, missing ones are created from the
// previous branch, and the last one is checked out.
InitStack(ctx context.Context, repo git.Repo, trunk string, branches []string) error
// AddTop creates (or adopts) name on top of the current stack and checks
// it out. The current branch must be the top of its stack.
AddTop(ctx context.Context, repo git.Repo, name string) error
}
Tracker registers branches with the backend.