incize
Version:
AI Commit Copilot for Power Developers
184 lines (177 loc) • 6.43 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitHookManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
class GitHookManager {
gitDir;
hooksDir;
constructor() {
this.gitDir = this.findGitDir();
this.hooksDir = path.join(this.gitDir, 'hooks');
}
/**
* Find the git directory
*/
findGitDir() {
try {
const result = (0, child_process_1.execSync)('git rev-parse --git-dir', { encoding: 'utf8' }).trim();
return path.resolve(result);
}
catch (error) {
throw new Error('Not in a git repository');
}
}
/**
* Install git hooks
*/
async install() {
if (!fs.existsSync(this.hooksDir)) {
throw new Error('Git hooks directory not found');
}
// Create pre-commit hook
const preCommitHook = this.generatePreCommitHook();
fs.writeFileSync(path.join(this.hooksDir, 'pre-commit'), preCommitHook);
fs.chmodSync(path.join(this.hooksDir, 'pre-commit'), 0o755);
// Create post-commit hook
const postCommitHook = this.generatePostCommitHook();
fs.writeFileSync(path.join(this.hooksDir, 'post-commit'), postCommitHook);
fs.chmodSync(path.join(this.hooksDir, 'post-commit'), 0o755);
console.log('✅ Git hooks installed successfully');
console.log('📝 Pre-commit hook: Will analyze staged changes before commit');
console.log('📝 Post-commit hook: Will analyze committed changes after commit');
}
/**
* Uninstall git hooks
*/
async uninstall() {
const preCommitPath = path.join(this.hooksDir, 'pre-commit');
const postCommitPath = path.join(this.hooksDir, 'post-commit');
if (fs.existsSync(preCommitPath)) {
fs.unlinkSync(preCommitPath);
}
if (fs.existsSync(postCommitPath)) {
fs.unlinkSync(postCommitPath);
}
console.log('✅ Git hooks uninstalled successfully');
}
/**
* Check hook status
*/
async status() {
const preCommitPath = path.join(this.hooksDir, 'pre-commit');
const postCommitPath = path.join(this.hooksDir, 'post-commit');
return {
preCommit: fs.existsSync(preCommitPath),
postCommit: fs.existsSync(postCommitPath)
};
}
/**
* Generate pre-commit hook script
*/
generatePreCommitHook() {
return `#!/bin/sh
# Incize pre-commit hook
# Analyzes staged changes before commit
echo "🔍 Incize: Analyzing staged changes..."
INCIZE_PATH="$(which incize 2>/dev/null || echo "incize")"
# Run analysis on staged changes
if $INCIZE_PATH analyze --silent; then
echo "✅ Incize: Analysis passed"
exit 0
else
echo "❌ Incize: Analysis failed"
echo ""
echo "💡 What happened?"
echo " • Incize found potential issues in your staged changes"
echo " • This could be security risks, performance issues, or code quality concerns"
echo ""
echo "🔧 How to fix:"
echo " • Review the analysis above for specific suggestions"
echo " • Address the issues and stage your fixes"
echo " • Run 'incize analyze' to verify the fixes"
echo ""
echo "🚀 Or continue anyway:"
echo " • Use 'git commit --no-verify' to bypass this check"
echo " • Only do this if you're confident the issues are acceptable"
echo ""
read -p "Continue with commit? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "⚠️ Proceeding with commit despite analysis warnings..."
exit 0
else
echo "✅ Commit cancelled. Please address the issues and try again."
exit 1
fi
fi
`;
}
/**
* Generate post-commit hook script
*/
generatePostCommitHook() {
return `#!/bin/sh
# Incize post-commit hook
# Analyzes committed changes for insights
echo "📊 Incize: Analyzing committed changes..."
INCIZE_PATH="$(which incize 2>/dev/null || echo "incize")"
# Run analysis on committed changes
if $INCIZE_PATH analyze --silent; then
echo "✅ Incize: Post-commit analysis complete"
echo ""
echo "💡 Insights:"
echo " • Review the analysis above for improvement opportunities"
echo " • Consider the suggestions for future commits"
echo " • Run 'incize suggest' for commit message ideas"
else
echo "⚠️ Incize: Post-commit analysis failed"
echo " • This won't affect your commit"
echo " • Run 'incize analyze' manually if needed"
fi
`;
}
/**
* Check if hooks are installed
*/
isInstalled() {
const preCommitPath = path.join(this.hooksDir, 'pre-commit');
const postCommitPath = path.join(this.hooksDir, 'post-commit');
return fs.existsSync(preCommitPath) && fs.existsSync(postCommitPath);
}
}
exports.GitHookManager = GitHookManager;