# Files changed · PR \#23624 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/23624/diffs)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

[fix\(server\): checkpoints no longer rehash every file when one entry is skip-worktree or assume-unchanged](https://git.cafe/pingdotgg/t3code/pull/23624.md)

Version: 16

Head commit: cfee52fc24f56169ed4c0ce48759ad1ff9a7f655

Base commit: f054d1b8018721bd997d99cbb918a7b17bf098a4

Comparison base: f054d1b8018721bd997d99cbb918a7b17bf098a4

Showing up to 20 entries from this revision. If the pull request changes, reopen the first page before continuing.

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

Status: modified; +38; −8

```diff
@@ -594,7 +594,17 @@
     Effect.gen(function* () {
       const fs = yield* FileSystem.FileSystem;
       const path = yield* Path.Path;
+      const liveProcess = yield* VcsProcess.VcsProcess;
+      let freshReadTrees = 0;
-      const driver = yield* GitVcsDriver.makeVcsDriverShape();
+      const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
+        Effect.provideService(VcsProcess.VcsProcess, {
+          run: (input) => {
+            if (input.args.includes("read-tree") && input.args.includes("index.sparse=true"))
+              freshReadTrees++;
+            return liveProcess.run(input);
+          },
+        }),
+      );
       const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-sparse-" });
       const { git } = yield* makeCheckpointFixture(driver, cwd);
       const write = Effect.fn(function* (name: string, contents: string) {
@@ -677,10 +687,11 @@
           "working outside\n",
         );
       }
+      assert.strictEqual(freshReadTrees, indexState === "missing" ? 2 : 0);
     }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
 );
 
+it.effect.each(["normal", "skip-worktree", "assume-unchanged", "sparse"] as const)(
-it.effect.each(["normal", "flags", "sparse"] as const)(
   "checkpoint index inspection handles entries beyond the output cap (index=%s)",
   (indexMode) =>
     Effect.gen(function* () {
@@ -693,6 +704,7 @@
       yield* fs.writeFileString(path.join(cwd, ".gitattributes"), "stable filter=probe\n");
       yield* fs.writeFileString(path.join(cwd, "stable"), "unchanged\n");
       yield* fs.writeFileString(path.join(cwd, "z-skipped"), "original\n");
+      yield* fs.writeFileString(path.join(cwd, "z-deleted"), "original\n");
       yield* fs.makeDirectory(path.join(cwd, "excluded"));
       yield* fs.writeFileString(path.join(cwd, "excluded/file"), "absent\n");
       yield* fs.writeFileString(
@@ -700,19 +712,31 @@
         'require("node:fs").appendFileSync(".git/reads", "read\\n"); process.stdin.pipe(process.stdout);',
       );
       yield* git(["config", "filter.probe.clean", "node .git/filter.cjs"]);
+      yield* git(["config", "core.trustctime", "false"]);
       yield* fs.utimes(path.join(cwd, "stable"), 1_700_000_000, 1_700_000_000);
+      yield* fs.utimes(path.join(cwd, "z-skipped"), 1_700_000_000, 1_700_000_000);
       yield* git(["add", "."]);
       yield* git(["commit", "-m", "inspection fixture"]);
+      const flagged = indexMode === "skip-worktree" || indexMode === "assume-unchanged";
+      if (flagged) {
+        yield* git(["update-index", `--${indexMode}`, "z-skipped", "z-deleted"]);
-      if (indexMode === "flags") yield* git(["update-index", "--skip-worktree", "z-skipped"]);
+        yield* git(["config", "core.ignorestat", "true"]);
+        yield* fs.remove(path.join(cwd, "z-deleted"));
+      }
       if (indexMode === "sparse")
         yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "included"]);
       yield* fs.writeFileString(path.join(cwd, "z-skipped"), "modified\n");
+      // A flagged edit with the indexed size and mtime is invisible to Git's stat check.
+      if (flagged) yield* fs.utimes(path.join(cwd, "z-skipped"), 1_700_000_000, 1_700_000_000);
       yield* fs.writeFileString(path.join(cwd, ".git/reads"), "");
       const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
+      let freshReadTrees = 0;
       const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
         Effect.provideService(VcsProcess.VcsProcess, {
-          run: (input) =>
+          run: (input) => {
+            if (input.args.includes("read-tree") && !input.args.includes("--reset"))
-            liveProcess.run(
+              freshReadTrees++;
+            return liveProcess.run(
               input.args.includes("ls-files")
                 ? {
                     ...input,
@@ -723,13 +747,19 @@
                     },
                   }
                 : input,
+            );
-            ),
+          },
         }),
       );
       yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
       assert.strictEqual((yield* git(["show", `${checkpointRef}:z-skipped`])).stdout, "modified\n");
+      const files = (yield* git(["ls-tree", "-z", "--name-only", checkpointRef])).stdout.split(
+        "\0",
+      );
+      if (flagged) assert.notInclude(files, "z-deleted");
+      else assert.include(files, "z-deleted");
+      assert.strictEqual(freshReadTrees, 0);
+      assert.strictEqual(yield* fs.readFileString(path.join(cwd, ".git/reads")), "");
-      if (indexMode !== "flags")
-        assert.strictEqual(yield* fs.readFileString(path.join(cwd, ".git/reads")), "");
       assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
     }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
 );
```

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

Status: modified; +68; −30

```diff
@@ -421,6 +421,7 @@
 >()("t3/vcs/GitVcsDriver") {}
 
 const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
+const FLAGGED_INDEX_ENTRIES_MAX_BYTES = 1024 * 1024;
 const CHECKPOINT_RECOVERY_MAX_CANDIDATES = 64;
 const CHECKPOINT_RECOVERY_TIMEOUT = "5 seconds";
 const GIT_CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
@@ -898,57 +899,94 @@
             });
             // read-tree can rewrite the index, so restore its racy timestamp afterward.
             yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
+            // Manually flagged entries hide edits from add -A, so they are re-added below
+            // with no stat cache. Every other entry keeps its stat data and is not rehashed.
+            let fallback = false;
-            let specialFlags = false;
+            let flaggedBytes = 0;
             let recordStart = true;
+            let collect: "flagged" | "skipped" | undefined;
-            let skipped = false;
-            let skippedRecord: number[] = [];
-            const skippedPaths: string[] = [];
+            let record: number[] = [];
+            const flagged: string[] = [];
+            const skipped: string[] = [];
             yield* vcsProcess.run({
               operation,
               command: "git",
               cwd: input.cwd,
-              args: [...indexConfig, "ls-files", "--full-name", "--sparse", "-v", "-z"],
+              args: [...indexConfig, "ls-files", "--full-name", "--sparse", "--stage", "-v", "-z"],
               env: commitEnv,
               maxOutputBytes: 4_096,
               outputMode: "truncate",
               onStdoutChunk: (chunk) => {
                 for (const byte of chunk) {
+                  if (recordStart) {
+                    record = [];
+                    // Sparse skip-worktree bits are expected; only in-cone ones are manual.
+                    collect =
+                      byte === 83 && sparseCheckout
+                        ? "skipped"
+                        : (byte >= 97 && byte <= 122) || byte === 83
+                          ? "flagged"
+                          : undefined;
+                    // index-info cannot keep skip-worktree while dropping assume-unchanged.
-                  if (recordStart) skipped = byte === 83;
-                  if (skipped && sparseCheckout) {
-                    if (byte !== 0) skippedRecord.push(byte);
-                    else {
-                      if (skippedRecord.at(-1) !== 47) {
-                        const name = Buffer.from(skippedRecord).subarray(2);
-                        if (!NodeBuffer.isUtf8(name)) specialFlags = true;
-                        else skippedPaths.push(name.toString("utf8"));
-                      }
-                      skippedRecord = [];
-                    }
-                  }
-                  if (
-                    recordStart &&
-                    ((byte >= 97 && byte <= 122) || (!sparseCheckout && byte === 83))
-                  ) {
+                    if (byte === 115 && sparseCheckout) fallback = true;
-                    specialFlags = true;
                   }
                   recordStart = byte === 0;
+                  if (fallback || collect === undefined) continue;
+                  if (byte !== 0) {
+                    record.push(byte);
+                    if (collect === "flagged" && ++flaggedBytes > FLAGGED_INDEX_ENTRIES_MAX_BYTES)
+                      fallback = true;
+                    continue;
+                  }
+                  // Sparse directory entries stay collapsed.
+                  if (collect === "skipped" && record.at(-1) === 47) continue;
+                  // Drop the "<tag> " prefix, leaving index-info's "<mode> <oid> <stage>\t<path>".
+                  const entry = Buffer.from(record).subarray(2);
+                  if (!NodeBuffer.isUtf8(entry)) fallback = true;
+                  else (collect === "flagged" ? flagged : skipped).push(entry.toString("utf8"));
                 }
               },
             });
+            if (fallback) return false;
+            if (skipped.length > 0) {
+              const paths = skipped.map((entry) => entry.slice(entry.indexOf("\t") + 1));
+              const stdin = paths.join("\0") + "\0";
+              // check-rules echoes a subset of its input, so the input size bounds its output.
+              const rules = yield* execute({
-            if (skippedPaths.length > 0 && !specialFlags) {
-              const selected = yield* execute({
                 operation,
                 cwd: input.cwd,
                 args: [...indexConfig, "sparse-checkout", "check-rules", "-z"],
+                stdin,
+                env: commitEnv,
+                maxOutputBytes: Buffer.byteLength(stdin),
+                outputMode: "error",
+              });
+              const selected = new Set(rules.stdout.split("\0"));
+              skipped.forEach((entry, index) => {
+                if (selected.has(paths[index]!)) flagged.push(entry);
+              });
+            }
+            if (flagged.length > 0) {
+              // Flagged gitlinks keep the fresh-index behaviour rather than a guessed one.
-                stdin: skippedPaths.join("\0") + "\0",
+              if (flagged.some((entry) => entry.startsWith("160000 "))) return false;
+              yield* execute({
+                operation,
+                cwd: input.cwd,
+                // core.ignorestat would mark these assume-unchanged, hiding edits from add -A.
+                args: [
+                  ...indexConfig,
+                  "-c",
+                  "core.ignorestat=false",
+                  "update-index",
+                  "-z",
+                  "--index-info",
+                ],
+                stdin: flagged.join("\0") + "\0",
                 env: commitEnv,
-                maxOutputBytes: 1,
-                outputMode: "truncate",
               });
+              yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
-              specialFlags = selected.stdout.length > 0 || selected.stdoutTruncated;
             }
-            // Sparse Git clears skip-worktree for present files. Manual flags still need a reset.
+            return true;
-            return !specialFlags;
           }).pipe(Effect.orElseSucceed(() => false));
           if (!reusedIndex) {
             if (sparseCheckout) {
```
