deerflow2/scripts/git/pre-push-rebase.sh

43 lines
1.2 KiB
Bash
Executable File

#!/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'"