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

hooks

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package hooks works with lefthook: reading what a run of it printed, reading its configuration, and writing a configuration for a repository whose hooks predate it.

Index

Constants

View Source
const MaxPlaces = 1000

MaxPlaces is the most places a FailureScan keeps. A run folds every line of its output into the scan as the line arrives, so an output naming ever more places would otherwise grow the scan, and the work of keeping each place once, without limit. Past it the scan keeps the first places, which are usually the causes of the rest.

Variables

View Source
var ErrUnreadableConfiguration = errors.New("lefthook's configuration could not be read")

ErrUnreadableConfiguration reports a dump of lefthook's configuration that is not one.

Functions

func DumpCommand added in v0.10.0

func DumpCommand(dir string) proc.Command

DumpCommand prints lefthook's configuration as JSON, merged as a run of it reads it: the repository's file, its local override, and the remotes it names, from lefthook's own cache.

lefthook logs on the standard output it dumps to, so a LEFTHOOK_VERBOSE this process inherited would put its debug lines ahead of the JSON. The command turns it off; a duplicated variable takes its last value.

func HasConfig

func HasConfig(repo fs.FS) bool

HasConfig reports whether a repository already configures lefthook. A local override alone does not count: it amends a configuration rather than being one.

func InstallCommand added in v0.10.0

func InstallCommand(dir string) proc.Command

InstallCommand installs lefthook's hooks in a repository: its root, not wherever the process happens to be.

func RunCommand

func RunCommand(dir, hook string) proc.Command

RunCommand runs one hook through lefthook, as git would, without committing.

func Write

func Write(dir string, generated Generated) error

Write creates the configuration and its scripts in a repository. It never replaces anything: not a lefthook configuration under any name lefthook reads, and not an existing script.

Everything is created through the repository opened as a root, which no path can leave: the repository's own tree decides what .lefthook is, and a clone can make it a link to anywhere at all.

Types

type Configured added in v0.10.0

type Configured struct {
	// Runnable is the hooks a run by hand can stand in for: git's own in the
	// order git documents them, then the configuration's own names, sorted.
	Runnable []string
	// GitOnly is the hooks only git can run, in git's order.
	GitOnly []GitOnly
}

Configured is the hooks lefthook's merged configuration defines, split by whether a run by hand can stand in for git's.

func ReadConfigured added in v0.10.0

func ReadConfigured(
	ctx context.Context, dir string, run func(context.Context, proc.Command) ([]byte, error),
) (Configured, error)

ReadConfigured reads the hooks lefthook's configuration in dir defines, asking lefthook through run for the configuration merged as a run of it reads it: whichever file lefthook finds, its local override even alone, or the file LEFTHOOK_CONFIG names. lefthook alone knows every place it looks, so it is always asked. It refuses to dump where it finds no configuration, and that refusal, from a directory holding no file lefthook reads, is no hook at all rather than a failure.

type FailureScan added in v0.4.0

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

FailureScan reads where a hook's tools pointed one output line at a time, as NextJob reads its jobs, so a run can keep the places as its output streams in: by the time a long run ends, its first lines are gone. Start one with NewFailureScan.

func NewFailureScan added in v0.4.0

func NewFailureScan(goos string) FailureScan

NewFailureScan starts reading a hook's output. goos is the running platform, which decides whether a Windows drive letter is read as part of a path.

func (FailureScan) Next added in v0.4.0

func (s FailureScan) Next(raw string) FailureScan

Next folds one more output line in. It does not modify the scan it is given.

The ESLint file is carried only while its rows follow it: a header opens a block when the next line is one of its rows, and any line that is neither a row nor a new header ends the block — so a blank line, or a later tool's output, is never read against a stale filename.

func (FailureScan) Places added in v0.4.0

func (s FailureScan) Places() []Location

Places is every place found so far, once each, in the order they appeared. A held line that nothing followed is read as any other line is.

type File

type File struct {
	// Path is relative to the repository root.
	Path     string
	Contents string
}

File is a file a generated configuration needs written.

type Generated

type Generated struct {
	Config  string
	Scripts []File
}

Generated is a lefthook configuration and the scripts it runs.

func Structured

func Structured(hooks []GitHook) Generated

Structured writes a configuration that runs each hook of plain commands as lefthook jobs, and keeps any other hook as a script. Only a hook whose every line is a command lefthook can run on its own is converted: one that reads its arguments, branches, or sets variables is kept whole rather than guessed at.

func Verbatim

func Verbatim(hooks []GitHook) Generated

Verbatim writes a configuration that keeps every hook as the script it is.

type GitHook

type GitHook struct {
	Name   string
	Script string
}

GitHook is a hook script found in the hooks directory.

func ExistingHooks

func ExistingHooks(hooksDir fs.FS, goos string) []GitHook

ExistingHooks finds the hooks git would run from a hooks directory, in git's order: files named for a hook, runnable as one, that are not lefthook's own shim. Whether a file is runnable is decided by goos, since Windows has no executable bit to check.

type GitOnly added in v0.10.0

type GitOnly struct {
	Name   string
	Reason string
}

GitOnly is a hook only git can run, and why: what git hands it that a run by hand would not.

type Job

type Job struct {
	Name  string
	State JobState
	// Duration is lefthook's own words for how long it took, once reported.
	Duration string
}

Job is one job in a hook run.

func NextJob added in v0.0.6

func NextJob(jobs []Job, inSummary bool, raw string) ([]Job, bool)

NextJob folds one more output line into the jobs seen so far, returning the updated jobs and whether the summary has begun: a job is running from its header until the summary reports it. It does not modify the jobs it is given, so a caller streaming output can keep the running set on its own value and never re-read the whole output — a chatty hook would otherwise cost a full re-parse on every line, and again on every redraw.

type JobState

type JobState int

JobState is how a job in a hook run stands.

const (
	// JobRunning has started and not yet been reported on.
	JobRunning JobState = iota
	// JobPassed finished successfully.
	JobPassed
	// JobFailed finished and failed the hook.
	JobFailed
	// JobSkipped did not run: no files matched it, or its skip rule applied.
	JobSkipped
)

type Location

type Location struct {
	File string
	Line int
	// Column is zero where the tool gave none.
	Column  int
	Message string
}

Location is a place in a file a tool pointed at.

Jump to

Keyboard shortcuts

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