Open History to see the last commit
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>;
21}
22
23/**
24 * A turn's range as its capture found it, so summarizing it need not ask Git again. Commits, not
25 * refs: a later capture cannot move them.
26 */
27export interface VcsCheckpointRange {
28 /** Where the turn started: the previous checkpoint, or its turn start after a branch switch. */
29 readonly from: string;
30 /** The checkpoint just captured. */
31 readonly to: string;
32 /** `from`'s commit time, in epoch seconds. */
33 readonly fromCapturedAt: string;
34 /** The HEAD `from` and `to` recorded as first parent; empty for none. */
35 readonly startHead: string;
36 readonly endHead: string;
37 /** `to` holds the same files as `from`, so the turn changed nothing. */
38 readonly unchanged: boolean;
39}
40
41export interface VcsCaptureHeadCheckpointInput {
42 readonly cwd: string;
43 readonly checkpointRef: CheckpointRef;
44 /** A commit `resolveCleanHead` returned for this folder. */
45 readonly head: string;
46 /**
47 * When the folder was seen clean, in epoch seconds, if that was earlier than now. Commits made
48 * after a checkpoint count as the turn's own work, so a late snapshot must not postdate them.
49 */
50 readonly seenAt?: number;
51}
52
53export interface VcsRestoreCheckpointInput {
54 readonly cwd: string;
55 readonly checkpointRef: CheckpointRef;
56 readonly fallbackToHead?: boolean;
57 /** Folders below `cwd` the restore leaves as they are, as in a capture. */
58 readonly excludePaths?: ReadonlyArray<string>;
59}
60
61export const CHECKPOINT_DIFF_MAX_OUTPUT_BYTES = 10_000_000;
62
63export interface VcsDiffCheckpointsInput {
64 readonly cwd: string;
65 readonly fromCheckpointRef: CheckpointRef;
66 readonly toCheckpointRef: CheckpointRef;
67 readonly fallbackFromToHead?: boolean;
68 readonly ignoreWhitespace: boolean;
69 readonly format?: "patch" | "numstat";
70 /** Maximum bytes collected from the diff command. */
71 readonly maxOutputBytes?: number;
72 /** An empty list yields an empty diff. */
73 readonly filePaths?: ReadonlyArray<string>;
74 /** Folders below `cwd` left out of the diff, as in a capture. */
75 readonly excludePaths?: ReadonlyArray<string>;
76 /** The refs' range as their capture returned it; the refs are then not read. */
77 readonly range?: VcsCheckpointRange;
78}
79
80export interface VcsListAuthoredPathsInput {
81 readonly cwd: string;
82 readonly fromCheckpointRef: CheckpointRef;
83 readonly toCheckpointRef: CheckpointRef;
84 /** The refs' range as their capture returned it; the refs are then not read. */
85 readonly range?: VcsCheckpointRange;
86}
87
88export interface VcsDeleteCheckpointRefsInput {
89 readonly cwd: string;
90 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
91}
92
93export interface VcsCheckpointOps {
94 /** The range from `previousCheckpointRef`, or null when there is none to compare with. */
95 readonly captureCheckpoint: (
96 input: VcsCaptureCheckpointInput,
97 ) => Effect.Effect<VcsCheckpointRange | null, VcsError>;
98 /**
99 * HEAD's commit when the folder matches it exactly: nothing staged, modified or untracked.
100 * Null otherwise. One status, where a capture stages the whole worktree.
101 */
102 readonly resolveCleanHead: (cwd: string) => Effect.Effect<string | null, VcsError>;
103 /** Snapshots a folder that was clean at `head` from that commit, without reading its files. */
104 readonly captureHeadCheckpoint: (
105 input: VcsCaptureHeadCheckpointInput,
106 ) => Effect.Effect<void, VcsError>;
107 readonly hasCheckpointRef: (
108 input: Omit<VcsRestoreCheckpointInput, "fallbackToHead">,
109 ) => Effect.Effect<boolean, VcsError>;
110 readonly restoreCheckpoint: (
111 input: VcsRestoreCheckpointInput,
112 ) => Effect.Effect<boolean, VcsError>;
113 readonly diffCheckpoints: (input: VcsDiffCheckpointsInput) => Effect.Effect<string, VcsError>;
114 readonly listAuthoredPaths: (
115 input: VcsListAuthoredPathsInput,
116 ) => Effect.Effect<ReadonlySet<string> | null, VcsError>;
117 readonly deleteCheckpointRefs: (
118 input: VcsDeleteCheckpointRefsInput,
119 ) => Effect.Effect<void, VcsError>;
120}
121
122export class VcsDriver extends Context.Service<
123 VcsDriver,
124 {
125 readonly execute: (
126 input: Omit<VcsProcess.VcsProcessInput, "command">,
127 ) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>;
128 readonly checkpoints: VcsCheckpointOps;
129 readonly detectRepository: (
130 cwd: string,
131 ) => Effect.Effect<VcsRepositoryIdentity | null, VcsError>;
132 readonly isInsideWorkTree: (cwd: string) => Effect.Effect<boolean, VcsError>;
133 readonly listWorkspaceFiles: (
134 cwd: string,
135 ) => Effect.Effect<VcsListWorkspaceFilesResult, VcsError>;
136 readonly listRemotes: (cwd: string) => Effect.Effect<VcsListRemotesResult, VcsError>;
137 readonly filterIgnoredPaths: (
138 cwd: string,
139 relativePaths: ReadonlyArray<string>,
140 ) => Effect.Effect<ReadonlyArray<string>, VcsError>;
141 readonly initRepository: (input: VcsInitInput) => Effect.Effect<void, VcsError>;
142 }
143>()("t3/vcs/VcsDriver") {}