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