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

GitWorkflowService.ts

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