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
149export const make = Effect.gen(function* () {
150 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
151 const git = yield* GitVcsDriver.GitVcsDriver;
152 const gitManager = yield* GitManager.GitManager;
153
154 const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
155 operation: string,
156 cwd: string,
157 ) {
158 const handle = yield* registry.resolve({ cwd }).pipe(
159 Effect.mapError(
160 (cause) =>
161 new GitManagerError({
162 operation,
163 cwd,
164 detail: "Failed to resolve the VCS driver for this Git workflow.",
165 cause,
166 }),
167 ),
168 );
169 if (handle.kind !== "git") {
170 return yield* new GitManagerError({
171 operation,
172 cwd,
173 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
174 });
175 }
176 });
177
178 const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
179 operation: string,
180 cwd: string,
181 ) {
182 const handle = yield* registry.resolve({ cwd }).pipe(
183 Effect.mapError(
184 (cause) =>
185 new GitCommandError({
186 operation,
187 command: "vcs-route",
188 cwd,
189 detail: "Failed to resolve the VCS driver for this Git command.",
190 cause,
191 }),
192 ),
193 );
194 if (handle.kind !== "git") {
195 return yield* new GitCommandError({
196 operation,
197 command: "vcs-route",
198 cwd,
199 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
200 });
201 }
202 });
203
204 const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
205 function* (operation: string, cwd: string) {
206 const handle = yield* registry.detect({ cwd }).pipe(
207 Effect.mapError(
208 (cause) =>
209 new GitManagerError({
210 operation,
211 cwd,
212 detail: "Failed to detect a VCS repository for this Git workflow.",
213 cause,
214 }),
215 ),
216 );
217 if (!handle) {
218 return false;
219 }
220 if (handle.kind !== "git") {
221 return yield* new GitManagerError({
222 operation,
223 cwd,
224 detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
225 });
226 }
227 return true;
228 },
229 );
230
231 const detectGitRepositoryForCommand = Effect.fn(
232 "GitWorkflowService.detectGitRepositoryForCommand",
233 )(function* (operation: string, cwd: string) {
234 const handle = yield* registry.detect({ cwd }).pipe(
235 Effect.mapError(
236 (cause) =>
237 new GitCommandError({
238 operation,
239 command: "vcs-route",
240 cwd,
241 detail: "Failed to detect a VCS repository for this Git command.",
242 cause,
243 }),
244 ),
245 );
246 if (!handle) {
247 return false;
248 }
249 if (handle.kind !== "git") {
250 return yield* new GitCommandError({
251 operation,
252 command: "vcs-route",
253 cwd,
254 detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
255 });
256 }
257 return true;
258 });
259
260 const routeGitManager =
261 <Input extends { readonly cwd: string }, Output>(
262 operation: string,
263 run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
264 ) =>
265 (input: Input) =>
266 ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
267
268 return GitWorkflowService.of({
269 status: (input) =>
270 detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
271 Effect.flatMap((isGitRepository) =>
272 isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
273 ),
274 ),
275 localStatus: (input) =>
276 detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
277 Effect.flatMap((isGitRepository) =>
278 isGitRepository
279 ? gitManager.localStatus(input)
280 : Effect.succeed(nonRepositoryLocalStatus()),
281 ),
282 ),
283 remoteStatus: (input, options) =>
284 detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
285 Effect.flatMap((isGitRepository) =>
286 isGitRepository ? gitManager.remoteStatus(input, options) : Effect.succeed(null),
287 ),
288 ),
289 invalidateLocalStatus: gitManager.invalidateLocalStatus,
290 invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
291 invalidateStatus: gitManager.invalidateStatus,
292 pullCurrentBranch: (cwd) =>
293 ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
294 Effect.andThen(git.pullCurrentBranch(cwd)),
295 ),
296 runStackedAction: (input, options) =>
297 ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
298 Effect.andThen(gitManager.runStackedAction(input, options)),
299 ),
300 resolvePullRequest: routeGitManager(
301 "GitWorkflowService.resolvePullRequest",
302 gitManager.resolvePullRequest,
303 ),
304 preparePullRequestThread: routeGitManager(
305 "GitWorkflowService.preparePullRequestThread",
306 gitManager.preparePullRequestThread,
307 ),
308 listRefs: (input) =>
309 detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
310 Effect.flatMap((isGitRepository) =>
311 isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
312 ),
313 ),
314 createWorktree: (input) =>
315 ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
316 Effect.andThen(git.createWorktree(input)),
317 ),
318 listLocalBranchNames: (cwd) =>
319 ensureGitCommand("GitWorkflowService.listLocalBranchNames", cwd).pipe(
320 Effect.andThen(git.listLocalBranchNames(cwd)),
321 ),
322 fetchRemote: (input) =>
323 ensureGitCommand("GitWorkflowService.fetchRemote", input.cwd).pipe(
324 Effect.andThen(git.fetchRemote(input)),
325 ),
326 remoteExists: (input) =>
327 ensureGitCommand("GitWorkflowService.remoteExists", input.cwd).pipe(
328 Effect.andThen(git.remoteExists(input)),
329 ),
330 remoteBranchExists: (input) =>
331 ensureGitCommand("GitWorkflowService.remoteBranchExists", input.cwd).pipe(
332 Effect.andThen(git.remoteBranchExists(input)),
333 ),
334 resolveRemoteTrackingCommit: (input) =>
335 ensureGitCommand("GitWorkflowService.resolveRemoteTrackingCommit", input.cwd).pipe(
336 Effect.andThen(git.resolveRemoteTrackingCommit(input)),
337 ),
338 removeWorktree: (input) =>
339 ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
340 Effect.andThen(git.removeWorktree(input)),
341 ),
342 pruneWorktrees: (input) =>
343 ensureGitCommand("GitWorkflowService.pruneWorktrees", input.cwd).pipe(
344 Effect.andThen(git.pruneWorktrees(input)),
345 ),
346 deleteLocalBranch: (input) =>
347 ensureGitCommand("GitWorkflowService.deleteLocalBranch", input.cwd).pipe(
348 Effect.andThen(git.deleteLocalBranch(input)),
349 ),
350 createRef: (input) =>
351 ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
352 Effect.andThen(git.createRef(input)),
353 ),
354 switchRef: (input) =>
355 ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
356 Effect.andThen(Effect.scoped(git.switchRef(input))),
357 ),
358 renameBranch: (input) =>
359 ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
360 Effect.andThen(git.renameBranch(input)),
361 ),
362 });
363});
364
365export const layer = Layer.effect(GitWorkflowService, make);