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