script: 添加git hook,每次push之前都rebase git-main

This commit is contained in:
肖应宇 2026-05-18 16:31:05 +08:00
parent ae2cfa2386
commit f0f7b8df4d
6 changed files with 80 additions and 5 deletions

7
.githooks/pre-push Executable file
View File

@ -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"

5
.gitignore vendored
View File

@ -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/
.planning/

View File

@ -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 "=========================================="

View File

@ -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建议先执行

16
scripts/git/install-hooks.sh Executable file
View File

@ -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"

42
scripts/git/pre-push-rebase.sh Executable file
View File

@ -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'"