Skip to content

Commit 13c2608

Browse files
fix(server): handle manual sparse flags and older Git
1 parent c9f1511 commit 13c2608

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎apps/server/src/vcs/GitVcsDriver.test.ts‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ it.effect("checkpoint capture does not rerun clean filters for unchanged indexed
120120
);
121121

122122
for (const nested of [false, true]) {
123-
for (const indexState of ["sparse", "flags", "missing", "non-cone-missing"] as const) {
123+
for (const indexState of [
124+
"sparse",
125+
"flags",
126+
"manual-skip",
127+
"missing",
128+
"non-cone-missing",
129+
] as const) {
124130
it.effect(
125131
`sparse checkpoint preserves two captures (nested=${nested}, index=${indexState})`,
126132
() =>
@@ -137,7 +143,7 @@ for (const nested of [false, true]) {
137143
for (const name of [
138144
"scope/in/edit",
139145
"scope/in/delete",
140-
"scope/out/absent",
146+
"scope/out/deep/absent",
141147
"scope/out/present",
142148
"elsewhere/file",
143149
]) {
@@ -160,6 +166,8 @@ for (const nested of [false, true]) {
160166
yield* git(["add", "."]);
161167
if (indexState === "flags")
162168
yield* git(["update-index", "--assume-unchanged", "scope/in/delete"]);
169+
if (indexState === "manual-skip")
170+
yield* git(["update-index", "--skip-worktree", "scope/in/delete"]);
163171
yield* git(["config", "sparse.expectFilesOutsideOfPatterns", "true"]);
164172
yield* write("scope/in/edit", "working\n");
165173
yield* write("scope/out/present", "modified skipped\n");
@@ -193,7 +201,7 @@ for (const nested of [false, true]) {
193201
}
194202
yield* capture;
195203
for (const [name, content] of [
196-
["scope/out/absent", "original\n"],
204+
["scope/out/deep/absent", "original\n"],
197205
["scope/out/present", "modified skipped\n"],
198206
["scope/in/edit", turn === 1 ? "working\n" : "second\n"],
199207
["elsewhere/file", nested ? "original\n" : "working outside\n"],
@@ -211,7 +219,7 @@ for (const nested of [false, true]) {
211219
yield* fs.readFile(indexPath).pipe(Effect.orElseSucceed(() => null)),
212220
originalIndex,
213221
);
214-
assert.isFalse(yield* fs.exists(path.join(cwd, "scope/out/absent")));
222+
assert.isFalse(yield* fs.exists(path.join(cwd, "scope/out/deep/absent")));
215223
assert.strictEqual(
216224
yield* fs.readFileString(path.join(cwd, "elsewhere/file")),
217225
"working outside\n",
@@ -222,6 +230,46 @@ for (const nested of [false, true]) {
222230
}
223231
}
224232

233+
it.effect("checkpoint capture keeps the legacy path when Git lacks add --sparse", () =>
234+
Effect.gen(function* () {
235+
const fs = yield* FileSystem.FileSystem;
236+
const path = yield* Path.Path;
237+
const liveProcess = yield* VcsProcess.VcsProcess;
238+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
239+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-legacy-" });
240+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
241+
yield* git(["sparse-checkout", "set", "--cone", "included"]);
242+
const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
243+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
244+
Effect.provideService(VcsProcess.VcsProcess, {
245+
run: (input) => {
246+
if (input.args.includes("-h"))
247+
return Effect.succeed({
248+
exitCode: ChildProcessSpawner.ExitCode(129),
249+
stdout: "usage: git add",
250+
stderr: "",
251+
stdoutTruncated: false,
252+
stderrTruncated: false,
253+
});
254+
return liveProcess.run(
255+
input.args.includes("--sparse")
256+
? {
257+
...input,
258+
args: input.args.map((arg) =>
259+
arg === "--sparse" ? "--unsupported-sparse" : arg,
260+
),
261+
}
262+
: input,
263+
);
264+
},
265+
}),
266+
);
267+
yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
268+
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
269+
assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
270+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
271+
);
272+
225273
for (const indexMode of ["normal", "flags", "sparse"] as const) {
226274
it.effect(
227275
`checkpoint index inspection handles entries beyond the output cap (index=${indexMode})`,

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeCrypto from "node:crypto";
2+
import * as NodeBuffer from "node:buffer";
23

34
import * as Context from "effect/Context";
45
import * as DateTime from "effect/DateTime";
@@ -784,7 +785,16 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
784785
args: ["config", "--bool", "core.sparseCheckout"],
785786
allowNonZeroExit: true,
786787
});
787-
const sparseCheckout = sparseConfig.stdout.trim() === "true";
788+
let sparseCheckout = sparseConfig.stdout.trim() === "true";
789+
if (sparseCheckout) {
790+
const help = yield* execute({
791+
operation,
792+
cwd: input.cwd,
793+
args: ["add", "-h"],
794+
allowNonZeroExit: true,
795+
});
796+
sparseCheckout = /--(?:\[no-\])?sparse\b/.test(`${help.stdout}${help.stderr}`);
797+
}
788798
if (headExists) {
789799
const reusedIndex = yield* Effect.gen(function* () {
790800
const indexPath = yield* execute({
@@ -809,17 +819,32 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
809819
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
810820
let specialFlags = false;
811821
let recordStart = true;
822+
let skipped = false;
823+
let skippedRecord: number[] = [];
824+
const skippedPaths: string[] = [];
812825
yield* vcsProcess.run({
813826
operation,
814827
command: "git",
815828
cwd: input.cwd,
816-
args: [...indexConfig, "ls-files", "--sparse", "-v", "-z"],
829+
args: [...indexConfig, "ls-files", "--full-name", "--sparse", "-v", "-z"],
817830
env: commitEnv,
818831
maxOutputBytes: 4_096,
819832
outputMode: "truncate",
820-
// Inspect every tag without retaining paths or stopping at the output cap.
833+
// Inspect every tag; retain only skipped file paths for checking sparse rules.
821834
onStdoutChunk: (chunk) => {
822835
for (const byte of chunk) {
836+
if (recordStart) skipped = byte === 83;
837+
if (skipped && sparseCheckout) {
838+
if (byte !== 0) skippedRecord.push(byte);
839+
else {
840+
if (skippedRecord.at(-1) !== 47) {
841+
const name = Buffer.from(skippedRecord).subarray(2);
842+
if (!NodeBuffer.isUtf8(name)) specialFlags = true;
843+
else skippedPaths.push(name.toString("utf8"));
844+
}
845+
skippedRecord = [];
846+
}
847+
}
823848
if (
824849
recordStart &&
825850
((byte >= 97 && byte <= 122) || (!sparseCheckout && byte === 83))
@@ -830,6 +855,19 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
830855
}
831856
},
832857
});
858+
if (skippedPaths.length > 0 && !specialFlags) {
859+
const selected = yield* execute({
860+
operation,
861+
cwd: input.cwd,
862+
args: [...indexConfig, "sparse-checkout", "check-rules", "-z"],
863+
stdin: skippedPaths.join("\0") + "\0",
864+
env: commitEnv,
865+
maxOutputBytes: 1,
866+
outputMode: "truncate",
867+
});
868+
// Any selected skipped file has a manual flag, not a sparse exclusion.
869+
specialFlags = selected.stdout.length > 0 || selected.stdoutTruncated;
870+
}
833871
// Sparse Git clears skip-worktree for present files. Manual flags still need a reset.
834872
return !specialFlags;
835873
}).pipe(Effect.orElseSucceed(() => false));
@@ -857,7 +895,9 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
857895
operation,
858896
cwd: input.cwd,
859897
// A fresh sparse index represents excluded directories without marking them deleted.
860-
args: [...indexConfig, "-c", "index.sparse=true", "read-tree", "--reset", "HEAD"],
898+
args: sparseCheckout
899+
? [...indexConfig, "-c", "index.sparse=true", "read-tree", "--reset", "HEAD"]
900+
: ["read-tree", "HEAD"],
861901
env: commitEnv,
862902
});
863903
}

0 commit comments

Comments
 (0)