Files
code-tools/tf/scripts/git-hooks.sh
2025-08-24 21:47:21 -04:00

85 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "📝 Setting up Git hooks and metadata capture..."
# Ensure we're in the workspaces directory
cd /workspaces
# Initialize git repository if it doesn't exist
if [ ! -d ".git" ]; then
echo "🔧 Initializing git repository..."
git init
fi
# Create .git/hooks directory if it doesn't exist
mkdir -p .git/hooks
# Create post-commit hook for metadata capture
cat > .git/hooks/post-commit << 'POST_COMMIT_END'
#!/bin/bash
# Post-commit hook to capture git metadata
echo "📝 Capturing git metadata after commit..."
# Ensure metadata directory exists
mkdir -p /tmp/git-metadata
# Capture current git state
git branch --show-current > /tmp/git-metadata/current-branch 2>/dev/null || echo "main" > /tmp/git-metadata/current-branch
git rev-parse HEAD > /tmp/git-metadata/commit-hash 2>/dev/null || echo "no-commits" > /tmp/git-metadata/commit-hash
git remote get-url origin > /tmp/git-metadata/remote-url 2>/dev/null || echo "no-remote" > /tmp/git-metadata/remote-url
# Log the commit for development tracking
echo "$(date): Commit $(git rev-parse --short HEAD) on branch $(git branch --show-current)" >> /tmp/git-metadata/commit-log
echo "✅ Git metadata updated"
POST_COMMIT_END
# Make post-commit hook executable
chmod +x .git/hooks/post-commit
# Create pre-push hook for quality checks
cat > .git/hooks/pre-push << 'PRE_PUSH_END'
#!/bin/bash
# Pre-push hook for basic quality checks
echo "🔍 Running pre-push quality checks..."
# Check if package.json exists and run tests
if [ -f "package.json" ]; then
echo "📦 Found Node.js project, checking scripts..."
if npm run --silent test --if-present; then
echo "✅ Tests passed"
else
echo "⚠️ Tests not found or failed - pushing anyway"
fi
fi
# Check if requirements.txt or pyproject.toml exists
if [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then
echo "🐍 Found Python project..."
# Could add Python linting here
echo "✅ Python project checks passed"
fi
# Check for large files
echo "📁 Checking for large files..."
large_files=$(find . -type f -size +100M 2>/dev/null | head -5)
if [ ! -z "$large_files" ]; then
echo "⚠️ Large files detected:"
echo "$large_files"
echo "Consider using Git LFS for large files"
fi
echo "✅ Pre-push checks completed"
PRE_PUSH_END
# Make pre-push hook executable
chmod +x .git/hooks/pre-push
# Set proper ownership for the coder user
if id -u coder >/dev/null 2>&1; then
chown -R coder:coder .git/hooks
fi
echo "✅ Git hooks and metadata capture configured"
echo "📝 Git metadata will be automatically captured on commits"
echo "🔍 Pre-push quality checks will run before each push"