Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitWorkflowService.ts

398 lines13.6 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import {
6 GitManagerError,
7 GitCommandError,
8 type VcsSwitchRefInput,
9 type VcsSwitchRefResult,
10 type VcsCreateRefInput,
11 type VcsCreateRefResult,
12 type VcsCreateWorktreeInput,
13 type VcsCreateWorktreeResult,
14 type VcsListRefsInput,
15 type VcsListRefsResult,
16 type GitManagerServiceError,
17 type GitPreparePullRequestThreadInput,
18 type GitPreparePullRequestThreadResult,
19 type GitPullRequestRefInput,
20 type VcsPullResult,
21 type VcsRemoveWorktreeInput,
22 type GitResolvePullRequestResult,
23 type GitRunStackedActionInput,
24 type GitRunStackedActionResult,
25 type VcsStatusInput,
26 type VcsStatusLocalResult,
27 type VcsStatusRemoteResult,
28 type VcsStatusResult,
29} from "@t3tools/contracts";
30​
31import * as GitManager from "./GitManager.ts";
32import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
33import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
34​
35export class GitWorkflowService extends Context.Service<
36 GitWorkflowService,
37 {
38 readonly isRepository: (cwd: string) => Effect.Effect<boolean, GitManagerServiceError>;
39 readonly hasCommit: (input: {
40 readonly cwd: string;
41 readonly refName: string;
42 }) => Effect.Effect<boolean, GitCommandError>;
43 readonly status: (
44 input: VcsStatusInput,
45 ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
46 readonly localStatus: (
47 input: VcsStatusInput,
48 ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
49 readonly remoteStatus: (
50 input: VcsStatusInput,
51 options?: GitManager.GitRemoteStatusOptions,
52 ) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
53 readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
54 readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
55 readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
56 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
57 readonly runStackedAction: (
58 input: GitRunStackedActionInput,
59 options?: GitManager.GitRunStackedActionOptions,
60 ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
61 readonly resolvePullRequest: (
62 input: GitPullRequestRefInput,
63 ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
64 readonly preparePullRequestThread: (
65 input: GitPreparePullRequestThreadInput,
66 ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
67 readonly listRefs: (
68 input: VcsListRefsInput,
69 ) => Effect.Effect<VcsListRefsResult, GitCommandError>;
70 readonly createWorktree: (
71 input: VcsCreateWorktreeInput,
72 options?: GitVcsDriver.CreateWorktreeOptions,
73 ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
74 readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
75 readonly fetchRemote: (input: {
76 readonly cwd: string;
77 readonly remoteName: string;
78 readonly refName?: string;
79 }) => Effect.Effect<void, GitCommandError>;
80 readonly remoteExists: (input: {
81 readonly cwd: string;
82 readonly remoteName: string;
83 }) => Effect.Effect<boolean, GitCommandError>;
84 readonly remoteBranchExists: (input: {
85 readonly cwd: string;
86 readonly remoteName: string;
87 readonly refName: string;
88 }) => Effect.Effect<boolean, GitCommandError>;
89 readonly resolveRemoteTrackingCommit: (input: {
90 readonly cwd: string;
91 readonly refName: string;
92 readonly fallbackRemoteName: string;
93 }) => Effect.Effect<
94 { readonly commitSha: string; readonly remoteRefName: string },
95 GitCommandError
96 >;
97 readonly removeWorktree: (
98 input: VcsRemoveWorktreeInput,
99 ) => Effect.Effect<void, GitCommandError>;
100 readonly pruneWorktrees: (input: {
101 readonly cwd: string;
102 }) => Effect.Effect<void, GitCommandError>;
103 readonly deleteLocalBranch: (
104 input: GitVcsDriver.GitDeleteLocalBranchInput,
105 ) => Effect.Effect<void, GitCommandError>;
106 readonly createRef: (
107 input: VcsCreateRefInput,
108 ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
109 readonly switchRef: (
110 input: VcsSwitchRefInput,
111 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
112 readonly renameBranch: (input: {
113 readonly cwd: string;
114 readonly oldBranch: string;
115 readonly newBranch: string;
116 }) => Effect.Effect<{ readonly branch: string }, GitManagerServiceError>;
117 }
118>()("t3/git/GitWorkflowService") {}
119​
120function nonRepositoryLocalStatus(): VcsStatusLocalResult {
121 return {
122 isRepo: false,
123 hasPrimaryRemote: false,
124 isDefaultRef: false,
125 refName: null,
126 hasWorkingTreeChanges: false,
127 workingTree: {
128 files: [],
129 insertions: 0,
130 deletions: 0,
131 },
132 };
134​
135function nonRepositoryStatus(): VcsStatusResult {
136 return {
137 ...nonRepositoryLocalStatus(),
138 hasUpstream: false,
139 aheadCount: 0,
140 behindCount: 0,
141 aheadOfDefaultCount: 0,
142 pr: null,
143 };
145​
146function nonRepositoryListRefs(): VcsListRefsResult {
147 return {
148 refs: [],
149 isRepo: false,
150 hasPrimaryRemote: false,
151 nextCursor: null,
152 totalCount: 0,
153 };
155​
156/** @public Service construction is part of the canonical Effect module API. */
157export const make = Effect.gen(function* () {
158 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
159 const git = yield* GitVcsDriver.GitVcsDriver;
160 const gitManager = yield* GitManager.GitManager;
161​
162 const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
163 operation: string,
164 cwd: string,
165 ) {
166 const handle = yield* registry.resolve({ cwd }).pipe(
167 Effect.mapError(
168 (cause) =>
169 new GitManagerError({
170 operation,
171 cwd,
172 detail: "Failed to resolve the VCS driver for this Git workflow.",
173 cause,
174 }),
175 ),
176 );
177 if (handle.kind !== "git") {
178 return yield* new GitManagerError({
179 operation,
180 cwd,
181 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
182 });
183 }
184 });
185​
186 const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
187 operation: string,
188 cwd: string,
189 ) {
190 const handle = yield* registry.resolve({ cwd }).pipe(
191 Effect.mapError(
192 (cause) =>
193 new GitCommandError({
194 operation,
195 command: "vcs-route",
196 cwd,
197 detail: "Failed to resolve the VCS driver for this Git command.",
198 cause,
199 }),
200 ),
201 );
202 if (handle.kind !== "git") {
203 return yield* new GitCommandError({
204 operation,
205 command: "vcs-route",
206 cwd,
207 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
208 });
209 }
210 });
211​
212 const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
213 function* (operation: string, cwd: string) {
214 const handle = yield* registry.detect({ cwd }).pipe(
215 Effect.mapError(
216 (cause) =>
217 new GitManagerError({
218 operation,
219 cwd,
220 detail: "Failed to detect a VCS repository for this Git workflow.",
221 cause,
222 }),
223 ),
224 );
225 if (!handle) {
226 return false;
227 }
228 if (handle.kind !== "git") {
229 return yield* new GitManagerError({
230 operation,
231 cwd,
232 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
233 });
234 }
235 return true;
236 },
237 );
238​
239 const detectGitRepositoryForCommand = Effect.fn(
240 "GitWorkflowService.detectGitRepositoryForCommand",
241 )(function* (operation: string, cwd: string) {
242 const handle = yield* registry.detect({ cwd }).pipe(
243 Effect.mapError(
244 (cause) =>
245 new GitCommandError({
246 operation,
247 command: "vcs-route",
248 cwd,
249 detail: "Failed to detect a VCS repository for this Git command.",
250 cause,
251 }),
252 ),
253 );
254 if (!handle) {
255 return false;
256 }
257 if (handle.kind !== "git") {
258 return yield* new GitCommandError({
259 operation,
260 command: "vcs-route",
261 cwd,
262 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
263 });
264 }
265 return true;
266 });
267​
268 const routeGitManager =
269 <Input extends { readonly cwd: string }, Output>(
270 operation: string,
271 run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
272 ) =>
273 (input: Input) =>
274 ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
275​
276 return GitWorkflowService.of({
277 isRepository: (cwd) =>
278 registry.detect({ cwd }).pipe(
279 Effect.map((handle) => handle?.kind === "git"),
280 Effect.mapError(
281 (cause) =>
282 new GitManagerError({
283 operation: "GitWorkflowService.isRepository",
284 cwd,
285 detail: "Failed to detect a VCS repository for this Git workflow.",
286 cause,
287 }),
288 ),
289 ),
290 hasCommit: (input) =>
291 ensureGitCommand("GitWorkflowService.hasCommit", input.cwd).pipe(
292 Effect.andThen(
293 git.execute({
294 operation: "GitWorkflowService.hasCommit",
295 cwd: input.cwd,
296 args: ["rev-parse", "--verify", `${input.refName}^{commit}`],
297 allowNonZeroExit: true,
298 }),
299 ),
300 Effect.map((result) => result.exitCode === 0),
301 ),
302 status: (input) =>
303 detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
304 Effect.flatMap((isGitRepository) =>
305 isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
306 ),
307 ),
308 localStatus: (input) =>
309 detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
310 Effect.flatMap((isGitRepository) =>
311 isGitRepository
312 ? gitManager.localStatus(input)
313 : Effect.succeed(nonRepositoryLocalStatus()),
314 ),
315 ),
316 remoteStatus: (input, options) =>
317 detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
318 Effect.flatMap((isGitRepository) =>
319 isGitRepository ? gitManager.remoteStatus(input, options) : Effect.succeed(null),
320 ),
321 ),
322 invalidateLocalStatus: gitManager.invalidateLocalStatus,
323 invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
324 invalidateStatus: gitManager.invalidateStatus,
325 pullCurrentBranch: (cwd) =>
326 ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
327 Effect.andThen(git.pullCurrentBranch(cwd)),
328 ),
329 runStackedAction: (input, options) =>
330 ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
331 Effect.andThen(gitManager.runStackedAction(input, options)),
332 ),
333 resolvePullRequest: routeGitManager(
334 "GitWorkflowService.resolvePullRequest",
335 gitManager.resolvePullRequest,
336 ),
337 preparePullRequestThread: routeGitManager(
338 "GitWorkflowService.preparePullRequestThread",
339 gitManager.preparePullRequestThread,
340 ),
341 listRefs: (input) =>
342 detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
343 Effect.flatMap((isGitRepository) =>
344 isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
345 ),
346 ),
347 createWorktree: (input, options) =>
348 ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
349 Effect.andThen(git.createWorktree(input, options)),
350 ),
351 listLocalBranchNames: (cwd) =>
352 ensureGitCommand("GitWorkflowService.listLocalBranchNames", cwd).pipe(
353 Effect.andThen(git.listLocalBranchNames(cwd)),
354 ),
355 fetchRemote: (input) =>
356 ensureGitCommand("GitWorkflowService.fetchRemote", input.cwd).pipe(
357 Effect.andThen(git.fetchRemote(input)),
358 ),
359 remoteExists: (input) =>
360 ensureGitCommand("GitWorkflowService.remoteExists", input.cwd).pipe(
361 Effect.andThen(git.remoteExists(input)),
362 ),
363 remoteBranchExists: (input) =>
364 ensureGitCommand("GitWorkflowService.remoteBranchExists", input.cwd).pipe(
365 Effect.andThen(git.remoteBranchExists(input)),
366 ),
367 resolveRemoteTrackingCommit: (input) =>
368 ensureGitCommand("GitWorkflowService.resolveRemoteTrackingCommit", input.cwd).pipe(
369 Effect.andThen(git.resolveRemoteTrackingCommit(input)),
370 ),
371 removeWorktree: (input) =>
372 ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
373 Effect.andThen(git.removeWorktree(input)),
374 ),
375 pruneWorktrees: (input) =>
376 ensureGitCommand("GitWorkflowService.pruneWorktrees", input.cwd).pipe(
377 Effect.andThen(git.pruneWorktrees(input)),
378 ),
379 deleteLocalBranch: (input) =>
380 ensureGitCommand("GitWorkflowService.deleteLocalBranch", input.cwd).pipe(
381 Effect.andThen(git.deleteLocalBranch(input)),
382 ),
383 createRef: (input) =>
384 ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
385 Effect.andThen(git.createRef(input)),
386 ),
387 switchRef: (input) =>
388 ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
389 Effect.andThen(Effect.scoped(git.switchRef(input))),
390 ),
391 renameBranch: (input) =>
392 ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
393 Effect.andThen(git.renameBranch(input)),
394 ),
395 });
396});
397​
398export const layer = Layer.effect(GitWorkflowService, make);