fix(dev-setup): quote the hook path, read core.hooksPath NUL-safely and lift an empty value
The repository path is shell-quoted (printf %q) where it is written into the installed hooks, so a path with spaces, quotes or $(...) is data. The local core.hooksPath values are read with -z and compared and restored element by element, so a value with a newline survives; the loop avoids mapfile. Any local value, including an empty or trailing empty one, is lifted for the install and restored. tests/scripts/test_dev_setup.py covers it with stub tools in throwaway repositories, and lint.yml now runs tests/scripts. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
74a5a0739b
commit
72e9813394
4 files changed
+315
-33
No files matched your search
+58
-32
@@ -37,51 +37,76 @@ 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)"
|
||||
# .git/hooks anyway. A repo-local setting whose values all resolve to this repository's own
|
||||
# hooks directory (or are empty) is harmless: it is lifted for the install and restored
|
||||
# afterwards, byte for byte. Anything else (another directory, or a value outside this
|
||||
# repository's config, such as a global one) fails up front.
|
||||
|
||||
# Fills the global array cur_paths with every repo-local core.hooksPath value, read
|
||||
# NUL-delimited so a value with a newline survives and an empty value is kept as an
|
||||
# element. (No mapfile: it needs bash 4; the loop also runs on the 3.2 of macOS, untested.)
|
||||
cur_paths=()
|
||||
read_local_paths() {
|
||||
local v
|
||||
cur_paths=()
|
||||
while IFS= read -r -d '' v; do
|
||||
cur_paths+=("$v")
|
||||
done < <(git -C "$root" config --local -z --get-all core.hooksPath 2>/dev/null || 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
|
||||
local i v same=1
|
||||
read_local_paths
|
||||
if [ "${#cur_paths[@]}" -ne "${#local_paths[@]}" ]; then
|
||||
same=0
|
||||
else
|
||||
i=0
|
||||
while [ "$i" -lt "${#local_paths[@]}" ]; do
|
||||
[ "${cur_paths[$i]}" = "${local_paths[$i]}" ] || same=0
|
||||
i=$((i + 1))
|
||||
done
|
||||
fi
|
||||
[ "$same" -eq 1 ] && 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
|
||||
|
||||
read_local_paths
|
||||
if [ "${#cur_paths[@]}" -gt 0 ]; 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
|
||||
for v in "${cur_paths[@]}"; do
|
||||
[ -n "$v" ] || continue
|
||||
case "$v" in
|
||||
/*) resolved="$(readlink -m "$v")" ;;
|
||||
*) resolved="$(readlink -m "$root/$v")" ;;
|
||||
esac
|
||||
if [ "$resolved" != "$own_hooks" ]; then
|
||||
echo "error: core.hooksPath is set to '$v', so git does not run the hooks pre-commit installs." >&2
|
||||
echo " Unset it for this clone (git config --unset-all core.hooksPath)" >&2
|
||||
echo " or manage the hooks in that directory yourself, then re-run this script." >&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
|
||||
done
|
||||
# Save every local value (the setting may be multi-valued, or empty), and install the
|
||||
# traps BEFORE changing anything, so an interruption between the two cannot lose it.
|
||||
local_paths=("${cur_paths[@]}")
|
||||
trap restore_hooks_path EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
git -C "$root" config --local --unset-all core.hooksPath
|
||||
fi
|
||||
if still="$(git -C "$root" config --get core.hooksPath)" && [ -n "$still" ]; then
|
||||
echo "error: core.hooksPath is still set to '$still' (outside this clone's own config: a global or" >&2
|
||||
echo " system value), so 'pre-commit install' would refuse to run." >&2
|
||||
echo " Unset that setting (git config --global --unset-all core.hooksPath), then re-run." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# A virtualenv made by an older Python is rebuilt rather than reused.
|
||||
@@ -104,7 +129,8 @@ for hook in pre-commit commit-msg; do
|
||||
tmp="$(mktemp)"
|
||||
{
|
||||
head -n 1 "$file"
|
||||
printf '%s\n' "$marker" "export PATH=\"$root/.venv/bin:\$PATH\""
|
||||
# %q shell-quotes the path: a repository path with spaces, quotes or $(...) is data.
|
||||
printf '%s\n' "$marker" "export PATH=$(printf '%q' "$root/.venv/bin"):\"\$PATH\""
|
||||
tail -n +2 "$file"
|
||||
} > "$tmp"
|
||||
cat "$tmp" > "$file" # keeps the file mode of the hook
|
||||
|
||||
Reference in new issue
Block a user