From f0f7b8df4d7a4ee1b8bcb2175af26fec3f80d83e Mon Sep 17 00:00:00 2001 From: MT-Mint <798521692@qq.com> Date: Mon, 18 May 2026 16:31:05 +0800 Subject: [PATCH] =?UTF-8?q?script:=20=E6=B7=BB=E5=8A=A0git=20hook=EF=BC=8C?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1push=E4=B9=8B=E5=89=8D=E9=83=BDrebase=20git-m?= =?UTF-8?q?ain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .githooks/pre-push | 7 ++++++ .gitignore | 5 +--- Makefile | 7 +++++- README_zh.md | 8 +++++++ scripts/git/install-hooks.sh | 16 +++++++++++++ scripts/git/pre-push-rebase.sh | 42 ++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100755 .githooks/pre-push create mode 100755 scripts/git/install-hooks.sh create mode 100755 scripts/git/pre-push-rebase.sh diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100755 index 00000000..dcb3fa72 --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(git rev-parse --show-toplevel)" +cd "$REPO_ROOT" + +"$REPO_ROOT/scripts/git/pre-push-rebase.sh" diff --git a/.gitignore b/.gitignore index b8b4834a..c9af8668 100644 --- a/.gitignore +++ b/.gitignore @@ -42,9 +42,6 @@ skills/ logs/ log/ -# Local git hooks (keep only on this machine, do not push) -.githooks/ - # pnpm .pnpm-store sandbox_image_cache.tar @@ -58,4 +55,4 @@ config.yaml.bak .playwright-mcp .gstack/ -.planning/ \ No newline at end of file +.planning/ diff --git a/Makefile b/Makefile index 64e96fa9..ddf74174 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # DeerFlow - Unified Development Environment -.PHONY: help config config-upgrade check install dev dev-daemon start stop up down clean docker-init docker-start docker-stop docker-logs docker-logs-frontend docker-logs-gateway +.PHONY: help config config-upgrade check install hooks-install dev dev-daemon start stop up down clean docker-init docker-start docker-stop docker-logs docker-logs-frontend docker-logs-gateway BASH ?= bash @@ -18,6 +18,7 @@ help: @echo " make config-upgrade - Merge new fields from config.example.yaml into config.yaml" @echo " make check - Check if all required tools are installed" @echo " make install - Install all dependencies (frontend + backend)" + @echo " make hooks-install - Install local Git hooks for this repo" @echo " make setup-sandbox - Pre-pull sandbox container image (recommended)" @echo " make dev - Start all services in development mode (with hot-reloading)" @echo " make dev-daemon - Start all services in background (daemon mode)" @@ -63,6 +64,10 @@ install: @echo " make setup-sandbox" @echo "" +# Install repository-local Git hooks +hooks-install: + @./scripts/git/install-hooks.sh + # Pre-pull sandbox Docker image (optional but recommended) setup-sandbox: @echo "==========================================" diff --git a/README_zh.md b/README_zh.md index 9b832eb0..96528784 100644 --- a/README_zh.md +++ b/README_zh.md @@ -192,6 +192,14 @@ make down # 停止并移除容器 make install # 安装 backend + frontend 依赖 ``` + 如果你希望每次 `git push` 之前自动把当前分支 rebase 到 `origin/git-main`,可以再执行: + + ```bash + make hooks-install + ``` + + 这会把仓库的 `core.hooksPath` 指向 `.githooks/`,启用 `pre-push` hook。 + 3. **(可选)预拉取 sandbox 镜像**: ```bash # 如果使用 Docker / Container sandbox,建议先执行 diff --git a/scripts/git/install-hooks.sh b/scripts/git/install-hooks.sh new file mode 100755 index 00000000..56b3fb1b --- /dev/null +++ b/scripts/git/install-hooks.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +cd "$REPO_ROOT" + +if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + echo "✗ Not inside a Git repository." + exit 1 +fi + +git config --local core.hooksPath .githooks + +echo "✓ Git hooks installed" +echo " core.hooksPath = .githooks" +echo " pre-push will rebase the current branch before push" diff --git a/scripts/git/pre-push-rebase.sh b/scripts/git/pre-push-rebase.sh new file mode 100755 index 00000000..ecd98809 --- /dev/null +++ b/scripts/git/pre-push-rebase.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +cd "$REPO_ROOT" + +current_branch="$(git symbolic-ref -q --short HEAD || true)" +if [ -z "$current_branch" ]; then + echo "✗ pre-push rebase: detached HEAD detected." + echo " Checkout a branch before pushing." + exit 1 +fi + +if ! git rev-parse --verify --quiet HEAD >/dev/null; then + echo "✗ pre-push rebase: current branch has no commits yet." + exit 1 +fi + +if [ -n "$(git status --porcelain)" ]; then + echo "✗ pre-push rebase: working tree is not clean." + echo " Commit or stash changes before pushing." + exit 1 +fi + +if [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ]; then + echo "✗ pre-push rebase: a rebase is already in progress." + exit 1 +fi + +echo "Fetching origin/git-main..." +git fetch origin git-main + +if ! git rev-parse --verify --quiet origin/git-main >/dev/null; then + echo "✗ pre-push rebase: origin/git-main is not available locally." + echo " Fetch origin first, then push again." + exit 1 +fi + +echo "Rebasing '$current_branch' onto origin/git-main before push..." +git rebase origin/git-main + +echo "✓ Rebase completed for '$current_branch'"