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