Skip to content
TypeScript

fix(server): cap generated worktree folder names (#25212)

maria0ed554001a9b
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index b26f7d82a90..47b3f3a62b7 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -3488,6 +3488,42 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);
+ it.effect("keeps generated worktree folders short without changing long branch names", () =>
+ Effect.gen(function* () {
+ const pathService = yield* Path.Path;
+ const cwd = yield* makeTmpDir();
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const worktreesDirectory = yield* makeTmpDir("custom-worktrees-");
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ const folders = new Set<string>();
+ const branches = [
+ `feature/${"a".repeat(90)}/${"b".repeat(90)}/${"c".repeat(90)}`,
+ `feature/${"a".repeat(90)}/${"b".repeat(90)}/${"d".repeat(90)}`,
+ `feature/${"a".repeat(40)}/5fac`,
+ `feature/${"a".repeat(40)}/18828`,
+ `${"a".repeat(22)}πŸ˜€/${"b".repeat(30)}`,
+ "z".repeat(40),
+ "z".repeat(41),
+ ];
+ for (const branch of branches) {
+ const created = yield* driver.createWorktree(
+ { cwd, path: null, refName: initialBranch, newRefName: branch },
+ { worktreesDirectory },
+ );
+ const folder = pathService.basename(created.worktree.path);
+ assert.isAtMost(folder.length, 40);
+ if (branch.length <= 40) assert.equal(folder, branch);
+ assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
+ assert.include(
+ yield* git(cwd, ["-c", "core.quotePath=false", "worktree", "list", "--porcelain"]),
+ `worktree ${created.worktree.path}`,
+ );
+ folders.add(folder);
+ }
+ assert.equal(folders.size, branches.length);
+ }),
+ );
+
it.effect("resolves the submodule mode from the option, then t3.json", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index a7ba2b86771..49149d95f58 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -3811,7 +3811,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.refName);
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.newRefName);
const targetBranch = input.newRefName ?? input.refName;
- const sanitizedBranch = targetBranch.replace(/\//g, "-");
// Resolve sub/.. before taking the basename.
const repoName = path.basename(path.resolve(input.cwd));
let worktreePath = input.path;
@@ -3830,7 +3829,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings β†’ Worktrees.`,
});
}
- worktreePath = path.join(parentDir, repoName, sanitizedBranch);
+ let directoryName = targetBranch.replace(/\//g, "-");
+ // Leave room for files beneath the worktree on Windows, regardless of branch length.
+ if (directoryName.length > 40) {
+ const digest = yield* crypto
+ .digest("SHA-256", new TextEncoder().encode(targetBranch))
+ .pipe(Effect.orDie);
+ // Avoid splitting a surrogate pair, which Git would list back as a different path.
+ const prefix = directoryName.slice(0, 23).replace(/[\uD800-\uDBFF]$/, "");
+ directoryName = `${prefix}-${Hex.encode(digest).slice(0, 16)}`;
+ }
+ worktreePath = path.join(parentDir, repoName, directoryName);
}
if (input.newRefName) {
// Kept apart from the checkout so the shared-config lock only covers the tracking write.