ccguard
Version:
Automated enforcement of net-negative LOC, complexity constraints, and quality standards for Claude code
143 lines • 4.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RevertManager = void 0;
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class RevertManager {
rootDir;
constructor(rootDir) {
this.rootDir = rootDir;
}
/**
* Revert files to their state in the given snapshot
*/
async revertToSnapshot(affectedFiles, snapshot) {
try {
// Create a backup of current changes in case revert fails
const backup = this.createBackup(affectedFiles);
try {
for (const filePath of affectedFiles) {
const absolutePath = path_1.default.isAbsolute(filePath)
? filePath
: path_1.default.join(this.rootDir, filePath);
const snapshotFile = snapshot.files.get(absolutePath);
if (!snapshotFile) {
// File didn't exist in snapshot, so remove it
if (fs_1.default.existsSync(absolutePath)) {
fs_1.default.unlinkSync(absolutePath);
}
}
else {
// Restore file content from snapshot
fs_1.default.mkdirSync(path_1.default.dirname(absolutePath), { recursive: true });
fs_1.default.writeFileSync(absolutePath, snapshotFile.content);
}
}
// Clean up backup on success
this.cleanupBackup(backup);
return { success: true };
}
catch (error) {
// Restore from backup on failure
this.restoreBackup(backup);
throw error;
}
}
catch (error) {
return {
success: false,
error: `Failed to revert changes: ${error instanceof Error ? error.message : String(error)}`,
};
}
}
/**
* Check if we're in a git repository
*/
isGitRepo() {
try {
(0, child_process_1.execSync)('git rev-parse --git-dir', {
cwd: this.rootDir,
stdio: 'pipe',
});
return true;
}
catch {
return false;
}
}
/**
* Check if a file is tracked by git
*/
isFileTracked(filePath) {
try {
(0, child_process_1.execSync)(`git ls-files --error-unmatch "${filePath}"`, {
cwd: this.rootDir,
stdio: 'pipe',
});
return true;
}
catch {
return false;
}
}
/**
* Create a backup of files before reverting
*/
createBackup(files) {
const backup = new Map();
for (const filePath of files) {
const absolutePath = path_1.default.isAbsolute(filePath)
? filePath
: path_1.default.join(this.rootDir, filePath);
if (fs_1.default.existsSync(absolutePath)) {
try {
const content = fs_1.default.readFileSync(absolutePath, 'utf-8');
backup.set(absolutePath, content);
}
catch {
// Skip files we can't read
}
}
}
return backup;
}
/**
* Restore files from backup
*/
restoreBackup(backup) {
for (const [filePath, content] of backup) {
try {
fs_1.default.writeFileSync(filePath, content);
}
catch {
// Best effort restore
}
}
}
/**
* Clean up backup (no-op for in-memory backup)
*/
cleanupBackup(backup) {
// In-memory backup, nothing to clean up
}
/**
* Get git status for diagnostics
*/
getGitStatus() {
try {
return (0, child_process_1.execSync)('git status --porcelain', {
cwd: this.rootDir,
encoding: 'utf-8',
});
}
catch {
return 'Not a git repository';
}
}
}
exports.RevertManager = RevertManager;
//# sourceMappingURL=RevertManager.js.map