@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 2,375 lines 89 KB
Raw
1 import * as NodeBuffer from "node:buffer";
2
3 import * as Clock from "effect/Clock";
4 import * as Context from "effect/Context";
5 import * as SecureRandom from "effect/crypto/SecureRandom";
6 import * as DateTime from "effect/DateTime";
7 import * as Effect from "effect/Effect";
8 import * as Exit from "effect/Exit";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12 import * as Path from "effect/Path";
13 import { ChildProcessSpawner } from "effect/process";
14
15 import {
16 GitCommandError,
17 VcsCheckpointStorageError,
18 VcsProcessExitError,
19 VcsUnsupportedOperationError,
20 type VcsSwitchRefInput,
21 type VcsSwitchRefResult,
22 type VcsCreateRefInput,
23 type VcsCreateRefResult,
24 type VcsCreateWorktreeInput,
25 type VcsCreateWorktreeResult,
26 type ReviewBranchCommitsInput,
27 type ReviewBranchCommitsResult,
28 type ReviewDiffPreviewInput,
29 type ReviewDiffPreviewResult,
30 type ReviewDiffFileContentsInput,
31 type ReviewDiffFileContentsResult,
32 type VcsInitInput,
33 type VcsListRefsInput,
34 type VcsListRefsResult,
35 type VcsPullResult,
36 type VcsRemoveWorktreeInput,
37 type VcsStatusInput,
38 type VcsStatusResult,
39 type WorktreeSubmodules,
40 } from "@t3tools/contracts";
41 import { isMissingExecutable } from "@t3tools/source-control-core/server/discovery";
42 import * as HostProcess from "@t3tools/shared/HostProcess";
43 import { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
44 import {
45 makeGitVcsDriverCore,
46 PATCH_RENDER_PREFIX_ARGS,
47 splitNullSeparatedGitStdoutPaths,
48 } from "./GitVcsDriverCore.ts";
49 import * as CheckoutCloner from "./CheckoutCloner.ts";
50 import * as CheckpointUntrackedBlobs from "./checkpointUntrackedBlobs.ts";
51 import * as VcsDriver from "./VcsDriver.ts";
52 import * as VcsProcess from "./VcsProcess.ts";
53
54 export interface ExecuteGitInput {
55 readonly operation: string;
56 readonly cwd: string;
57 readonly args: ReadonlyArray<string>;
58 readonly stdin?: string;
59 readonly env?: NodeJS.ProcessEnv;
60 readonly allowNonZeroExit?: boolean;
61 readonly timeoutMs?: number | null;
62 readonly maxOutputBytes?: number;
63 readonly appendTruncationMarker?: boolean;
64 /** With appendTruncationMarker, line callbacks continue beyond the buffer cap for progress-only consumers. */
65 readonly keepLineCallbacksAfterTruncation?: boolean;
66 readonly progress?: ExecuteGitProgress;
67 /** Writes .git/config, which every worktree shares and Git locks without waiting. */
68 readonly writesSharedConfig?: boolean;
69 }
70
71 export interface ExecuteGitResult {
72 readonly exitCode: ChildProcessSpawner.ExitCode;
73 readonly stdout: string;
74 readonly stderr: string;
75 readonly stdoutTruncated: boolean;
76 readonly stderrTruncated: boolean;
77 }
78
79 export interface GitStatusDetails {
80 isRepo: boolean;
81 sourceControlProvider?: VcsStatusResult["sourceControlProvider"];
82 hasOriginRemote: boolean;
83 isDefaultBranch: boolean;
84 branch: string | null;
85 upstreamRef: string | null;
86 hasWorkingTreeChanges: boolean;
87 workingTree: VcsStatusResult["workingTree"];
88 branchChanges?: VcsStatusResult["branchChanges"];
89 hasUpstream: boolean;
90 aheadCount: number;
91 behindCount: number;
92 aheadOfDefaultCount: number;
93 }
94
95 export interface GitLocalStatusOptions {
96 readonly includeDivergence?: boolean;
97 readonly includeBranchChanges?: boolean;
98 }
99
100 export interface GitRemoteStatusDetails {
101 isRepo: boolean;
102 defaultBranch: string | null;
103 isDefaultBranch: boolean;
104 branch: string | null;
105 upstreamRef: string | null;
106 hasUpstream: boolean;
107 aheadCount: number;
108 behindCount: number;
109 aheadOfDefaultCount: number;
110 }
111
112 export interface GitPreparedCommitContext {
113 stagedSummary: string;
114 stagedPatch: string;
115 }
116
117 export interface ExecuteGitProgress {
118 readonly onStdoutLine?: (line: string) => Effect.Effect<void, never>;
119 readonly onStderrLine?: (line: string) => Effect.Effect<void, never>;
120 readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
121 readonly onHookFinished?: (input: {
122 hookName: string;
123 exitCode: number | null;
124 durationMs: number | null;
125 }) => Effect.Effect<void, never>;
126 }
127
128 export interface CreateWorktreeProgress {
129 /**
130 * Runs uninterruptibly once the worktree exists; from then on the caller owns its cleanup.
131 * For new-branch calls on Git supporting add --reason, cancellation before the claim removes what it made.
132 */
133 readonly onWorktreeClaimed?: (path: string) => Effect.Effect<void, never>;
134 readonly onCheckoutProgress?: (input: {
135 percent: number;
136 completed: number;
137 total: number;
138 }) => Effect.Effect<void, never>;
139 readonly onSubmodulesStarted?: () => Effect.Effect<void, never>;
140 readonly onSubmodulesDisabled?: (input: {
141 source: "settings" | "t3.json";
142 }) => Effect.Effect<void, never>;
143 readonly onSubmoduleLine?: (line: string) => Effect.Effect<void, never>;
144 readonly onSubmodulesFinished?: (input: {
145 ok: boolean;
146 detail: string | null;
147 }) => Effect.Effect<void, never>;
148 }
149
150 export interface CreateWorktreeOptions {
151 readonly progress?: CreateWorktreeProgress;
152 /** Null or omitted defers to the checkout's t3.json. */
153 readonly submodules?: WorktreeSubmodules | null;
154 readonly worktreesDirectory?: string;
155 /** Clone the checkout from the macOS worktree checkout cache. */
156 readonly fastCheckout?: boolean;
157 }
158
159 export interface GitCommitProgress {
160 readonly onOutputLine?: (input: {
161 stream: "stdout" | "stderr";
162 text: string;
163 }) => Effect.Effect<void, never>;
164 readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
165 readonly onHookFinished?: (input: {
166 hookName: string;
167 exitCode: number | null;
168 durationMs: number | null;
169 }) => Effect.Effect<void, never>;
170 }
171
172 export interface GitCommitOptions {
173 readonly timeoutMs?: number;
174 readonly progress?: GitCommitProgress;
175 readonly stage?: { readonly filePaths?: readonly string[] };
176 }
177
178 export interface GitDeleteLocalBranchInput {
179 readonly cwd: string;
180 readonly refName: string;
181 readonly force?: boolean;
182 }
183
184 export interface GitPushResult {
185 status: "pushed" | "skipped_up_to_date";
186 branch: string;
187 upstreamBranch?: string | undefined;
188 /** Set when `upstreamBranch` is on a configured push remote instead of the tracked upstream. */
189 pushRemote?: string | undefined;
190 setUpstream?: boolean | undefined;
191 }
192
193 export interface GitPushTarget {
194 remoteName: string;
195 branchName: string;
196 /** The tracked upstream is current, yet pushes go to this configured push remote. */
197 viaPushRemote: boolean;
198 }
199
200 export interface GitRangeContext {
201 commitSummary: string;
202 diffSummary: string;
203 diffPatch: string;
204 }
205
206 export interface GitRenameBranchInput {
207 exactName?: boolean;
208 cwd: string;
209 oldBranch: string;
210 newBranch: string;
211 }
212
213 export interface GitRenameBranchResult {
214 branch: string;
215 }
216
217 export interface GitFetchRefInput {
218 cwd: string;
219 /** A ref on the primary remote, such as a host's change request head ref. */
220 ref: string;
221 }
222
223 export interface GitResolveCommitInput {
224 cwd: string;
225 revision: string;
226 }
227
228 export interface GitResolveCommitResult {
229 commitSha: string;
230 }
231
232 export interface GitRefreshCheckedOutBranchInput {
233 cwd: string;
234 targetCommit: string;
235 /** Pre-fetch upstream commit; resetting away is safe only if HEAD stayed here. */
236 resetWhenHeadCommit?: string | null | undefined;
237 }
238
239 export interface GitRefreshCheckedOutBranchResult {
240 headCommit: string;
241 moved: boolean;
242 onTarget: boolean;
243 }
244
245 export interface GitEnsureRemoteInput {
246 cwd: string;
247 preferredName: string;
248 url: string;
249 }
250
251 export interface GitFetchRemoteBranchInput {
252 cwd: string;
253 remoteName: string;
254 remoteBranch: string;
255 localBranch: string;
256 }
257
258 export interface GitFetchRemoteTrackingBranchInput {
259 cwd: string;
260 remoteName: string;
261 remoteBranch: string;
262 }
263
264 export interface GitFetchRemoteInput {
265 cwd: string;
266 remoteName: string;
267 refName?: string;
268 }
269
270 export interface GitRemoteExistsInput {
271 cwd: string;
272 remoteName: string;
273 }
274
275 export interface GitRemoteBranchExistsInput extends GitRemoteExistsInput {
276 refName: string;
277 }
278
279 export interface GitResolveRemoteTrackingCommitInput {
280 cwd: string;
281 refName: string;
282 fallbackRemoteName: string;
283 }
284
285 export interface GitResolveRemoteTrackingCommitResult {
286 commitSha: string;
287 remoteRefName: string;
288 }
289
290 export interface GitFetchWorktreeBaseCommitInput {
291 cwd: string;
292 refName: string;
293 }
294
295 export interface GitCreateBranchInput {
296 readonly cwd: string;
297 readonly branch: string;
298 readonly startPoint: string;
299 }
300
301 export interface GitWriteConfigValueInput {
302 readonly cwd: string;
303 readonly key: string;
304 readonly value: string;
305 }
306
307 export interface GitWorktreeBaseRemote {
308 remoteName: string;
309 branchName: string;
310 }
311
312 export interface GitSetBranchUpstreamInput {
313 cwd: string;
314 branch: string;
315 remoteName: string;
316 remoteBranch: string;
317 }
318
319 export interface GitRemoteStatusOptions {
320 readonly refreshUpstream?: boolean;
321 }
322
323 /**
324 * Set by a one-time reader, such as the startup automatic pull, that has no later poll to see a
325 * slow status fetch land. Its status read waits for a running fetch instead of using the last
326 * fetched references after five seconds. The fetch keeps its own safety limit.
327 */
328 export const AwaitStatusFetch = Context.Reference<boolean>("t3/vcs/AwaitStatusFetch", {
329 defaultValue: () => false,
330 });
331
332 /** Set by background discovery so its Git commands run in their own small lane, not the user's. */
333 export const BackgroundGitWork = Context.Reference<boolean>("t3/vcs/BackgroundGitWork", {
334 defaultValue: () => false,
335 });
336
337 export class GitVcsDriver extends Context.Service<
338 GitVcsDriver,
339 {
340 readonly execute: (input: ExecuteGitInput) => Effect.Effect<ExecuteGitResult, GitCommandError>;
341 readonly status: (input: VcsStatusInput) => Effect.Effect<VcsStatusResult, GitCommandError>;
342 readonly statusDetails: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
343 readonly statusDetailsLocal: (
344 cwd: string,
345 options?: GitLocalStatusOptions,
346 ) => Effect.Effect<GitStatusDetails, GitCommandError>;
347 readonly statusDetailsRemote: (
348 cwd: string,
349 options?: GitRemoteStatusOptions,
350 ) => Effect.Effect<GitRemoteStatusDetails, GitCommandError>;
351 /** Refs, HEAD and shallow boundaries. Null when the snapshot cannot be read. */
352 readonly readRefSnapshot: (cwd: string) => Effect.Effect<string | null>;
353 readonly prepareCommitContext: (
354 cwd: string,
355 filePaths?: readonly string[],
356 options?: { readonly includePatch?: boolean },
357 ) => Effect.Effect<GitPreparedCommitContext | null, GitCommandError>;
358 readonly commit: (
359 cwd: string,
360 subject: string,
361 body: string,
362 options?: GitCommitOptions,
363 ) => Effect.Effect<{ commitSha: string }, GitCommandError>;
364 readonly pushCurrentBranch: (
365 cwd: string,
366 fallbackBranch: string | null,
367 options?: { readonly remoteName?: string | null },
368 ) => Effect.Effect<GitPushResult, GitCommandError>;
369 /** Where `pushCurrentBranch` would push the checked-out branch; null with no remote or when Git picks. */
370 readonly resolvePushTarget: (
371 cwd: string,
372 current: { readonly branch: string; readonly hasUpstream: boolean },
373 ) => Effect.Effect<GitPushTarget | null, GitCommandError>;
374 readonly readRangeContext: (
375 cwd: string,
376 baseRef: string,
377 ) => Effect.Effect<GitRangeContext, GitCommandError>;
378 readonly getReviewBranchCommits: (
379 input: ReviewBranchCommitsInput,
380 ) => Effect.Effect<ReviewBranchCommitsResult, GitCommandError>;
381 readonly getReviewDiffPreview: (
382 input: ReviewDiffPreviewInput,
383 ) => Effect.Effect<ReviewDiffPreviewResult, GitCommandError>;
384 readonly getReviewDiffFileContents: (
385 input: ReviewDiffFileContentsInput,
386 ) => Effect.Effect<ReviewDiffFileContentsResult, GitCommandError>;
387 readonly readConfigValue: (
388 cwd: string,
389 key: string,
390 ) => Effect.Effect<string | null, GitCommandError>;
391 /**
392 * The branch, named as on the host, that T3 recorded `branch` was cut from. Null when none
393 * was recorded.
394 */
395 readonly readBranchBase: (
396 cwd: string,
397 branch: string,
398 ) => Effect.Effect<string | null, GitCommandError>;
399 readonly resolveBranchName: (
400 cwd: string,
401 refName: string,
402 ) => Effect.Effect<string | null, GitCommandError>;
403 readonly listRefs: (
404 input: VcsListRefsInput,
405 ) => Effect.Effect<VcsListRefsResult, GitCommandError>;
406 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
407 readonly createWorktree: (
408 input: VcsCreateWorktreeInput,
409 options?: CreateWorktreeOptions,
410 ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
411 /** Deletes the macOS worktree checkout cache in the background. */
412 readonly clearWorktreeCheckoutCache: Effect.Effect<void>;
413 /** Force-updates local `branch` to the primary remote's `ref`. */
414 readonly fetchRefIntoBranch: (
415 input: GitFetchRefInput & { readonly branch: string },
416 ) => Effect.Effect<void, GitCommandError>;
417 readonly fetchRefCommit: (
418 input: GitFetchRefInput,
419 ) => Effect.Effect<GitResolveCommitResult, GitCommandError>;
420 readonly resolveCommit: (
421 input: GitResolveCommitInput,
422 ) => Effect.Effect<GitResolveCommitResult, GitCommandError>;
423 readonly refreshCheckedOutBranch: (
424 input: GitRefreshCheckedOutBranchInput,
425 ) => Effect.Effect<GitRefreshCheckedOutBranchResult, GitCommandError>;
426 readonly ensureRemote: (input: GitEnsureRemoteInput) => Effect.Effect<string, GitCommandError>;
427 readonly resolvePrimaryRemoteName: (cwd: string) => Effect.Effect<string, GitCommandError>;
428 readonly resolveDefaultBranchName: (
429 cwd: string,
430 remoteName: string,
431 ) => Effect.Effect<string | null, GitCommandError>;
432 readonly fetchRemote: (input: GitFetchRemoteInput) => Effect.Effect<void, GitCommandError>;
433 readonly remoteExists: (input: GitRemoteExistsInput) => Effect.Effect<boolean, GitCommandError>;
434 readonly remoteBranchExists: (
435 input: GitRemoteBranchExistsInput,
436 ) => Effect.Effect<boolean, GitCommandError>;
437 readonly resolveRemoteTrackingCommit: (
438 input: GitResolveRemoteTrackingCommitInput,
439 ) => Effect.Effect<GitResolveRemoteTrackingCommitResult, GitCommandError>;
440 readonly resolveWorktreeBaseRemote: (
441 input: GitFetchWorktreeBaseCommitInput,
442 ) => Effect.Effect<GitWorktreeBaseRemote | null, GitCommandError>;
443 /**
444 * Fetches the configured upstream, explicit remote ref, or origin; null uses the local base.
445 * A caller that already ran `resolveWorktreeBaseRemote` passes its result as `remote`.
446 */
447 readonly fetchWorktreeBaseCommit: (
448 input: GitFetchWorktreeBaseCommitInput & { readonly remote?: GitWorktreeBaseRemote },
449 ) => Effect.Effect<GitResolveRemoteTrackingCommitResult | null, GitCommandError>;
450 readonly fetchRemoteBranch: (
451 input: GitFetchRemoteBranchInput,
452 ) => Effect.Effect<void, GitCommandError>;
453 readonly fetchRemoteTrackingBranch: (
454 input: GitFetchRemoteTrackingBranchInput,
455 ) => Effect.Effect<void, GitCommandError>;
456 readonly setBranchUpstream: (
457 input: GitSetBranchUpstreamInput,
458 ) => Effect.Effect<void, GitCommandError>;
459 /** Tracks the start point like `git branch` does. */
460 readonly createBranch: (input: GitCreateBranchInput) => Effect.Effect<void, GitCommandError>;
461 readonly writeConfigValue: (
462 input: GitWriteConfigValueInput,
463 ) => Effect.Effect<void, GitCommandError>;
464 readonly removeWorktree: (
465 input: VcsRemoveWorktreeInput,
466 ) => Effect.Effect<void, GitCommandError>;
467 readonly pruneWorktrees: (input: {
468 readonly cwd: string;
469 }) => Effect.Effect<void, GitCommandError>;
470 readonly listWorktreePaths: (cwd: string) => Effect.Effect<string[], GitCommandError>;
471 readonly deleteLocalBranch: (
472 input: GitDeleteLocalBranchInput,
473 ) => Effect.Effect<void, GitCommandError>;
474 readonly renameBranch: (
475 input: GitRenameBranchInput,
476 ) => Effect.Effect<GitRenameBranchResult, GitCommandError>;
477 readonly createRef: (
478 input: VcsCreateRefInput,
479 ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
480 readonly switchRef: (
481 input: VcsSwitchRefInput,
482 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
483 readonly initRepo: (input: VcsInitInput) => Effect.Effect<void, GitCommandError>;
484 /** Drops cached refs after something outside this driver changed them, such as a clone into `cwd`. */
485 readonly invalidateRefs: (cwd: string) => Effect.Effect<void>;
486 readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
487 }
488 >()("t3/vcs/GitVcsDriver") {}
489
490 const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
491 const FLAGGED_INDEX_ENTRIES_MAX_BYTES = 1024 * 1024;
492 const CHECKPOINT_RECOVERY_MAX_CANDIDATES = 64;
493 const CHECKPOINT_RECOVERY_TIMEOUT = "5 seconds";
494 const GIT_CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
495 // Reflog subjects that only point a ref at an existing commit. Anything else counts as creating
496 // one, so an unknown subject keeps its commit attributed.
497 const REF_MOVE_REFLOG_SUBJECT =
498 /^(?:$|(?:checkout|reset|branch|clone|fetch)\b|[^:]*\((?:start|finish|abort|reset)\): |(?:merge|pull|rebase|cherry-pick)\b[^()]*: fast-forward$)/i;
499
500 function parseCheckpointReflogEntry(line: string) {
501 const [commit = "", selector = "", ...subject] = line.split(" ");
502 return {
503 commit,
504 fetched: selector.startsWith("refs/remotes/"),
505 subject: subject.join(" "),
506 };
507 }
508
509 function isLocalReflogCreation(entry: ReturnType<typeof parseCheckpointReflogEntry>) {
510 return !entry.fetched && !REF_MOVE_REFLOG_SUBJECT.test(entry.subject);
511 }
512
513 const WORKSPACE_GIT_HARDENED_CONFIG_ARGS = [
514 "-c",
515 "core.fsmonitor=false",
516 "-c",
517 "core.untrackedCache=false",
518 ] as const;
519
520 const nowFreshness = Effect.fn("GitVcsDriver.nowFreshness")(function* () {
521 const now = yield* DateTime.now;
522 return {
523 source: "live-local" as const,
524 observedAt: now,
525 expiresAt: Option.none(),
526 };
527 });
528
529 function chunkPathsForGitCheckIgnore(relativePaths: ReadonlyArray<string>): string[][] {
530 const chunks: string[][] = [];
531 let chunk: string[] = [];
532 let chunkBytes = 0;
533
534 for (const relativePath of relativePaths) {
535 const relativePathBytes = Buffer.byteLength(relativePath) + 1;
536 if (chunk.length > 0 && chunkBytes + relativePathBytes > GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
537 chunks.push(chunk);
538 chunk = [];
539 chunkBytes = 0;
540 }
541
542 chunk.push(relativePath);
543 chunkBytes += relativePathBytes;
544
545 if (chunkBytes >= GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
546 chunks.push(chunk);
547 chunk = [];
548 chunkBytes = 0;
549 }
550 }
551
552 if (chunk.length > 0) {
553 chunks.push(chunk);
554 }
555
556 return chunks;
557 }
558
559 function parseGitRemoteVerboseOutput(
560 output: string,
561 ): Map<string, { url?: string; pushUrl?: string }> {
562 const remotes = new Map<string, { url?: string; pushUrl?: string }>();
563 for (const line of output.split(/\r?\n/)) {
564 // Git uses a tab before the URL and one space before the direction; both can occur in URLs.
565 // Partial clones append the promisor filter, e.g. `(fetch) [blob:none]`.
566 const match = /^(\S+)\t(.+) \((fetch|push)\)(?:\s+\[[^\]]+\])?$/.exec(line);
567 if (!match) {
568 continue;
569 }
570
571 const name = match[1];
572 const url = match[2];
573 const direction = match[3];
574 if (!name || !url || !direction) {
575 continue;
576 }
577 const remote = remotes.get(name) ?? {};
578 if (direction === "fetch") {
579 remote.url = url;
580 } else {
581 remote.pushUrl = url;
582 }
583 remotes.set(name, remote);
584 }
585 return remotes;
586 }
587
588 // Trace output and deprecation warnings can precede Git's own message, so check every line.
589 const hasStderrLine = (stderr: string, prefixes: ReadonlyArray<string>) =>
590 stderr.split("\n").some((line) => prefixes.some((prefix) => line.startsWith(prefix)));
591
592 /** Operation of the error raised when Git cannot say whether a cwd is a work tree. */
593 export const WORK_TREE_PROBE_OPERATION = "GitVcsDriver.probeWorkTree";
594
595 const gitCommand = Effect.fn("GitVcsDriver.gitCommand")(
596 (
597 process: VcsProcess.VcsProcess["Service"],
598 operation: string,
599 cwd: string,
600 args: ReadonlyArray<string>,
601 options?: {
602 readonly stdin?: string;
603 readonly onSpawn?: () => void;
604 readonly env?: NodeJS.ProcessEnv;
605 readonly allowNonZeroExit?: boolean;
606 readonly timeoutMs?: number;
607 readonly maxOutputBytes?: number;
608 readonly outputMode?: VcsProcess.VcsProcessInput["outputMode"];
609 readonly appendTruncationMarker?: boolean;
610 },
611 ) =>
612 process.run({
613 operation,
614 command: "git",
615 args: ["-C", cwd, ...args],
616 cwd,
617 spawnCwd: globalThis.process.cwd(),
618 ...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
619 ...(options?.onSpawn !== undefined ? { onSpawn: options.onSpawn } : {}),
620 ...(options?.env !== undefined ? { env: options.env } : {}),
621 ...(options?.allowNonZeroExit !== undefined
622 ? { allowNonZeroExit: options.allowNonZeroExit }
623 : {}),
624 ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
625 ...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
626 ...(options?.outputMode !== undefined ? { outputMode: options.outputMode } : {}),
627 ...(options?.appendTruncationMarker !== undefined
628 ? { appendTruncationMarker: options.appendTruncationMarker }
629 : {}),
630 }),
631 );
632
633 const HEAD_MOVES_SCAN_LIMIT = 512;
634 const HEAD_MARK_TRAILER = "T3-Head-Moved-At";
635 const HEAD_MARK_TRAILER_PATTERN = new RegExp(`^${HEAD_MARK_TRAILER}: (\\d+) (\\d+)$`, "m");
636
637 // Parses "git version 2.34.1" or "git version 2.45.1.windows.1".
638 const gitSupportsRemergeDiff = (versionOutput: string): boolean => {
639 const match = /^git version (\d+)\.(\d+)/.exec(versionOutput);
640 if (match === null) return false;
641 const major = Number(match[1]);
642 return major > 2 || (major === 2 && Number(match[2]) >= 36);
643 };
644
645 export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* () {
646 const fileSystem = yield* FileSystem.FileSystem;
647 const path = yield* Path.Path;
648 const vcsProcess = yield* VcsProcess.VcsProcess;
649 const random = yield* SecureRandom.SecureRandom;
650 const hostPlatform = yield* HostProcess.Platform;
651 // Returns stdout, or null when there is no work tree. A non-repository exits 128 like a broken
652 // one, so only Git's message (pinned to the C locale) or a missing cwd tells them apart.
653 const probeWorkTree = Effect.fn("GitVcsDriver.probeWorkTree")(function* (
654 operation: string,
655 cwd: string,
656 args: ReadonlyArray<string>,
657 maxOutputBytes: number,
658 ) {
659 const result = yield* gitCommand(vcsProcess, operation, cwd, args, {
660 env: { LC_ALL: "C" },
661 allowNonZeroExit: true,
662 timeoutMs: 5_000,
663 maxOutputBytes,
664 }).pipe(Effect.catchIf(isMissingExecutable, () => Effect.succeed(null)));
665 if (result === null) return null;
666 if (result.exitCode === 0) return result.stdout;
667 // Git before 2.38 answers "false" for a bare repo, then fails on --show-toplevel.
668 // Match line prefixes only: a path inside a diagnostic may contain the same words.
669 if (
670 result.stdout.startsWith("false") ||
671 hasStderrLine(result.stderr, [
672 "fatal: not a git repository",
673 "fatal: cannot use bare repository",
674 ]) ||
675 !(yield* fileSystem.exists(cwd).pipe(Effect.orElseSucceed(() => true)))
676 ) {
677 return null;
678 }
679 return yield* new VcsProcessExitError({
680 operation: WORK_TREE_PROBE_OPERATION,
681 command: "git rev-parse",
682 cwd,
683 exitCode: result.exitCode,
684 detail: result.stderr.trim() || "git rev-parse failed",
685 });
686 });
687
688 const isInsideWorkTree: VcsDriver.VcsDriver["Service"]["isInsideWorkTree"] = (cwd) =>
689 probeWorkTree(
690 "GitVcsDriver.isInsideWorkTree",
691 cwd,
692 ["rev-parse", "--is-inside-work-tree"],
693 4_096,
694 ).pipe(Effect.map((stdout) => stdout?.trim() === "true"));
695
696 const execute: VcsDriver.VcsDriver["Service"]["execute"] = (input) =>
697 gitCommand(vcsProcess, input.operation, input.cwd, input.args, {
698 ...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
699 ...(input.onSpawn !== undefined ? { onSpawn: input.onSpawn } : {}),
700 ...(input.env !== undefined ? { env: input.env } : {}),
701 ...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}),
702 ...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}),
703 ...(input.maxOutputBytes !== undefined ? { maxOutputBytes: input.maxOutputBytes } : {}),
704 ...(input.outputMode !== undefined ? { outputMode: input.outputMode } : {}),
705 ...(input.appendTruncationMarker !== undefined
706 ? { appendTruncationMarker: input.appendTruncationMarker }
707 : {}),
708 });
709
710 // --diff-merges=remerge needs Git 2.36.
711 let remergeDiffSupported: boolean | undefined;
712 const supportsRemergeDiff = Effect.fn("GitVcsDriver.supportsRemergeDiff")(function* (
713 cwd: string,
714 ) {
715 if (remergeDiffSupported === undefined) {
716 const version = yield* execute({
717 operation: "GitVcsDriver.version",
718 cwd,
719 args: ["version"],
720 }).pipe(Effect.option);
721 if (Option.isNone(version)) return false;
722 remergeDiffSupported = gitSupportsRemergeDiff(version.value.stdout);
723 }
724 return remergeDiffSupported;
725 });
726
727 const detectRepository: VcsDriver.VcsDriver["Service"]["detectRepository"] = Effect.fn(
728 "detectRepository",
729 )(function* (cwd) {
730 // One spawn answers all three; outside a work tree it exits non-zero or prints "false".
731 const stdout = yield* probeWorkTree(
732 "GitVcsDriver.detectRepository",
733 cwd,
734 ["rev-parse", "--is-inside-work-tree", "--show-toplevel", "--git-common-dir"],
735 16_384,
736 );
737 if (stdout === null) return null;
738 const lines = stdout.replace(/\r?\n$/u, "").split(/\r?\n/u);
739 if (lines[0]?.trim() !== "true") return null;
740 // A newline inside a path breaks the line framing; ask for each path on its own.
741 if (lines.length !== 3) {
742 const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
743 "rev-parse",
744 "--show-toplevel",
745 ]);
746 const gitCommonDir = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
747 "rev-parse",
748 "--git-common-dir",
749 ]).pipe(Effect.orElseSucceed(() => null));
750 return {
751 kind: "git" as const,
752 rootPath: root.stdout.trim(),
753 metadataPath: gitCommonDir?.stdout.trim() || null,
754 freshness: yield* nowFreshness(),
755 };
756 }
757
758 return {
759 kind: "git" as const,
760 rootPath: lines[1]!.trim(),
761 metadataPath: lines[2]!.trim() || null,
762 freshness: yield* nowFreshness(),
763 };
764 });
765
766 const listWorkspaceFiles: VcsDriver.VcsDriver["Service"]["listWorkspaceFiles"] = (cwd) =>
767 gitCommand(
768 vcsProcess,
769 "GitVcsDriver.listWorkspaceFiles",
770 cwd,
771 [
772 ...WORKSPACE_GIT_HARDENED_CONFIG_ARGS,
773 "ls-files",
774 "--cached",
775 "--others",
776 "--exclude-standard",
777 "-z",
778 ],
779 {
780 allowNonZeroExit: true,
781 timeoutMs: 20_000,
782 maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
783 appendTruncationMarker: true,
784 },
785 ).pipe(
786 Effect.flatMap((result) =>
787 result.exitCode === 0
788 ? Effect.gen(function* () {
789 const freshness = yield* nowFreshness();
790 return {
791 paths: splitNullSeparatedGitStdoutPaths(result),
792 truncated: result.stdoutTruncated,
793 freshness,
794 };
795 })
796 : Effect.fail(
797 new VcsProcessExitError({
798 operation: "GitVcsDriver.listWorkspaceFiles",
799 command: "git ls-files",
800 cwd,
801 exitCode: result.exitCode,
802 detail: result.stderr.trim() || "git ls-files failed",
803 }),
804 ),
805 ),
806 );
807
808 const listRemotes: VcsDriver.VcsDriver["Service"]["listRemotes"] = Effect.fn("listRemotes")(
809 function* (cwd) {
810 const result = yield* gitCommand(
811 vcsProcess,
812 "GitVcsDriver.listRemotes",
813 cwd,
814 ["remote", "-v"],
815 {
816 allowNonZeroExit: true,
817 timeoutMs: 5_000,
818 maxOutputBytes: 64 * 1024,
819 },
820 );
821
822 if (result.exitCode !== 0) {
823 return yield* new VcsProcessExitError({
824 operation: "GitVcsDriver.listRemotes",
825 command: "git remote -v",
826 cwd,
827 exitCode: result.exitCode,
828 detail: result.stderr.trim() || "git remote -v failed",
829 });
830 }
831
832 const parsed = parseGitRemoteVerboseOutput(result.stdout);
833 const remotes = Array.from(parsed.entries()).flatMap(([name, remote]) => {
834 if (!remote.url) {
835 return [];
836 }
837 return [
838 {
839 name,
840 url: remote.url,
841 pushUrl: remote.pushUrl ? Option.some(remote.pushUrl) : Option.none(),
842 isPrimary: name === "origin",
843 },
844 ];
845 });
846
847 return {
848 remotes,
849 freshness: yield* nowFreshness(),
850 };
851 },
852 );
853
854 const filterIgnoredPaths: VcsDriver.VcsDriver["Service"]["filterIgnoredPaths"] = Effect.fn(
855 "filterIgnoredPaths",
856 )(function* (cwd, relativePaths) {
857 if (relativePaths.length === 0) {
858 return relativePaths;
859 }
860
861 const ignoredPaths = new Set<string>();
862 const chunks = chunkPathsForGitCheckIgnore(relativePaths);
863
864 for (const chunk of chunks) {
865 const result = yield* gitCommand(
866 vcsProcess,
867 "GitVcsDriver.filterIgnoredPaths",
868 cwd,
869 [...WORKSPACE_GIT_HARDENED_CONFIG_ARGS, "check-ignore", "--no-index", "-z", "--stdin"],
870 {
871 stdin: `${chunk.join("\0")}\0`,
872 allowNonZeroExit: true,
873 timeoutMs: 20_000,
874 maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
875 appendTruncationMarker: true,
876 },
877 );
878
879 if (result.exitCode !== 0 && result.exitCode !== 1) {
880 return yield* new VcsProcessExitError({
881 operation: "GitVcsDriver.filterIgnoredPaths",
882 command: "git check-ignore",
883 cwd,
884 exitCode: result.exitCode,
885 detail: result.stderr.trim() || "git check-ignore failed",
886 });
887 }
888
889 for (const ignoredPath of splitNullSeparatedGitStdoutPaths(result)) {
890 ignoredPaths.add(ignoredPath);
891 }
892 }
893
894 if (ignoredPaths.size === 0) {
895 return relativePaths;
896 }
897
898 return relativePaths.filter((relativePath) => !ignoredPaths.has(relativePath));
899 });
900
901 const initRepository: VcsDriver.VcsDriver["Service"]["initRepository"] = (input) =>
902 gitCommand(vcsProcess, "GitVcsDriver.initRepository", input.cwd, ["init"], {
903 timeoutMs: 10_000,
904 maxOutputBytes: 64 * 1024,
905 }).pipe(Effect.asVoid);
906
907 const resolveHeadCommit = (cwd: string) =>
908 execute({
909 operation: "GitVcsDriver.checkpoints.resolveHeadCommit",
910 cwd,
911 args: ["rev-parse", "--verify", "--quiet", "HEAD^{commit}"],
912 allowNonZeroExit: true,
913 }).pipe(
914 Effect.map((result) => {
915 if (result.exitCode !== 0) {
916 return null;
917 }
918 const commit = result.stdout.trim();
919 return commit.length > 0 ? commit : null;
920 }),
921 );
922
923 const hasHeadCommit = (cwd: string, env?: NodeJS.ProcessEnv) =>
924 execute({
925 operation: "GitVcsDriver.checkpoints.hasHeadCommit",
926 cwd,
927 args: ["rev-parse", "--verify", "HEAD"],
928 allowNonZeroExit: true,
929 ...(env !== undefined ? { env } : {}),
930 }).pipe(Effect.map((result) => result.exitCode === 0));
931
932 const resolveCheckpointCommit = (cwd: string, checkpointRef: string) =>
933 execute({
934 operation: "GitVcsDriver.checkpoints.resolveCheckpointCommit",
935 cwd,
936 args: ["rev-parse", "--verify", "--quiet", `${checkpointRef}^{commit}`],
937 env: { LC_ALL: "C" },
938 allowNonZeroExit: true,
939 }).pipe(
940 Effect.flatMap((result) => {
941 // --quiet reports an absent ref as exit 1 with no error; an unreadable object also exits 1
942 // but reports an error line.
943 if (result.exitCode === 1 && !hasStderrLine(result.stderr, ["error:", "fatal:"])) {
944 return Effect.succeed(null);
945 }
946 if (result.exitCode !== 0) {
947 return Effect.fail(
948 new VcsProcessExitError({
949 operation: "GitVcsDriver.checkpoints.resolveCheckpointCommit",
950 command: "git rev-parse",
951 cwd,
952 exitCode: result.exitCode,
953 detail: result.stderr.trim() || "git rev-parse failed",
954 }),
955 );
956 }
957 const commit = result.stdout.trim();
958 return Effect.succeed(commit.length > 0 ? commit : null);
959 }),
960 );
961
962 // Git reports relative paths from the physical cwd, which can differ from a symlink.
963 const resolveGitCommonDir = Effect.fn("GitVcsDriver.resolveGitCommonDir")(function* (
964 cwd: string,
965 ) {
966 const result = yield* execute({
967 operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
968 cwd,
969 args: ["rev-parse", "--git-common-dir"],
970 });
971 const gitCommonDir = result.stdout.trim();
972 if (path.isAbsolute(gitCommonDir)) return gitCommonDir;
973 const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
974 return path.resolve(physicalCwd, gitCommonDir);
975 });
976
977 // --verify prints HEAD after the paths; --quiet leaves only paths for an unborn HEAD.
978 const resolveCaptureRepository = Effect.fn("GitVcsDriver.resolveCaptureRepository")(function* (
979 cwd: string,
980 ) {
981 const result = yield* execute({
982 operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
983 cwd,
984 args: [
985 "rev-parse",
986 "--path-format=absolute",
987 "--git-common-dir",
988 "--git-path",
989 "index",
990 "--show-toplevel",
991 "--show-prefix",
992 "--verify",
993 "--quiet",
994 "HEAD",
995 ],
996 allowNonZeroExit: true,
997 });
998 const headExists = result.exitCode === 0;
999 const headChecked = headExists || result.exitCode === 1;
1000 const lines = result.stdout.split("\n");
1001 const [gitCommonDir = "", indexPath = "", topLevel = "", prefix = "", head = ""] = lines;
1002 if (headChecked && lines.length === (headExists ? 6 : 5) && lines.at(-1) === "") {
1003 return {
1004 gitCommonDir: gitCommonDir.trim(),
1005 indexPath: indexPath.trim(),
1006 topLevel: topLevel.trim(),
1007 prefix,
1008 headExists,
1009 head: headExists ? head : undefined,
1010 };
1011 }
1012 // Git before 2.31 echoes --path-format, and a path containing a newline spans lines.
1013 // Separate commands read the paths as before and report a failure the same way.
1014 const fallbackCommonDir = yield* resolveGitCommonDir(cwd);
1015 const prefixResult = yield* execute({
1016 operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
1017 cwd,
1018 args: ["rev-parse", "--show-prefix"],
1019 });
1020 return {
1021 gitCommonDir: fallbackCommonDir,
1022 indexPath: undefined,
1023 topLevel: undefined,
1024 prefix: prefixResult.stdout.replace(/\n$/, ""),
1025 headExists: headChecked ? headExists : yield* hasHeadCommit(cwd),
1026 head: undefined,
1027 };
1028 });
1029
1030 // $displaypath is relative to cwd, so "../" marks submodules outside a nested workspace.
1031 const listSubmodules = Effect.fn("GitVcsDriver.listSubmodules")(function* (cwd: string) {
1032 const result = yield* execute({
1033 operation: "GitVcsDriver.checkpoints.listSubmodules",
1034 cwd,
1035 args: ["submodule", "foreach", "--quiet", 'printf "%s\\0" "$displaypath"'],
1036 });
1037 return result.stdout
1038 .split("\0")
1039 .filter((entry) => entry !== "" && !entry.startsWith("../"))
1040 .map((entry) => path.join(cwd, entry));
1041 });
1042
1043 // Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication.
1044 // Set fsync first: Git 2.36 ignores unknown batch without resetting the method.
1045 // Batch fully flushes refs and equals fsync on macOS HFS+/APFS and Windows NTFS/ReFS.
1046 // Keep per-object fsync elsewhere; VcsProcess retries unsupported macOS full flushes with plain fsync.
1047 const durableWrite = [
1048 "-c",
1049 "core.fsync=objects,reference",
1050 "-c",
1051 "core.fsyncMethod=fsync",
1052 ...(hostPlatform === "darwin" || hostPlatform === "win32"
1053 ? ["-c", "core.fsyncMethod=batch"]
1054 : []),
1055 ] as const;
1056
1057 // Checkpoints must handle existing paths beyond Windows MAX_PATH, even when
1058 // core.longpaths is disabled in the user's Git configuration.
1059 const longPaths = hostPlatform === "win32" ? ["-c", "core.longpaths=true"] : [];
1060
1061 // Newest first: the commit each HEAD move landed on, when, and Git's reason for it.
1062 const readHeadMoves = (cwd: string) =>
1063 execute({
1064 operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
1065 cwd,
1066 args: [
1067 "-c",
1068 "log.showSignature=false",
1069 "log",
1070 "--walk-reflogs",
1071 "-n",
1072 String(HEAD_MOVES_SCAN_LIMIT),
1073 "--date=unix",
1074 "--format=%H %gd %gs",
1075 "HEAD",
1076 "--",
1077 ],
1078 allowNonZeroExit: true,
1079 }).pipe(
1080 Effect.map((result) =>
1081 result.exitCode !== 0
1082 ? []
1083 : result.stdout.split("\n").flatMap((line) => {
1084 const [, to, at, reason] = /^([0-9a-f]+) HEAD@\{(\d+)\} (.*)$/.exec(line) ?? [];
1085 return to === undefined || at === undefined || reason === undefined
1086 ? []
1087 : [{ to, at, reason }];
1088 }),
1089 ),
1090 Effect.orElseSucceed(() => []),
1091 );
1092
1093 // A checkpoint taken after a branch switch has a second parent: the checkpoint before it with
1094 // the switch applied, which is where that turn started. Other ranges start at `from` itself.
1095 const resolveTurnStart = (input: VcsDriver.VcsListAuthoredPathsInput) =>
1096 execute({
1097 operation: "GitVcsDriver.checkpoints.resolveTurnStart",
1098 cwd: input.cwd,
1099 args: [
1100 "-c",
1101 "log.showSignature=false",
1102 "log",
1103 "--no-walk=unsorted",
1104 "--format=%H %P",
1105 `${input.fromCheckpointRef}^{commit}`,
1106 `${input.toCheckpointRef}^2`,
1107 ],
1108 allowNonZeroExit: true,
1109 }).pipe(
1110 Effect.map((result): string => {
1111 const [fromLine = "", startLine = ""] = result.stdout.trimEnd().split("\n");
1112 const [start = "", , startedFrom] = startLine.split(" ");
1113 return result.exitCode === 0 && startedFrom === fromLine.split(" ")[0]
1114 ? start
1115 : input.fromCheckpointRef;
1116 }),
1117 );
1118
1119 const untrackedBlobs = CheckpointUntrackedBlobs.make(execute, fileSystem);
1120
1121 const checkpoints: VcsDriver.VcsCheckpointOps = {
1122 captureCheckpoint: Effect.fn("GitVcsDriver.checkpoints.captureCheckpoint")(function* (input) {
1123 const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
1124 // Scratch split-index writes can expire sharedindex files still used by the real index.
1125 const indexConfig = [
1126 "-c",
1127 "core.fsmonitor=false",
1128 "-c",
1129 "sparse.expectFilesOutsideOfPatterns=false",
1130 "-c",
1131 "core.splitIndex=false",
1132 "-c",
1133 "splitIndex.sharedIndexExpire=never",
1134 ];
1135 const { gitCommonDir, indexPath, topLevel, prefix, headExists, head } =
1136 yield* resolveCaptureRepository(input.cwd);
1137 // Read before the snapshot and pinned to the same HEAD: a switch that lands while files are
1138 // being staged then belongs to the next checkpoint instead of being applied to this one.
1139 const headMoves = head === undefined ? [] : yield* readHeadMoves(input.cwd);
1140 const lastMove = headMoves.at(0);
1141 // Moves share a second, so the mark also counts how many of that second it has seen.
1142 const headMark =
1143 lastMove !== undefined && lastMove.to === head
1144 ? `${lastMove.at} ${headMoves.filter((move) => move.at === lastMove.at).length}`
1145 : undefined;
1146 const startedAt = yield* Clock.currentTimeMillis;
1147 const indexId = yield* random.uuidV4();
1148 const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${indexId}`);
1149 const hostEnv = yield* HostProcess.Environment;
1150 // VcsProcess clears inherited object selectors, so every other command uses this store.
1151 const sharedObjects = path.join(gitCommonDir, "objects");
1152 const quotedObjects = quoteGitPatchPath(sharedObjects);
1153 // Isolate bulk-checkin packs so cleanup cannot remove another Git writer's objects.
1154 const privateObjects = yield* Effect.acquireRelease(
1155 fileSystem
1156 .makeTempDirectory({ directory: sharedObjects, prefix: "tmp_objdir-t3-checkpoint-" })
1157 .pipe(
1158 Effect.mapError(
1159 (cause) =>
1160 new VcsCheckpointStorageError({
1161 operation,
1162 cwd: input.cwd,
1163 stage: "isolate",
1164 cause,
1165 }),
1166 ),
1167 ),
1168 (directory) =>
1169 fileSystem.remove(directory, { recursive: true, force: true }).pipe(
1170 Effect.catch((cause) =>
1171 Effect.logWarning("Could not remove private checkpoint objects", {
1172 reason: cause.reason._tag,
1173 }),
1174 ),
1175 ),
1176 );
1177 const commitEnv: NodeJS.ProcessEnv = {
1178 GIT_INDEX_FILE: tempIndexPath,
1179 GIT_OBJECT_DIRECTORY: privateObjects,
1180 // A filter must not publish refs that depend on our disposable store.
1181 GIT_QUARANTINE_PATH: privateObjects,
1182 // Git accepts C-quoted alternate paths, including path-list separators.
1183 GIT_ALTERNATE_OBJECT_DIRECTORIES: quotedObjects.startsWith('"')
1184 ? quotedObjects
1185 : `"${quotedObjects}"`,
1186 GIT_AUTHOR_NAME: "T3 Code",
1187 GIT_AUTHOR_EMAIL: "t3code@users.noreply.github.com",
1188 GIT_COMMITTER_NAME: "T3 Code",
1189 GIT_COMMITTER_EMAIL: "t3code@users.noreply.github.com",
1190 };
1191
1192 const cleanupTempIndex = Effect.forEach(
1193 [tempIndexPath, `${tempIndexPath}.lock`],
1194 (indexFile) => fileSystem.remove(indexFile, { force: true }).pipe(Effect.ignore),
1195 { discard: true },
1196 );
1197
1198 yield* Effect.gen(function* () {
1199 const sparseConfig = yield* execute({
1200 operation,
1201 cwd: input.cwd,
1202 args: ["config", "--bool", "core.sparseCheckout"],
1203 allowNonZeroExit: true,
1204 });
1205 let sparseCheckout = sparseConfig.stdout.trim() === "true";
1206 if (sparseCheckout) {
1207 const help = yield* execute({
1208 operation,
1209 cwd: input.cwd,
1210 args: ["add", "-h"],
1211 allowNonZeroExit: true,
1212 });
1213 sparseCheckout = /--(?:\[no-\])?sparse\b/.test(`${help.stdout}${help.stderr}`);
1214 }
1215 if (headExists) {
1216 const reusedIndex = yield* Effect.gen(function* () {
1217 const userIndexPath =
1218 indexPath ??
1219 (yield* execute({
1220 operation,
1221 cwd: input.cwd,
1222 args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
1223 })).stdout.trim();
1224 const { mtime } = yield* fileSystem.stat(userIndexPath);
1225 if (Option.isNone(mtime)) return false;
1226 // Stay below the source timestamp even if Date rounded up, preserving Git's racy check.
1227 const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000);
1228 if (indexTime <= 0) return false;
1229 yield* fileSystem.copyFile(userIndexPath, tempIndexPath);
1230 yield* execute({
1231 operation,
1232 cwd: input.cwd,
1233 args: [...indexConfig, "read-tree", "--reset", "HEAD"],
1234 env: commitEnv,
1235 });
1236 // read-tree can rewrite the index, so restore its racy timestamp afterward.
1237 yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
1238 // Manually flagged entries hide edits from add -A, so they are re-added below
1239 // with no stat cache. Every other entry keeps its stat data and is not rehashed.
1240 let fallback = false;
1241 let flaggedBytes = 0;
1242 let recordStart = true;
1243 let collect: "flagged" | "skipped" | undefined;
1244 let record: Uint8Array[] = [];
1245 const flagged: string[] = [];
1246 const skipped: string[] = [];
1247 yield* vcsProcess.run({
1248 operation,
1249 command: "git",
1250 cwd: input.cwd,
1251 args: [...indexConfig, "ls-files", "--full-name", "--sparse", "--stage", "-v", "-z"],
1252 env: commitEnv,
1253 maxOutputBytes: 4_096,
1254 outputMode: "truncate",
1255 // Only tags and flagged or skip-worktree records matter; scan NUL-delimited records.
1256 onStdoutChunk: (chunk) => {
1257 let start = 0;
1258 while (start < chunk.length && !fallback) {
1259 const end = chunk.indexOf(0, start);
1260 if (recordStart) {
1261 const tag = chunk[start]!;
1262 record = [];
1263 // Sparse skip-worktree bits are expected; only in-cone ones are manual.
1264 collect =
1265 tag === 83 && sparseCheckout
1266 ? "skipped"
1267 : (tag >= 97 && tag <= 122) || tag === 83
1268 ? "flagged"
1269 : undefined;
1270 // index-info cannot keep skip-worktree while dropping assume-unchanged.
1271 if (tag === 115 && sparseCheckout) fallback = true;
1272 }
1273 if (collect !== undefined && !fallback) {
1274 const piece = chunk.subarray(start, end === -1 ? undefined : end);
1275 record.push(Buffer.from(piece));
1276 if (
1277 collect === "flagged" &&
1278 (flaggedBytes += piece.length) > FLAGGED_INDEX_ENTRIES_MAX_BYTES
1279 )
1280 fallback = true;
1281 if (end !== -1 && !fallback) {
1282 const full = Buffer.concat(record);
1283 record = [];
1284 // Sparse directory entries stay collapsed.
1285 if (collect === "flagged" || full.at(-1) !== 47) {
1286 // Drop the "<tag> " prefix, leaving index-info's "<mode> <oid> <stage>\t<path>".
1287 const entry = full.subarray(2);
1288 if (!NodeBuffer.isUtf8(entry)) fallback = true;
1289 else
1290 (collect === "flagged" ? flagged : skipped).push(entry.toString("utf8"));
1291 }
1292 }
1293 }
1294 recordStart = end !== -1;
1295 if (!recordStart) break;
1296 start = end + 1;
1297 }
1298 },
1299 });
1300 if (fallback) return false;
1301 if (skipped.length > 0) {
1302 const paths = skipped.map((entry) => entry.slice(entry.indexOf("\t") + 1));
1303 const stdin = paths.join("\0") + "\0";
1304 // check-rules echoes a subset of its input, so the input size bounds its output.
1305 const rules = yield* execute({
1306 operation,
1307 cwd: input.cwd,
1308 args: [...indexConfig, "sparse-checkout", "check-rules", "-z"],
1309 stdin,
1310 env: commitEnv,
1311 maxOutputBytes: Buffer.byteLength(stdin),
1312 outputMode: "error",
1313 });
1314 const selected = new Set(rules.stdout.split("\0"));
1315 skipped.forEach((entry, index) => {
1316 if (selected.has(paths[index]!)) flagged.push(entry);
1317 });
1318 }
1319 if (flagged.length > 0) {
1320 // Flagged gitlinks keep the fresh-index behaviour rather than a guessed one.
1321 if (flagged.some((entry) => entry.startsWith("160000 "))) return false;
1322 yield* execute({
1323 operation,
1324 cwd: input.cwd,
1325 // core.ignorestat would mark these assume-unchanged, hiding edits from add -A.
1326 args: [
1327 ...indexConfig,
1328 "-c",
1329 "core.ignorestat=false",
1330 "update-index",
1331 "-z",
1332 "--index-info",
1333 ],
1334 stdin: flagged.join("\0") + "\0",
1335 env: commitEnv,
1336 });
1337 yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
1338 }
1339 return true;
1340 }).pipe(Effect.orElseSucceed(() => false));
1341 if (!reusedIndex) {
1342 if (sparseCheckout) {
1343 const cone = yield* execute({
1344 operation,
1345 cwd: input.cwd,
1346 args: ["config", "--bool", "core.sparseCheckoutCone"],
1347 allowNonZeroExit: true,
1348 });
1349 // Rebuilding a non-cone index loses exclusions; do not publish false deletions.
1350 if (cone.stdout.trim() !== "true") {
1351 return yield* new VcsProcessExitError({
1352 operation,
1353 command: "git read-tree",
1354 cwd: input.cwd,
1355 exitCode: 1,
1356 detail: "Cannot rebuild a checkpoint index for non-cone sparse checkout.",
1357 });
1358 }
1359 }
1360 yield* cleanupTempIndex;
1361 yield* execute({
1362 operation,
1363 cwd: input.cwd,
1364 args: sparseCheckout
1365 ? [...indexConfig, "-c", "index.sparse=true", "read-tree", "--reset", "HEAD"]
1366 : [...indexConfig, "read-tree", "HEAD"],
1367 env: commitEnv,
1368 });
1369 }
1370 }
1371
1372 // The working-tree root is never ignored. Probe a subdirectory without the trailing slash
1373 // Git adds to ".", which a blank or whitespace-only ignore line matches as an empty pattern.
1374 const workspaceIgnored =
1375 prefix !== "" &&
1376 (yield* filterIgnoredPaths(input.cwd, [`../${prefix.slice(0, -1).split("/").at(-1)}`]))
1377 .length === 0;
1378 if (workspaceIgnored) {
1379 // HEAD omits staged-only paths. Seed just this workspace's additions, keeping
1380 // unrelated staged changes and the scratch index's sparse flags untouched.
1381 const additions = yield* execute({
1382 operation,
1383 cwd: input.cwd,
1384 args: [
1385 "-c",
1386 "core.quotePath=true",
1387 "diff",
1388 "--cached",
1389 "--raw",
1390 "--no-abbrev",
1391 "--no-renames",
1392 "--no-relative",
1393 "--ignore-submodules=none",
1394 "--no-color",
1395 "--no-ext-diff",
1396 "--no-textconv",
1397 "--ita-visible-in-index",
1398 "--diff-filter=A",
1399 "--",
1400 ".",
1401 ],
1402 maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
1403 outputMode: "error",
1404 });
1405 if (additions.stdout.length > 0) {
1406 // Git's quoted paths round-trip through index-info, including non-UTF-8 names.
1407 yield* execute({
1408 operation,
1409 cwd: input.cwd,
1410 args: [...indexConfig, "-c", "index.sparse=false", "update-index", "--index-info"],
1411 stdin: additions.stdout.replace(
1412 /^:000000 ([0-7]{6}) 0+ ([0-9a-f]+) A\t/gm,
1413 "$1 $2\t",
1414 ),
1415 env: commitEnv,
1416 });
1417 }
1418 const tracked = yield* execute({
1419 operation,
1420 cwd: input.cwd,
1421 args: [...indexConfig, "ls-files", "--cached", "-z", "--", "."],
1422 env: commitEnv,
1423 maxOutputBytes: 1,
1424 outputMode: "truncate",
1425 });
1426 if (tracked.stdout.length === 0) {
1427 return yield* new VcsUnsupportedOperationError({
1428 operation,
1429 kind: "git",
1430 detail: "The workspace directory is ignored by its Git repository.",
1431 });
1432 }
1433 }
1434
1435 // Untracked files have no stat data in a new index, so git add would rehash every one.
1436 const untracked =
1437 topLevel === undefined || head === undefined || sparseCheckout || workspaceIgnored
1438 ? undefined
1439 : { cwd: input.cwd, topLevel, head, env: commitEnv };
1440 const { reused, before } =
1441 untracked === undefined
1442 ? CheckpointUntrackedBlobs.NOTHING
1443 : yield* untrackedBlobs.reuse({ ...untracked, indexConfig });
1444
1445 const stageFiles = (exclusions: ReadonlyArray<string>, ignoreErrors = false) =>
1446 execute({
1447 operation,
1448 cwd: input.cwd,
1449 args: [
1450 ...indexConfig,
1451 ...durableWrite,
1452 ...longPaths,
1453 // safecrlf=true refuses LF files under CRLF conversion; capture them as Git's default does.
1454 "-c",
1455 "core.safecrlf=false",
1456 "add",
1457 // Unreadable files keep their indexed version and unreadable new files are left out.
1458 ...(ignoreErrors ? ["--ignore-errors"] : []),
1459 ...(sparseCheckout ? ["--sparse"] : []),
1460 // add -A rejects ignored directories even when they contain tracked files.
1461 workspaceIgnored ? "-u" : "-A",
1462 "--",
1463 ".",
1464 ...exclusions,
1465 ],
1466 env: commitEnv,
1467 }).pipe(
1468 // With --ignore-errors, exit 1 only reports the files it skipped.
1469 Effect.catchIf(
1470 (error) =>
1471 ignoreErrors && error._tag === "VcsProcessExitError" && error.exitCode === 1,
1472 () => Effect.void,
1473 ),
1474 );
1475 const stagingAt = yield* Clock.currentTimeMillis;
1476 yield* stageFiles([]).pipe(
1477 Effect.catchTags({
1478 VcsProcessExitError: (error) =>
1479 Effect.gen(function* () {
1480 // Git cannot stage uncommitted embedded repos; scan for them only after staging fails.
1481 const untracked = yield* execute({
1482 operation,
1483 cwd: input.cwd,
1484 args: [
1485 ...longPaths,
1486 "ls-files",
1487 "--others",
1488 "--exclude-standard",
1489 "-z",
1490 "--",
1491 ".",
1492 ],
1493 env: commitEnv,
1494 maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
1495 });
1496 if (untracked.stdoutTruncated) return yield* error;
1497 const candidates = splitNullSeparatedGitStdoutPaths(untracked).filter((entry) =>
1498 entry.endsWith("/"),
1499 );
1500 if (candidates.length > CHECKPOINT_RECOVERY_MAX_CANDIDATES) return yield* error;
1501 const nestedRepoEnv: NodeJS.ProcessEnv = {
1502 ...hostEnv,
1503 GIT_DIR: undefined,
1504 GIT_WORK_TREE: undefined,
1505 GIT_COMMON_DIR: undefined,
1506 GIT_INDEX_FILE: undefined,
1507 GIT_OBJECT_DIRECTORY: undefined,
1508 GIT_ALTERNATE_OBJECT_DIRECTORIES: undefined,
1509 GIT_QUARANTINE_PATH: undefined,
1510 };
1511 const exclusions: Array<string> = [];
1512 for (const entry of candidates) {
1513 const nestedCwd = path.join(input.cwd, entry);
1514 if (
1515 (yield* fileSystem
1516 .exists(path.join(nestedCwd, ".git"))
1517 .pipe(Effect.mapError(() => error))) &&
1518 !(yield* hasHeadCommit(nestedCwd, nestedRepoEnv))
1519 ) {
1520 exclusions.push(`:(exclude,literal)${entry}`);
1521 }
1522 }
1523 return yield* stageFiles(exclusions, true);
1524 }).pipe(
1525 Effect.timeoutOrElse({
1526 duration: CHECKPOINT_RECOVERY_TIMEOUT,
1527 orElse: () => Effect.fail(error),
1528 }),
1529 ),
1530 }),
1531 );
1532
1533 const stagingMs = (yield* Clock.currentTimeMillis) - stagingAt;
1534
1535 const writeTreeResult = yield* execute({
1536 operation,
1537 cwd: input.cwd,
1538 args: [...indexConfig, ...durableWrite, "write-tree"],
1539 env: commitEnv,
1540 });
1541 const treeOid = writeTreeResult.stdout.trim();
1542 if (treeOid.length === 0) {
1543 return yield* new VcsProcessExitError({
1544 operation,
1545 command: "git write-tree",
1546 cwd: input.cwd,
1547 exitCode: 0,
1548 detail: "git write-tree returned an empty tree oid.",
1549 });
1550 }
1551
1552 // Switching branches rewrites files the turn never wrote, and only HEAD's reflog tells a
1553 // switch from a reset. The trailer marks where that reflog stood for the next checkpoint.
1554 const turnStart =
1555 headMark === undefined || input.previousCheckpointRef === undefined
1556 ? null
1557 : yield* Effect.gen(function* () {
1558 const previous = yield* execute({
1559 operation,
1560 cwd: input.cwd,
1561 args: [
1562 "-c",
1563 "log.showSignature=false",
1564 "log",
1565 "--no-walk",
1566 "--format=%H %T %ct %P%n%B",
1567 `${input.previousCheckpointRef}^{commit}`,
1568 ],
1569 allowNonZeroExit: true,
1570 });
1571 if (previous.exitCode !== 0) return null;
1572 const [commit = "", tree = "", capturedAt = "", previousHead = ""] = previous.stdout
1573 .slice(0, previous.stdout.indexOf("\n"))
1574 .split(" ");
1575 const [, markedAt, markedCount] =
1576 HEAD_MARK_TRAILER_PATTERN.exec(previous.stdout) ?? [];
1577 const oldestMarked = headMoves.findLastIndex((move) => move.at === markedAt);
1578 // -1 when the scan ended inside the marked second, so the count cannot be placed.
1579 const since =
1580 oldestMarked === HEAD_MOVES_SCAN_LIMIT - 1
1581 ? -1
1582 : oldestMarked - Number(markedCount) + 1;
1583 if (headMoves[since]?.to !== previousHead) return null;
1584 const switches = headMoves
1585 .slice(0, since)
1586 .map((move, index) => ({ ...move, from: headMoves[index + 1]!.to }))
1587 .filter(
1588 (move) =>
1589 move.reason.startsWith("checkout: moving from ") && move.from !== move.to,
1590 )
1591 .toReversed();
1592 const landedOn = switches.at(-1)?.to;
1593 if (landedOn === undefined) return null;
1594
1595 yield* execute({
1596 operation,
1597 cwd: input.cwd,
1598 args: [...indexConfig, "-c", "index.sparse=false", "read-tree", tree],
1599 env: commitEnv,
1600 });
1601 for (const { from, to } of switches) {
1602 const changes = yield* execute({
1603 operation,
1604 cwd: input.cwd,
1605 args: [
1606 "-c",
1607 "core.quotePath=true",
1608 "diff-tree",
1609 "-r",
1610 "--no-renames",
1611 from,
1612 to,
1613 ],
1614 maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
1615 outputMode: "error",
1616 });
1617 // A switch succeeds only where the files it replaces were unmodified, so each
1618 // changed path takes the destination's version. Mode 000000 drops the path.
1619 const entries = changes.stdout
1620 .split("\n")
1621 // A switch leaves a submodule's own checkout where it was.
1622 .filter((line) => line !== "" && !/^:(?:160000 |[0-7]{6} 160000 )/.test(line))
1623 .map((line) =>
1624 line.replace(
1625 /^:[0-7]{6} ([0-7]{6}) [0-9a-f]+ ([0-9a-f]+) [A-Z]\t/,
1626 "$1 $2\t",
1627 ),
1628 );
1629 if (entries.length === 0) continue;
1630 yield* execute({
1631 operation,
1632 cwd: input.cwd,
1633 args: [
1634 ...indexConfig,
1635 "-c",
1636 "index.sparse=false",
1637 "update-index",
1638 "--index-info",
1639 ],
1640 stdin: `${entries.join("\n")}\n`,
1641 env: commitEnv,
1642 });
1643 }
1644 const startTree = (yield* execute({
1645 operation,
1646 cwd: input.cwd,
1647 args: [...indexConfig, ...durableWrite, "write-tree"],
1648 env: commitEnv,
1649 })).stdout.trim();
1650 if (startTree === "" || startTree === tree) return null;
1651 const startCommit = yield* execute({
1652 operation,
1653 cwd: input.cwd,
1654 args: [
1655 ...durableWrite,
1656 "commit-tree",
1657 startTree,
1658 "-p",
1659 landedOn,
1660 "-p",
1661 commit,
1662 "-m",
1663 `t3 checkpoint turn start ref=${input.checkpointRef}`,
1664 ],
1665 // listAuthoredPaths reads commits newer than the start as the turn's own.
1666 env: { ...commitEnv, GIT_COMMITTER_DATE: `${capturedAt} +0000` },
1667 });
1668 return startCommit.stdout.trim() || null;
1669 }).pipe(
1670 Effect.catch((cause) =>
1671 Effect.logWarning("checkpoint branch switch turn start failed", {
1672 cwd: input.cwd,
1673 checkpointRef: input.checkpointRef,
1674 cause: String(cause),
1675 }).pipe(Effect.as(null)),
1676 ),
1677 );
1678
1679 const message = `t3 checkpoint ref=${input.checkpointRef}${
1680 headMark === undefined ? "" : `\n\n${HEAD_MARK_TRAILER}: ${headMark}`
1681 }`;
1682 // HEAD is the first parent so listAuthoredPaths can see how HEAD moved between checkpoints.
1683 const commitTreeResult = yield* execute({
1684 operation,
1685 cwd: input.cwd,
1686 args: [
1687 ...durableWrite,
1688 "commit-tree",
1689 treeOid,
1690 ...(headExists ? ["-p", head ?? "HEAD"] : []),
1691 ...(turnStart === null ? [] : ["-p", turnStart]),
1692 "-m",
1693 message,
1694 ],
1695 env: commitEnv,
1696 });
1697 const commitOid = commitTreeResult.stdout.trim();
1698 if (commitOid.length === 0) {
1699 return yield* new VcsProcessExitError({
1700 operation,
1701 command: "git commit-tree",
1702 cwd: input.cwd,
1703 exitCode: 0,
1704 detail: "git commit-tree returned an empty commit oid.",
1705 });
1706 }
1707
1708 // Git writers have exited; bound publication by fanout directory.
1709 yield* fileSystem.readDirectory(privateObjects).pipe(
1710 Effect.flatMap((directories) =>
1711 Effect.forEach(
1712 directories.filter(
1713 (directory) => directory === "pack" || /^[0-9a-f]{2}$/.test(directory),
1714 ),
1715 (directory) =>
1716 Effect.gen(function* () {
1717 const source = path.join(privateObjects, directory);
1718 const destination = path.join(sharedObjects, directory);
1719 // Most fanouts already exist; avoid constructing an EEXIST error
1720 // for each of them on every capture. mkdir still handles the race.
1721 if (!(yield* fileSystem.exists(destination)))
1722 yield* fileSystem.makeDirectory(destination).pipe(
1723 Effect.andThen(
1724 Effect.gen(function* () {
1725 // Git has already applied core.sharedRepository to the source.
1726 // chmod bypasses umask, but never changes a pre-existing bucket.
1727 const { mode } = yield* fileSystem.stat(source);
1728 yield* fileSystem.chmod(destination, mode & 0o7777);
1729 }),
1730 ),
1731 Effect.catchIf(
1732 (cause) => cause.reason._tag === "AlreadyExists",
1733 () => Effect.void,
1734 ),
1735 Effect.uninterruptible,
1736 );
1737 const names = yield* fileSystem.readDirectory(source);
1738 // Per-object spans would swamp the capture span for large workspaces.
1739 const linkObject = Effect.fnUntraced(function* (name: string) {
1740 const from = path.join(source, name);
1741 const to = path.join(destination, name);
1742 yield* fileSystem.link(from, to).pipe(
1743 Effect.catchIf(
1744 (cause) =>
1745 cause.reason._tag === "PermissionDenied" ||
1746 cause.reason._tag === "Unknown",
1747 () =>
1748 Effect.gen(function* () {
1749 if (yield* fileSystem.exists(to)) return;
1750 // Git also falls back to rename without hardlinks; identical object names are interchangeable.
1751 yield* fileSystem.rename(from, to);
1752 }),
1753 ),
1754 Effect.catchIf(
1755 (cause) => cause.reason._tag === "AlreadyExists",
1756 () => Effect.void,
1757 ),
1758 );
1759 });
1760 if (directory !== "pack") {
1761 yield* Effect.forEach(
1762 names.filter((name) => /^(?:[0-9a-f]{38}|[0-9a-f]{62})$/.test(name)),
1763 linkObject,
1764 { discard: true },
1765 );
1766 return;
1767 }
1768 const files = new Set(names);
1769 for (const name of names) {
1770 if (!/^pack-(?:[0-9a-f]{40}|[0-9a-f]{64})\.pack$/.test(name)) continue;
1771 const base = name.slice(0, -5);
1772 if (!files.has(`${base}.idx`)) continue;
1773 // Link metadata before the pack so an I/O error cannot strand a
1774 // large unindexed pack. Defer cancellation across this short group
1775 // of links. Never roll back shared links: a concurrent writer may
1776 // already depend on them. Ref publication still happens last.
1777 yield* Effect.forEach(
1778 [".keep", ".promisor", ".rev", ".idx", ".pack"]
1779 .map((extension) => `${base}${extension}`)
1780 .filter((file) => files.has(file)),
1781 linkObject,
1782 { discard: true },
1783 ).pipe(Effect.uninterruptible);
1784 }
1785 }),
1786 { concurrency: 16, discard: true },
1787 ),
1788 ),
1789 Effect.mapError(
1790 (cause) =>
1791 new VcsCheckpointStorageError({ operation, cwd: input.cwd, stage: "publish", cause }),
1792 ),
1793 );
1794
1795 yield* execute({
1796 operation,
1797 cwd: input.cwd,
1798 args: [...durableWrite, "update-ref", input.checkpointRef, commitOid],
1799 });
1800 if (untracked !== undefined) {
1801 yield* untrackedBlobs.remember({
1802 ...untracked,
1803 tree: treeOid,
1804 startedAt,
1805 stagingMs,
1806 reused,
1807 before,
1808 });
1809 }
1810 }).pipe(Effect.ensuring(cleanupTempIndex));
1811
1812 // The tree records only each submodule's HEAD, so submodules keep their own ref.
1813 const hasSubmodules =
1814 topLevel === undefined ||
1815 (yield* fileSystem
1816 .exists(path.join(topLevel, ".gitmodules"))
1817 .pipe(Effect.orElseSucceed(() => true)));
1818 if (!hasSubmodules) return;
1819 // A submodule that cannot be captured drops its ref, so restore leaves it alone.
1820 yield* listSubmodules(input.cwd).pipe(
1821 Effect.flatMap((submodules) =>
1822 Effect.forEach(
1823 submodules,
1824 (cwd) =>
1825 checkpoints
1826 .captureCheckpoint({ cwd, checkpointRef: input.checkpointRef })
1827 .pipe(
1828 Effect.catch((cause) =>
1829 Effect.logWarning("submodule checkpoint capture failed", { cwd, cause }).pipe(
1830 Effect.andThen(
1831 checkpoints
1832 .deleteCheckpointRefs({ cwd, checkpointRefs: [input.checkpointRef] })
1833 .pipe(Effect.ignore),
1834 ),
1835 ),
1836 ),
1837 ),
1838 { discard: true },
1839 ),
1840 ),
1841 Effect.catch((cause) =>
1842 Effect.logWarning("submodule checkpoint capture failed", { cwd: input.cwd, cause }),
1843 ),
1844 );
1845 }, Effect.scoped),
1846
1847 hasCheckpointRef: (input) =>
1848 resolveCheckpointCommit(input.cwd, input.checkpointRef).pipe(
1849 Effect.map((commit) => commit !== null),
1850 ),
1851
1852 restoreCheckpoint: Effect.fn("GitVcsDriver.checkpoints.restoreCheckpoint")(function* (input) {
1853 const operation = "GitVcsDriver.checkpoints.restoreCheckpoint";
1854
1855 let commitOid = yield* resolveCheckpointCommit(input.cwd, input.checkpointRef);
1856
1857 if (!commitOid && input.fallbackToHead === true) {
1858 commitOid = yield* resolveHeadCommit(input.cwd);
1859 }
1860
1861 if (!commitOid) {
1862 return false;
1863 }
1864
1865 const tracked = yield* execute({
1866 operation,
1867 cwd: input.cwd,
1868 args: ["ls-files", "--cached", `--with-tree=${commitOid}`, "-z", "--", "."],
1869 });
1870 const sparseConfig =
1871 tracked.stdout.length > 0
1872 ? yield* execute({
1873 operation,
1874 cwd: input.cwd,
1875 args: ["config", "--bool", "core.sparseCheckout"],
1876 allowNonZeroExit: true,
1877 })
1878 : null;
1879 const sparseCheckout = sparseConfig?.stdout.trim() === "true";
1880 // Restoring away the last tracked file can remove a nested workspace directory.
1881 const recreateWorkspace = fileSystem.makeDirectory(input.cwd, { recursive: true }).pipe(
1882 Effect.mapError(
1883 (cause) =>
1884 new VcsProcessExitError({
1885 operation,
1886 command: "git restore",
1887 cwd: input.cwd,
1888 exitCode: 0,
1889 detail: `Could not recreate the checkpoint workspace: ${cause.message}`,
1890 }),
1891 ),
1892 );
1893 const resetIndex = Effect.gen(function* () {
1894 // On Windows clean can remove './' and exit 0.
1895 yield* recreateWorkspace;
1896 yield* execute({
1897 operation,
1898 cwd: input.cwd,
1899 args: [...longPaths, "reset", "--quiet", "--", "."],
1900 });
1901 });
1902 let mutationStarted = false;
1903 const onSpawn = () => {
1904 mutationStarted = true;
1905 };
1906 // Queued commands preserve user staging; spawned children stop before this scope resets it.
1907 yield* Effect.addFinalizer((exit) =>
1908 mutationStarted && Exit.hasInterrupts(exit) ? resetIndex.pipe(Effect.ignore) : Effect.void,
1909 );
1910 // An empty index and checkpoint have nothing for git restore's pathspec to match.
1911 if (tracked.stdout.length > 0) {
1912 const restore = [...longPaths, "restore", "--source", commitOid, "--worktree", "--staged"];
1913 // restore leaves skip-worktree paths alone. Outside a sparse checkout every such bit was
1914 // set by hand and capture recorded the file, so override them all.
1915 yield* execute({
1916 operation,
1917 cwd: input.cwd,
1918 args: [...restore, ...(sparseCheckout ? [] : ["--ignore-skip-worktree-bits"]), "--", "."],
1919 onSpawn,
1920 });
1921 if (sparseCheckout) {
1922 // Sparse exclusions stay excluded; only skip-worktree paths the rules select are manual.
1923 const manual = yield* Effect.gen(function* () {
1924 const skipped: string[] = [];
1925 let recordStart = true;
1926 let keep = false;
1927 let record: Uint8Array[] = [];
1928 yield* vcsProcess.run({
1929 operation,
1930 command: "git",
1931 cwd: input.cwd,
1932 args: ["ls-files", "--full-name", "--sparse", "-v", "-z", "--", "."],
1933 maxOutputBytes: 4_096,
1934 outputMode: "truncate",
1935 // The index can be huge; keep only the "S "/"s " records of the NUL-delimited list.
1936 onStdoutChunk: (chunk) => {
1937 let start = 0;
1938 while (start < chunk.length) {
1939 const end = chunk.indexOf(0, start);
1940 if (recordStart) {
1941 keep = chunk[start] === 83 || chunk[start] === 115;
1942 record = [];
1943 }
1944 if (keep) {
1945 record.push(Buffer.from(chunk.subarray(start, end === -1 ? undefined : end)));
1946 if (end !== -1) {
1947 const entry = Buffer.concat(record).subarray(2);
1948 record = [];
1949 // Sparse directory entries are exclusions; stdin cannot carry non-UTF-8.
1950 if (entry.at(-1) !== 47 && NodeBuffer.isUtf8(entry))
1951 skipped.push(entry.toString("utf8"));
1952 }
1953 }
1954 recordStart = end !== -1;
1955 if (!recordStart) break;
1956 start = end + 1;
1957 }
1958 },
1959 });
1960 if (skipped.length === 0) return skipped;
1961 const stdin = skipped.join("\0") + "\0";
1962 // check-rules echoes a subset of its input, so the input size bounds its output.
1963 const rules = yield* execute({
1964 operation,
1965 cwd: input.cwd,
1966 args: ["sparse-checkout", "check-rules", "-z"],
1967 stdin,
1968 maxOutputBytes: Buffer.byteLength(stdin),
1969 outputMode: "error",
1970 });
1971 return rules.stdout.split("\0").filter((entry) => entry.length > 0);
1972 }).pipe(
1973 // A Git without ls-files --sparse or check-rules keeps the plain restore's result.
1974 Effect.orElseSucceed((): string[] => []),
1975 );
1976 if (manual.length > 0) {
1977 yield* execute({
1978 operation,
1979 cwd: input.cwd,
1980 args: [
1981 ...restore,
1982 "--ignore-skip-worktree-bits",
1983 "--pathspec-from-file=-",
1984 "--pathspec-file-nul",
1985 ],
1986 stdin: manual.map((entry) => `:(top,literal)${entry}\0`).join(""),
1987 });
1988 }
1989 }
1990 }
1991 yield* recreateWorkspace;
1992 const clean = execute({
1993 operation,
1994 cwd: input.cwd,
1995 args: [...longPaths, "clean", "-fd", "--", "."],
1996 env: { LC_ALL: "C" },
1997 allowNonZeroExit: true,
1998 onSpawn,
1999 });
2000 let cleaned = yield* clean;
2001 // Removing a new .gitignore exposes the files it hid; clean again until nothing is left.
2002 for (
2003 let pass = 1;
2004 pass < 4 && cleaned.exitCode === 0 && /^Removing /m.test(cleaned.stdout);
2005 pass++
2006 ) {
2007 yield* recreateWorkspace;
2008 cleaned = yield* clean;
2009 }
2010 if (cleaned.exitCode === 0 && /^Removing /m.test(cleaned.stdout)) {
2011 yield* recreateWorkspace;
2012 const remaining = yield* execute({
2013 operation,
2014 cwd: input.cwd,
2015 args: [...longPaths, "clean", "-dn", "--", "."],
2016 env: { LC_ALL: "C" },
2017 });
2018 if (/^Would remove /m.test(remaining.stdout)) {
2019 yield* resetIndex;
2020 return yield* new VcsProcessExitError({
2021 operation,
2022 command: "git clean",
2023 cwd: input.cwd,
2024 exitCode: 0,
2025 detail: "Could not clean the checkpoint workspace: untracked files remain.",
2026 });
2027 }
2028 }
2029 if (cleaned.exitCode !== 0) {
2030 // Git can remove every child, then fail trying to remove './' itself.
2031 const emptiedWorkspace =
2032 cleaned.exitCode === 1 &&
2033 /^warning: failed to remove \.\/: [^\n]+$/.test(cleaned.stderr.trim()) &&
2034 (yield* fileSystem.readDirectory(input.cwd).pipe(
2035 Effect.map((entries) => entries.length === 0),
2036 Effect.orElseSucceed(() => false),
2037 ));
2038 if (!emptiedWorkspace) {
2039 yield* resetIndex;
2040 return yield* new VcsProcessExitError({
2041 operation,
2042 command: "git clean",
2043 cwd: input.cwd,
2044 exitCode: cleaned.exitCode,
2045 detail: cleaned.stderr.trim() || "Could not clean the checkpoint workspace.",
2046 });
2047 }
2048 }
2049 yield* resetIndex;
2050
2051 // A submodule without this ref was not captured, and restoring it to HEAD would lose edits.
2052 // The root is already restored, so a failing submodule must not hide that from the caller.
2053 yield* listSubmodules(input.cwd).pipe(
2054 Effect.flatMap((submodules) =>
2055 Effect.forEach(
2056 submodules,
2057 (cwd) =>
2058 checkpoints
2059 .restoreCheckpoint({
2060 cwd,
2061 checkpointRef: input.checkpointRef,
2062 fallbackToHead: false,
2063 })
2064 .pipe(
2065 Effect.catch((cause) =>
2066 Effect.logWarning("submodule checkpoint restore failed", { cwd, cause }),
2067 ),
2068 ),
2069 { discard: true },
2070 ),
2071 ),
2072 Effect.catch((cause) =>
2073 Effect.logWarning("submodule checkpoint restore failed", { cwd: input.cwd, cause }),
2074 ),
2075 );
2076
2077 return true;
2078 }, Effect.scoped),
2079
2080 diffCheckpoints: Effect.fn("GitVcsDriver.checkpoints.diffCheckpoints")(function* (input) {
2081 const operation = "GitVcsDriver.checkpoints.diffCheckpoints";
2082 if (input.filePaths?.length === 0) return "";
2083 yield* Effect.annotateCurrentSpan({
2084 "checkpoint.cwd": input.cwd,
2085 "checkpoint.from_ref": input.fromCheckpointRef,
2086 "checkpoint.to_ref": input.toCheckpointRef,
2087 "checkpoint.ignore_whitespace": input.ignoreWhitespace,
2088 "checkpoint.format": input.format ?? "patch",
2089 "checkpoint.fallback_from_to_head": input.fallbackFromToHead,
2090 });
2091
2092 let fromRevision = yield* resolveTurnStart(input);
2093 if (input.fallbackFromToHead === true) {
2094 const resolvedFromCommit = yield* resolveCheckpointCommit(
2095 input.cwd,
2096 input.fromCheckpointRef,
2097 );
2098 if (!resolvedFromCommit) {
2099 const headCommit = yield* resolveHeadCommit(input.cwd);
2100 if (!headCommit) {
2101 return yield* new VcsProcessExitError({
2102 operation,
2103 command: "git diff",
2104 cwd: input.cwd,
2105 exitCode: 1,
2106 detail: "Checkpoint ref is unavailable for diff operation.",
2107 });
2108 }
2109 fromRevision = headCommit;
2110 }
2111 }
2112
2113 const result = yield* execute({
2114 operation,
2115 cwd: input.cwd,
2116 args: [
2117 // Not --no-relative, which Git before 2.28 rejects.
2118 "-c",
2119 "diff.relative=false",
2120 "-c",
2121 "diff.suppressBlankEmpty=false",
2122 "diff",
2123 ...(input.format === "numstat" ? ["--numstat", "-z"] : ["--patch"]),
2124 "--no-color",
2125 "--no-ext-diff",
2126 "--no-textconv",
2127 "--submodule=short",
2128 ...PATCH_RENDER_PREFIX_ARGS,
2129 ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []),
2130 `${fromRevision}^{commit}`,
2131 `${input.toCheckpointRef}^{commit}`,
2132 // Paths are repository-relative, while cwd can be a subdirectory.
2133 ...(input.filePaths
2134 ? ["--", ...input.filePaths.map((file) => `:(top,literal)${file}`)]
2135 : []),
2136 ],
2137 allowNonZeroExit: true,
2138 maxOutputBytes: input.maxOutputBytes ?? VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
2139 outputMode: input.format === "numstat" ? "error" : "truncate",
2140 });
2141
2142 if (result.exitCode !== 0) {
2143 return yield* new VcsProcessExitError({
2144 operation,
2145 command: "git diff",
2146 cwd: input.cwd,
2147 exitCode: result.exitCode,
2148 detail: result.stderr.trim() || "Checkpoint ref is unavailable for diff operation.",
2149 });
2150 }
2151
2152 return result.stdout;
2153 }),
2154
2155 listAuthoredPaths: Effect.fn("GitVcsDriver.checkpoints.listAuthoredPaths")(function* (input) {
2156 const operation = "GitVcsDriver.checkpoints.listAuthoredPaths";
2157 const turnStart = yield* resolveTurnStart(input);
2158 const heads = yield* execute({
2159 operation,
2160 cwd: input.cwd,
2161 args: [
2162 "log",
2163 "--no-walk=unsorted",
2164 "--format=%ct %P",
2165 `${turnStart}^{commit}`,
2166 `${input.toCheckpointRef}^{commit}`,
2167 ],
2168 });
2169 const [fromLine = "", toLine = ""] = heads.stdout.trimEnd().split("\n");
2170 const [capturedAt = "", startHead = ""] = fromLine.split(" ");
2171 const [, endHead = ""] = toLine.split(" ");
2172 if (startHead === "" || endHead === "" || startHead === endHead) {
2173 return null;
2174 }
2175
2176 const listLines = (args: ReadonlyArray<string>, stdin?: string, timeoutMs?: number) =>
2177 execute({
2178 operation,
2179 cwd: input.cwd,
2180 // log.showSignature prints verification text into the output.
2181 args: ["-c", "log.showSignature=false", ...args],
2182 ...(stdin === undefined ? {} : { stdin }),
2183 ...(timeoutMs === undefined ? {} : { timeoutMs }),
2184 maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
2185 outputMode: "error",
2186 }).pipe(Effect.map((result) => result.stdout.split("\n").filter((line) => line !== "")));
2187 const listPaths = (args: ReadonlyArray<string>, stdin?: string) =>
2188 execute({
2189 operation,
2190 cwd: input.cwd,
2191 args: [
2192 "-c",
2193 "diff.relative=false",
2194 // log.showSignature prints verification text into the path list.
2195 "-c",
2196 "log.showSignature=false",
2197 ...args,
2198 "--name-only",
2199 "-z",
2200 "--no-renames",
2201 "--no-ext-diff",
2202 ],
2203 ...(stdin === undefined ? {} : { stdin }),
2204 maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
2205 outputMode: "error",
2206 }).pipe(Effect.map((result) => result.stdout.split("\0")));
2207
2208 // A commit is an import when a fetch delivered it since the previous checkpoint and no
2209 // reflog in any worktree shows this repository creating it. --all covers every worktree's
2210 // HEAD, and with --walk-reflogs --since compares each entry's own time.
2211 const listImportedCommits = Effect.gen(function* () {
2212 const entries = (yield* listLines([
2213 "log",
2214 "--walk-reflogs",
2215 "--all",
2216 `--since=@${capturedAt}`,
2217 "--format=%H %gD %gs",
2218 ])).map(parseCheckpointReflogEntry);
2219 // A push records commits this repository already had.
2220 const fetchedTips = new Set(
2221 entries
2222 .filter((entry) => entry.fetched && entry.subject !== "update by push")
2223 .map((entry) => entry.commit),
2224 );
2225 if (fetchedTips.size === 0) return fetchedTips;
2226 // Fetching another branch can deliver a commit that was made here and pushed.
2227 const createdHere = new Set(
2228 entries.filter(isLocalReflogCreation).map((entry) => entry.commit),
2229 );
2230 const fetched = yield* listLines(
2231 ["rev-list", `--since=@${capturedAt}`, "--stdin"],
2232 `${[...fetchedTips, `^${startHead}`].join("\n")}\n`,
2233 );
2234 return new Set(fetched.filter((commit) => !createdHere.has(commit)));
2235 });
2236
2237 // Remerge diffs retain merge conflict fixes while excluding clean upstream changes.
2238 // Older Git gets dense combined diffs, which miss a conflict resolved to one side's
2239 // version, so it also keeps every path more than one parent changed since the merge base.
2240 const remerge = yield* supportsRemergeDiff(input.cwd);
2241 const listMergeSharedPaths = (args: ReadonlyArray<string>, stdin?: string) =>
2242 listLines(["rev-list", "--merges", "--parents", ...args], stdin).pipe(
2243 Effect.flatMap((merges) =>
2244 Effect.forEach(
2245 merges,
2246 Effect.fnUntraced(function* (merge) {
2247 const parents = merge.split(" ").slice(1);
2248 const [base] = yield* listLines(["merge-base", "--octopus", ...parents]).pipe(
2249 // Unrelated histories have no merge base; the combined diff still applies.
2250 Effect.orElseSucceed((): ReadonlyArray<string> => []),
2251 );
2252 if (base === undefined) return [];
2253 const changed = yield* Effect.forEach(
2254 parents,
2255 (parent) => listPaths(["diff", base, parent]),
2256 { concurrency: "unbounded" },
2257 );
2258 const counts = new Map<string, number>();
2259 for (const path of changed.flatMap((paths) => [...new Set(paths)])) {
2260 if (path === "") continue;
2261 counts.set(path, (counts.get(path) ?? 0) + 1);
2262 }
2263 return [...counts].flatMap(([path, count]) => (count > 1 ? [path] : []));
2264 }),
2265 { concurrency: 4 },
2266 ),
2267 ),
2268 Effect.map((lists) => lists.flat()),
2269 );
2270 const listCommitPaths = (args: ReadonlyArray<string>, stdin?: string) =>
2271 remerge
2272 ? listPaths(["log", ...args, "--format=", "--diff-merges=remerge"], stdin)
2273 : Effect.all(
2274 [
2275 listPaths(["log", ...args, "--format=", "--cc"], stdin),
2276 listMergeSharedPaths(args, stdin),
2277 ],
2278 { concurrency: "unbounded" },
2279 ).pipe(Effect.map((lists) => lists.flat()));
2280 const [recentCommits, ...pathLists] = yield* Effect.all(
2281 [
2282 listLines(["rev-list", `--since=@${capturedAt}`, `${startHead}..${endHead}`]),
2283 listPaths(["diff", startHead, `${turnStart}^{commit}`]),
2284 listPaths(["diff", endHead, `${input.toCheckpointRef}^{commit}`]),
2285 listCommitPaths([`${endHead}..${startHead}`]),
2286 ],
2287 { concurrency: "unbounded" },
2288 );
2289 // Local creation evidence already overrides fetched history in the full scan.
2290 const unprovenCommits = new Set(recentCommits);
2291 if (recentCommits.length > 0 && recentCommits.length <= HEAD_MOVES_SCAN_LIMIT) {
2292 yield* listLines(
2293 [
2294 "log",
2295 "--walk-reflogs",
2296 `--max-count=${HEAD_MOVES_SCAN_LIMIT}`,
2297 "HEAD",
2298 `--since=@${capturedAt}`,
2299 "--format=%H %gD %gs",
2300 ],
2301 undefined,
2302 1_000,
2303 ).pipe(
2304 Effect.map((lines) => {
2305 for (const line of lines) {
2306 const entry = parseCheckpointReflogEntry(line);
2307 if (isLocalReflogCreation(entry)) unprovenCommits.delete(entry.commit);
2308 }
2309 }),
2310 Effect.catch((error) =>
2311 Effect.logDebug("local authorship probe failed; using full reflogs", {
2312 operation,
2313 errorTag: error._tag,
2314 }),
2315 ),
2316 );
2317 }
2318 // Without readable reflogs every recent commit stays attributed, as before.
2319 const importedCommits =
2320 unprovenCommits.size === 0
2321 ? new Set<string>()
2322 : yield* listImportedCommits.pipe(Effect.orElseSucceed(() => new Set<string>()));
2323 const ownCommits = recentCommits.filter((commit) => !importedCommits.has(commit));
2324 const ownCommitPaths =
2325 ownCommits.length === 0
2326 ? []
2327 : yield* listCommitPaths(["--no-walk=unsorted", "--stdin"], `${ownCommits.join("\n")}\n`);
2328 return new Set([...pathLists.flat(), ...ownCommitPaths].filter((path) => path.length > 0));
2329 }),
2330
2331 deleteCheckpointRefs: Effect.fn("GitVcsDriver.checkpoints.deleteCheckpointRefs")(
2332 function* (input) {
2333 yield* Effect.forEach(
2334 input.checkpointRefs,
2335 (checkpointRef) =>
2336 execute({
2337 operation: "GitVcsDriver.checkpoints.deleteCheckpointRefs",
2338 cwd: input.cwd,
2339 args: ["update-ref", "-d", checkpointRef],
2340 allowNonZeroExit: true,
2341 }),
2342 { discard: true },
2343 );
2344 const submodules = yield* listSubmodules(input.cwd).pipe(Effect.orElseSucceed(() => []));
2345 for (const cwd of submodules) {
2346 yield* checkpoints.deleteCheckpointRefs({ cwd, checkpointRefs: input.checkpointRefs });
2347 }
2348 },
2349 ),
2350 };
2351
2352 return {
2353 execute,
2354 checkpoints,
2355 detectRepository,
2356 isInsideWorkTree,
2357 listWorkspaceFiles,
2358 listRemotes,
2359 filterIgnoredPaths,
2360 initRepository,
2361 };
2362 });
2363
2364 export const makeVcsDriver = Effect.gen(function* () {
2365 const driver = yield* makeVcsDriverShape();
2366 return VcsDriver.VcsDriver.of(driver);
2367 });
2368
2369 export const make = Effect.gen(function* () {
2370 const git = yield* makeGitVcsDriverCore();
2371 return GitVcsDriver.of(git);
2372 });
2373
2374 export const layerVcs = Layer.effect(VcsDriver.VcsDriver, makeVcsDriver);
2375 export const layer = Layer.effect(GitVcsDriver, make).pipe(Layer.provide(CheckoutCloner.layer));