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