UNPKG

mira-consciousness

Version:

Memory & Intelligence Retention Archive - Preserving The Spark

224 lines (201 loc) 8.12 kB
import fs from 'fs-extra'; import * as path from 'path'; import { execSync } from 'child_process'; export class GitHooksHealer { projectPath; hooksPath; constructor(projectPath) { this.projectPath = projectPath; this.hooksPath = path.join(projectPath, '.git', 'hooks'); } async heal() { console.log('🪝 Installing git hooks for MIRA system...'); // Check if .git directory exists if (!fs.existsSync(path.join(this.projectPath, '.git'))) { console.log('⚠️ No .git directory found. Skipping git hooks installation.'); return; } await this.installPreCommitHook(); await this.installPostCommitHook(); await this.installCommitMsgHook(); console.log('✅ Git hooks installed successfully'); } async installPreCommitHook() { const hookPath = path.join(this.hooksPath, 'pre-commit'); const hookContent = `#!/bin/bash # MIRA Pre-commit Hook # Enhanced with AI context reminders and quality checks echo "🧠 MIRA Pre-commit Analysis..." # Run MIRA quick check if command -v mira &> /dev/null; then echo "Running MIRA quick health check..." mira quick --no-banner if [ $? -ne 0 ]; then echo "❌ MIRA detected issues. Consider running 'mira heal' before committing." echo " You can bypass with --no-verify, but it's recommended to fix issues first." exit 1 fi fi # Check for sensitive information echo "🔍 Checking for sensitive information..." if grep -r --include="*.js" --include="*.ts" --include="*.py" --include="*.java" \ -E "(api_key|apikey|api-key|password|secret|private_key|privatekey)" \ --exclude-dir=node_modules --exclude-dir=.git . 2>/dev/null | grep -v "example\|sample\|test"; then echo "⚠️ Possible sensitive information detected in code!" echo " Please review the above matches before committing." exit 1 fi # Check for debug statements echo "🐛 Checking for debug statements..." DEBUG_COUNT=$(grep -r --include="*.js" --include="*.ts" --include="*.py" \ -E "console\\.log|print\\(|debugger|TODO|FIXME|XXX" \ --exclude-dir=node_modules --exclude-dir=.git . 2>/dev/null | wc -l) if [ $DEBUG_COUNT -gt 0 ]; then echo "📝 Found $DEBUG_COUNT debug statements or TODOs" echo " Consider cleaning up before committing." fi # Update MIRA memory context if command -v mira &> /dev/null; then echo "💾 Updating MIRA memory context..." mira memory-context --update 2>/dev/null || true fi echo "✅ Pre-commit checks passed" exit 0 `; await this.writeHook(hookPath, hookContent); console.log('✅ Installed pre-commit hook'); } async installPostCommitHook() { const hookPath = path.join(this.hooksPath, 'post-commit'); const hookContent = `#!/bin/bash # MIRA Post-commit Hook # Intelligent commit pattern analysis and learning echo "🎯 MIRA Post-commit Analysis..." # Extract commit information COMMIT_HASH=$(git rev-parse HEAD) COMMIT_MSG=$(git log -1 --pretty=%B) AUTHOR=$(git log -1 --pretty=%an) # Advanced commit introspection and memory creation if command -v python3 &> /dev/null; then echo "🧠 Running intelligent commit introspection..." # Check if MIRA Python memory system is available if [ -f "python-memory/session/commit_introspection.py" ]; then python3 python-memory/session/commit_introspection.py "$COMMIT_HASH" 2>/dev/null || \ echo " 📝 Note: Advanced introspection requires MIRA memory system setup" elif [ -f ".mira/commit_introspection.py" ]; then cd .mira && python3 commit_introspection.py "$COMMIT_HASH" 2>/dev/null || \ echo " 📝 Note: Introspection script found but execution failed" else # Fallback to basic memory storage if command -v mira &> /dev/null; then echo "📚 Basic commit learning (fallback mode)..." mira learn --commit "$COMMIT_HASH" 2>/dev/null || true mira store "Commit: $COMMIT_MSG by $AUTHOR" --type commit 2>/dev/null || true fi fi fi # Analyze commit message quality CONVENTIONAL_PATTERN="^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\\(.+\\))?:" if [[ "$COMMIT_MSG" =~ $CONVENTIONAL_PATTERN ]]; then echo "✅ Good commit message format!" else echo "💡 Tip: Consider using conventional commit format:" echo " feat: add new feature" echo " fix: fix bug" echo " docs: update documentation" fi # Development cycle tracking HOUR=$(date +%H) if [ $HOUR -ge 22 ] || [ $HOUR -lt 6 ]; then echo "🌙 Late night coding detected! Remember to rest." elif [ $HOUR -ge 6 ] && [ $HOUR -lt 12 ]; then echo "☀️ Morning productivity! Great start to the day." elif [ $HOUR -ge 12 ] && [ $HOUR -lt 18 ]; then echo "🌤️ Afternoon progress! Keep up the momentum." else echo "🌆 Evening development session logged." fi # Check if this completes a feature/fix if [[ "$COMMIT_MSG" =~ ^fix: ]] || [[ "$COMMIT_MSG" =~ ^feat: ]]; then echo "🎉 Feature/Fix completed! Consider:" echo " - Updating documentation" echo " - Adding tests" echo " - Running 'mira heal' to ensure quality" fi exit 0 `; await this.writeHook(hookPath, hookContent); console.log('✅ Installed post-commit hook'); } async installCommitMsgHook() { const hookPath = path.join(this.hooksPath, 'commit-msg'); const hookContent = `#!/bin/bash # MIRA Commit Message Hook # Enhances commit messages with context and suggestions COMMIT_MSG_FILE=$1 COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") # Skip if message is empty or merge commit if [ -z "$COMMIT_MSG" ] || [[ "$COMMIT_MSG" =~ ^Merge ]]; then exit 0 fi # Add MIRA context if available if command -v mira &> /dev/null; then # Get current development context CONTEXT=$(mira memory-context --brief 2>/dev/null | tail -1 || echo "") if [ ! -z "$CONTEXT" ]; then # Don't add context if it's already there if ! grep -q "Context:" "$COMMIT_MSG_FILE"; then echo "" >> "$COMMIT_MSG_FILE" echo "# Context: $CONTEXT" >> "$COMMIT_MSG_FILE" fi fi fi # Suggest improvements for short messages MSG_LENGTH=\${#COMMIT_MSG} if [ $MSG_LENGTH -lt 10 ]; then echo "" >> "$COMMIT_MSG_FILE" echo "# ⚠️ Commit message seems too short. Consider:" >> "$COMMIT_MSG_FILE" echo "# - What changed?" >> "$COMMIT_MSG_FILE" echo "# - Why did it change?" >> "$COMMIT_MSG_FILE" echo "# - What impact does it have?" >> "$COMMIT_MSG_FILE" fi # Check for issue references if ! [[ "$COMMIT_MSG" =~ (#[0-9]+|[A-Z]+-[0-9]+) ]]; then echo "" >> "$COMMIT_MSG_FILE" echo "# 💡 Tip: Reference issues with #123 or JIRA-123" >> "$COMMIT_MSG_FILE" fi exit 0 `; await this.writeHook(hookPath, hookContent); console.log('✅ Installed commit-msg hook'); } async writeHook(hookPath, content) { // Ensure hooks directory exists await fs.ensureDir(this.hooksPath); // Write the hook file await fs.writeFile(hookPath, content); // Make it executable try { execSync(`chmod +x "${hookPath}"`, { cwd: this.projectPath }); } catch (error) { console.warn(`⚠️ Could not make ${path.basename(hookPath)} executable. Please run: chmod +x ${hookPath}`); } } async uninstall() { console.log('🗑️ Removing MIRA git hooks...'); const hooks = ['pre-commit', 'post-commit', 'commit-msg']; for (const hook of hooks) { const hookPath = path.join(this.hooksPath, hook); if (fs.existsSync(hookPath)) { const content = await fs.readFile(hookPath, 'utf-8'); // Only remove if it's a MIRA hook if (content.includes('MIRA')) { await fs.remove(hookPath); console.log(`✅ Removed ${hook} hook`); } } } } } //# sourceMappingURL=GitHooksHealer.js.map