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