Files
syops/scripts/install_git_hooks.sh
T

47 lines
1.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
if ! git -C "${ROOT_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "[hook-install] ${ROOT_DIR} is not a git repository."
exit 1
fi
HOOKS_DIR="${ROOT_DIR}/.githooks"
REQUIRED_HOOKS=(pre-commit pre-push commit-msg _common.sh)
for hook_name in "${REQUIRED_HOOKS[@]}"; do
hook_path="${HOOKS_DIR}/${hook_name}"
if [[ ! -f "${hook_path}" ]]; then
echo "[hook-install] missing hook file: ${hook_path}"
exit 1
fi
chmod +x "${hook_path}"
done
if ! git -C "${ROOT_DIR}" config core.hooksPath .githooks; then
echo "[hook-install] failed to set git config core.hooksPath."
echo "[hook-install] please run manually in your local shell:"
echo " git config core.hooksPath .githooks"
exit 1
fi
if [[ -f "${ROOT_DIR}/package.json" ]]; then
echo "[hook-install] Installing commitlint dependencies (npm install)..."
if ! (cd "${ROOT_DIR}" && npm install); then
echo "[hook-install] npm install failed. commit-msg hook requires root node_modules."
echo "[hook-install] please run manually: cd ${ROOT_DIR} && npm install"
exit 1
fi
fi
echo "[hook-install] Installed Git hooks successfully."
echo "[hook-install] pre-commit: models 变更需带 migration;frontend 暂存时跑 vue-tsc"
echo "[hook-install] commit-msg: 校验提交信息格式(Conventional Commits)"
echo "[hook-install] pre-push: 推送含 frontend 变更时跑 npm run build"
echo "[hook-install] core.hooksPath=$(git -C "${ROOT_DIR}" config --get core.hooksPath)"
echo "[hook-install] 跳过前端检查: SKIP_FRONTEND_HOOKS=1"
echo "[hook-install] 跳过全部 hook: SKIP_GIT_HOOKS=1 或 git commit/push --no-verify"