Help us shape the future of Go! Take the 2026 Go Developer Survey

common

package
v0.30.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package common provides shared helper functions for CLI commands.

Package common provides shared helper functions for CLI commands.

Index

Constants

View Source
const (
	ReasonNoRestackNeeded = "does not need restacking"
	ReasonLocked          = "is locked"
	ReasonFrozen          = "is frozen"
)

Restack reason constants used by sync and get handlers.

Variables

This section is empty.

Functions

func ApplyReadOnlyCurrentBranch added in v0.18.0

func ApplyReadOnlyCurrentBranch(opts app.GlobalOptions) app.GlobalOptions

ApplyReadOnlyCurrentBranch tunes opts for commands that only inspect the current branch or its parent chain: skip the full metadata bootstrap and the managed-worktree check. Compose this when the optimization only applies to some code paths (e.g. show vs. write modes of a single command).

func Checkout

func Checkout(ctx *app.Context, opts checkout.Options, handler checkout.Handler) (checkout.Result, error)

Checkout runs checkout.Action and completes any worktree switch.

func CheckoutDeps added in v0.30.1

func CheckoutDeps(ctx *app.Context) checkout.Deps

CheckoutDeps builds checkout's deps.

func CommandPhase added in v0.19.0

func CommandPhase(cmd *cobra.Command, phase PhasePrefix) string

CommandPhase returns the lookup key under hooks: in .stackit.yaml for the given cobra command and phase, e.g. "pre-modify" or "post-worktree-create".

func CompleteBranches

func CompleteBranches(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

CompleteBranches is a helper for cobra.ValidArgsFunction and RegisterFlagCompletionFunc that returns all branch names in the repository.

func CompleteCheckout

func CompleteCheckout(ctx *app.Context, result checkout.Result, fallbackOpts checkout.Options, handler checkout.Handler) error

CompleteCheckout handles a checkout.Action result, including a worktree switch when shell integration is available. Without shell integration, retain the regular-checkout fallback only when the target is not already checked out in another worktree; Git permits a branch to be checked out by only one worktree at a time.

func FormatBranchSummary

func FormatBranchSummary(out output.Output, results []BranchResult) (success, fail int)

FormatBranchSummary formats a list of branch results for display. Returns the count of successful and failed operations.

func FormatPRInfo

func FormatPRInfo(prNumber *git.PRNumber) string

FormatPRInfo formats a PR number for display. Returns empty string if prNumber is nil.

func FormatRestackLines added in v0.30.1

func FormatRestackLines(event actions.RestackBranchEvent) []string

FormatRestackLines renders the streaming lines for one restack event: the reparent notice when there is one, then the row led by its status marker.

func FormatRestackReparent added in v0.30.1

func FormatRestackReparent(event actions.RestackBranchEvent) string

FormatRestackReparent renders the line announcing that a branch moved past a merged or deleted parent, or "" when it did not.

func FormatRestackResolveHint added in v0.30.1

func FormatRestackResolveHint(branch string) string

FormatRestackResolveHint renders the follow-up command for a conflicted branch.

func FormatRestackSummary added in v0.30.1

func FormatRestackSummary(summary actions.RestackSummary, upToDate int) string

FormatRestackSummary renders "restacked N, skipped M (conflict), blocked K, P already current". upToDate is the count of rows the caller suppressed. Returns "" when there is nothing to summarize.

func GetGlobalOptions

func GetGlobalOptions(cmd *cobra.Command) app.GlobalOptions

GetGlobalOptions returns runtime.GlobalOptions populated from a cobra.Command's flags

func HandleCheckoutResult

func HandleCheckoutResult(out output.Output, result checkout.Result) bool

HandleCheckoutResult handles the worktree switch result from checkout.Action. Returns true if shell integration handled the switch (caller should return nil). Returns false with tips printed if no shell integration.

func HandleCommandError

func HandleCommandError(err error) error

HandleCommandError formats known error types for user display.

func HasShellIntegration

func HasShellIntegration() bool

HasShellIntegration checks if stackit shell integration is installed. The shell wrapper sets STACKIT_SHELL_INTEGRATION=1 when running commands.

func MergeDeps added in v0.30.1

func MergeDeps(ctx *app.Context) merge.Deps

MergeDeps builds merge's deps, including the sync, submit and checkout deps its post-merge follow-ups run with.

func ReadMessage

func ReadMessage(message, messageFile string) (string, error)

ReadMessage resolves a commit message from either an inline -m string or a --message-file path. Used by create/modify/squash/split to keep variable commit-message text out of the literal command line, which keeps Claude permission rules stable across distinct messages and avoids shell-escaping pitfalls for multi-line subject+body messages.

If messageFile is "-", reads from stdin. If both flags are set, returns an error. Trims surrounding whitespace; preserves internal newlines so multi-line bodies survive intact. Errors when the resolved message is empty (file is empty or stdin received nothing) — the caller passed --message-file expecting a message, so silent fallthrough to other input paths would mask the mistake.

func ResolveBranchArg

func ResolveBranchArg(ctx *app.Context, args []string, missingErr error) (string, error)

ResolveBranchArg returns the first positional branch argument, or the current branch name when no argument was provided.

func RestackChanged added in v0.30.1

func RestackChanged(summary actions.RestackSummary) bool

RestackChanged reports whether a restack moved, conflicted on, or blocked any branch — anything other than "everything was already current".

func Run

func Run(cmd *cobra.Command, fn func(ctx *app.Context) error) error

Run is a helper that provides a runtime context to a command's execution function

func RunCommandHooks added in v0.19.0

func RunCommandHooks(ctx *app.Context, cmd *cobra.Command, phase PhasePrefix) error

RunCommandHooks fires the configured hooks for the given lifecycle phase.

Zero-cost when absent: the only work performed when no hook is configured for this command + phase is a slice-length check against the in-memory project config that was already loaded during context construction.

Returns the hook's error verbatim for pre-hooks (so the caller can abort the command). For post-hooks, errors are surfaced as warnings on ctx.Output and nil is returned.

func RunReadOnlyCurrentBranch added in v0.18.0

func RunReadOnlyCurrentBranch(cmd *cobra.Command, fn func(ctx *app.Context) error) error

RunReadOnlyCurrentBranch is for commands whose every code path is a read-only inspection of the current branch or its parent chain.

func RunWithOptions added in v0.18.0

func RunWithOptions(cmd *cobra.Command, opts app.GlobalOptions, fn func(ctx *app.Context) error) error

RunWithOptions is like Run but allows commands to tune context construction.

func SubmitDeps added in v0.30.1

func SubmitDeps(ctx *app.Context) submit.Deps

SubmitDeps builds submit's deps.

func SyncDeps added in v0.30.1

func SyncDeps(ctx *app.Context) sync.Deps

SyncDeps builds sync's deps.

Types

type BaseHandler

type BaseHandler struct {
	Output output.Output
	// contains filtered or unexported fields
}

BaseHandler provides common functionality for CLI handlers. Embed this in handler structs to reduce boilerplate.

func NewBaseHandler

func NewBaseHandler(out output.Output) BaseHandler

NewBaseHandler creates a new BaseHandler with the given output.

func (*BaseHandler) Cleanup

func (h *BaseHandler) Cleanup()

Cleanup is a default no-op implementation.

func (*BaseHandler) IsInteractive

func (h *BaseHandler) IsInteractive() bool

IsInteractive returns false by default (for simple handlers).

func (*BaseHandler) Lock

func (h *BaseHandler) Lock()

Lock acquires the handler's mutex.

func (*BaseHandler) Unlock

func (h *BaseHandler) Unlock()

Unlock releases the handler's mutex.

type BranchResult

type BranchResult struct {
	BranchName string
	Status     BranchResultStatus
	Output     string
	Error      error
	IsCurrent  bool
}

BranchResult holds data for formatting branch operation results.

type BranchResultStatus

type BranchResultStatus int

BranchResultStatus represents the status of a branch operation.

const (
	// StatusDone indicates the operation completed successfully.
	StatusDone BranchResultStatus = iota
	// StatusError indicates the operation failed.
	StatusError
)

type ExitCodeError added in v0.18.0

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

ExitCodeError wraps an error from a child process with the exit code stackit should return to its caller.

func NewExitCodeError added in v0.18.0

func NewExitCodeError(code int, err error) *ExitCodeError

func (*ExitCodeError) Error added in v0.18.0

func (e *ExitCodeError) Error() string

func (*ExitCodeError) ExitCode added in v0.18.0

func (e *ExitCodeError) ExitCode() int

func (*ExitCodeError) Unwrap added in v0.18.0

func (e *ExitCodeError) Unwrap() error

type PhasePrefix added in v0.19.0

type PhasePrefix string

PhasePrefix identifies whether a hook fires before or after the command.

const (
	PhasePre  PhasePrefix = "pre"
	PhasePost PhasePrefix = "post"
)

type RestackRowStatus added in v0.30.1

type RestackRowStatus int

RestackRowStatus classifies a restack row for the marker that leads it.

const (
	// RestackRowDone is a branch that was restacked or needed nothing.
	RestackRowDone RestackRowStatus = iota
	// RestackRowWarning is a branch that was held back, conflicted, or blocked.
	RestackRowWarning
)

func FormatRestackRow added in v0.30.1

func FormatRestackRow(event actions.RestackBranchEvent) (string, RestackRowStatus)

FormatRestackRow renders one restack outcome without its leading marker. Every command that restacks prints its rows through here, so a branch reads the same whichever command moved it.

func (RestackRowStatus) Mark added in v0.30.1

func (s RestackRowStatus) Mark() string

Mark returns the single-width status marker for streaming output.

type TUIPrompter added in v0.29.0

type TUIPrompter struct{}

TUIPrompter implements handler.Prompter with the shared Bubble Tea prompts. It calls through the tui prompt variables at call time so tests that stub them (e.g. tui.PromptConfirm) keep working.

func (TUIPrompter) Confirm added in v0.29.0

func (TUIPrompter) Confirm(message string, defaultYes bool) (bool, error)

Confirm implements handler.Confirmer.

func (TUIPrompter) MultiSelect added in v0.29.0

func (TUIPrompter) MultiSelect(title string, options []string) ([]string, error)

MultiSelect implements handler.MultiSelector.

func (TUIPrompter) Select added in v0.29.0

func (TUIPrompter) Select(title string, options []handler.SelectOption, defaultIndex int) (string, error)

Select implements handler.Selector.

func (TUIPrompter) TextInput added in v0.29.0

func (TUIPrompter) TextInput(prompt, defaultValue string) (string, error)

TextInput implements handler.TextInputPrompter.

Jump to

Keyboard shortcuts

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