fix(scripts): make dev-setup.sh check Python, core.hooksPath and the installed hooks #15

Merged
Defame1297 merged 6 commits from fix/dev-setup-robustness into main 2026-10-25 20:38:05 +00:00
2 changed files with 36 additions and 12 deletions
Showing only changes of commit 19faa49a4c - Show all commits

No files matched your search

@@ -1,4 +1,4 @@
trivial:
- >-
scripts/dev-setup.sh checks for Python 3.11 or newer (PYTHON selects one), rebuilds an older
scripts/dev-setup.sh checks for Python 3.11 or newer (KAMINO_PYTHON selects one), derives the repository from its own location, rebuilds an older
virtualenv, and fails early when core.hooksPath is set or the hooks are missing.
+35 -11
View File
@@ -2,7 +2,13 @@
# 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 # PYTHON=python3.12 scripts/dev-setup.sh to pick one
# 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.
#
# 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
@@ -13,24 +19,42 @@ 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="${PYTHON:-python3}"
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 PYTHON=/path/to/python3.12" >&2
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
root="$(git rev-parse --show-toplevel)"
# 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. Say so up front instead of failing half way.
hooks_path="$(git config --get core.hooksPath || true)"
# .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)"
restore_hooks_path=""
if [ -n "$hooks_path" ]; then
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
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
restore_hooks_path="$hooks_path"
git -C "$root" config --local --unset-all core.hooksPath
trap 'git -C "$root" config --local core.hooksPath "$restore_hooks_path"' EXIT
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.
@@ -44,7 +68,7 @@ fi
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 rev-parse --path-format=absolute --git-path "hooks/$hook")"
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