This commit is contained in:
sophie committed 2026-05-21 11:38:54 +08:00
1 parent a6f35b375c
commit b1613824ce
8 files changed
+200 -12

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
# shellcheck shell=bash
# 供 pre-commit / pre-push 引用;勿直接执行。
hook_should_skip() {
[[ "${SKIP_GIT_HOOKS:-}" == "1" ]] || [[ "${SKIP_FRONTEND_HOOKS:-}" == "1" ]]
}
frontend_paths_in() {
local file_list="${1:-}"
echo "${file_list}" | grep -qE '^frontend/'
}
ensure_frontend_deps() {
local frontend_dir="${1}/frontend"
if [[ ! -d "${frontend_dir}/node_modules" ]]; then
echo "[git-hook] frontend/node_modules 不存在,请先执行:"
echo " cd frontend && npm install"
return 1
fi
return 0
}
run_frontend_typecheck() {
local root_dir="${1}"
echo "[pre-commit] 检测到 frontend/ 变更,运行 vue-tsc 类型检查..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npx vue-tsc -b --pretty false
)
}
run_frontend_build() {
local root_dir="${1}"
echo "[pre-push] 检测到将要推送的提交包含 frontend/ 变更,运行 npm run build..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npm run build
)
}
+16
View File
@@ -3,6 +3,8 @@ set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_DIR}"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
STAGED_FILES="$(git diff --cached --name-only --diff-filter=ACMR || true)"
if [[ -z "${STAGED_FILES}" ]]; then
@@ -30,4 +32,18 @@ if [[ "${MODELS_CHANGED}" -eq 1 && "${MIGRATION_CHANGED}" -eq 0 ]]; then
exit 1
fi
if hook_should_skip; then
exit 0
fi
if frontend_paths_in "${STAGED_FILES}"; then
if ! run_frontend_typecheck "${ROOT_DIR}"; then
echo "[pre-commit] vue-tsc 失败,已阻止提交。"
echo "[pre-commit] 跳过前端检查: SKIP_FRONTEND_HOOKS=1 git commit"
echo "[pre-commit] 或跳过全部 hook: git commit --no-verify"
exit 1
fi
echo "[pre-commit] frontend 类型检查通过。"
fi
exit 0
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
if hook_should_skip; then
exit 0
fi
zero="0000000000000000000000000000000000000000"
need_frontend_build=0
while read -r local_ref local_sha remote_ref remote_sha; do
[[ "${local_sha}" == "${zero}" ]] && continue
changed_files=""
if [[ "${remote_sha}" == "${zero}" ]]; then
upstream_ref="$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null || true)"
if [[ -n "${upstream_ref}" ]]; then
remote_branch="${upstream_ref#*/}"
remote_name="${upstream_ref%%/*}"
merge_base="$(git merge-base "${local_sha}" "${remote_name}/${remote_branch}" 2>/dev/null || true)"
if [[ -n "${merge_base}" ]]; then
changed_files="$(git diff --name-only --diff-filter=ACMR "${merge_base}" "${local_sha}" || true)"
fi
fi
if [[ -z "${changed_files}" ]]; then
changed_files="$(git diff-tree --no-commit-id --name-only --diff-filter=ACMR -r "${local_sha}" || true)"
fi
else
changed_files="$(git diff --name-only --diff-filter=ACMR "${remote_sha}" "${local_sha}" || true)"
fi
if frontend_paths_in "${changed_files}"; then
need_frontend_build=1
break
fi
done
if [[ "${need_frontend_build}" -eq 0 ]]; then
exit 0
fi
if ! run_frontend_build "${ROOT_DIR}"; then
echo "[pre-push] 前端 build 失败,已阻止 push。"
echo "[pre-push] 跳过检查可设置: SKIP_FRONTEND_HOOKS=1 git push"
echo "[pre-push] 或跳过全部 hook: git push --no-verify"
exit 1
fi
echo "[pre-push] 前端 build 通过。"
exit 0