validation

package
v0.17.15 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package validation provides reusable precondition validators for actions.

Validators can be composed into chains for consistent validation:

err := validation.Chain{
    validation.MustBeOnBranch(eng),
    validation.MustNotHaveRebaseInProgress(git, ctx),
    validation.MustNotHaveUncommittedChanges(git, ctx),
}.Validate()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateSourceBranch

func ValidateSourceBranch(eng BranchValidationEngine, branchName, operation string) error

ValidateSourceBranch validates that a source branch can be used for operations like move, pluck, or delete. Returns nil if valid, error otherwise.

Checks performed:

  • Branch is not trunk
  • Branch is tracked by stackit
  • Branch is not a worktree anchor

The operation parameter is used in error messages (e.g., "move", "pluck", "delete").

func ValidateTargetBranch

func ValidateTargetBranch(eng BranchValidationEngine, sourceName, targetName, operation string) error

ValidateTargetBranch validates that a target branch can be used for reparenting operations like move or pluck. Returns nil if valid, error otherwise.

Checks performed:

  • Target is provided (not empty)
  • Target exists (as trunk, tracked, or untracked git branch)
  • Target is not a worktree anchor
  • Target is not the same as source

The operation parameter is used in error messages (e.g., "move", "pluck").

Types

type BranchValidationEngine

type BranchValidationEngine interface {
	ValidateOnBranch() (string, error)
	GetBranch(branchName string) engine.Branch
	CurrentBranch() *engine.Branch
	AllBranches() []engine.Branch
}

BranchValidationEngine is the minimal engine contract needed by validators. Keeping this narrow avoids coupling callers to the full engine.Engine surface.

type Chain

type Chain []Validator

Chain is a sequence of validators that are checked in order. Validation stops at the first error.

func AbsorbChain

func AbsorbChain(ctx context.Context, eng BranchValidationEngine, g git.Runner, operation string) Chain

AbsorbChain validates for absorb operations (works with staged changes). Checks: on branch, not trunk, modifiable, no rebase in progress.

func GitOperationChain

func GitOperationChain(ctx context.Context, eng BranchValidationEngine, g git.Runner, operation string) Chain

GitOperationChain validates for git history modifications (fold, pop, reorder). Checks: on branch, not trunk, tracked, no rebase in progress, no uncommitted changes, modifiable.

func ModifyBranchChain

func ModifyBranchChain(eng BranchValidationEngine, operation string) Chain

ModifyBranchChain validates for simple branch modifications (rename, scope). Checks: on branch, not trunk, modifiable.

func (Chain) Validate

func (c Chain) Validate() error

Validate runs all validators in sequence and returns the first error.

type Validator

type Validator interface {
	// Validate checks the precondition and returns an error if it fails.
	Validate() error
}

Validator validates a single precondition.

func BranchMustBeModifiable

func BranchMustBeModifiable(eng BranchValidationEngine, branchName string) Validator

BranchMustBeModifiable validates that a branch can be modified (not locked, frozen, or worktree anchor).

func BranchMustBeTracked

func BranchMustBeTracked(eng BranchValidationEngine, branchName string) Validator

BranchMustBeTracked validates that a branch is tracked by stackit.

func BranchMustExist

func BranchMustExist(g git.Runner, branchName string) Validator

BranchMustExist validates that a branch exists in git. Uses git.Runner to check if the branch ref exists.

func BranchMustNotBeTrunk

func BranchMustNotBeTrunk(eng BranchValidationEngine, branchName string) Validator

BranchMustNotBeTrunk validates that a branch is not the trunk branch.

func CurrentBranchMustBeModifiable

func CurrentBranchMustBeModifiable(eng BranchValidationEngine) Validator

CurrentBranchMustBeModifiable validates that the current branch can be modified.

func CurrentBranchMustBeTracked

func CurrentBranchMustBeTracked(eng BranchValidationEngine) Validator

CurrentBranchMustBeTracked validates that the current branch is tracked by stackit.

func CurrentBranchMustNotBeTrunk

func CurrentBranchMustNotBeTrunk(eng BranchValidationEngine, operation string) Validator

CurrentBranchMustNotBeTrunk validates that the current branch is not trunk. The operation parameter is used in the error message (e.g., "reorder", "fold").

func MustBeOnBranch

func MustBeOnBranch(eng BranchValidationEngine) Validator

MustBeOnBranch validates that HEAD is on a branch (not detached). Returns a Validator that checks the engine's current branch state.

func MustHaveStagedChanges

func MustHaveStagedChanges(ctx context.Context, g git.Runner) Validator

MustHaveStagedChanges validates that there are staged changes ready to commit.

func MustNotHaveRebaseInProgress

func MustNotHaveRebaseInProgress(ctx context.Context, g git.Runner) Validator

MustNotHaveRebaseInProgress validates that no rebase operation is in progress.

func MustNotHaveUncommittedChanges

func MustNotHaveUncommittedChanges(ctx context.Context, g git.Runner) Validator

MustNotHaveUncommittedChanges validates that there are no uncommitted changes.

func SourceBranchMustBeValid

func SourceBranchMustBeValid(eng BranchValidationEngine, branchName, operation string) Validator

SourceBranchMustBeValid validates that a source branch can be used for operations like move, pluck, or delete. It checks:

  • Branch is not trunk
  • Branch is tracked by stackit
  • Branch is not a worktree anchor

The operation parameter is used in error messages (e.g., "move", "pluck", "delete").

func TargetBranchMustBeValid

func TargetBranchMustBeValid(eng BranchValidationEngine, sourceName, targetName, operation string) Validator

TargetBranchMustBeValid validates that a target branch can be used for reparenting operations like move or pluck. It checks:

  • Target is provided (not empty)
  • Target exists (as trunk, tracked, or untracked git branch)
  • Target is not a worktree anchor
  • Target is not the same as source

The operation parameter is used in error messages (e.g., "move", "pluck").

type ValidatorFunc

type ValidatorFunc func() error

ValidatorFunc allows using a function as a Validator.

func (ValidatorFunc) Validate

func (f ValidatorFunc) Validate() error

Validate implements Validator.

Jump to

Keyboard shortcuts

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