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

SourceControlHost.ts

143 lines5.5 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 /**
72 * Like `get`, with each secret provider reference in one host kind's credentials replaced
73 * by the secret it names. An unreadable reference stays in place; read the field with
74 * `requireSettingsSecretLiteral` so it fails rather than passing for an unset credential.
75 * Only a reader of that kind should call it: resolving can wait on a secret provider.
76 */
77 readonly getWithSecrets: (kind: string) => Effect.Effect<ServerSettings, ServerSettingsError>;
78 /** For credentials the host obtains itself, such as a sign-in it refreshes. */
79 readonly update: (
80 patch: ServerSettingsPatch,
81 ) => Effect.Effect<ServerSettings, ServerSettingsError>;
82 };
83 readonly process: {
84 readonly run: (
85 input: SourceControlProcessInput,
86 ) => Effect.Effect<SourceControlProcessOutput, VcsError>;
87 };
88 readonly git: {
89 readonly execute: (input: {
90 readonly operation: string;
91 readonly cwd: string;
92 readonly args: ReadonlyArray<string>;
93 readonly maxOutputBytes?: number;
94 readonly appendTruncationMarker?: boolean;
95 }) => Effect.Effect<SourceControlGitOutput, GitCommandError>;
96 readonly resolveCommit: (input: {
97 readonly cwd: string;
98 readonly revision: string;
99 }) => Effect.Effect<{ readonly commitSha: string }, GitCommandError>;
100 /** The outer effect resolves the repository; the inner effect lists its remotes. */
101 readonly remotes: (
102 cwd: string,
103 ) => Effect.Effect<Effect.Effect<VcsListRemotesResult, VcsError>, VcsError>;
104 readonly readConfigValue: (
105 cwd: string,
106 key: string,
107 ) => Effect.Effect<string | null, GitCommandError>;
108 readonly resolvePrimaryRemoteName: (cwd: string) => Effect.Effect<string, GitCommandError>;
109 readonly ensureRemote: (input: {
110 readonly cwd: string;
111 readonly preferredName: string;
112 readonly url: string;
113 }) => Effect.Effect<string, GitCommandError>;
114 readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
115 readonly fetchRemoteBranch: (
116 input: SourceControlGitRemoteBranch & { readonly localBranch: string },
117 ) => Effect.Effect<void, GitCommandError>;
118 readonly fetchRemoteTrackingBranch: (
119 input: SourceControlGitRemoteBranch,
120 ) => Effect.Effect<void, GitCommandError>;
121 readonly setBranchUpstream: (
122 input: SourceControlGitRemoteBranch & { readonly branch: string },
123 ) => Effect.Effect<void, GitCommandError>;
124 /**
125 * Writes to .git/config, which every worktree shares, go through these rather than
126 * `execute`: the host serializes them per repository.
127 */
128 readonly createBranch: (input: {
129 readonly cwd: string;
130 readonly branch: string;
131 readonly startPoint: string;
132 }) => Effect.Effect<void, GitCommandError>;
133 readonly writeConfigValue: (input: {
134 readonly cwd: string;
135 readonly key: string;
136 readonly value: string;
137 }) => Effect.Effect<void, GitCommandError>;
138 readonly switchRef: (
139 input: VcsSwitchRefInput,
140 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
141 };
142 }
143>()("@t3tools/source-control-core/server/SourceControlHost") {}