stack

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 5 Imported by: 1 module, 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 is the domain model: stacks of branches rooted at a trunk, pure navigation over them, and the ports a backend must implement.

Index

Constants

View Source
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.

func (Branch) Merged

func (b Branch) Merged() bool

Merged reports whether the branch's PR has been merged.

type Direction

type Direction int

Direction of a navigation request.

const (
	Up Direction = iota
	Down
	Top
	Bottom
)

func (Direction) String

func (d Direction) String() string

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 New

func New(kind Kind, msg string) *Error

New creates an Error.

func Newf

func Newf(kind Kind, format string, args ...any) *Error

Newf creates an Error with a formatted message.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is lets errors.Is match on kind: errors.Is(err, &stack.Error{Kind: stack.KindConflict}).

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithCause

func (e *Error) WithCause(err error) *Error

WithCause records the underlying error.

func (*Error) WithDetail

func (e *Error) WithDetail(detail string) *Error

WithDetail records diagnostic output.

func (*Error) WithSteps

func (e *Error) WithSteps(steps ...string) *Error

WithSteps appends next steps.

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 NewGraph

func NewGraph(stacks []Stack) *Graph

NewGraph builds a Graph and derives Trunks.

func (*Graph) Branches

func (g *Graph) Branches() []string

Branches returns every stacked (non-trunk) branch name.

func (*Graph) Children

func (g *Graph) Children(name string) []string

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) IsTrunk

func (g *Graph) IsTrunk(name string) bool

IsTrunk reports whether name is the trunk of any stack.

func (*Graph) Parent

func (g *Graph) Parent(name string) (string, bool)

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.

func (*Graph) StackOf

func (g *Graph) StackOf(name string) (*Stack, int, bool)

StackOf returns the stack containing the (non-trunk) branch and its index.

func (*Graph) StacksOn

func (g *Graph) StacksOn(trunk string) []*Stack

StacksOn returns the stacks rooted at trunk.

func (*Graph) Tracked

func (g *Graph) Tracked(name string) bool

Tracked reports whether name is a trunk or a stacked branch.

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: stacked PRs are not available for the repository.
	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
)

func (Kind) Code

func (k Kind) Code() string

Code is the stable machine-readable name of the kind.

func (Kind) String

func (k Kind) String() string

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 struct {
	From string
	Dir  Direction
	// Steps for Up/Down; 0 means 1.
	Steps int
	// To optionally names a branch that must lie in the chosen direction. It
	// 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 struct {
	Target string
	// Moved is the number of levels actually traversed.
	Moved int
	// Clamped is true when the request hit the end of the stack.
	Clamped bool
	// Message explains a clamp in Graphite's words.
	Message string
}

NavResult is the computed destination.

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 PRRef

type PRRef struct {
	Number int
	URL    string
	Merged bool
}

PRRef is the backend's snapshot of a branch's pull request.

type Scope

type Scope int

Scope selects which part of the current stack an operation covers.

const (
	ScopeAll Scope = iota
	ScopeUpstack
	ScopeDownstack
	ScopeOnly
)

func (Scope) String

func (s Scope) String() string

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.

func (*Stack) Bottom

func (s *Stack) Bottom() string

Bottom is the branch directly above trunk ("" for an empty stack).

func (*Stack) Index

func (s *Stack) Index(name string) int

Index returns the position of name in the stack, or -1.

func (*Stack) Names

func (s *Stack) Names() []string

Names returns the branch names bottom to top.

func (*Stack) Top

func (s *Stack) Top() string

Top is the branch furthest from trunk ("" for an empty stack).

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.

Directories

Path Synopsis
Package stacktest provides an in-memory stack backend for tests.
Package stacktest provides an in-memory stack backend for tests.

Jump to

Keyboard shortcuts

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