app

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 21 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 app holds the use cases. Both the cobra commands and the MCP tools call into it, so no behaviour lives anywhere else.

Index

Constants

View Source
const (
	TrunkUpToDate      = "up-to-date"
	TrunkAhead         = "ahead" // local commits the remote doesn't have; left alone
	TrunkFastForwarded = "fast-forwarded"
	TrunkReset         = "reset" // diverged and reset to the remote with consent
	TrunkDiverged      = "diverged"
	TrunkDirty         = "dirty"
	TrunkNoRemote      = "no-remote"
)

Trunk statuses.

View Source
const (
	ReasonMerged  = "merged"
	ReasonClosed  = "closed"
	ReasonInTrunk = "in-trunk"
	ReasonGone    = "gone" // already deleted locally; only the metadata changed
)

Why a branch was deleted.

View Source
const (
	KeptPolicy        = "policy"
	KeptOtherWorktree = "other-worktree"
	KeptDirty         = "dirty"
	KeptNotInTrunk    = "not-in-trunk"
	KeptNoTrunk       = "no-trunk"
	KeptUnpushed      = "unpushed"
)

Why a candidate was kept.

Variables

View Source
var ErrNoForge = stack.New(stack.KindUnsupported, "no forge configured for pull requests")

ErrNoForge is returned when no forge adapter is configured.

Functions

func FindPRTemplate

func FindPRTemplate(topLevel string) (string, bool)

FindPRTemplate returns the repository's pull request template, if any, with YAML front matter removed. The lookup mirrors gh: a regular file named pull_request_template.* in .github/, the root, or docs/.

func PRsFor

func PRsFor(prs []forge.PullRequest) map[string]forge.PullRequest

PRsFor picks the pull request to show for each branch: an open or draft PR wins over merged, which wins over closed; ties go to the most recent.

func Slug

func Slug(s string) string

Slug turns free text (a commit subject, an AI suggestion) into a kebab-case branch fragment: lowercase, runs of non-alphanumerics become one dash, dashes trimmed, truncated at a word boundary.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App exposes the use cases.

func New

func New(d Deps) *App

New wires an App. Log defaults to a discarding logger.

func (*App) CachedPRs

func (a *App) CachedPRs(repo git.Repo) ([]forge.PullRequest, cache.State, time.Time)

CachedPRs returns the cached snapshot and its freshness.

func (*App) Checkout

func (a *App) Checkout(ctx context.Context, repo git.Repo, name string) error

Checkout switches to a local branch, explaining worktree conflicts.

func (*App) Create

func (a *App) Create(ctx context.Context, repo git.Repo, o CreateOptions) (CreateResult, error)

Create stages, creates a branch on top of the current one, commits and registers the branch with the backend.

func (*App) Merge added in v0.1.5

func (a *App) Merge(ctx context.Context, repo git.Repo, o MergeOptions) (MergeResult, error)

Merge lands the pull requests of the current stack up to and including a branch, in one all or nothing operation on the forge, then syncs so the merged branches go and whatever is left above is restacked onto trunk. Nothing is merged while any pull request in the way is missing, a draft or closed; the forge's own rules (required checks, reviews) apply on top.

func (*App) Modify

func (a *App) Modify(ctx context.Context, repo git.Repo, o ModifyOptions) (ModifyResult, error)

Modify amends (or adds to) the current branch and restacks its descendants.

func (*App) Navigate

func (a *App) Navigate(ctx context.Context, repo git.Repo, req stack.NavRequest) (stack.NavResult, error)

Navigate moves along the current stack (up/down/top/bottom) and checks out the destination. The pure computation lives in stack.Navigate.

func (*App) RefreshPRs

func (a *App) RefreshPRs(ctx context.Context, repo git.Repo) ([]forge.PullRequest, error)

RefreshPRs fetches pull requests from the forge and updates the cache.

func (*App) Restack

func (a *App) Restack(ctx context.Context, repo git.Repo, o RestackOptions) (RestackResult, error)

Restack rebases part of the current stack onto its parents, locally and without checking anything out, the way gt restack does. The bottom branch goes onto the local trunk. A conflict stops at that branch.

func (*App) Submit

func (a *App) Submit(ctx context.Context, repo git.Repo, o SubmitOptions) (SubmitResult, error)

Submit pushes the current stack and creates or updates its pull requests.

func (*App) Sync

func (a *App) Sync(ctx context.Context, repo git.Repo, o SyncOptions) (SyncResult, error)

Sync fetches, moves trunk, deletes merged branches, pulls in branches the remote advanced and restacks every stack, the way gt sync does. Nothing is pushed; submit does that.

func (*App) Trunk

func (a *App) Trunk(ctx context.Context, repo git.Repo) (string, error)

Trunk returns the trunk to use from the current position: the trunk of the current stack, the current branch if it is a trunk, or the only trunk.

func (*App) View

func (a *App) View(ctx context.Context, repo git.Repo, o ViewOptions) (*View, error)

View builds the stack tree from backend metadata and local git state. It never talks to the forge; see DecoratePRs.

type BranchMove added in v0.1.4

type BranchMove struct {
	Name  string `json:"name"`
	Stack string `json:"stack,omitempty"` // the stack's bottom branch
	From  string `json:"from"`
	To    string `json:"to"`
}

BranchMove is a branch whose tip moved.

type CommitInfo

type CommitInfo struct {
	SHA     string `json:"sha"`
	Subject string `json:"subject"`
}

CommitInfo identifies a commit for output.

func (CommitInfo) Short

func (c CommitInfo) Short() string

Short returns the abbreviated id.

type Conflict added in v0.1.4

type Conflict struct {
	Stack  string   `json:"stack"`
	Branch string   `json:"branch"`
	Onto   string   `json:"onto"`
	Files  []string `json:"files,omitempty"`
}

Conflict is a branch that could not be restacked.

type CreateOptions

type CreateOptions struct {
	// Name is the explicit branch name (used as-is, no prefix).
	Name string
	// Message paragraphs for the commit; empty opens the editor when
	// something is staged.
	Message []string
	Staging StagingMode
	// UseAI drafts the branch name (and the message when none is given).
	UseAI    bool
	NoVerify bool
}

CreateOptions mirrors `gt create`.

type CreateResult

type CreateResult struct {
	Branch string      `json:"branch"`
	Parent string      `json:"parent"`
	Commit *CommitInfo `json:"commit,omitempty"`
	// NewStack is true when the branch started a new stack on trunk.
	NewStack bool `json:"newStack"`
}

CreateResult reports what happened.

type DeletedBranch added in v0.1.4

type DeletedBranch struct {
	Name   string `json:"name"`
	Head   string `json:"head,omitempty"`
	Reason string `json:"reason"`
}

DeletedBranch is a branch sync deleted and why.

type Deps

type Deps struct {
	Git      *git.Client
	Meta     stack.Metadata
	Tracker  stack.Tracker
	Submit   stack.Submitter
	Forge    forge.Forge
	AI       ai.Drafter
	Cache    *cache.Store
	Config   *config.Config
	Log      *slog.Logger
	Prompter Prompter
	// Progress, when set, wraps slow operations (backend calls, AI calls) so
	// the CLI can show a headline and spinner. It must run fn and return its
	// error. The phase says what kind of work it is; the message is the
	// headline without decoration ("Submitting stack").
	Progress func(ctx context.Context, phase Phase, message string, fn func(ctx context.Context) error) error
}

Deps are the ports an App needs. Optional ports may be nil; the use cases that need them return KindUnsupported / KindNotInstalled errors.

type KeptBranch added in v0.1.4

type KeptBranch struct {
	Name   string `json:"name"`
	Reason string `json:"reason"`
}

KeptBranch is a deletion candidate that was kept and why.

type MergeOptions added in v0.1.5

type MergeOptions struct {
	// Branch is the highest branch to merge; every branch below it in the
	// stack goes with it. Empty means the current branch.
	Branch string
	// Method overrides stack.merge.method; empty leaves it to the config,
	// and an empty config leaves it to the repository's settings.
	Method forge.MergeMethod
	// NoSync skips the sync that normally follows a merge.
	NoSync bool
}

MergeOptions controls Merge.

type MergeResult added in v0.1.5

type MergeResult struct {
	Trunk string `json:"trunk"`
	// PullRequests that were merged, bottom to top.
	PullRequests []MergedPR        `json:"pullRequests"`
	Status       forge.MergeStatus `json:"status"`
	// SHA is the merge commit on trunk; empty when enqueued.
	SHA string `json:"sha,omitempty"`
	// Sync is what the sync afterwards did, when it ran.
	Sync    *SyncResult `json:"sync,omitempty"`
	Notices []string    `json:"notices,omitempty"`
}

MergeResult reports what landed.

type MergedPR added in v0.1.5

type MergedPR struct {
	Branch string `json:"branch"`
	Number int    `json:"number"`
	URL    string `json:"url,omitempty"`
}

MergedPR is one pull request that landed.

type ModifyOptions

type ModifyOptions struct {
	// NewCommit creates a commit instead of amending (-c).
	NewCommit bool
	Staging   StagingMode
	Message   []string
	// Edit opens the editor when amending (-e); ignored with NewCommit.
	Edit        bool
	ResetAuthor bool
	NoVerify    bool
	// Continue / Abort resume or abandon an interrupted restack.
	Continue bool
	Abort    bool
}

ModifyOptions mirrors `gt modify`.

type ModifyResult

type ModifyResult struct {
	Branch string     `json:"branch"`
	Commit CommitInfo `json:"commit"`
	// Amended is false when a new commit was created.
	Amended bool `json:"amended"`
	// ForcedNewCommit is true when the branch had no commits of its own, so
	// amending would have rewritten the parent's commit.
	ForcedNewCommit bool `json:"forcedNewCommit"`
	// Restacked lists the branches above that were rebased.
	Restacked []string `json:"restacked"`
}

ModifyResult reports what happened.

type PRMode

type PRMode int

PRMode selects how View obtains pull-request state.

const (
	// PRsNone leaves rows with the backend's snapshot only.
	PRsNone PRMode = iota
	// PRsCached applies the cache regardless of age; never touches the network.
	PRsCached
	// PRsFresh refreshes from the forge when the cache is stale or missing,
	// falling back to the cache on failure.
	PRsFresh
)

type PRSnapshot

type PRSnapshot struct {
	PRs []forge.PullRequest `json:"prs"`
}

PRSnapshot is the cached forge state.

type PRText

type PRText struct {
	Title string `json:"title"`
	Body  string `json:"body"`
}

PRText is a title and body supplied for a branch.

type Phase

type Phase string

Phase names a kind of long running work. The CLI decorates each phase the same way every time (see docs/style.md); use cases only pick the phase.

const (
	PhaseCreate  Phase = "create"
	PhaseModify  Phase = "modify"
	PhaseRestack Phase = "restack"
	PhaseSubmit  Phase = "submit"
	PhaseSync    Phase = "sync"
	PhaseAI      Phase = "ai"
	PhaseUpdate  Phase = "update"
	PhaseInstall Phase = "install"
	PhaseMerge   Phase = "merge"
)

Phases.

type Prompter

type Prompter interface {
	Confirm(question string, defaultYes bool) (bool, error)
	Select(question string, options []string) (int, error)
}

Prompter asks the user a question. It is nil under --no-interactive and in the MCP server, in which case use cases must fall back to a default or return KindInteractionRequired.

type RemoteAhead added in v0.1.4

type RemoteAhead struct {
	Name    string `json:"name"`
	Commits int    `json:"commits"`
}

RemoteAhead is a branch with commits on the remote we don't have.

type RestackOptions

type RestackOptions struct {
	Scope stack.Scope
	// Branch is where the scope is worked out from; the current branch when
	// empty. Nothing is checked out.
	Branch string
	// Continue resumes an interrupted restack after the conflicts were
	// staged; Abort gives it up and puts every moved branch back.
	Continue bool
	Abort    bool
	// StageAll runs git add -A before continuing.
	StageAll bool
}

RestackOptions mirrors `gt restack`.

type RestackResult

type RestackResult struct {
	Scope stack.Scope `json:"scope"`
	// Branches is the scope, bottom to top.
	Branches []string `json:"branches"`
	// Moved are the branches that were rebased, bottom to top.
	Moved []BranchMove `json:"moved,omitempty"`
	// InPlace were already on their parent.
	InPlace []string `json:"in_place,omitempty"`
	// Restored are the branches an abort put back.
	Restored []string `json:"restored,omitempty"`
	Notices  []string `json:"notices,omitempty"`
}

RestackResult reports what a restack did.

type Row

type Row struct {
	Name string
	// Depth is 0 for a trunk, 1 for a bottom branch, and so on.
	Depth int
	// Parent is the branch below (empty for trunks and untracked branches).
	Parent    string
	IsTrunk   bool
	IsCurrent bool
	// Tracked is false for branches that are in no stack.
	Tracked bool
	// NeedsRestack is true when the parent's tip is not in the branch's
	// history.
	NeedsRestack bool
	// NeedsPush is true when the branch is on the remote but the local
	// branch has moved on from it (new commits, a restack, an amend), so
	// its PR is behind until the next submit.
	NeedsPush  bool
	Head       string
	LastCommit time.Time
	// Worktree is the path of another worktree the branch is checked out in.
	Worktree string
	// PR is the known pull request, if any. State is StateUnknown until the
	// forge has been consulted.
	PR *forge.PullRequest
	// StackIndex identifies the stack within View.Graph (-1 for trunks and
	// untracked branches).
	StackIndex int
}

Row is one line of the stack tree, in display order.

type StagingMode

type StagingMode int

StagingMode mirrors Graphite's -a / -u / -p flags.

const (
	StageNone StagingMode = iota
	StageAll
	StageUpdate
	StagePatch
)

type SubmitOptions

type SubmitOptions struct {
	// Draft / Publish force the state of new PRs. With neither, the
	// configured default applies: ready for review unless stack.submit.default
	// says draft, or ask (which prompts on a terminal and publishes otherwise).
	Draft   bool
	Publish bool
	// NoEdit skips the backend's interactive PR editor.
	NoEdit bool
	// DryRun reports what would happen without pushing.
	DryRun bool
	// UseAI drafts title and body for every new PR.
	UseAI bool
	// Texts supplies title/body per branch (the MCP path). Branches without
	// a PR need an entry unless UseAI is set or the editor can open.
	Texts map[string]PRText
	// Force pushes even when a branch has commits on the remote that aren't
	// in the local branch, overwriting them.
	Force bool
}

SubmitOptions mirrors `gt submit`.

type SubmitResult

type SubmitResult struct {
	Stack        []string      `json:"stack"`
	PullRequests []SubmittedPR `json:"pullRequests"`
	Draft        bool          `json:"draft"`
	DryRun       bool          `json:"dryRun"`
	// Output is the backend's own output (captured runs only).
	Output  string   `json:"output,omitempty"`
	Notices []string `json:"notices,omitempty"`
}

SubmitResult reports the outcome.

type SubmittedPR

type SubmittedPR struct {
	Branch  string      `json:"branch"`
	Number  int         `json:"number,omitempty"`
	URL     string      `json:"url,omitempty"`
	State   forge.State `json:"state,omitempty"`
	Created bool        `json:"created"`
	// WouldCreate is set in dry runs for branches without a PR.
	WouldCreate bool `json:"wouldCreate,omitempty"`
	// TextUpdated is true when a supplied or drafted title/body was applied.
	TextUpdated bool `json:"textUpdated,omitempty"`
	// BaseFixed is true when the PR's base was moved back onto its parent
	// branch after gh stack had pointed it elsewhere (see fixBases).
	BaseFixed bool `json:"baseFixed,omitempty"`
}

SubmittedPR is one line of the result.

type SyncOptions

type SyncOptions struct {
	// Force approves resetting a diverged trunk to the remote and deleting
	// merged or closed branches without asking.
	Force bool
	// DeleteAll approves deleting merged or closed branches without asking.
	DeleteAll bool
	// NoRestack skips the restack phase.
	NoRestack bool
}

SyncOptions mirrors gt sync's flags.

type SyncResult

type SyncResult struct {
	Remote    string          `json:"remote"`
	Trunks    []TrunkSync     `json:"trunks"`
	Deleted   []DeletedBranch `json:"deleted,omitempty"`
	Kept      []KeptBranch    `json:"kept,omitempty"`
	Updated   []BranchMove    `json:"updated,omitempty"`
	Restacked []BranchMove    `json:"restacked,omitempty"`
	Conflicts []Conflict      `json:"conflicts,omitempty"`
	Behind    []RemoteAhead   `json:"behind,omitempty"`
	Notices   []string        `json:"notices,omitempty"`
}

SyncResult reports everything sync did.

type TrunkSync added in v0.1.4

type TrunkSync struct {
	Name   string `json:"name"`
	From   string `json:"from,omitempty"`
	To     string `json:"to,omitempty"`
	Status string `json:"status"`
}

TrunkSync is what happened to one trunk.

type View

type View struct {
	Repo    git.Repo
	Current string
	Graph   *stack.Graph
	// Rows lists trunks and their stacks in display order, then untracked
	// branches when requested.
	Rows []Row
}

View is the rendered state of the repository's stacks.

func (*View) ApplyPRs

func (v *View) ApplyPRs(prs []forge.PullRequest)

ApplyPRs decorates rows with forge state. Rows whose branch has no PR in prs keep the backend snapshot, if any.

func (*View) Row

func (v *View) Row(name string) (Row, bool)

Row returns the row for a branch.

type ViewOptions

type ViewOptions struct {
	// CurrentStackOnly limits rows to the stack of the current branch (and
	// its trunk).
	CurrentStackOnly bool
	// IncludeUntracked adds local branches that are in no stack.
	IncludeUntracked bool
	// SkipRestackCheck avoids the per-branch ancestry check (completion).
	SkipRestackCheck bool
	// PRs selects how pull-request state is obtained (default: none).
	PRs PRMode
}

ViewOptions controls View.

Jump to

Keyboard shortcuts

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