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