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

GitWorkflowService.ts

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