build(git): .githooks补一个 commit-msg + commitlint

This commit is contained in:
sophie committed 2026-06-08 14:07:40 +08:00
1 parent c7f25ec8a1
commit 6057069c1c
9 files changed
+157 -12

No files matched your search

+24
View File
@@ -43,3 +43,27 @@ run_frontend_build() {
npm run build
)
}
ensure_commitlint_deps() {
local root_dir="${1}"
if [[ ! -d "${root_dir}/node_modules/@commitlint/cli" ]]; then
echo "[commit-msg] 根目录 node_modules 不存在,请先执行:"
echo " cd ${root_dir}"
echo " npm install"
echo " 或重新运行: ./scripts/install_git_hooks.sh"
return 1
fi
return 0
}
run_commitlint() {
local root_dir="${1}"
local msg_file="${2}"
if ! ensure_commitlint_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}"
npx --no-install commitlint --edit "${msg_file}"
)
}
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_DIR}"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
COMMIT_MSG_FILE="${1:-}"
if [[ -z "${COMMIT_MSG_FILE}" ]]; then
echo "[commit-msg] missing commit message file path."
exit 1
fi
if [[ -f "${ROOT_DIR}/.git/MERGE_HEAD" ]]; then
exit 0
fi
if ! run_commitlint "${ROOT_DIR}" "${COMMIT_MSG_FILE}"; then
echo "[commit-msg] 提交信息不符合规范,详见 docs/convention/COMMIT_CONVENTION.md"
echo "[commit-msg] 示例: feat(auth): add SMS login support"
echo "[commit-msg] 跳过校验: git commit --no-verify"
exit 1
fi
exit 0