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