Skip to content

Commit 5d28843

Browse files
fix(server): commit-message preparation no longer writes objects into the repository (#34595)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 6a8f82c commit 5d28843

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5929,6 +5929,60 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
59295929
});
59305930

59315931
describe("commit context", () => {
5932+
it.effect("reads history through a chain of shared clones", () =>
5933+
Effect.gen(function* () {
5934+
const root = yield* makeTmpDir();
5935+
const fileSystem = yield* FileSystem.FileSystem;
5936+
const driver = yield* GitVcsDriver.GitVcsDriver;
5937+
const seed = `${root}/clone-0`;
5938+
yield* fileSystem.makeDirectory(seed);
5939+
yield* initRepoWithCommit(seed);
5940+
let previous = seed;
5941+
for (let index = 1; index <= 6; index += 1) {
5942+
const next = `${root}/clone-${index}`;
5943+
yield* git(root, ["clone", "--quiet", "--shared", previous, next]);
5944+
previous = next;
5945+
}
5946+
// Six shared clones deep is within Git's alternate limit, not one level deeper.
5947+
yield* writeTextFile(previous, "README.md", "changed\n");
5948+
const chained = yield* driver.prepareCommitContext(previous, ["README.md"]);
5949+
assert.include(chained?.stagedSummary ?? "", "M\tREADME.md");
5950+
assert.include(chained?.stagedPatch ?? "", "-# test");
5951+
}),
5952+
);
5953+
5954+
it.effect("leaves no objects behind when no commit follows, in a linked worktree too", () =>
5955+
Effect.gen(function* () {
5956+
const repo = yield* makeTmpDir();
5957+
yield* initRepoWithCommit(repo);
5958+
const linked = `${repo}/linked`;
5959+
yield* git(repo, ["worktree", "add", "--quiet", linked]);
5960+
const driver = yield* GitVcsDriver.GitVcsDriver;
5961+
5962+
for (const cwd of [repo, linked]) {
5963+
yield* writeTextFile(cwd, "README.md", `changed in ${cwd}\n`);
5964+
yield* writeTextFile(cwd, "draft.txt", `drafted in ${cwd}\n`);
5965+
const blobs = [
5966+
yield* git(cwd, ["hash-object", "draft.txt"]),
5967+
yield* git(cwd, ["hash-object", "README.md"]),
5968+
];
5969+
const context = yield* driver.prepareCommitContext(cwd);
5970+
assert.include(context?.stagedPatch ?? "", `+drafted in ${cwd}`);
5971+
assert.include(context?.stagedPatch ?? "", "-# test");
5972+
for (const blob of blobs) {
5973+
const probe = yield* driver.execute({
5974+
operation: "GitVcsDriver.test.git",
5975+
cwd,
5976+
args: ["cat-file", "-e", blob],
5977+
allowNonZeroExit: true,
5978+
timeoutMs: 10_000,
5979+
});
5980+
assert.notEqual(probe.exitCode, 0, `${blob} stayed in the repository`);
5981+
}
5982+
}
5983+
}),
5984+
);
5985+
59325986
it.effect("prepares source patches without running textconv", () =>
59335987
Effect.gen(function* () {
59345988
const cwd = yield* makeTmpDir();

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,17 +2526,45 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
25262526
Effect.fn("prepareCommitContext")(function* (cwd, filePaths, options) {
25272527
return yield* Effect.scoped(
25282528
Effect.gen(function* () {
2529-
const indexValue = yield* runGitStdout(
2530-
"GitVcsDriver.prepareCommitContext.indexPath",
2529+
const [indexValue = "", objectsValue = ""] = (yield* runGitStdout(
2530+
"GitVcsDriver.prepareCommitContext.gitPaths",
25312531
cwd,
2532-
["rev-parse", "--git-path", "index"],
2533-
);
2532+
["rev-parse", "--git-path", "index", "--git-path", "objects"],
2533+
)).split("\n");
25342534
const indexPath = path.resolve(cwd, indexValue.trim());
25352535
const directory = yield* fileSystem.makeTempDirectoryScoped({
25362536
prefix: "t3code-commit-index-",
25372537
});
25382538
const tempIndexPath = path.join(directory, "index");
2539-
const env = { GIT_INDEX_FILE: tempIndexPath } satisfies NodeJS.ProcessEnv;
2539+
// Staging writes blobs; put them in the temp directory so a draft leaves nothing behind.
2540+
// List every object directory Git resolved (nested and environment alternates) flat,
2541+
// since nesting the repository's objects one level deeper can pass Git's depth limit.
2542+
const objectDirectories = yield* runGitStdout(
2543+
"GitVcsDriver.prepareCommitContext.alternates",
2544+
cwd,
2545+
["count-objects", "-v"],
2546+
);
2547+
// Git prints an entry C-quoted when it needs quoting; quote the rest the same way, so a
2548+
// path holding the list separator stays one entry (Git 2.11.1+).
2549+
const quoted = (entry: string) =>
2550+
entry.startsWith('"') ? entry : `"${entry.replace(/[\\"]/g, "\\$&")}"`;
2551+
const alternates = [
2552+
path.resolve(cwd, objectsValue.trim()),
2553+
...objectDirectories
2554+
.split("\n")
2555+
.flatMap((line) =>
2556+
line.startsWith("alternate: ") ? [line.slice("alternate: ".length)] : [],
2557+
),
2558+
];
2559+
const tempObjectsPath = path.join(directory, "objects");
2560+
yield* fileSystem.makeDirectory(tempObjectsPath);
2561+
const env = {
2562+
GIT_INDEX_FILE: tempIndexPath,
2563+
GIT_OBJECT_DIRECTORY: tempObjectsPath,
2564+
GIT_ALTERNATE_OBJECT_DIRECTORIES: alternates
2565+
.map(quoted)
2566+
.join(hostPlatform === "win32" ? ";" : ":"),
2567+
} satisfies NodeJS.ProcessEnv;
25402568
const config = [
25412569
"-c",
25422570
"core.splitIndex=false",

0 commit comments

Comments
 (0)