revert-ai 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ export class GitHelper {
4
+ /**
5
+ * Check if the current directory is inside a Git repository.
6
+ * @returns {boolean}
7
+ */
8
+ static isGitRepository() {
9
+ try {
10
+ execSync('git rev-parse --is-inside-work-tree 2>/dev/null');
11
+ return true;
12
+ } catch (e) {
13
+ return false;
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Check if there are any uncommitted changes (dirty index or working tree).
19
+ * @returns {boolean}
20
+ */
21
+ static isWorkspaceDirty() {
22
+ if (!this.isGitRepository()) return false;
23
+ try {
24
+ const status = execSync('git status --porcelain', { encoding: 'utf8' }).trim();
25
+ return status.length > 0;
26
+ } catch (e) {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Save the current state of the workspace to a Git stash.
33
+ * @param {string} message
34
+ * @returns {boolean}
35
+ */
36
+ static saveStash(message = 'revert-ai: Pre-undo backup') {
37
+ if (!this.isGitRepository()) return false;
38
+ try {
39
+ execSync(`git stash save "${message}"`, { stdio: 'inherit' });
40
+ return true;
41
+ } catch (e) {
42
+ console.error('Failed to create Git stash:', e.message);
43
+ return false;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Create a Git checkpoint commit.
49
+ * @param {string} message
50
+ * @returns {string|null} commit hash or null
51
+ */
52
+ static createCheckpointCommit(message = 'revert-ai: Checkpoint before undo') {
53
+ if (!this.isGitRepository()) return null;
54
+ try {
55
+ // Stage all changes (created, modified, deleted)
56
+ execSync('git add -A');
57
+ // Commit without run hooks to be fast/safe
58
+ execSync(`git commit -m "${message}" --no-verify`, { stdio: 'ignore' });
59
+ const hash = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
60
+ return hash;
61
+ } catch (e) {
62
+ console.error('Failed to create Git checkpoint commit:', e.message);
63
+ return null;
64
+ }
65
+ }
66
+ }