Skip to content

Latest commit

 

History

History
157 lines (127 loc) · 8.91 KB

hook_install.md

File metadata and controls

157 lines (127 loc) · 8.91 KB

hook_install.sh

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

This is a library. It is sourced, never executed. Mode rw-rw-r--. Its header carries Last verified: 2026-10-29.

scripts/lib/hook_install.sh is the one implementation of "install a git hook into a consumer repository safely" (§11.4.164 hook wiring). It is shared by scripts/setup.sh (steps 2, 3, 3b) and scripts/post_update_hook.sh (STEP 4), so both apply the same scope policy and the same backup rules instead of drifting apart.

The policy it encodes, in plain terms:

  • A machine-wide hooks directory is refused. If core.hooksPath is set at global, system or command-line scope, the installer will not write there — one project must not install hooks that fire for every repository on the machine. local / worktree scope is fine.
  • A physical hooks dir outside the repository is refused. Symlinks are resolved first, so a symlinked .git/hooks pointing elsewhere counts as "elsewhere".
  • An existing hook is backed up before it is replaced, and the backup never overwrites an earlier backup.
  • A symlinked hook is backed up as a link, never dereferenced — plus a .link text record, because a relative target would dangle from the backup directory.
  • A non-regular file is never opened. A FIFO, socket, device or directory at the hook path is refused rather than read (reading a FIFO could block forever).
  • Installation is atomic — a temp file in the hooks dir, chmod +x, then rename.
  • A committed hooks directory is a deliberate project choice, so chi_tracked_hooks_dir reports it and the callers warn (the old hooks are backed up first) rather than refuse.

Prerequisites

  • bash, git, cp, cmp, chmod, mv, mktemp, readlink, dirname, basename, head.
  • The caller must define two printers: info and error (used for every human message).
  • BACKUP_ROOT must be <git common dir>/constitution-hook-backup — never inside a work tree and never in a shared hooks dir.
  • STAMP must match [A-Za-z0-9._-]+; the caller checks this, not the library.
  • Ambient GIT_DIR & co: every git call goes through chi__git, which is cl__git from scripts/lib/constitution_layout.sh — sourced automatically when the caller has not done so and a resolver sits beside this file. A lone copy (no resolver beside it) falls back to an inline scrub with the same variable list, kept equal by tests/layout/test_round8_git_env.sh.

Usage examples

. "$CONSTITUTION/scripts/lib/hook_install.sh"

# resolve a physical path (every symlink resolved; works for a path that does not exist yet)
p="$(chi_phys "$repo/.git/hooks")"

# is it inside the repository? (string test, callers pass physical paths)
chi_under "$p" "$repo" && echo "inside"

# may we install here?
if chi_hooks_dir_check "$repo" "$hooks_dir"; then
  chi_install_hook "$src_hook" post-merge constitution "$hooks_dir" "$backup_root" "$stamp"
else
  error "refused: $CHI_WHY"
fi

# the constitution SUBMODULE's consumer hook may only use the submodule's own git dir
chi_hooks_dir_check "$sub" "$hooks_dir" gitdir

# a committed hooks dir: warn, do not refuse
if chi_tracked_hooks_dir "$repo" "$hooks_dir"; then
  info "note: $CHI_TRACKED_PHYS holds TRACKED files; installing rewrites committed project hooks"
fi

# back up a hook on its own
chi_backup_hook "$hooks_dir/pre-commit" pre-commit constitution "$backup_root" "$stamp"

Exit codes / return values

Function Returns Meaning
chi_phys PATH 0 + prints the physical path (symlinks resolved; for a non-existent leaf, the physical parent + remaining name)
1 unresolvable (or a symlink chain deeper than 40 hops)
chi_under PATH BASE 0 / 1 PATH is BASE or below it (string test on the trailing-slash form)
chi_hooks_dir_check REPO HOOKS_DIR [MODE] 0 allowed; MODE is repo (default) or gitdir
1 refused; CHI_WHY holds one sentence saying why
chi_tracked_hooks_dir REPO HOOKS_DIR 0 the physical hooks dir is in REPO's work tree and holds files tracked by REPO; CHI_TRACKED_PHYS holds that physical dir
1 not tracked / not inside
chi_backup_hook DST NAME LABEL BACKUP_ROOT STAMP 0 backed up, or already backed up (the same hook present is reused)
1 not backed up; the reason was printed
chi_install_hook SRC NAME LABEL HOOKS_DIR BACKUP_ROOT STAMP 0 installed, or unchanged (an identical regular file already there)
1 not installed; the reason was printed

Variables set for the caller: CHI_WHY (by chi_hooks_dir_check), CHI_TRACKED_PHYS (by chi_tracked_hooks_dir).

Edge cases

  • core.hooksPath at non-local scope is refused with a sentence naming the scope and the value. That is the machine-wide case: one project must not install hooks for every repository on the host.
  • A symlinked hooks dir that resolves outside the repo is refused, and CHI_WHY names both the given path and the resolved path, so the reason is diagnosable in one step.
  • A non-existent hooks dir path is still resolvable by chi_phys (physical parent + remaining name), which is why the check works before the directory exists.
  • A symlink chain longer than 40 hops — chi_phys returns 1 rather than looping.
  • An existing hook that is a symlink is backed up as a link (ln -s, verified by reading it back) plus NAME.link containing TARGET=, ABSOLUTE= and a ready-to-run RESTORE= line — because a relative target would dangle from the backup directory.
  • An existing hook that is a FIFO/socket/device/directory is refused without being opened, both by the backup function and by the installer ("refused, NOT replaced").
  • Backup collisions — NAME.1 … NAME.999; the same hook already present is reused (compared with cmp -s for a regular file, by target string for a symlink) rather than duplicated. Beyond 999 the function fails rather than overwriting.
  • A regular-file copy uses cp -p (mode preserved) and is verified with cmp -s; a failed copy is a failure, not a silent partial backup.
  • An identical hook already installed is left alone and reported as NAME unchanged — but a missing execute bit is repaired with chmod +x.
  • A backup that fails means the hook is NOT replaced — the installer aborts rather than overwriting an unbacked-up hook.
  • A write failure into the hooks dir leaves no stray temp file (rm -f "$tmp" on the failure path).

Internal behaviour

  1. On load it makes chi__git available, sourcing constitution_layout.sh from beside itself when cl__git is absent, else defining an inline scrub with the identical variable list.
  2. chi_phys walks up to an existing directory, cd -P/pwd -P to resolve it, then re-appends the remaining components, resolving any symlink it meets (relative targets resolved against the containing directory).
  3. chi_hooks_dir_check reads git config --show-scope --get core.hooksPath (scope + value in one call), refuses any scope other than local/worktree, resolves --absolute-git-dir and --path-format=absolute --git-common-dir, resolves the physical hooks dir, then accepts when it is under the git dir or the common dir — or, in repo mode, under the work tree top level.
  4. chi_tracked_hooks_dir resolves the physical hooks dir and top level, checks containment, then asks git ls-files (with --literal-pathspecs) whether anything under that relative path is tracked.
  5. chi_backup_hook classifies the destination (symlink / regular file / other → refuse), creates BACKUP_ROOT/STAMP/LABEL, then finds the first free NAME/NAME.N, reusing an identical existing backup.
  6. chi_install_hook refuses a non-regular destination, short-circuits an identical file (cmp -s, repairing the execute bit), backs up anything else, then writes a mktemp temp file in the hooks dir, chmod +x and mv -f into place.

Related scripts

  • scripts/lib/constitution_layout.sh — supplies cl__git and the canonical scrub list.
  • scripts/setup.sh — caller (steps 2, 3, 3b).
  • scripts/post_update_hook.sh — caller (STEP 4).
  • scripts/hooks/post-merge, scripts/hooks/constitution-submodule-post-checkout — the hooks typically installed.
  • tests/layout/test_setup_layout.sh, tests/layout/test_round4_hardening.sh, tests/layout/test_round6_hardening.sh, tests/layout/test_round8_git_env.sh — the binding suites.

Last verified

2026-12-04 — bash -n scripts/lib/hook_install.sh → exit 0 (a library: sourced, not executed).

Behaviour a reader can repeat without installing anything (pure path predicates):

bash -c '. scripts/lib/hook_install.sh; p="$(chi_phys "$PWD")" && echo "phys=$p"; \
  chi_under "$PWD/docs" "$PWD" && echo under-ok'

Expected: phys=<this checkout, symlinks resolved> then under-ok.