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

VcsDriver.ts

118 lines4.4 KB

1import * as Context from "effect/Context";
2import type * as Effect from "effect/Effect";
3​
4import type {
5 VcsError,
6 VcsInitInput,
7 VcsListRemotesResult,
8 VcsListWorkspaceFilesResult,
9 VcsRepositoryIdentity,
10} from "@t3tools/contracts";
11import { CheckpointRef } from "@t3tools/contracts";
12import * as VcsProcess from "./VcsProcess.ts";
13​
14export interface VcsCaptureCheckpointInput {
15 readonly cwd: string;
16 readonly checkpointRef: CheckpointRef;
17 /** The checkpoint this one follows, so a turn that switched branches starts where it landed. */
18 readonly previousCheckpointRef?: CheckpointRef;
19 /** Folders below `cwd` (relative, `/`-separated) that other checkpoints own, such as nested clones. */
20 readonly excludePaths?: ReadonlyArray<string>;
22​
23export interface VcsCaptureHeadCheckpointInput {
24 readonly cwd: string;
25 readonly checkpointRef: CheckpointRef;
26 /** A commit `resolveCleanHead` returned for this folder. */
27 readonly head: string;
28 /**
29 * When the folder was seen clean, in epoch seconds, if that was earlier than now. Commits made
30 * after a checkpoint count as the turn's own work, so a late snapshot must not postdate them.
31 */
32 readonly seenAt?: number;
34​
35export interface VcsRestoreCheckpointInput {
36 readonly cwd: string;
37 readonly checkpointRef: CheckpointRef;
38 readonly fallbackToHead?: boolean;
39 /** Folders below `cwd` the restore leaves as they are, as in a capture. */
40 readonly excludePaths?: ReadonlyArray<string>;
42​
43export const CHECKPOINT_DIFF_MAX_OUTPUT_BYTES = 10_000_000;
44​
45export interface VcsDiffCheckpointsInput {
46 readonly cwd: string;
47 readonly fromCheckpointRef: CheckpointRef;
48 readonly toCheckpointRef: CheckpointRef;
49 readonly fallbackFromToHead?: boolean;
50 readonly ignoreWhitespace: boolean;
51 readonly format?: "patch" | "numstat";
52 /** Maximum bytes collected from the diff command. */
53 readonly maxOutputBytes?: number;
54 /** An empty list yields an empty diff. */
55 readonly filePaths?: ReadonlyArray<string>;
56 /** Folders below `cwd` left out of the diff, as in a capture. */
57 readonly excludePaths?: ReadonlyArray<string>;
59​
60export interface VcsListAuthoredPathsInput {
61 readonly cwd: string;
62 readonly fromCheckpointRef: CheckpointRef;
63 readonly toCheckpointRef: CheckpointRef;
65​
66export interface VcsDeleteCheckpointRefsInput {
67 readonly cwd: string;
68 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
70​
71export interface VcsCheckpointOps {
72 readonly captureCheckpoint: (input: VcsCaptureCheckpointInput) => Effect.Effect<void, VcsError>;
73 /**
74 * HEAD's commit when the folder matches it exactly: nothing staged, modified or untracked.
75 * Null otherwise. One status, where a capture stages the whole worktree.
76 */
77 readonly resolveCleanHead: (cwd: string) => Effect.Effect<string | null, VcsError>;
78 /** Snapshots a folder that was clean at `head` from that commit, without reading its files. */
79 readonly captureHeadCheckpoint: (
80 input: VcsCaptureHeadCheckpointInput,
81 ) => Effect.Effect<void, VcsError>;
82 readonly hasCheckpointRef: (
83 input: Omit<VcsRestoreCheckpointInput, "fallbackToHead">,
84 ) => Effect.Effect<boolean, VcsError>;
85 readonly restoreCheckpoint: (
86 input: VcsRestoreCheckpointInput,
87 ) => Effect.Effect<boolean, VcsError>;
88 readonly diffCheckpoints: (input: VcsDiffCheckpointsInput) => Effect.Effect<string, VcsError>;
89 readonly listAuthoredPaths: (
90 input: VcsListAuthoredPathsInput,
91 ) => Effect.Effect<ReadonlySet<string> | null, VcsError>;
92 readonly deleteCheckpointRefs: (
93 input: VcsDeleteCheckpointRefsInput,
94 ) => Effect.Effect<void, VcsError>;
96​
97export class VcsDriver extends Context.Service<
98 VcsDriver,
99 {
100 readonly execute: (
101 input: Omit<VcsProcess.VcsProcessInput, "command">,
102 ) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>;
103 readonly checkpoints: VcsCheckpointOps;
104 readonly detectRepository: (
105 cwd: string,
106 ) => Effect.Effect<VcsRepositoryIdentity | null, VcsError>;
107 readonly isInsideWorkTree: (cwd: string) => Effect.Effect<boolean, VcsError>;
108 readonly listWorkspaceFiles: (
109 cwd: string,
110 ) => Effect.Effect<VcsListWorkspaceFilesResult, VcsError>;
111 readonly listRemotes: (cwd: string) => Effect.Effect<VcsListRemotesResult, VcsError>;
112 readonly filterIgnoredPaths: (
113 cwd: string,
114 relativePaths: ReadonlyArray<string>,
115 ) => Effect.Effect<ReadonlyArray<string>, VcsError>;
116 readonly initRepository: (input: VcsInitInput) => Effect.Effect<void, VcsError>;
117 }
118>()("t3/vcs/VcsDriver") {}