hook

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package hook runs lifecycle hooks defined in .treepad.toml.

Index

Constants

This section is empty.

Variables

CutEvents are the events a worktree cut fires, in the order it fires them. PreSync and PostSync are on the list because config sync happens inside the cut, so a caller that only knew about pre_new and post_new would miss half of what a cut can run. Declared beside the constants so a new event is named and listed in the same file.

View Source
var TeardownEvents = []Event{PreRemove, PostRemove}

Functions

func Run

func Run(ctx context.Context, r Runner, cfg Config, e Event, data Data) error

Run executes the hooks for event e, returning an error on any hook failure.

func ShouldRun

func ShouldRun(entry HookEntry, branch string) bool

ShouldRun reports whether a hook entry should execute for the given branch. If Only is set, the branch must match at least one pattern. If Except is set, the branch must not match any pattern. Both conditions apply when both are set (AND semantics).

Types

type CommandRunner

type CommandRunner interface {
	Run(ctx context.Context, name string, args ...string) ([]byte, error)
}

type Config

type Config struct {
	PreNew         []HookEntry `toml:"pre_new,omitempty"`
	PostNew        []HookEntry `toml:"post_new,omitempty"`
	PreRemove      []HookEntry `toml:"pre_remove,omitempty"`
	PostRemove     []HookEntry `toml:"post_remove,omitempty"`
	PreSync        []HookEntry `toml:"pre_sync,omitempty"`
	PostSync       []HookEntry `toml:"post_sync,omitempty"`
	PostConfigInit []HookEntry `toml:"post_config_init,omitempty"`
}

Config holds the hook entries for each event.

func (Config) For

func (c Config) For(e Event) []HookEntry

func (Config) IsZero

func (c Config) IsZero() bool

type Data

type Data struct {
	Branch       string // raw branch name
	WorktreePath string // absolute path of the worktree on disk
	Slug         string // repo slug
	HookType     string // event being fired, e.g. "post_new"
	OutputDir    string // artifact output directory
}

Data is the context available when rendering hook command templates.

type Event

type Event string

Event identifies a lifecycle point in a treepad operation.

const (
	PreNew         Event = "pre_new"
	PostNew        Event = "post_new"
	PreRemove      Event = "pre_remove"
	PostRemove     Event = "post_remove"
	PreSync        Event = "pre_sync"
	PostSync       Event = "post_sync"
	PostConfigInit Event = "post_config_init"
)

type ExecRunner

type ExecRunner struct {
	Runner CommandRunner
	TTY    TTYRunner
}

ExecRunner renders each hook command as a text/template and executes it via sh -c.

func (ExecRunner) Run

func (e ExecRunner) Run(ctx context.Context, hooks []HookEntry, data Data) error

Run executes each hook entry sequentially, skipping entries whose branch filters do not match data.Branch, stopping on the first error.

type HookEntry

type HookEntry struct {
	Command     string   `toml:"command"`
	Only        []string `toml:"only"`
	Except      []string `toml:"except"`
	Interactive bool     `toml:"interactive,omitempty"`
}

HookEntry is a single hook command with optional branch filters. Only and Except use glob patterns (** crosses path separators). If Only is non-empty the branch must match at least one pattern. If Except is non-empty the branch must not match any pattern. Both conditions apply when both are set.

Interactive gives the command the caller's terminal so it can prompt. Without it the command runs with no TTY and its output is discarded, which makes pickers and confirmation prompts silently fall back to non-interactive mode.

type PostErr

type PostErr struct {
	Event Event
	Err   error
}

PostErr holds a post-hook failure. The caller should log it as a warning — post failures are non-blocking; the main operation is already complete.

func RunSandwich

func RunSandwich(
	ctx context.Context, p profile.Profiler, r Runner, cfg Config,
	pre, post Event, data Data, do func() error,
) (*PostErr, error)

RunSandwich runs pre → do → post. Pre failure aborts and returns an error. Post failure returns a non-nil *PostErr with a nil main error — the caller should log it as a warning. p times the pre and post hook phases as "<event>_hooks" stages.

func (*PostErr) Error

func (p *PostErr) Error() string

type Runner

type Runner interface {
	Run(ctx context.Context, hooks []HookEntry, data Data) error
}

Runner executes a list of hook entries with the provided data.

type TTYRunner

type TTYRunner interface {
	Run(ctx context.Context, dir, name string, args ...string) (int, error)
}

TTYRunner executes a command with the caller's terminal inherited, returning the child's exit code. A non-zero exit is reported through the code, not the error. Satisfied by passthrough.Runner.

Jump to

Keyboard shortcuts

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