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