From 1ee7d17712a72de5a07f5055d8e43e8472a8a5c6 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Mon, 31 Aug 2026 12:41:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E5=9C=B0=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E5=89=8D=E4=BB=A3=E7=A0=81=E6=A3=80=E6=9F=A5=E9=92=A9?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .githooks/pre-push | 30 ++++++++++++++++++++++++++++++ README.md | 12 ++++++++++++ scripts/install-git-hooks.sh | 6 ++++++ 3 files changed, 48 insertions(+) create mode 100755 .githooks/pre-push create mode 100755 scripts/install-git-hooks.sh diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100755 index 0000000..e34f2ac --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(git rev-parse --show-toplevel)" + +push_lines="$(cat)" +has_updates=0 +has_deletions=0 +while read -r local_ref local_sha remote_ref remote_sha; do + [ -z "${local_ref}" ] && continue + if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then + has_deletions=1 + else + has_updates=1 + fi +done <<< "$push_lines" + +if [ "$has_updates" = "0" ] && [ "$has_deletions" = "1" ]; then + echo "[pre-push] 分支删除推送,跳过本地检查" + exit 0 +fi + +if [ "${SKIP_CHECKS:-0}" = "1" ]; then + echo "[pre-push] SKIP_CHECKS=1,跳过本地检查" + exit 0 +fi + +echo "[pre-push] 运行本地代码门禁..." +bash scripts/check.sh +echo "[pre-push] 本地检查通过" diff --git a/README.md b/README.md index a369adc..cad86bc 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,18 @@ npm run dev `./scripts/deploy-prod.sh` 在构建镜像前会自动调用 `check.sh`(本地有 `go` 和 `npm` 时)。检查未通过会中止部署;确需跳过用 `--skip-check`(不推荐)。`check.sh` 也支持 `--skip-backend`、`--skip-frontend`、`--skip-tests` 做局部调试。 +需要在本地推送前自动执行同一套检查时,启用 Git hook: + +```bash +bash scripts/install-git-hooks.sh +``` + +启用后,`git push` 会执行 `scripts/check.sh`;仅临时跳过时使用: + +```bash +SKIP_CHECKS=1 git push +``` + ## 开发态短信与实名 - 后端日志使用单行可读文本。开发环境同时输出控制台和 `backend/logs/app-YYYY-MM-DD.log`,生产示例只输出文件以避免重复存储;可通过 `LOG_LEVEL=debug|info|warn|error` 调整级别。 diff --git a/scripts/install-git-hooks.sh b/scripts/install-git-hooks.sh new file mode 100755 index 0000000..2f6eaf7 --- /dev/null +++ b/scripts/install-git-hooks.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +git -C "$ROOT_DIR" config core.hooksPath .githooks +echo "已启用 .githooks(推送前执行 scripts/check.sh;SKIP_CHECKS=1 可跳过)"