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 v1" on their second line. The package never runs git: the caller reads the repository layout through internal/gitx.
Index ¶
- Constants
- Variables
- func ChainedTakenLine(e *ChainedTakenError) string
- func HooksPathSnippet(t Target) string
- func HuskySnippet(t Target) string
- func InstallLines(res Result) []string
- func LefthookSnippet(t Target) string
- func PreCommitHookID(t Target) string
- func PreCommitSnippet(t Target) string
- func Script(t Target) []byte
- func UndoLine(t Target) string
- func UninstallLines(res Result) []string
- type ChainedTakenError
- type Change
- type ChangeKind
- type Manager
- type Repo
- type Result
- type Target
Constants ¶
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).
const ( NameHusky = "Husky" NameLefthook = "lefthook" NamePreCommit = "pre-commit" NameHooksPath = "core.hooksPath" )
Manager names.
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.
const ChainedSuffix = ".tokenhound-chained"
ChainedSuffix is appended to a hook's name when an existing hook is chained.
const Marker = "# tokenhound-managed v1"
Marker is the exact second line of every hook script TokenHound writes.
Variables ¶
var ErrWrite = errors.New("githook: hook file operation failed")
ErrWrite: a hook file could not be read, written, moved or removed.
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 ¶
HooksPathSnippet is the line for <core.hooksPath>/<hook>.
func InstallLines ¶
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 ¶
LefthookSnippet is the lefthook.yml block for t.
func PreCommitHookID ¶
PreCommitHookID is the id of t in .pre-commit-hooks.yaml.
func PreCommitSnippet ¶
PreCommitSnippet is the .pre-commit-config.yaml block for t.
func Script ¶
Script returns the hook script of t: POSIX sh, LF line endings, Marker on its second line, and no timestamp, path or version, so a reinstall compares bytes.
func UninstallLines ¶
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
}
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 ¶
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
}
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 ¶
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.