Skip to content

Commit 0faeb68

Browse files
committed
chore: fix pre-commit wc -l empty line bug [skip-version]
1 parent c7428fb commit 0faeb68

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

.githooks/pre-commit

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
# pre-commit hook — Validate & sync all stack repos before commit
3+
4+
DEVSTACK_DIR=""
5+
check_dir="$PWD"
6+
while [ "$check_dir" != "/" ]; do
7+
[ -f "$check_dir/VERSION" ] && [ -f "$check_dir/lpb.stack.env" ] && DEVSTACK_DIR="$check_dir" && break
8+
check_dir=$(dirname "$check_dir")
9+
done
10+
[ -z "$DEVSTACK_DIR" ] && DEVSTACK_DIR="$PWD"
11+
12+
ERRORS=0
13+
CURRENT_VERSION=$(cat "$DEVSTACK_DIR/VERSION" 2>/dev/null || echo "NOT FOUND")
14+
15+
declare -a REPOS=(
16+
"$DEVSTACK_DIR/workspace/pi"
17+
"/home/lpb/.pi/agent/git/github.com/localpibox/lemonade-pi-plugin"
18+
"/home/lpb/workspace/localpibox/config"
19+
"/home/lpb/workspace/localpibox/lpb-memory"
20+
"/home/lpb/.pi/agent/git/github.com/localpibox/pi-subagents"
21+
)
22+
23+
SETTINGS_JSON="/home/lpb/.pi/agent/settings.json"
24+
25+
info() { echo "$*"; }
26+
error() { echo "$*"; ERRORS=$((ERRORS + 1)); }
27+
28+
echo "=== Stack Version Sync ==="
29+
for repo in "${REPOS[@]}"; do
30+
repo_name=$(basename "$repo")
31+
[ ! -f "$repo/VERSION" ] && error "$repo_name: missing VERSION" && continue
32+
repo_version=$(cat "$repo/VERSION")
33+
[ "$repo_version" != "$CURRENT_VERSION" ] && error "$repo_name: $repo_version != $CURRENT_VERSION" || info "$repo_name: $repo_version"
34+
done
35+
36+
echo ""
37+
echo "=== Environment Sync ==="
38+
if [ -f "$DEVSTACK_DIR/lpb.stack.env" ]; then
39+
PI_REF=$(grep '^LPB_PI_REF=' "$DEVSTACK_DIR/lpb.stack.env" | cut -d= -f2)
40+
[ "$PI_REF" != "$CURRENT_VERSION" ] && { error "LPB_PI_REF=$PI_REF != $CURRENT_VERSION"; sed -i "s/^LPB_PI_REF=.*/LPB_PI_REF=$CURRENT_VERSION/" "$DEVSTACK_DIR/lpb.stack.env"; } || info "LPB_PI_REF=$PI_REF"
41+
fi
42+
43+
echo ""
44+
echo "=== Extension Pins ==="
45+
if [ -f "$SETTINGS_JSON" ]; then
46+
for repo in lemonade-pi-plugin lpb-memory pi-subagents; do
47+
python3 -c "
48+
import json
49+
d = json.load(open('$SETTINGS_JSON'))
50+
for p in d.get('packages',[]):
51+
if isinstance(p,dict) and '$repo' in p.get('extension',''):
52+
assert p.get('version','') == '$CURRENT_VERSION', f'MISMATCH: {p.get(\"version\")}'
53+
" 2>/dev/null && info "$repo: pinned" || warn "$repo: verification skipped"
54+
done
55+
fi
56+
57+
echo ""
58+
echo "=== Unstaged Changes ==="
59+
check_unstaged() {
60+
local repo_path="$1"
61+
local repo_name="$2"
62+
[ -d "$repo_path" ] || return 0
63+
local dirty
64+
dirty=$(cd "$repo_path" && git diff --name-only 2>/dev/null | grep -vE '^$' | grep -vE 'VERSION|lpb\.stack\.env|lpb\.conf\.env|\.githooks/' | grep -c . || true)
65+
[ "$dirty" -gt 0 ] && error "$repo_name: $dirty unstaged change(s)" || info "$repo_name: clean"
66+
}
67+
68+
for repo in "${REPOS[@]}"; do
69+
check_unstaged "$repo" "$(basename "$repo")"
70+
done
71+
72+
check_unstaged "$DEVSTACK_DIR" "devstack"
73+
74+
# Auto-stage VERSION + env
75+
if [ $ERRORS -eq 0 ] && [ "$CURRENT_VERSION" != "NOT FOUND" ]; then
76+
echo ""
77+
echo "=== Auto-stage VERSION ==="
78+
cd "$DEVSTACK_DIR" && git add VERSION lpb.stack.env 2>/dev/null
79+
for repo in "${REPOS[@]}"; do
80+
[ -f "$repo/VERSION" ] && (cd "$repo" && git add VERSION 2>/dev/null)
81+
done
82+
info "VERSION files + lpb.stack.env staged"
83+
fi
84+
85+
echo ""
86+
if [ $ERRORS -gt 0 ]; then
87+
echo "❌ Pre-commit FAILED ($ERRORS error(s))"
88+
exit 1
89+
else
90+
echo "✅ All checks passed"
91+
exit 0
92+
fi

0 commit comments

Comments
 (0)