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