Files
Coredesktop-Dev/lib/hooks.sh
T
2026-11-04 09:43:05 -07:00

71 lines
2.1 KiB
Bash

#!/bin/bash
#
# Purpose : coredesktop-dev hooks: install or remove the working copy's pre-push
# hook, which runs coredesktop-dev check before each push
hooks_usage() {
cat << EOF
Usage: coredesktop-dev hooks [status|install|remove]
status Show whether the pre-push hook is installed (the default)
install Run coredesktop-dev check before each git push from the working copy
remove Remove that hook
git push --no-verify skips the hook for one push. A pre-push hook that
coredesktop-dev didn't install is never replaced or removed.
EOF
}
# Where git looks for the working copy's pre-push hook (core.hooksPath aware)
hook_path() {
local path
path="$(git -C "$WORKING_COPY" rev-parse --git-path hooks/pre-push)" || return 1
[[ "$path" == /* ]] || path="$WORKING_COPY/$path"
echo "$path"
}
hook_is_ours() {
[ -f "$1" ] && grep -qxF "$HOOK_MARKER" "$1"
}
cmd_hooks() {
local hook
case "${1:-status}" in
-h | --help) hooks_usage; return 0 ;;
status | install | remove) ;;
*) hooks_usage >&2; return 1 ;;
esac
require_working_copy
hook="$(hook_path)" || die "Couldn't find the working copy's git hooks folder."
case "${1:-status}" in
status)
if hook_is_ours "$hook"; then
echo "The pre-push hook is installed: $hook"
elif [ -e "$hook" ]; then
echo "$hook is someone else's hook; coredesktop-dev leaves it alone."
else
echo "The pre-push hook isn't installed. To install it: coredesktop-dev hooks install"
fi
;;
install)
if [ -e "$hook" ] && ! hook_is_ours "$hook"; then
die "$hook already exists and coredesktop-dev didn't write it, so it's left alone."
fi
mkdir -p "$(dirname "$hook")"
install -m 755 "$(sibling_dir)/hooks/pre-push" "$hook"
echo "Installed $hook: each git push now runs coredesktop-dev check first."
;;
remove)
if hook_is_ours "$hook"; then
rm "$hook"
echo "Removed $hook."
elif [ -e "$hook" ]; then
die "$hook isn't coredesktop-dev's, so it's left alone."
else
echo "The pre-push hook isn't installed."
fi
;;
esac
}