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