echo "π Running pre-push validations..."
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Check for sensitive files in staged changes
echo "π Checking for sensitive files..."
if git diff --name-only HEAD~1..HEAD 2>/dev/null | grep -E "\.(key|pem|p12|pfx)$|\.env$|\.env\.(local|development|test|production)$" > /dev/null; then
echo "β Attempting to push sensitive files. Please remove them from the commit."
git diff --name-only HEAD~1..HEAD | grep -E "\.(key|pem|p12|pfx)$|\.env$|\.env\.(local|development|test|production)$"
exit 1
fi
# Check for alternative file versions (forbidden)
echo "π Checking for forbidden file patterns..."
ALT_FILES=$(find src -name "*-v2.ts" -o -name "*-new.ts" -o -name "*-old.ts" -o -name "*-temp.ts" -o -name "*-backup.ts" -o -name "*-enhanced.ts" -o -name "*-improved.ts" 2>/dev/null)
if [ -n "$ALT_FILES" ]; then
echo "β Forbidden alternative file versions found:"
echo "$ALT_FILES"
exit 1
fi
# NEW: Comprehensive untracked file check (REQ-3)
echo "π Checking for untracked source files..."
UNTRACKED_SRC=$(find src -name "*.ts" -type f 2>/dev/null | while read -r file; do
if ! git ls-files --error-unmatch "$file" >/dev/null 2>&1; then
echo "$file"
fi
done)
if [ -n "$UNTRACKED_SRC" ]; then
echo "β Found untracked source files that WILL cause CI failures:"
echo "$UNTRACKED_SRC"
echo ""
echo "π‘ These files exist locally but are not in git."
echo " CI will fail because it only sees tracked files."
echo " To fix:"
echo " 1. git add <files>"
echo " 2. git commit --amend (to add to current commit)"
echo " Or create a new commit:"
echo " 1. git add <files>"
echo " 2. git commit -m 'fix: add missing source files'"
exit 1
fi
# NEW: Validate no source files are gitignored (REQ-3, REQ-7)
echo "π Validating .gitignore doesn't ignore source code..."
IGNORED_SRC=$(git check-ignore src/**/*.ts 2>/dev/null || true)
if [ -n "$IGNORED_SRC" ]; then
echo "β Source files are being ignored by .gitignore:"
echo "$IGNORED_SRC"
echo ""
echo "π‘ This WILL cause CI failures. The .gitignore patterns are"
echo " preventing these source files from being committed."
echo ""
echo " To fix .gitignore patterns:"
echo " 1. Use leading '/' to anchor patterns to repository root"
echo " 2. Example: Change 'memory/' to '/memory/'"
echo " 3. This ensures only root-level directories are ignored,"
echo " not src/memory/ or other nested directories."
exit 1
fi
# NEW: Validate TypeScript imports resolve correctly (REQ-7)
echo "π Validating TypeScript import resolution..."
timeout 60s npm run typecheck >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "β οΈ TypeScript import resolution issues detected."
echo " Running full typecheck for details..."
npm run typecheck
echo ""
echo "π‘ Import resolution failures often indicate:"
echo " - Missing source files (not tracked by git)"
echo " - Incorrect import paths"
echo " - Missing type definitions"
exit 1
fi
# Run full validation with strict linting (same as CI)
echo "ποΈ Running CI validation..."
timeout 220s npm run validate:ci
if [ $? -ne 0 ]; then
echo "β CI validation failed. Please fix the issues above."
echo "π‘ CI validation includes: security audit, format check, strict lint (zero warnings), typecheck, tests with coverage."
exit 1
fi
# Build the project
echo "π¦ Building project..."
timeout 120s npm run build:quick
if [ $? -ne 0 ]; then
echo "β Build failed. Please fix the issues above."
exit 1
fi
# Additional checks for protected branches
if [ "$current_branch" = "main" ] || [ "$current_branch" = "master" ]; then
echo "π Protected branch: $current_branch"
if git log --oneline -5 | grep -E "(bump|version|release|v[0-9])" > /dev/null; then
echo "β
Version update detected."
else
echo "β οΈ No version update detected. Consider bumping version."
fi
fi
echo "β
Pre-push validations passed!"