diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index e93eba4..0f45fe2 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -10,6 +10,9 @@ # # 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 @@ -38,7 +41,21 @@ cd "$root" # 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="" +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 @@ -46,9 +63,19 @@ if [ -n "$hooks_path" ]; then *) 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" + # 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 - trap 'git -C "$root" config --local core.hooksPath "$restore_hooks_path"' EXIT + 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