-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
348 lines (307 loc) · 10.9 KB
/
Copy pathgit.ts
File metadata and controls
348 lines (307 loc) · 10.9 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import type {
GitAddWorktreeResult,
GitBranchListResult,
GitDiffBatchResult,
GitDiffResult,
GitFileContentResult,
GitFinishMergeResult,
GitGetWorktreeSourceBranchResult,
GitMergeToSourceResult,
GitPullFromSourceResult,
GitStatusResult,
GitSwitchBranchResult,
GitWorktreeListResult,
ProjectLocation,
} from "@/shared/contracts";
import { buildWorktreeLocation } from "@/shared/worktree";
import { parallelWslCommandsAsync, quotePosixShellArg as quote } from "./agents/base";
import {
computeDefaultWorktreePath,
execGit,
GIT_HOOK_TIMEOUT,
getLocationIdentity,
parseRemoteUrl,
} from "./git/exec";
import { GitMergeService } from "./git/mergeService";
import {
GitStatusService,
buildGitStatusResultFromOutputs,
parseStatusPorcelainV2,
} from "./git/statusService";
import {
GitWorktreeService,
buildBranchListArgs,
parseBranchListOutput,
parseWorktreeListOutput,
} from "./git/worktreeService";
export {
computeDefaultWorktreePath,
execGit,
getLocationIdentity,
parseRemoteUrl,
parseStatusPorcelainV2,
};
export class GitService {
private readonly statusService = new GitStatusService();
private readonly worktreeService = new GitWorktreeService();
private readonly mergeService = new GitMergeService(this.worktreeService);
async getStatus(location: ProjectLocation): Promise<GitStatusResult> {
return this.statusService.getStatus(location);
}
/**
* WSL fast path for project refresh: collapse `rev-parse + status + remote +
* diff numstats + branch list + worktree list (+ gh --version)` into a single
* `wsl.exe` spawn that runs all commands in parallel inside the distro. Saves
* ~6 separate bash-init cycles vs spawning each git op individually.
*/
async batchedWslProjectSnapshot(
location: ProjectLocation & { kind: "wsl" },
includeGhCheck: boolean,
): Promise<{
status: GitStatusResult | null;
branches: GitBranchListResult | null;
worktrees: GitWorktreeListResult["worktrees"] | null;
ghAvailable: boolean | null;
}> {
const cwd = location.linuxPath;
const branchListArgs = buildBranchListArgs(true)
.map((a) => quote(a))
.join(" ");
const commands: { cwd: string; cmd: string }[] = [
{ cwd, cmd: "git rev-parse --is-inside-work-tree" },
{ cwd, cmd: "git status --porcelain=v2 -b" },
{ cwd, cmd: "git remote -v" },
{ cwd, cmd: "git diff --cached --numstat" },
{ cwd, cmd: "git diff --numstat" },
{ cwd, cmd: `git ${branchListArgs}` },
{ cwd, cmd: "git worktree list --porcelain" },
...(includeGhCheck ? [{ cwd, cmd: "gh --version" }] : []),
];
const results = await parallelWslCommandsAsync(location.distro, commands, {
timeoutMs: 30_000,
});
const isRepo = results[0]!.ok;
const baseStatus = buildGitStatusResultFromOutputs({
isRepo,
statusOutput: results[1]!.stdout,
remoteOutput: results[2]!.stdout,
stagedNumstat: results[3]!.stdout,
unstagedNumstat: results[4]!.stdout,
});
const status = isRepo
? await this.statusService.applyMergeState(
location,
results[1]!.stdout,
await this.statusService.enrichStatus(location, baseStatus),
)
: baseStatus;
const branches = results[5]!.ok ? parseBranchListOutput(results[5]!.stdout) : null;
const worktrees = results[6]!.ok
? parseWorktreeListOutput(results[6]!.stdout, location.kind).worktrees
: null;
const ghAvailable = includeGhCheck ? results[7]!.ok : null;
return { status, branches, worktrees, ghAvailable };
}
/**
* Fetch `git status` for many worktree paths at once. On WSL collapses N
* separate `wsl.exe` invocations into one parallel batch script — what used
* to be N×bash-init cycles becomes one. Worktrees whose status fetch fails
* are silently dropped from the result.
*/
async getWorktreeStatusBatch(
location: ProjectLocation,
worktreePaths: string[],
): Promise<Record<string, GitStatusResult>> {
if (worktreePaths.length === 0) return {};
if (location.kind === "wsl") {
return this.statusService.getWorktreeStatusBatchWsl(location, worktreePaths);
}
const entries = await Promise.all(
worktreePaths.map(async (path) => {
try {
const wtLocation = buildWorktreeLocation(location, path);
const status = await this.statusService.getStatus(wtLocation);
return [path, status] as const;
} catch {
return undefined;
}
}),
);
return Object.fromEntries(entries.filter((e) => e !== undefined));
}
async getDiff(
location: ProjectLocation,
filePath?: string,
staged?: boolean,
): Promise<GitDiffResult> {
return this.statusService.getDiff(location, filePath, staged);
}
async stage(location: ProjectLocation, filePath: string): Promise<void> {
await execGit(location, ["add", "--", filePath]);
}
async unstage(location: ProjectLocation, filePath: string): Promise<void> {
await execGit(location, ["reset", "HEAD", "--", filePath]);
}
async revert(location: ProjectLocation, filePath: string): Promise<void> {
const statusOutput = await execGit(location, ["status", "--porcelain=v2", "--", filePath]);
const parsed = parseStatusPorcelainV2(statusOutput);
const unstagedEntry = parsed.unstaged.find(
(entry) => entry.path === filePath.replace(/\\/g, "/"),
);
if (unstagedEntry?.status === "?") {
await execGit(location, ["clean", "-f", "--", filePath]);
return;
}
if (unstagedEntry?.status === "R" && unstagedEntry.oldPath) {
await execGit(location, ["clean", "-f", "--", filePath]);
await execGit(location, ["checkout", "--", unstagedEntry.oldPath]);
return;
}
await execGit(location, ["checkout", "--", filePath]);
}
async getDiffBatch(
location: ProjectLocation,
untrackedPaths: string[],
): Promise<GitDiffBatchResult> {
return this.statusService.getDiffBatch(location, untrackedPaths);
}
async getFileContent(
location: ProjectLocation,
filePath: string,
staged: boolean,
): Promise<GitFileContentResult> {
return this.statusService.getFileContent(location, filePath, staged);
}
async stageAll(location: ProjectLocation): Promise<void> {
await execGit(location, ["add", "."]);
}
async unstageAll(location: ProjectLocation): Promise<void> {
await execGit(location, ["reset", "HEAD"]);
}
async revertAll(location: ProjectLocation): Promise<void> {
await execGit(location, ["checkout", "--", "."]);
await execGit(location, ["clean", "-fd"]);
}
async commit(
location: ProjectLocation,
message: string,
addAll: boolean,
): Promise<{ hash: string }> {
if (addAll) {
await execGit(location, ["add", "."]);
}
const output = await execGit(location, ["commit", "-m", message], {
timeout: GIT_HOOK_TIMEOUT,
});
const hashMatch = output.match(/\[.+?\s+([a-f0-9]+)\]/);
return { hash: hashMatch?.[1] ?? "" };
}
async getStagedDiff(location: ProjectLocation): Promise<string> {
return execGit(location, ["diff", "--cached"]);
}
async getAllDiff(location: ProjectLocation): Promise<string> {
return execGit(location, ["diff"]);
}
async getLogRange(location: ProjectLocation, base: string, head: string): Promise<string> {
return execGit(location, ["log", "--oneline", `${base}..${head}`]);
}
async getDiffRange(location: ProjectLocation, base: string, head: string): Promise<string> {
return execGit(location, ["diff", `${base}...${head}`]);
}
async listBranches(
location: ProjectLocation,
includeRemote: boolean,
): Promise<GitBranchListResult> {
return this.worktreeService.listBranches(location, includeRemote);
}
async fetch(location: ProjectLocation, remote: string, prune: boolean): Promise<void> {
return this.worktreeService.fetch(location, remote, prune);
}
async pull(location: ProjectLocation, remote: string): Promise<void> {
return this.worktreeService.pull(location, remote);
}
async pullRebase(location: ProjectLocation, remote: string): Promise<void> {
return this.worktreeService.pullRebase(location, remote);
}
async push(
location: ProjectLocation,
remote: string,
branch?: string,
setUpstream?: boolean,
): Promise<void> {
return this.worktreeService.push(location, remote, branch, setUpstream);
}
async listWorktrees(location: ProjectLocation): Promise<GitWorktreeListResult> {
return this.worktreeService.listWorktrees(location);
}
async addWorktree(
location: ProjectLocation,
path: string | undefined,
branch?: string,
createBranch?: boolean,
startPoint?: string,
): Promise<GitAddWorktreeResult> {
return this.worktreeService.addWorktree(location, path, branch, createBranch, startPoint);
}
async removeWorktree(
location: ProjectLocation,
path: string,
force: boolean,
deleteBranch?: boolean,
): Promise<void> {
return this.worktreeService.removeWorktree(location, path, force, deleteBranch);
}
async deleteRemoteBranch(
location: ProjectLocation,
remote: string,
branch: string,
): Promise<void> {
return this.worktreeService.deleteRemoteBranch(location, remote, branch);
}
async deleteBranch(location: ProjectLocation, branch: string, force: boolean): Promise<void> {
return this.worktreeService.deleteBranch(location, branch, force);
}
async switchBranch(
location: ProjectLocation,
branch: string,
createNew: boolean,
): Promise<GitSwitchBranchResult> {
return this.worktreeService.switchBranch(location, branch, createNew);
}
async getWorktreeSourceBranch(
location: ProjectLocation,
branch: string,
sourceBranchOverride?: string,
): Promise<GitGetWorktreeSourceBranchResult> {
return this.worktreeService.getWorktreeSourceBranch(location, branch, sourceBranchOverride);
}
async mergeToSource(
repoLocation: ProjectLocation,
worktreeLocation: ProjectLocation,
worktreeBranch: string,
sourceBranch: string,
): Promise<GitMergeToSourceResult> {
return this.mergeService.mergeToSource(
repoLocation,
worktreeLocation,
worktreeBranch,
sourceBranch,
);
}
async pullFromSource(
worktreeLocation: ProjectLocation,
sourceBranch: string,
preserveLocalChanges = false,
): Promise<GitPullFromSourceResult> {
return this.mergeService.pullFromSource(worktreeLocation, sourceBranch, preserveLocalChanges);
}
async abortMerge(worktreeLocation: ProjectLocation): Promise<void> {
return this.mergeService.abortMerge(worktreeLocation);
}
async finishMerge(worktreeLocation: ProjectLocation): Promise<GitFinishMergeResult> {
return this.mergeService.finishMerge(worktreeLocation);
}
async pruneWorktrees(location: ProjectLocation, activeWorktreePaths: string[]): Promise<void> {
return this.worktreeService.pruneWorktrees(location, activeWorktreePaths);
}
}