Files
Kamino/scripts/dev-setup.sh
T
Defame1297andClaude Sonnet 5.5 74a5a0739b
Molecule / molecule (pull_request) Skipped
Build / build (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 6s
fix(dev-setup): install the restore trap first and fail clearly on a global hooksPath
Install the EXIT/INT/TERM traps before core.hooksPath is lifted, make the
restore idempotent, and save and restore every local value of a
multi-valued setting in order. When a global or system value still applies
after the local one is lifted, print the script's own message and exit 1
before pre-commit runs. Note that readlink -f/-m are GNU.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-10-25 19:05:20 +00:00

116 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sets up a local checkout for development: a virtualenv with the pinned tools
# (requirements-dev.txt, the versions CI runs) and git hooks that find them.
#
# scripts/dev-setup.sh # KAMINO_PYTHON=python3.12 scripts/dev-setup.sh to pick one
#
# KAMINO_PYTHON (default python3) selects the interpreter that is checked and that creates
# a new virtualenv. An existing virtualenv is kept when it already runs Python 3.11+, even
# if it was built by another interpreter; only an older one is rebuilt.
#
# The repository is the one this script lives in, whatever the caller's working directory.
#
# Requires GNU readlink (-f and -m): on macOS that is 12.3+ for -f, or coreutils (greadlink
# on PATH as readlink) for -m.
#
# Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint,
# ansible-lint): they run their tools by name, so the tools must be on PATH when git
# runs the hook. `pre-commit install` does not arrange that, so this script puts the
# virtualenv first on PATH inside the installed hook scripts. It is idempotent; run it
# again after changing requirements-dev.txt or the hook types.
set -euo pipefail
# ansible-core 2.19 (requirements-dev.txt) needs Python 3.11+. The ci image uses the
# distribution's 3.12; any 3.11+ gives the same pins.
python="${KAMINO_PYTHON:-python3}"
py_ok='import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)'
if ! "$python" -c "$py_ok" 2>/dev/null; then
echo "error: $python is missing or older than 3.11 (ansible-core 2.19 needs 3.11+); set KAMINO_PYTHON=/path/to/python3.12" >&2
exit 1
fi
# The repository is derived from this script's own location (symlinks resolved), never
# from the caller's working directory: running it from another repository must not
# create or remove that repository's .venv.
script="$(readlink -f "${BASH_SOURCE[0]}")"
root="$(git -C "$(dirname "$script")/.." rev-parse --show-toplevel)"
cd "$root"
# `pre-commit install` refuses to run when core.hooksPath is set, and git would not run
# .git/hooks anyway. A local setting that resolves to this repository's own hooks
# directory is harmless: it is lifted for the install and restored afterwards. Anything
# else (another directory, or a setting outside this repository's config) fails up front.
hooks_path="$(git -C "$root" config --get core.hooksPath || true)"
local_paths=()
restore_hooks_path() {
# Idempotent and harmless when the unset below failed: it only rewrites the setting when
# the local values differ from the saved ones. Never fails (it runs from the EXIT trap).
[ "${#local_paths[@]}" -gt 0 ] || return 0
local now saved v
now="$(git -C "$root" config --local --get-all core.hooksPath 2>/dev/null || true)"
saved="$(printf '%s\n' "${local_paths[@]}")"
[ "$now" = "$saved" ] && return 0
git -C "$root" config --local --unset-all core.hooksPath 2>/dev/null || true
for v in "${local_paths[@]}"; do
git -C "$root" config --local --add core.hooksPath "$v" || true
done
return 0
}
if [ -n "$hooks_path" ]; then
own_hooks="$(readlink -f "$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)/hooks")"
case "$hooks_path" in
/*) resolved="$(readlink -m "$hooks_path")" ;;
*) resolved="$(readlink -m "$root/$hooks_path")" ;;
esac
if [ "$resolved" = "$own_hooks" ] && git -C "$root" config --local --get core.hooksPath >/dev/null 2>&1; then
# Save every local value (the setting may be multi-valued), and install the traps
# BEFORE changing anything, so an interruption between the two cannot lose the setting.
mapfile -t local_paths < <(git -C "$root" config --local --get-all core.hooksPath)
trap restore_hooks_path EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
git -C "$root" config --local --unset-all core.hooksPath
if still="$(git -C "$root" config --get core.hooksPath)"; then
echo "error: core.hooksPath is still set to '$still' after lifting this clone's own setting, so a" >&2
echo " global or system value applies and 'pre-commit install' would refuse to run." >&2
echo " Unset that setting (git config --global --unset-all core.hooksPath), or run this script with it removed, then re-run." >&2
exit 1
fi
else
echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2
echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2
echo " or manage the hooks in that directory yourself, then re-run this script." >&2
exit 1
fi
fi
# A virtualenv made by an older Python is rebuilt rather than reused.
if [ -x .venv/bin/python ] && ! .venv/bin/python -c "$py_ok" 2>/dev/null; then
rm -rf "$root/.venv"
fi
[ -x .venv/bin/python ] || "$python" -m venv .venv
.venv/bin/pip install --quiet --disable-pip-version-check -r requirements-dev.txt
.venv/bin/pre-commit install --install-hooks
marker="# kamino-dev-setup: virtualenv first on PATH"
# default_install_hook_types in .pre-commit-config.yaml names these; keep in step.
for hook in pre-commit commit-msg; do
file="$(git -C "$root" rev-parse --path-format=absolute --git-path "hooks/$hook")"
if [ ! -f "$file" ]; then
echo "error: $file was not installed by 'pre-commit install'; check default_install_hook_types in .pre-commit-config.yaml." >&2
exit 1
fi
if ! grep -qxF "$marker" "$file"; then
tmp="$(mktemp)"
{
head -n 1 "$file"
printf '%s\n' "$marker" "export PATH=\"$root/.venv/bin:\$PATH\""
tail -n +2 "$file"
} > "$tmp"
cat "$tmp" > "$file" # keeps the file mode of the hook
rm -f "$tmp"
fi
done
echo "Done: git commit now runs the hooks with the tools in $root/.venv/bin."