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

GitWorkflowService.ts

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