-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-push
More file actions
executable file
·56 lines (47 loc) · 1.43 KB
/
pre-push
File metadata and controls
executable file
·56 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# pre-push hook - Prevents pushing when local and remote have diverged
#
# Install: cp scripts/git-hooks/pre-push .git/hooks/pre-push && chmod +x .git/hooks/pre-push
# Or run: make install-hooks
set -e
remote="$1"
url="$2"
# Only check pushes to origin
if [[ "$remote" != "origin" ]]; then
exit 0
fi
# Get current branch
branch=$(git rev-parse --abbrev-ref HEAD)
# Skip for new branches (no upstream)
if ! git rev-parse --verify "origin/$branch" &>/dev/null; then
exit 0
fi
# Check for divergence
local_only=$(git rev-list "origin/$branch..$branch" --count 2>/dev/null || echo "0")
remote_only=$(git rev-list "$branch..origin/$branch" --count 2>/dev/null || echo "0")
if [[ "$local_only" -gt 0 ]] && [[ "$remote_only" -gt 0 ]]; then
echo ""
echo "ERROR: Branches have diverged!"
echo ""
echo " Local has $local_only commit(s) not on remote"
echo " Remote has $remote_only commit(s) not on local"
echo ""
echo "To fix:"
echo " 1. git fetch origin"
echo " 2. git rebase origin/$branch # or git merge origin/$branch"
echo " 3. Resolve any conflicts"
echo " 4. git push"
echo ""
exit 1
fi
# Verify build passes before push (optional - can be slow)
if [[ -f "go.mod" ]]; then
echo "Verifying build..."
if ! go build ./... 2>/dev/null; then
echo ""
echo "ERROR: Build failed! Fix build errors before pushing."
echo ""
exit 1
fi
fi
exit 0