UNPKG

@iyulab/oops

Version:

Core SDK for Oops - Safe text file editing with automatic backup

156 lines 4.76 kB
"use strict"; /** * Git operations wrapper for Oops */ Object.defineProperty(exports, "__esModule", { value: true }); exports.GitWrapper = void 0; const simple_git_1 = require("simple-git"); const errors_1 = require("./errors"); const file_system_1 = require("./file-system"); class GitWrapper { _workingDir; git = null; isInitialized = false; constructor(_workingDir) { this._workingDir = _workingDir; // Lazy initialization - only create git instance when needed } async init() { try { // Ensure working directory exists await file_system_1.FileSystem.mkdir(this._workingDir); // Initialize git instance this.git = (0, simple_git_1.simpleGit)(this._workingDir); await this.git.init(); // Set up initial git configuration await this.git.addConfig('user.name', 'Oops'); await this.git.addConfig('user.email', 'oops@localhost'); this.isInitialized = true; } catch (error) { throw new errors_1.GitOperationError('init', { error: error.message, workingDir: this._workingDir }); } } async ensureInitialized() { if (!this.isInitialized || !this.git) { await this.init(); } } async add(filePath) { try { await this.ensureInitialized(); await this.git.add(filePath); } catch (error) { throw new errors_1.GitOperationError('add', { error: error.message, filePath, workingDir: this._workingDir, }); } } async commit(message) { try { await this.ensureInitialized(); await this.git.commit(message); } catch (error) { throw new errors_1.GitOperationError('commit', { error: error.message, message, workingDir: this._workingDir, }); } } async diff(filePath) { try { await this.ensureInitialized(); // Check if there are any commits const commitCount = await this.getCommitCount(); if (commitCount === 0) { // No commits yet, show staged files as diff if (filePath) { return await this.git.diff(['--cached', filePath]); } return await this.git.diff(['--cached']); } if (filePath) { return await this.git.diff(['HEAD', filePath]); } return await this.git.diff(['HEAD']); } catch (error) { // Return empty diff instead of throwing for graceful handling return ''; } } async status() { try { await this.ensureInitialized(); return await this.git.status(); } catch (error) { throw new errors_1.GitOperationError('status', { error: error.message, workingDir: this._workingDir }); } } async reset(filePath) { try { await this.ensureInitialized(); if (filePath) { await this.git.reset(['HEAD', filePath]); } else { await this.git.reset(simple_git_1.ResetMode.HARD); } } catch (error) { throw new errors_1.GitOperationError('reset', { error: error.message, filePath, workingDir: this._workingDir, }); } } async checkout(filePath) { try { await this.ensureInitialized(); await this.git.checkout(['HEAD', filePath]); } catch (error) { throw new errors_1.GitOperationError('checkout', { error: error.message, filePath, workingDir: this._workingDir, }); } } async isHealthy() { try { // Don't auto-initialize, just check if it's already initialized if (!this.isInitialized || !this.git) { return false; } await this.git.status(); return true; } catch { return false; } } async getCommitCount() { try { await this.ensureInitialized(); const log = await this.git.log(); return log.total; } catch { return 0; } } async hasCommits() { const count = await this.getCommitCount(); return count > 0; } } exports.GitWrapper = GitWrapper; //# sourceMappingURL=git.js.map