-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitManager.js
More file actions
306 lines (270 loc) · 10.1 KB
/
gitManager.js
File metadata and controls
306 lines (270 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* guIDE 2.0 — Git Manager
*
* Wraps git CLI commands into a class used by both:
* 1. mcpToolServer (AI tool calls via setGitManager)
* 2. /api/git/* REST endpoints in server/main.js
*
* Uses child_process.execFile where possible for safety (no shell injection).
* Falls back to execSync for complex pipelines.
*/
'use strict';
const { execFileSync, execSync } = require('child_process');
const path = require('path');
class GitManager {
constructor() {
this._projectPath = null;
}
/** Set the working directory for all git commands. */
setProjectPath(projectPath) {
this._projectPath = projectPath;
}
get projectPath() {
return this._projectPath;
}
/* ── Status ────────────────────────────────────────────── */
/**
* Get repository status: branch, staged, modified, untracked files.
* @param {string} [cwd] — override project path
* @returns {{ branch: string, staged: string[], modified: string[], untracked: string[] }}
*/
getStatus(cwd) {
const dir = cwd || this._projectPath;
if (!dir) return { error: 'No project path set', branch: '', staged: [], modified: [], untracked: [] };
const opts = { cwd: dir, encoding: 'utf8', timeout: 5000 };
let branch = '';
try {
branch = execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], opts).trim();
} catch (_) {}
if (!branch) {
return { error: 'Not a git repository', branch: '', staged: [], modified: [], untracked: [] };
}
let statusOutput = '';
try {
statusOutput = execFileSync('git', ['status', '--porcelain'], opts);
} catch (_) {}
const staged = [];
const modified = [];
const untracked = [];
for (const line of statusOutput.split('\n')) {
if (!line.trim()) continue;
const x = line[0], y = line[1];
const file = line.substring(3).trim();
if (x === '?' && y === '?') untracked.push(file);
else if (x !== ' ' && x !== '?') staged.push(file);
if (y !== ' ' && y !== '?') modified.push(file);
}
return { branch, staged, modified, untracked };
}
/* ── Staging ───────────────────────────────────────────── */
/**
* Stage all changes.
* @param {string} [cwd]
*/
stageAll(cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
execFileSync('git', ['add', '-A'], { cwd: dir, encoding: 'utf8', timeout: 10000 });
return { success: true };
}
/**
* Stage specific files.
* @param {string[]} files
* @param {string} [cwd]
*/
stageFiles(files, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
for (const f of files) {
execFileSync('git', ['add', f], { cwd: dir, encoding: 'utf8', timeout: 10000 });
}
return { success: true };
}
/**
* Unstage all files.
* @param {string} [cwd]
*/
unstageAll(cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
execFileSync('git', ['reset', 'HEAD'], { cwd: dir, encoding: 'utf8', timeout: 10000 });
return { success: true };
}
/**
* Unstage specific files.
* @param {string[]} files
* @param {string} [cwd]
*/
unstageFiles(files, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
for (const f of files) {
execFileSync('git', ['reset', 'HEAD', f], { cwd: dir, encoding: 'utf8', timeout: 10000 });
}
return { success: true };
}
/* ── Commit ────────────────────────────────────────────── */
/**
* Commit staged changes.
* @param {string} message
* @param {string} [cwd]
*/
commit(message, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
if (!message || !message.trim()) throw new Error('Commit message required');
const output = execFileSync('git', ['commit', '-m', message.trim()],
{ cwd: dir, encoding: 'utf8', timeout: 15000 });
return { success: true, output };
}
/* ── Diff ──────────────────────────────────────────────── */
/**
* Get diff output.
* @param {{ staged?: boolean, file?: string }} [options]
* @param {string} [cwd]
*/
getDiff(options = {}, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const args = ['diff'];
if (options.staged) args.push('--cached');
if (options.file) { args.push('--'); args.push(options.file); }
const diff = execFileSync('git', args, { cwd: dir, encoding: 'utf8', timeout: 10000 });
return { success: true, diff };
}
/* ── Discard ───────────────────────────────────────────── */
/**
* Discard changes in specific files (checkout from HEAD).
* @param {string[]} files
* @param {string} [cwd]
*/
discardFiles(files, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
for (const f of files) {
execFileSync('git', ['checkout', '--', f], { cwd: dir, encoding: 'utf8', timeout: 10000 });
}
return { success: true };
}
/* ── Log ───────────────────────────────────────────────── */
/**
* Get commit log.
* @param {number} [count=20]
* @param {string} [cwd]
*/
getLog(count = 20, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
count = Math.min(100, Math.max(1, parseInt(count) || 20));
const result = execFileSync('git',
['log', '--oneline', `--format=%h|%s|%an|%ar`, `-${count}`],
{ cwd: dir, encoding: 'utf8', timeout: 10000 });
const entries = result.split('\n').filter(Boolean).map(line => {
const [hash, message, author, date] = line.split('|');
return { hash, message, author, date };
});
return { success: true, entries };
}
/* ── Branches ──────────────────────────────────────────── */
/**
* List local branches.
* @param {string} [cwd]
*/
getBranches(cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const result = execFileSync('git', ['branch'],
{ cwd: dir, encoding: 'utf8', timeout: 5000 });
const branches = result.split('\n').filter(Boolean).map(line => ({
name: line.replace(/^\*?\s*/, '').trim(),
current: line.startsWith('*'),
}));
return { success: true, branches };
}
/**
* Checkout (switch to) a branch.
* @param {string} branch
* @param {{ create?: boolean }} [options]
* @param {string} [cwd]
*/
checkout(branch, options = {}, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
if (!branch || !branch.trim()) throw new Error('Branch name required');
const args = ['checkout'];
if (options.create) args.push('-b');
args.push(branch.trim());
const output = execFileSync('git', args,
{ cwd: dir, encoding: 'utf8', timeout: 15000 });
return { success: true, output };
}
/* ── Stash ─────────────────────────────────────────────── */
/**
* Stash operations.
* @param {'push'|'pop'|'list'|'drop'} action
* @param {string} [message] — for push
* @param {string} [cwd]
*/
stash(action = 'push', message, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const args = ['stash'];
switch (action) {
case 'push':
args.push('push');
if (message) { args.push('-m'); args.push(message); }
break;
case 'pop': args.push('pop'); break;
case 'list': args.push('list'); break;
case 'drop': args.push('drop'); break;
default: break;
}
const output = execFileSync('git', args,
{ cwd: dir, encoding: 'utf8', timeout: 10000 });
return { success: true, output };
}
/* ── Push / Pull ───────────────────────────────────────── */
/**
* Push to remote.
* @param {string} [remote='origin']
* @param {string} [branch]
* @param {string} [cwd]
*/
push(remote = 'origin', branch, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const args = ['push', remote];
if (branch) args.push(branch);
const output = execFileSync('git', args,
{ cwd: dir, encoding: 'utf8', timeout: 30000 });
return { success: true, output };
}
/**
* Pull from remote.
* @param {string} [remote='origin']
* @param {string} [branch]
* @param {string} [cwd]
*/
pull(remote = 'origin', branch, cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const args = ['pull', remote];
if (branch) args.push(branch);
const output = execFileSync('git', args,
{ cwd: dir, encoding: 'utf8', timeout: 30000 });
return { success: true, output };
}
/* ── Init ──────────────────────────────────────────────── */
/**
* Initialize a new git repository.
* @param {string} [cwd]
*/
init(cwd) {
const dir = cwd || this._projectPath;
if (!dir) throw new Error('No project path set');
const output = execFileSync('git', ['init'],
{ cwd: dir, encoding: 'utf8', timeout: 5000 });
return { success: true, output };
}
}
module.exports = { GitManager };