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

GitWorkflowService.ts

386 lines13.0 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 fetchRemote: (input: {
75 readonly cwd: string;
76 readonly remoteName: string;
77 readonly refName?: string;
78 }) => Effect.Effect<void, GitCommandError>;
79 readonly remoteExists: (input: {
80 readonly cwd: string;
81 readonly remoteName: string;
82 }) => Effect.Effect<boolean, GitCommandError>;
83 readonly remoteBranchExists: (input: {
84 readonly cwd: string;
85 readonly remoteName: string;
86 readonly refName: string;
87 }) => Effect.Effect<boolean, GitCommandError>;
88 readonly resolveRemoteTrackingCommit: (input: {
89 readonly cwd: string;
90 readonly refName: string;
91 readonly fallbackRemoteName: string;
92 }) => Effect.Effect<
93 { readonly commitSha: string; readonly remoteRefName: string },
94 GitCommandError
95 >;
96 readonly removeWorktree: (
97 input: VcsRemoveWorktreeInput,
98 ) => Effect.Effect<void, GitCommandError>;
99 readonly pruneWorktrees: (input: {
100 readonly cwd: string;
101 }) => Effect.Effect<void, GitCommandError>;
102 readonly createRef: (
103 input: VcsCreateRefInput,
104 ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
105 readonly switchRef: (
106 input: VcsSwitchRefInput,
107 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
108 readonly renameBranch: (input: {
109 readonly cwd: string;
110 readonly oldBranch: string;
111 readonly newBranch: string;
112 }) => Effect.Effect<{ readonly branch: string }, GitManagerServiceError>;
113 }
114>()("t3/git/GitWorkflowService") {}
115​
116function nonRepositoryLocalStatus(): VcsStatusLocalResult {
117 return {
118 isRepo: false,
119 hasPrimaryRemote: false,
120 isDefaultRef: false,
121 refName: null,
122 hasWorkingTreeChanges: false,
123 workingTree: {
124 files: [],
125 insertions: 0,
126 deletions: 0,
127 },
128 };
130​
131function nonRepositoryStatus(): VcsStatusResult {
132 return {
133 ...nonRepositoryLocalStatus(),
134 hasUpstream: false,
135 aheadCount: 0,
136 behindCount: 0,
137 aheadOfDefaultCount: 0,
138 pr: null,
139 };
141​
142function nonRepositoryListRefs(): VcsListRefsResult {
143 return {
144 refs: [],
145 isRepo: false,
146 hasPrimaryRemote: false,
147 nextCursor: null,
148 totalCount: 0,
149 };
151​
152/** @public Service construction is part of the canonical Effect module API. */
153export const make = Effect.gen(function* () {
154 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
155 const git = yield* GitVcsDriver.GitVcsDriver;
156 const gitManager = yield* GitManager.GitManager;
157​
158 const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
159 operation: string,
160 cwd: string,
161 ) {
162 const handle = yield* registry.resolve({ cwd }).pipe(
163 Effect.mapError(
164 (cause) =>
165 new GitManagerError({
166 operation,
167 cwd,
168 detail: "Failed to resolve the VCS driver for this Git workflow.",
169 cause,
170 }),
171 ),
172 );
173 if (handle.kind !== "git") {
174 return yield* new GitManagerError({
175 operation,
176 cwd,
177 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
178 });
179 }
180 });
181​
182 const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
183 operation: string,
184 cwd: string,
185 ) {
186 const handle = yield* registry.resolve({ cwd }).pipe(
187 Effect.mapError(
188 (cause) =>
189 new GitCommandError({
190 operation,
191 command: "vcs-route",
192 cwd,
193 detail: "Failed to resolve the VCS driver for this Git command.",
194 cause,
195 }),
196 ),
197 );
198 if (handle.kind !== "git") {
199 return yield* new GitCommandError({
200 operation,
201 command: "vcs-route",
202 cwd,
203 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
204 });
205 }
206 });
207​
208 const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
209 function* (operation: string, cwd: string) {
210 const handle = yield* registry.detect({ cwd }).pipe(
211 Effect.mapError(
212 (cause) =>
213 new GitManagerError({
214 operation,
215 cwd,
216 detail: "Failed to detect a VCS repository for this Git workflow.",
217 cause,
218 }),
219 ),
220 );
221 if (!handle) {
222 return false;
223 }
224 if (handle.kind !== "git") {
225 return yield* new GitManagerError({
226 operation,
227 cwd,
228 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
229 });
230 }
231 return true;
232 },
233 );
234​
235 const detectGitRepositoryForCommand = Effect.fn(
236 "GitWorkflowService.detectGitRepositoryForCommand",
237 )(function* (operation: string, cwd: string) {
238 const handle = yield* registry.detect({ cwd }).pipe(
239 Effect.mapError(
240 (cause) =>
241 new GitCommandError({
242 operation,
243 command: "vcs-route",
244 cwd,
245 detail: "Failed to detect a VCS repository for this Git command.",
246 cause,
247 }),
248 ),
249 );
250 if (!handle) {
251 return false;
252 }
253 if (handle.kind !== "git") {
254 return yield* new GitCommandError({
255 operation,
256 command: "vcs-route",
257 cwd,
258 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
259 });
260 }
261 return true;
262 });
263​
264 const routeGitManager =
265 <Input extends { readonly cwd: string }, Output>(
266 operation: string,
267 run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
268 ) =>
269 (input: Input) =>
270 ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
271​
272 return GitWorkflowService.of({
273 isRepository: (cwd) =>
274 registry.detect({ cwd }).pipe(
275 Effect.map((handle) => handle?.kind === "git"),
276 Effect.mapError(
277 (cause) =>
278 new GitManagerError({
279 operation: "GitWorkflowService.isRepository",
280 cwd,
281 detail: "Failed to detect a VCS repository for this Git workflow.",
282 cause,
283 }),
284 ),
285 ),
286 hasCommit: (input) =>
287 ensureGitCommand("GitWorkflowService.hasCommit", input.cwd).pipe(
288 Effect.andThen(
289 git.execute({
290 operation: "GitWorkflowService.hasCommit",
291 cwd: input.cwd,
292 args: ["rev-parse", "--verify", `${input.refName}^{commit}`],
293 allowNonZeroExit: true,
294 }),
295 ),
296 Effect.map((result) => result.exitCode === 0),
297 ),
298 status: (input) =>
299 detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
300 Effect.flatMap((isGitRepository) =>
301 isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
302 ),
303 ),
304 localStatus: (input) =>
305 detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
306 Effect.flatMap((isGitRepository) =>
307 isGitRepository
308 ? gitManager.localStatus(input)
309 : Effect.succeed(nonRepositoryLocalStatus()),
310 ),
311 ),
312 remoteStatus: (input, options) =>
313 detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
314 Effect.flatMap((isGitRepository) =>
315 isGitRepository ? gitManager.remoteStatus(input, options) : Effect.succeed(null),
316 ),
317 ),
318 invalidateLocalStatus: gitManager.invalidateLocalStatus,
319 invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
320 invalidateStatus: gitManager.invalidateStatus,
321 pullCurrentBranch: (cwd) =>
322 ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
323 Effect.andThen(git.pullCurrentBranch(cwd)),
324 ),
325 runStackedAction: (input, options) =>
326 ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
327 Effect.andThen(gitManager.runStackedAction(input, options)),
328 ),
329 resolvePullRequest: routeGitManager(
330 "GitWorkflowService.resolvePullRequest",
331 gitManager.resolvePullRequest,
332 ),
333 preparePullRequestThread: routeGitManager(
334 "GitWorkflowService.preparePullRequestThread",
335 gitManager.preparePullRequestThread,
336 ),
337 listRefs: (input) =>
338 detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
339 Effect.flatMap((isGitRepository) =>
340 isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
341 ),
342 ),
343 createWorktree: (input, options) =>
344 ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
345 Effect.andThen(git.createWorktree(input, options)),
346 ),
347 fetchRemote: (input) =>
348 ensureGitCommand("GitWorkflowService.fetchRemote", input.cwd).pipe(
349 Effect.andThen(git.fetchRemote(input)),
350 ),
351 remoteExists: (input) =>
352 ensureGitCommand("GitWorkflowService.remoteExists", input.cwd).pipe(
353 Effect.andThen(git.remoteExists(input)),
354 ),
355 remoteBranchExists: (input) =>
356 ensureGitCommand("GitWorkflowService.remoteBranchExists", input.cwd).pipe(
357 Effect.andThen(git.remoteBranchExists(input)),
358 ),
359 resolveRemoteTrackingCommit: (input) =>
360 ensureGitCommand("GitWorkflowService.resolveRemoteTrackingCommit", input.cwd).pipe(
361 Effect.andThen(git.resolveRemoteTrackingCommit(input)),
362 ),
363 removeWorktree: (input) =>
364 ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
365 Effect.andThen(git.removeWorktree(input)),
366 ),
367 pruneWorktrees: (input) =>
368 ensureGitCommand("GitWorkflowService.pruneWorktrees", input.cwd).pipe(
369 Effect.andThen(git.pruneWorktrees(input)),
370 ),
371 createRef: (input) =>
372 ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
373 Effect.andThen(git.createRef(input)),
374 ),
375 switchRef: (input) =>
376 ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
377 Effect.andThen(Effect.scoped(git.switchRef(input))),
378 ),
379 renameBranch: (input) =>
380 ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
381 Effect.andThen(git.renameBranch(input)),
382 ),
383 });
384});
385​
386export const layer = Layer.effect(GitWorkflowService, make);