Files
code-tools/tf/scripts/git-hooks.sh
Travis Vasceannie bb469f8d2b Refactor workspace setup script for cross-platform compatibility and improved user management
- Added system detection logic to handle different OS types (Linux, macOS, Windows).
- Enhanced user creation logic to support non-root execution and proper ownership.
- Updated directory creation to use dynamic home paths based on detected OS.
- Improved Git configuration and metadata capture with error handling.
- Modularized system package installation based on OS type.
- Streamlined Node.js, Python, and Rust setup scripts with error handling.
- Updated shell configuration to include dynamic aliases and environment info script.
- Deprecated `devcontainer_image` variable in favor of `devcontainer_repo_url` for better repository management.
- Adjusted Terraform workspace configuration to support new repository URL and caching options.
2025-09-07 20:56:56 +00:00

169 lines
7.0 KiB
Bash

#!/bin/bash
# Convert CRLF to LF if present (handles Windows line endings)
if command -v dos2unix >/dev/null 2>&1; then
dos2unix "$0" 2>/dev/null || true
fi
set -e
echo "📝 Setting up Git hooks and metadata capture..."
# Cross-platform directory and user detection
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then
HOME_DIR="${USERPROFILE:-$HOME}"
USER_NAME="${USERNAME:-${USER:-coder}}"
WORKSPACES_DIR="${HOME_DIR}/workspaces"
TEMP_DIR="${TEMP:-/tmp}"
else
HOME_DIR="${HOME:-/home/coder}"
USER_NAME="${USER:-coder}"
WORKSPACES_DIR="/workspaces"
TEMP_DIR="/tmp"
fi
# Ensure workspaces directory exists and navigate to it
mkdir -p "$WORKSPACES_DIR"
cd "$WORKSPACES_DIR"
# 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 using printf
{
printf '#!/bin/bash\n'
printf '# Post-commit hook to capture git metadata\n'
printf 'echo "📝 Capturing git metadata after commit..."\n'
printf '\n'
printf '# Cross-platform temp directory detection\n'
printf 'if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then\n'
printf ' TEMP_DIR="${TEMP:-/tmp}"\n'
printf 'else\n'
printf ' TEMP_DIR="/tmp"\n'
printf 'fi\n'
printf '\n'
printf '# Ensure metadata directory exists\n'
printf 'mkdir -p "$TEMP_DIR/git-metadata"\n'
printf '\n'
printf '# Capture current git state\n'
printf 'git branch --show-current > "$TEMP_DIR/git-metadata/current-branch" 2>/dev/null || printf "main" > "$TEMP_DIR/git-metadata/current-branch"\n'
printf 'git rev-parse HEAD > "$TEMP_DIR/git-metadata/commit-hash" 2>/dev/null || printf "no-commits" > "$TEMP_DIR/git-metadata/commit-hash"\n'
printf 'git remote get-url origin > "$TEMP_DIR/git-metadata/remote-url" 2>/dev/null || printf "no-remote" > "$TEMP_DIR/git-metadata/remote-url"\n'
printf '\n'
printf '# Log the commit for development tracking\n'
printf 'printf "%%s: Commit %%s on branch %%s\\n" "$(date)" "$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")" "$(git branch --show-current 2>/dev/null || echo "unknown")" >> "$TEMP_DIR/git-metadata/commit-log"\n'
printf '\n'
printf 'echo "✅ Git metadata updated"\n'
} > .git/hooks/post-commit
# Create pre-push hook for quality checks using printf
{
printf '#!/bin/bash\n'
printf '# Pre-push hook for basic quality checks\n'
printf 'echo "🔍 Running pre-push quality checks..."\n'
printf '\n'
printf '# Check if package.json exists and run tests\n'
printf 'if [ -f "package.json" ]; then\n'
printf ' echo "📦 Found Node.js project, checking scripts..."\n'
printf ' if npm run --silent test --if-present 2>/dev/null; then\n'
printf ' echo "✅ Tests passed"\n'
printf ' else\n'
printf ' echo "⚠️ Tests not found or failed - pushing anyway"\n'
printf ' fi\n'
printf 'fi\n'
printf '\n'
printf '# Check if requirements.txt or pyproject.toml exists\n'
printf 'if [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then\n'
printf ' echo "🐍 Found Python project..."\n'
printf ' # Could add Python linting here\n'
printf ' echo "✅ Python project checks passed"\n'
printf 'fi\n'
printf '\n'
printf '# Check for large files (cross-platform compatible)\n'
printf 'echo "📁 Checking for large files..."\n'
printf 'if command -v find >/dev/null 2>&1; then\n'
printf ' large_files=$(find . -type f -size +100M 2>/dev/null | head -5)\n'
printf ' if [ ! -z "$large_files" ]; then\n'
printf ' echo "⚠️ Large files detected:"\n'
printf ' printf "%%s\\n" "$large_files"\n'
printf ' echo "Consider using Git LFS for large files"\n'
printf ' fi\n'
printf 'else\n'
printf ' echo "⚠️ find command not available, skipping large file check"\n'
printf 'fi\n'
printf '\n'
printf 'echo "✅ Pre-push checks completed"\n'
} > .git/hooks/pre-push
# Make hooks executable (cross-platform compatible)
if [[ "$OSTYPE" != "msys" && "$OSTYPE" != "cygwin" && "$OSTYPE" != "win32" ]]; then
chmod +x .git/hooks/post-commit
chmod +x .git/hooks/pre-push
else
# On Windows, Git Bash should handle executable permissions automatically
echo "🔧 Git hooks created (Windows will handle executable permissions)"
fi
# Set proper ownership (Unix-like systems only)
if [[ "$OSTYPE" != "msys" && "$OSTYPE" != "cygwin" && "$OSTYPE" != "win32" ]]; then
if command -v chown >/dev/null 2>&1 && [ "$USER_NAME" != "$(whoami)" ]; then
chown -R "$USER_NAME:$USER_NAME" .git/hooks 2>/dev/null || {
echo "⚠️ Could not set ownership - you may need to run with appropriate permissions"
}
fi
fi
# Create a helper script for viewing git metadata
{
printf '#!/bin/bash\n'
printf '# Helper script to view captured git metadata\n'
printf '\n'
printf '# Cross-platform temp directory detection\n'
printf 'if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then\n'
printf ' TEMP_DIR="${TEMP:-/tmp}"\n'
printf 'else\n'
printf ' TEMP_DIR="/tmp"\n'
printf 'fi\n'
printf '\n'
printf 'METADATA_DIR="$TEMP_DIR/git-metadata"\n'
printf '\n'
printf 'echo "📊 Git Metadata Summary"\n'
printf 'echo "====================="\n'
printf '\n'
printf 'if [ -d "$METADATA_DIR" ]; then\n'
printf ' if [ -f "$METADATA_DIR/current-branch" ]; then\n'
printf ' printf "Current Branch: %%s\\n" "$(cat "$METADATA_DIR/current-branch")"\n'
printf ' fi\n'
printf ' \n'
printf ' if [ -f "$METADATA_DIR/commit-hash" ]; then\n'
printf ' printf "Latest Commit: %%s\\n" "$(cat "$METADATA_DIR/commit-hash")"\n'
printf ' fi\n'
printf ' \n'
printf ' if [ -f "$METADATA_DIR/remote-url" ]; then\n'
printf ' printf "Remote URL: %%s\\n" "$(cat "$METADATA_DIR/remote-url")"\n'
printf ' fi\n'
printf ' \n'
printf ' if [ -f "$METADATA_DIR/commit-log" ]; then\n'
printf ' echo ""\n'
printf ' echo "Recent Commits:"\n'
printf ' tail -5 "$METADATA_DIR/commit-log" 2>/dev/null || echo "No commit log available"\n'
printf ' fi\n'
printf 'else\n'
printf ' echo "No git metadata found. Make a commit to generate metadata."\n'
printf 'fi\n'
} > .git/hooks/show-metadata
# Make metadata viewer executable
if [[ "$OSTYPE" != "msys" && "$OSTYPE" != "cygwin" && "$OSTYPE" != "win32" ]]; then
chmod +x .git/hooks/show-metadata
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"
echo "💡 Run '.git/hooks/show-metadata' to view captured git metadata"
echo "💡 Metadata is stored in: $TEMP_DIR/git-metadata/"