- 吸收来自 hose-sealai-bridge 仓库的近期协作与开发规范演进 - 增强 Git Hooks 防护体系: - .githooks/pre-commit:拦截主干分支 (main/master) 直接提交,校验分支全小写与设备前缀,拦截 >15MB 单文件与 Windows 非法字符/保留设备名 - .githooks/pre-push:拦截向远端推送含大写字母的分支名 - .githooks/branch-guard.sh:增加大写字符强校验 - .githooks/setup.sh & setup.ps1:跨平台一键配置 hooksPath、autocrlf 与 symlinks - 跨平台与敏感文件防护: - 新增 .gitattributes 固化 LF 换行符(防止 Windows 下 CRLF 篡改 Shell 脚本) - 更新 .gitignore 增加 .env、*.pem、*.key 及 .drafts/* 忽略 - 确立 Markdown 实体先行标准 (Markdown-First): - 设立 .drafts/ 目录作为草稿落盘规范 - 新增项目级 Skill .agents/skills/repo-gh/SKILL.md - 新增 .agents/rules/windows-development.md - 更新 .agents/rules/issue-pr-workflow.md、.agents/rules/branch-naming.md、.agents/README.md 与 AGENTS.md
87 lines
3.5 KiB
Bash
Executable File
87 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
# Git pre-push hook: Prevent accidental direct push to origin/main or GitHub/main
|
|
# Enforces the PR workflow required by AGENTS.md & .agents/rules/issue-pr-workflow.md
|
|
# Also enforces the device branch-prefix rule via .githooks/branch-guard.sh
|
|
|
|
source "$(dirname "$0")/branch-guard.sh"
|
|
|
|
remote="$1"
|
|
url="$2"
|
|
zero="0000000000000000000000000000000000000000"
|
|
|
|
while read local_ref local_oid remote_ref remote_oid; do
|
|
# Device branch-prefix rule (.agents/rules/branch-naming.md); deletions are exempt
|
|
case "$local_ref" in
|
|
refs/heads/*)
|
|
if [ "$local_oid" != "$zero" ]; then
|
|
guard_branch_name "${local_ref#refs/heads/}" || exit 1
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
# Check uppercase in remote ref (unless deleting remote branch)
|
|
if [ "$local_oid" != "$zero" ] && echo "$remote_ref" | grep -q 'refs/heads/.*[A-Z]'; then
|
|
branch_name="${remote_ref#refs/heads/}"
|
|
lowercase_branch=$(echo "$branch_name" | tr '[:upper:]' '[:lower:]')
|
|
echo "" >&2
|
|
echo "======================================================================" >&2
|
|
echo "❌ [Git Hook 拦截] 严禁推送包含大写字母的分支名到远端!" >&2
|
|
echo "======================================================================" >&2
|
|
echo "📌 目标分支: $branch_name" >&2
|
|
echo "📌 规范说明:为规避 Windows 与 Linux 远端大小写冲突,远程分支必须为全小写。" >&2
|
|
echo "💡 请重命名本地分支后再推送:" >&2
|
|
echo " git branch -m $lowercase_branch" >&2
|
|
echo " git push -u $remote $lowercase_branch" >&2
|
|
echo "======================================================================" >&2
|
|
echo "" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# If pushing the main branch (or pushing to remote main)
|
|
if [ "$remote_ref" = "refs/heads/main" ] || [ "$local_ref" = "refs/heads/main" ]; then
|
|
# When pushing to origin or GitHub (main repository)
|
|
if [ "$remote" = "origin" ] || [ "$remote" = "GitHub" ]; then
|
|
# Emergency override
|
|
if [ "$ALLOW_MAIN_PUSH" = "1" ]; then
|
|
continue
|
|
fi
|
|
|
|
# If deleting remote branch, let git handle or allow
|
|
if [ "$local_oid" = "$zero" ]; then
|
|
continue
|
|
fi
|
|
|
|
# Check remote head
|
|
git fetch "$remote" main --quiet 2>/dev/null
|
|
remote_main_oid=$(git rev-parse "$remote/main" 2>/dev/null)
|
|
|
|
if [ -n "$remote_main_oid" ] && [ "$local_oid" = "$remote_main_oid" ]; then
|
|
continue
|
|
fi
|
|
|
|
# Check if local_oid is strictly contained within remote_main_oid (e.g. older commit)
|
|
if [ -n "$remote_main_oid" ] && git merge-base --is-ancestor "$local_oid" "$remote_main_oid" 2>/dev/null; then
|
|
continue
|
|
fi
|
|
|
|
# If local_oid is not an ancestor of remote/main, then there are new unmerged commits
|
|
new_commits=$(git rev-list "${remote_main_oid}..${local_oid}" --count 2>/dev/null || echo "1")
|
|
echo "" >&2
|
|
echo "🛑 [git pre-push hook] Direct push to $remote/main is blocked!" >&2
|
|
echo " Repository policy strictly forbids direct pushes to main." >&2
|
|
echo " Found $new_commits unmerged commit(s) destined for $remote/main." >&2
|
|
echo "" >&2
|
|
echo " 👉 Please follow the standard PR workflow:" >&2
|
|
echo " 1. git switch -c <feature-branch>" >&2
|
|
echo " 2. git push $remote HEAD:refs/heads/<feature-branch>" >&2
|
|
echo " 3. gh pr create --base main --head <feature-branch>" >&2
|
|
echo "" >&2
|
|
echo " (Emergency override: ALLOW_MAIN_PUSH=1 git push $remote main)" >&2
|
|
echo "" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
done
|
|
|
|
exit 0
|