commit-guardian
Version:
Interactive CLI tool with browser-based GitHub-style diff viewer for reviewing and approving git changes before commit. Features React-based UI, approval workflow, line comments, and safe commit protection.
67 lines (66 loc) ⢠2.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const simple_git_1 = require("simple-git");
const router = express_1.default.Router();
router.post('/approve', async (req, res) => {
try {
const { message, fileComments } = req.body;
if (!message?.trim()) {
return res.status(400).json({
success: false,
error: 'Commit message is required'
});
}
console.log('\nā
Changes approved!');
console.log(`š Commit message: ${message}`);
if (fileComments && fileComments.length > 0) {
console.log(`š¬ Comments: ${fileComments.length}`);
fileComments.forEach((comment, index) => {
console.log(` ${index + 1}. ${comment.file}:${comment.line} - ${comment.text}`);
});
}
// Execute git commit
const git = (0, simple_git_1.simpleGit)();
await git.commit(message);
console.log('š Successfully committed changes!');
// Call the approval callback if provided
const options = global.commitGuardianOptions;
if (options?.onApproval) {
options.onApproval(true, message, undefined, fileComments);
}
const response = {
success: true,
autoClose: true, // Close browser after success
};
res.json(response);
// Shutdown server after successful commit
setTimeout(() => {
console.log('šŖ Shutting down server after successful commit...');
process.exit(0);
}, 1000);
}
catch (error) {
console.error('\nā Commit failed!');
console.error('Error details:');
if (error instanceof Error) {
console.error(error.message);
}
else {
console.error(error);
}
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Commit failed'
});
// Exit process after commit failure so LLM can continue
setTimeout(() => {
console.log('šŖ Shutting down server after commit failure...');
process.exit(1);
}, 1000);
}
});
exports.default = router;