Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlHost.ts

136 lines5.0 KB

1import type {
2 GitCommandError,
3 ServerSettings,
4 ServerSettingsError,
5 ServerSettingsPatch,
6 VcsError,
7 VcsListRemotesResult,
8 VcsProcessExitFailureKind,
9 VcsSwitchRefInput,
10 VcsSwitchRefResult,
11} from "@t3tools/contracts";
12import * as Context from "effect/Context";
13import type * as Effect from "effect/Effect";
14import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15​
16export interface SourceControlProcessInput {
17 readonly operation: string;
18 readonly command: string;
19 readonly args: ReadonlyArray<string>;
20 readonly cwd: string;
21 readonly spawnCwd?: string;
22 readonly stdin?: string;
23 readonly onStdoutChunk?: (chunk: Uint8Array) => void;
24 readonly env?: NodeJS.ProcessEnv;
25 readonly allowNonZeroExit?: boolean;
26 readonly timeoutMs?: number;
27 readonly maxOutputBytes?: number;
28 readonly outputMode?: "error" | "truncate" | undefined;
29 readonly appendTruncationMarker?: boolean;
30 /**
31 * Reads a failure the generic rules cannot, such as a host CLI's sign-in hint, from its stderr.
32 * Null leaves it to them.
33 */
34 readonly classifyFailure?: (stderr: string) => VcsProcessExitFailureKind | null;
35 /** The error's detail when the command reports something it was asked for as missing. */
36 readonly notFoundDetail?: string;
37 /**
38 * Runs sharing a key run at most `limit` at a time, for a host that throttles bursts of its CLI.
39 * The first run of a key fixes its limit.
40 */
41 readonly concurrency?: { readonly key: string; readonly limit: number };
43​
44export interface SourceControlProcessOutput {
45 readonly exitCode: ChildProcessSpawner.ExitCode;
46 readonly stdout: string;
47 readonly stderr: string;
48 readonly stdoutTruncated: boolean;
49 readonly stderrTruncated: boolean;
50 readonly stdoutInvalidUtf8?: boolean;
51 readonly stderrInvalidUtf8?: boolean;
53​
54export type SourceControlGitOutput = Omit<
55 SourceControlProcessOutput,
56 "stdoutInvalidUtf8" | "stderrInvalidUtf8"
57>;
58​
59export interface SourceControlGitRemoteBranch {
60 readonly cwd: string;
61 readonly remoteName: string;
62 readonly remoteBranch: string;
64​
65export class SourceControlHost extends Context.Service<
66 SourceControlHost,
67 {
68 readonly settings: {
69 /** Read fresh on each call to pick up credential changes. */
70 readonly get: Effect.Effect<ServerSettings, ServerSettingsError>;
71 /** For credentials the host obtains itself, such as a sign-in it refreshes. */
72 readonly update: (
73 patch: ServerSettingsPatch,
74 ) => Effect.Effect<ServerSettings, ServerSettingsError>;
75 };
76 readonly process: {
77 readonly run: (
78 input: SourceControlProcessInput,
79 ) => Effect.Effect<SourceControlProcessOutput, VcsError>;
80 };
81 readonly git: {
82 readonly execute: (input: {
83 readonly operation: string;
84 readonly cwd: string;
85 readonly args: ReadonlyArray<string>;
86 readonly maxOutputBytes?: number;
87 readonly appendTruncationMarker?: boolean;
88 }) => Effect.Effect<SourceControlGitOutput, GitCommandError>;
89 readonly resolveCommit: (input: {
90 readonly cwd: string;
91 readonly revision: string;
92 }) => Effect.Effect<{ readonly commitSha: string }, GitCommandError>;
93 /** The outer effect resolves the repository; the inner effect lists its remotes. */
94 readonly remotes: (
95 cwd: string,
96 ) => Effect.Effect<Effect.Effect<VcsListRemotesResult, VcsError>, VcsError>;
97 readonly readConfigValue: (
98 cwd: string,
99 key: string,
100 ) => Effect.Effect<string | null, GitCommandError>;
101 readonly resolvePrimaryRemoteName: (cwd: string) => Effect.Effect<string, GitCommandError>;
102 readonly ensureRemote: (input: {
103 readonly cwd: string;
104 readonly preferredName: string;
105 readonly url: string;
106 }) => Effect.Effect<string, GitCommandError>;
107 readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
108 readonly fetchRemoteBranch: (
109 input: SourceControlGitRemoteBranch & { readonly localBranch: string },
110 ) => Effect.Effect<void, GitCommandError>;
111 readonly fetchRemoteTrackingBranch: (
112 input: SourceControlGitRemoteBranch,
113 ) => Effect.Effect<void, GitCommandError>;
114 readonly setBranchUpstream: (
115 input: SourceControlGitRemoteBranch & { readonly branch: string },
116 ) => Effect.Effect<void, GitCommandError>;
117 /**
118 * Writes to .git/config, which every worktree shares, go through these rather than
119 * `execute`: the host serializes them per repository.
120 */
121 readonly createBranch: (input: {
122 readonly cwd: string;
123 readonly branch: string;
124 readonly startPoint: string;
125 }) => Effect.Effect<void, GitCommandError>;
126 readonly writeConfigValue: (input: {
127 readonly cwd: string;
128 readonly key: string;
129 readonly value: string;
130 }) => Effect.Effect<void, GitCommandError>;
131 readonly switchRef: (
132 input: VcsSwitchRefInput,
133 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
134 };
135 }
136>()("@t3tools/source-control-core/server/SourceControlHost") {}