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

GitWorkflowService.ts

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