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