-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathupdate-graph.sh
More file actions
90 lines (77 loc) · 2.79 KB
/
Copy pathupdate-graph.sh
File metadata and controls
90 lines (77 loc) · 2.79 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
# update-graph.sh — PostToolUse hook for Edit and Write tools
# Incrementally updates the codegraph after source file edits.
# On the first edit of a stale session (no full rebuild in >24h), upgrades
# to a full rebuild so complexity/dataflow/cohesion data stays fresh.
# Always exits 0 (informational only, never blocks).
set -euo pipefail
INPUT=$(cat)
# Extract file path and normalize backslashes — all in node to avoid
# bash backslash issues on Windows/Git Bash
FILE_PATH=$(echo "$INPUT" | node -e "
let d='';
process.stdin.on('data',c=>d+=c);
process.stdin.on('end',()=>{
const p=(JSON.parse(d).tool_input?.file_path||'').replace(/\\\\/g,'/');
if(p)process.stdout.write(p);
});
" 2>/dev/null) || true
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Only rebuild for source files codegraph tracks
# Skip docs, configs, test fixtures, and non-code files
case "$FILE_PATH" in
*.js|*.ts|*.tsx|*.jsx|*.py|*.go|*.rs|*.java|*.cs|*.php|*.rb|*.tf|*.hcl)
;;
*)
exit 0
;;
esac
# Skip test fixtures — they're copied to tmp dirs anyway
if echo "$FILE_PATH" | grep -qE '(fixtures|__fixtures__|testdata)/'; then
exit 0
fi
# Guard: codegraph DB must exist (project has been built at least once)
PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) || PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}"
DB_PATH="$PROJECT_DIR/.codegraph/graph.db"
if [ ! -f "$DB_PATH" ]; then
exit 0
fi
# --- Staleness check ---
# If no full rebuild has happened in >24h, upgrade this one build to
# --no-incremental so complexity/dataflow/cohesion are recomputed for
# all files. Subsequent edits in the same session stay incremental.
# See docs/guides/incremental-builds.md for what incremental skips.
MARKER="$PROJECT_DIR/.codegraph/last-full-build"
BUILD_FLAGS=""
STALE_SECONDS=86400 # 24 hours
if [ ! -f "$MARKER" ]; then
# No marker = never had a tracked full rebuild — do one now
BUILD_FLAGS="--no-incremental"
else
# Check marker age (cross-platform: use node for reliable epoch math)
MARKER_AGE=$(node -e "
const fs = require('fs');
try {
const mtime = fs.statSync('${MARKER//\\/\\\\}').mtimeMs;
console.log(Math.floor((Date.now() - mtime) / 1000));
} catch { console.log('999999'); }
" 2>/dev/null) || MARKER_AGE=999999
if [ "$MARKER_AGE" -gt "$STALE_SECONDS" ]; then
BUILD_FLAGS="--no-incremental"
fi
fi
# Run the build
BUILD_OK=0
if command -v codegraph &>/dev/null; then
codegraph build "$PROJECT_DIR" -d "$DB_PATH" $BUILD_FLAGS 2>/dev/null && BUILD_OK=1 || true
else
npx --yes @optave/codegraph build "$PROJECT_DIR" -d "$DB_PATH" $BUILD_FLAGS 2>/dev/null && BUILD_OK=1 || true
fi
# Update marker only if we did a full rebuild AND it succeeded
if [ -n "$BUILD_FLAGS" ] && [ "$BUILD_OK" -eq 1 ]; then
mkdir -p "$(dirname "$MARKER")"
touch "$MARKER"
fi
exit 0