Open History to see the last commit
1import { Context, Effect, Layer } from "effect";
2
3import {
4 GitManagerError,
5 GitCommandError,
6 type VcsSwitchRefInput,
7 type VcsSwitchRefResult,
8 type VcsCreateRefInput,
9 type VcsCreateRefResult,
10 type VcsCreateWorktreeInput,
11 type VcsCreateWorktreeResult,
12 type VcsListRefsInput,
13 type VcsListRefsResult,
14 type GitManagerServiceError,
15 type GitPreparePullRequestThreadInput,
16 type GitPreparePullRequestThreadResult,
17 type GitPullRequestRefInput,
18 type VcsPullResult,
19 type VcsRemoveWorktreeInput,
20 type GitResolvePullRequestResult,
21 type GitRunStackedActionInput,
22 type GitRunStackedActionResult,
23 type VcsStatusInput,
24 type VcsStatusLocalResult,
25 type VcsStatusRemoteResult,
26 type VcsStatusResult,
27} from "@t3tools/contracts";
28
29import { GitManager, type GitRunStackedActionOptions } from "./GitManager.ts";
30import { GitVcsDriver } from "../vcs/GitVcsDriver.ts";
31import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
32
33export interface GitWorkflowServiceShape {
34 readonly status: (
35 input: VcsStatusInput,
36 ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
37 readonly localStatus: (
38 input: VcsStatusInput,
39 ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
40 readonly remoteStatus: (
41 input: VcsStatusInput,
42 ) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
43 readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
44 readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
45 readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
46 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
47 readonly runStackedAction: (
48 input: GitRunStackedActionInput,
49 options?: GitRunStackedActionOptions,
50 ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
51 readonly resolvePullRequest: (
52 input: GitPullRequestRefInput,
53 ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
54 readonly preparePullRequestThread: (
55 input: GitPreparePullRequestThreadInput,
56 ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
57 readonly listRefs: (input: VcsListRefsInput) => Effect.Effect<VcsListRefsResult, GitCommandError>;
58 readonly createWorktree: (
59 input: VcsCreateWorktreeInput,
60 ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
61 readonly removeWorktree: (input: VcsRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
62 readonly createRef: (
63 input: VcsCreateRefInput,
64 ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
65 readonly switchRef: (
66 input: VcsSwitchRefInput,
67 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
68 readonly renameBranch: (input: {
69 readonly cwd: string;
70 readonly oldBranch: string;
71 readonly newBranch: string;
72 }) => Effect.Effect<{ readonly branch: string }, GitManagerServiceError>;
73}
74
75export class GitWorkflowService extends Context.Service<
76 GitWorkflowService,
77 GitWorkflowServiceShape
78>()("t3/git/GitWorkflowService") {}
79
80const unsupportedGitWorkflow = (operation: string, cwd: string, detail: string) =>
81 new GitManagerError({
82 operation,
83 detail: `${detail} (${cwd})`,
84 });
85
86const unsupportedGitCommand = (operation: string, cwd: string, detail: string) =>
87 new GitCommandError({
88 operation,
89 command: "vcs-route",
90 cwd,
91 detail,
92 });
93
94function nonRepositoryLocalStatus(): VcsStatusLocalResult {
95 return {
96 isRepo: false,
97 hasPrimaryRemote: false,
98 isDefaultRef: false,
99 refName: null,
100 hasWorkingTreeChanges: false,
101 workingTree: {
102 files: [],
103 insertions: 0,
104 deletions: 0,
105 },
106 };
107}
108
109function nonRepositoryStatus(): VcsStatusResult {
110 return {
111 ...nonRepositoryLocalStatus(),
112 hasUpstream: false,
113 aheadCount: 0,
114 behindCount: 0,
115 aheadOfDefaultCount: 0,
116 pr: null,
117 };
118}
119
120function nonRepositoryListRefs(): VcsListRefsResult {
121 return {
122 refs: [],
123 isRepo: false,
124 hasPrimaryRemote: false,
125 nextCursor: null,
126 totalCount: 0,
127 };
128}
129
130export const make = Effect.fn("makeGitWorkflowService")(function* () {
131 const registry = yield* VcsDriverRegistry;
132 const git = yield* GitVcsDriver;
133 const gitManager = yield* GitManager;
134
135 const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
136 operation: string,
137 cwd: string,
138 ) {
139 const handle = yield* registry
140 .resolve({ cwd })
141 .pipe(
142 Effect.mapError((error) =>
143 unsupportedGitWorkflow(
144 operation,
145 cwd,
146 error instanceof Error ? error.message : String(error),
147 ),
148 ),
149 );
150 if (handle.kind !== "git") {
151 return yield* unsupportedGitWorkflow(
152 operation,
153 cwd,
154 `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
155 );
156 }
157 });
158
159 const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
160 operation: string,
161 cwd: string,
162 ) {
163 const handle = yield* registry
164 .resolve({ cwd })
165 .pipe(
166 Effect.mapError((error) =>
167 unsupportedGitCommand(
168 operation,
169 cwd,
170 error instanceof Error ? error.message : String(error),
171 ),
172 ),
173 );
174 if (handle.kind !== "git") {
175 return yield* unsupportedGitCommand(
176 operation,
177 cwd,
178 `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
179 );
180 }
181 });
182
183 const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
184 function* (operation: string, cwd: string) {
185 const handle = yield* registry
186 .detect({ cwd })
187 .pipe(
188 Effect.mapError((error) =>
189 unsupportedGitWorkflow(
190 operation,
191 cwd,
192 error instanceof Error ? error.message : String(error),
193 ),
194 ),
195 );
196 if (!handle) {
197 return false;
198 }
199 if (handle.kind !== "git") {
200 return yield* unsupportedGitWorkflow(
201 operation,
202 cwd,
203 `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
204 );
205 }
206 return true;
207 },
208 );
209
210 const detectGitRepositoryForCommand = Effect.fn(
211 "GitWorkflowService.detectGitRepositoryForCommand",
212 )(function* (operation: string, cwd: string) {
213 const handle = yield* registry
214 .detect({ cwd })
215 .pipe(
216 Effect.mapError((error) =>
217 unsupportedGitCommand(
218 operation,
219 cwd,
220 error instanceof Error ? error.message : String(error),
221 ),
222 ),
223 );
224 if (!handle) {
225 return false;
226 }
227 if (handle.kind !== "git") {
228 return yield* unsupportedGitCommand(
229 operation,
230 cwd,
231 `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
232 );
233 }
234 return true;
235 });
236
237 const routeGitManager =
238 <Input extends { readonly cwd: string }, Output>(
239 operation: string,
240 run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
241 ) =>
242 (input: Input) =>
243 ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
244
245 return GitWorkflowService.of({
246 status: (input) =>
247 detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
248 Effect.flatMap((isGitRepository) =>
249 isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
250 ),
251 ),
252 localStatus: (input) =>
253 detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
254 Effect.flatMap((isGitRepository) =>
255 isGitRepository
256 ? gitManager.localStatus(input)
257 : Effect.succeed(nonRepositoryLocalStatus()),
258 ),
259 ),
260 remoteStatus: (input) =>
261 detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
262 Effect.flatMap((isGitRepository) =>
263 isGitRepository ? gitManager.remoteStatus(input) : Effect.succeed(null),
264 ),
265 ),
266 invalidateLocalStatus: gitManager.invalidateLocalStatus,
267 invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
268 invalidateStatus: gitManager.invalidateStatus,
269 pullCurrentBranch: (cwd) =>
270 ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
271 Effect.andThen(git.pullCurrentBranch(cwd)),
272 ),
273 runStackedAction: (input, options) =>
274 ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
275 Effect.andThen(gitManager.runStackedAction(input, options)),
276 ),
277 resolvePullRequest: routeGitManager(
278 "GitWorkflowService.resolvePullRequest",
279 gitManager.resolvePullRequest,
280 ),
281 preparePullRequestThread: routeGitManager(
282 "GitWorkflowService.preparePullRequestThread",
283 gitManager.preparePullRequestThread,
284 ),
285 listRefs: (input) =>
286 detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
287 Effect.flatMap((isGitRepository) =>
288 isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
289 ),
290 ),
291 createWorktree: (input) =>
292 ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
293 Effect.andThen(git.createWorktree(input)),
294 ),
295 removeWorktree: (input) =>
296 ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
297 Effect.andThen(git.removeWorktree(input)),
298 ),
299 createRef: (input) =>
300 ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
301 Effect.andThen(git.createRef(input)),
302 ),
303 switchRef: (input) =>
304 ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
305 Effect.andThen(Effect.scoped(git.switchRef(input))),
306 ),
307 renameBranch: (input) =>
308 ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
309 Effect.andThen(git.renameBranch(input)),
310 ),
311 });
312});
313
314export const layer = Layer.effect(GitWorkflowService, make());