Revision: 1 Last modified: 2026-12-04T18:15:21Z
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.hooksPathis set atglobal,systemor command-line scope, the installer will not write there — one project must not install hooks that fire for every repository on the machine.local/worktreescope is fine. - A physical hooks dir outside the repository is refused. Symlinks are resolved first, so a symlinked
.git/hookspointing 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
.linktext 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, thenrename. - A committed hooks directory is a deliberate project choice, so
chi_tracked_hooks_dirreports it and the callers warn (the old hooks are backed up first) rather than refuse.
bash,git,cp,cmp,chmod,mv,mktemp,readlink,dirname,basename,head.- The caller must define two printers:
infoanderror(used for every human message). BACKUP_ROOTmust be<git common dir>/constitution-hook-backup— never inside a work tree and never in a shared hooks dir.STAMPmust match[A-Za-z0-9._-]+; the caller checks this, not the library.- Ambient
GIT_DIR& co: every git call goes throughchi__git, which iscl__gitfromscripts/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 bytests/layout/test_round8_git_env.sh.
. "$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"| 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).
core.hooksPathat 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_WHYnames 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_physreturns 1 rather than looping. - An existing hook that is a symlink is backed up as a link (
ln -s, verified by reading it back) plusNAME.linkcontainingTARGET=,ABSOLUTE=and a ready-to-runRESTORE=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 withcmp -sfor 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 withcmp -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 withchmod +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).
- On load it makes
chi__gitavailable, sourcingconstitution_layout.shfrom beside itself whencl__gitis absent, else defining an inline scrub with the identical variable list. chi_physwalks up to an existing directory,cd -P/pwd -Pto resolve it, then re-appends the remaining components, resolving any symlink it meets (relative targets resolved against the containing directory).chi_hooks_dir_checkreadsgit config --show-scope --get core.hooksPath(scope + value in one call), refuses any scope other thanlocal/worktree, resolves--absolute-git-dirand--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, inrepomode, under the work tree top level.chi_tracked_hooks_dirresolves the physical hooks dir and top level, checks containment, then asksgit ls-files(with--literal-pathspecs) whether anything under that relative path is tracked.chi_backup_hookclassifies the destination (symlink / regular file / other → refuse), createsBACKUP_ROOT/STAMP/LABEL, then finds the first freeNAME/NAME.N, reusing an identical existing backup.chi_install_hookrefuses a non-regular destination, short-circuits an identical file (cmp -s, repairing the execute bit), backs up anything else, then writes amktemptemp file in the hooks dir,chmod +xandmv -finto place.
scripts/lib/constitution_layout.sh— suppliescl__gitand 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.
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.