githook

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package githook installs and uninstalls the TokenHound git pre-commit and pre-push hooks with a single owner (AD-18). It writes under the repository's hooks directory only when no hook manager (Husky, lefthook, the pre-commit framework or another core.hooksPath) owns the hooks; with one, it returns the manager's snippet instead. An existing hook is chained: it moves to <hook>.tokenhound-chained, runs first, and is restored byte for byte on uninstall. The generated scripts are deterministic POSIX sh, marked with "# tokenhound-managed v2" on their second line (v1 hooks of earlier releases are still recognised and rewritten on install). The package never runs git: the caller reads the repository layout through internal/gitx.

Index

Constants

View Source
const (
	// NotRepoLine: the working directory is outside a git work tree (exit 2).
	NotRepoLine = "Not in a git repo: no git hook installed."
	// NoGitLine: git is not on PATH (exit 2).
	NoGitLine = "git not found: install git to install a git hook"
)

Fixed one-line messages of the install and uninstall verbs (UX-DR13).

View Source
const (
	MissingEnv   = "TOKENHOUND_HOOK_MISSING"
	MissingAllow = "allow"
)

MissingEnv and MissingAllow are the opt-out of the fail-closed hook: with TOKENHOUND_HOOK_MISSING=allow a hook whose tokenhound is not on PATH lets the commit or push through with a warning instead of blocking it.

View Source
const (
	NameHusky     = "Husky"
	NameLefthook  = "lefthook"
	NamePreCommit = "pre-commit"
	NameHooksPath = "core.hooksPath"
)

Manager names.

View Source
const (
	PreCommitRepo = "https://github.com/aiagentpro/tokenhound"
	PreCommitRev  = "v1.0.0"
)

PreCommitRepo and PreCommitRev are the repository and the release the pre-commit framework snippet pins.

View Source
const ChainedSuffix = ".tokenhound-chained"

ChainedSuffix is appended to a hook's name when an existing hook is chained.

View Source
const InstallURL = "https://github.com/aiagentpro/tokenhound#install"

InstallURL is where the blocked-hook message sends the user to install TokenHound.

View Source
const Marker = "# tokenhound-managed v2"

Marker is the exact second line of every hook script TokenHound writes. v2 hooks fail closed when tokenhound is not found.

Variables

View Source
var ErrWrite = errors.New("githook: hook file operation failed")

ErrWrite: a hook file could not be read, written, moved or removed.

View Source
var Targets = []Target{PreCommit, PrePush}

Targets lists every target in install order.

Functions

func ChainedTakenLine

func ChainedTakenLine(e *ChainedTakenError) string

ChainedTakenLine is the one line printed for a *ChainedTakenError (exit 2).

func HooksPathSnippet

func HooksPathSnippet(t Target) string

HooksPathSnippet is the line for <core.hooksPath>/<hook>.

func HuskySnippet

func HuskySnippet(t Target) string

HuskySnippet is the line for .husky/<hook>.

func InstallLines

func InstallLines(res Result) []string

InstallLines renders an install Result: one line per file changed then the Undo line, the "Already installed" line, or the manager's snippet with its file.

func LefthookSnippet

func LefthookSnippet(t Target) string

LefthookSnippet is the lefthook.yml block for t.

func PreCommitHookID

func PreCommitHookID(t Target) string

PreCommitHookID is the id of t in .pre-commit-hooks.yaml.

func PreCommitSnippet

func PreCommitSnippet(t Target) string

PreCommitSnippet is the .pre-commit-config.yaml block for t.

func Script

func Script(t Target) []byte

Script returns the hook script of t without a recorded program (see ScriptFor).

func ScriptFor added in v1.0.0

func ScriptFor(t Target, program string) []byte

ScriptFor returns the hook script of t: POSIX sh, LF line endings, Marker on its second line, and no timestamp or version, so a reinstall compares bytes. program, when set, is the absolute path of the tokenhound that installed the hook: the hook runs it when tokenhound is not on PATH (a GUI git client often has a short PATH). Without either it fails closed, unless TOKENHOUND_HOOK_MISSING=allow.

func UndoLine

func UndoLine(t Target) string

UndoLine is the last line of an install that changed files.

func UninstallLines

func UninstallLines(res Result) []string

UninstallLines renders an uninstall Result: one line per file changed, or "Not installed".

Types

type ChainedTakenError

type ChainedTakenError struct {
	// Hook and Chained are display paths.
	Hook, Chained string
}

ChainedTakenError is returned by Install when a foreign hook and its chained name both exist, so the hook cannot be chained. Nothing was written.

func (*ChainedTakenError) Error

func (e *ChainedTakenError) Error() string

type Change

type Change struct {
	Kind ChangeKind
	// Path is the file changed (for Moved and Restored, the source).
	Path string
	// To is the destination of Moved and Restored.
	To string
	// Chained says the managed hook runs a chained hook first (Wrote, Updated).
	Chained bool
	// Outdated says Updated replaced a hook of an earlier marker version (one that
	// let the commit or push through when tokenhound was not on PATH).
	Outdated bool
}

Change is one file changed by Install or Uninstall. Paths are for display: relative to the top level with forward slashes when inside it, else absolute.

type ChangeKind

type ChangeKind int

ChangeKind is what happened to one file.

const (
	// Wrote: a new managed hook was written.
	Wrote ChangeKind = iota + 1
	// Updated: an older managed hook was rewritten.
	Updated
	// Moved: a foreign hook was moved to its chained name.
	Moved
	// Removed: the managed hook was deleted.
	Removed
	// Restored: the chained hook was moved back to its own name.
	Restored
)

The change kinds.

type Manager

type Manager struct {
	// Name is the manager's display name ("Husky", "lefthook", "pre-commit",
	// "core.hooksPath").
	Name string
	// File is where the snippet belongs, relative to the top level (slash path), or
	// a hooks path as configured.
	File string
	// Snippet is the text to add to File, ending in a newline.
	Snippet string
}

Manager is a hook manager that owns a repository's git hooks, with the snippet that makes it run TokenHound.

func Detect

func Detect(top, hooksPath string, t Target) *Manager

Detect returns the manager that owns the hooks of the work tree at top, given the effective core.hooksPath (empty when unset), or nil when none does. The first match wins: Husky (a .husky directory, or a hooks path that is or sits under .husky), lefthook, the pre-commit framework, then any other hooks path.

type Repo

type Repo struct {
	// TopLevel is the absolute top-level directory of the work tree.
	TopLevel string
	// CommonDir is the absolute common git directory; hooks live in its hooks/.
	CommonDir string
	// HooksPath is the effective core.hooksPath ("" when unset).
	HooksPath string
	// Program is the absolute path of the tokenhound binary that runs the install,
	// recorded in the script and used when tokenhound is not on PATH (a GUI git
	// client's short PATH); "" records none.
	Program string
}

Repo is the layout of a repository, as internal/gitx reads it.

type Result

type Result struct {
	Target Target
	// Changes lists the files changed, in order.
	Changes []Change
	// Hook is the display path of the hook file.
	Hook string
	// AlreadyInstalled: the managed hook is current; nothing was written.
	AlreadyInstalled bool
	// NotInstalled: Uninstall found no managed hook; nothing was changed.
	NotInstalled bool
	// Manager, when set, owns the hooks: nothing was written.
	Manager *Manager
}

Result is the outcome of Install or Uninstall for one target.

func Install

func Install(r Repo, t Target) (Result, error)

Install installs the hook of t in r, unless a manager owns the hooks. A current managed hook is left alone, an older one is rewritten, and a foreign hook is moved to its chained name first. It returns *ChainedTakenError when the chained name is taken, and an error matching ErrWrite when a file operation fails.

func Uninstall

func Uninstall(r Repo, t Target) (Result, error)

Uninstall removes the managed hook of t from r and moves a chained hook back, byte for byte with its mode. A missing or foreign hook is left alone (NotInstalled).

type Target

type Target string

Target is a git hook TokenHound installs.

const (
	PreCommit Target = "pre-commit"
	PrePush   Target = "pre-push"
)

The targets.

func (Target) Command

func (t Target) Command() string

Command is the TokenHound command the hook runs.

func (Target) Flag

func (t Target) Flag() string

Flag is the command-line flag that selects t ("--pre-commit").

Jump to

Keyboard shortcuts

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