githooks

package
v0.2.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package githooks installs baum's Git hook dispatchers into a clone, so Git reports the commits made there to the daemon without parsing anything.

A dispatcher runs baum's hook, then the hooks the clone had before, which install moved to <hook>.pre-baum. A hook baum cannot preserve is reported and left as it is, never replaced. `git config baum.hooks false` opts a clone out: Ensure then takes the dispatchers out again.

Index

Constants

View Source
const (
	// Marker identifies a dispatcher baum wrote.
	Marker = "# Managed by the baum desktop app."
)

Variables

View Source
var Hooks = []string{"post-commit", "post-rewrite", "pre-push"}

Hooks are the Git hooks baum installs a dispatcher for.

Functions

func Dispatcher

func Dispatcher(hook, baum string) string

Dispatcher is the script installed for hook. Hooks whose input Git feeds on standard input buffer it, so baum and the chained hooks each read all of it; when it cannot be buffered, baum is not run and the chained hooks read the original input. Baum never fails a hook; only a chained pre-push hook can stop a push.

Types

type Clone

type Clone struct {
	ID, Path, CommonDir string
}

Clone is a registered clone as the service needs it.

type HookResult

type HookResult struct {
	Hook    string
	Outcome Outcome
	Reason  string
}

HookResult is the outcome for one hook.

type Options

type Options struct {
	// Baum is the absolute path of the baum binary a dispatcher falls back to
	// when none is on PATH; empty for none.
	Baum string
	// Env is added to the environment of the git commands.
	Env []string
}

Options adjust Ensure and Restore.

type Outcome

type Outcome string

Outcome is what Ensure did for one hook.

const (
	// Installed means the clone had no hook and now has the dispatcher.
	Installed Outcome = "installed"
	// Current means the dispatcher was already in place.
	Current Outcome = "current"
	// Repaired means an outdated or legacy dispatcher was replaced.
	Repaired Outcome = "repaired"
	// Chained means another tool's hook was moved to <hook>.pre-baum, which
	// the dispatcher runs after baum's.
	Chained Outcome = "chained"
	// Unclaimed means the hook was left as it is; Reason says why.
	Unclaimed Outcome = "unclaimed"
	// Restored means the clone opted out and its own hook is back in place.
	Restored Outcome = "restored"
	// Removed means the clone opted out and the dispatcher, which had no hook
	// to restore, is gone.
	Removed Outcome = "removed"
	// Absent means the clone opted out and has no dispatcher of baum's.
	Absent Outcome = "absent"
)

type Report

type Report struct {
	// Skipped is why the clone got no hooks at all, empty when it was
	// examined.
	Skipped string
	Hooks   []HookResult
}

Report is what Ensure did for one clone.

func Ensure

func Ensure(ctx context.Context, root string) (Report, error)

Ensure installs the dispatchers into the clone at root, repairing outdated ones, or takes them out when the clone opted out. It is idempotent and never deletes or follows a file it did not write. It goes on past a hook it cannot handle and returns what failed, joined.

func EnsureWith

func EnsureWith(ctx context.Context, root string, opts Options) (Report, error)

EnsureWith is Ensure with options.

func Restore

func Restore(ctx context.Context, root string, opts Options) (Report, error)

Restore takes baum's dispatchers out of the clone at root and puts back the hooks they replaced, as Ensure does for a clone that opted out. The daemon uses it for a clone it no longer registers.

func (Report) Changed

func (r Report) Changed() bool

Changed reports whether Ensure wrote anything.

type Service

type Service struct {
	// Clones lists the available registered clones.
	Clones func(ctx context.Context) ([]Clone, error)
	// Registered lists every registered clone, available or not; Removed uses
	// it to leave hooks that another clone of the same repository shares.
	Registered func(ctx context.Context) ([]Clone, error)
	// Ensure and Restore act on one clone; nil are EnsureWith and Restore.
	Ensure  func(ctx context.Context, root string) (Report, error)
	Restore func(ctx context.Context, root string) (Report, error)
	// Options are passed to the default Ensure and Restore.
	Options Options
	Log     *slog.Logger
	// Every is the period of the sweep; zero is a day.
	Every time.Duration
	// contains filtered or unexported fields
}

Service keeps the dispatchers installed in the registered clones: for a clone as it is registered, and for every available clone at start and daily after, which repairs dispatchers an update outdated or a tool replaced. A clone that is no longer registered gets its own hooks back.

func (*Service) Added

func (s *Service) Added(c Clone)

Added ensures a clone just registered, in the background; it never blocks the registration or fails it.

func (*Service) All

func (s *Service) All(ctx context.Context)

All ensures every available clone.

func (*Service) Removed

func (s *Service) Removed(c Clone)

Removed gives a clone no longer registered its own hooks back, in the background, unless another registered clone shares its hooks.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run ensures every clone at once and then every period, until ctx ends.

Jump to

Keyboard shortcuts

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