Skip to content

Commit e7ac6bf

Browse files
only21milclaude
andcommitted
fix(server): generated worktree folders are valid on Windows
Git accepts branch names such as `fix/a|b`, `con` and `aux.c`, but the generated worktree folder reused them as-is, which Windows cannot create. Replace `<>"|` and Windows device-name stems in the generated folder, and add the branch digest whenever anything changed so distinct branches keep distinct folders. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1d626d0 commit e7ac6bf

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,22 +4230,30 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
42304230
}),
42314231
);
42324232

4233-
it.effect("keeps generated worktree folders short without changing long branch names", () =>
4233+
it.effect("keeps generated worktree folders short and valid on Windows", () =>
42344234
Effect.gen(function* () {
42354235
const pathService = yield* Path.Path;
42364236
const cwd = yield* makeTmpDir();
42374237
const { initialBranch } = yield* initRepoWithCommit(cwd);
42384238
const worktreesDirectory = yield* makeTmpDir("custom-worktrees-");
42394239
const driver = yield* GitVcsDriver.GitVcsDriver;
42404240
const folders = new Set<string>();
4241+
const unchanged = ["z".repeat(40), "fix-a-b", "console"];
42414242
const branches = [
4243+
...unchanged,
42424244
`feature/${"a".repeat(90)}/${"b".repeat(90)}/${"c".repeat(90)}`,
42434245
`feature/${"a".repeat(90)}/${"b".repeat(90)}/${"d".repeat(90)}`,
42444246
`feature/${"a".repeat(40)}/5fac`,
42454247
`feature/${"a".repeat(40)}/18828`,
42464248
`${"a".repeat(22)}😀/${"b".repeat(30)}`,
4247-
"z".repeat(40),
42484249
"z".repeat(41),
4250+
"fix/a<b",
4251+
"fix/a>b",
4252+
"fix/a|b",
4253+
"con",
4254+
"aux.c",
4255+
"lpt0",
4256+
`nul.${"n".repeat(40)}`,
42494257
];
42504258
for (const branch of branches) {
42514259
const created = yield* driver.createWorktree(
@@ -4254,7 +4262,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
42544262
);
42554263
const folder = pathService.basename(created.worktree.path);
42564264
assert.isAtMost(folder.length, 40);
4257-
if (branch.length <= 40) assert.equal(folder, branch);
4265+
if (unchanged.includes(branch)) assert.equal(folder, branch);
4266+
assert.notMatch(folder, /[<>"|]|^(con|aux|nul|lpt0)(\.|$)/i);
42584267
assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
42594268
assert.include(
42604269
yield* git(cwd, ["-c", "core.quotePath=false", "worktree", "list", "--porcelain"]),

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,13 +4124,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41244124
});
41254125
}
41264126
let directoryName = targetBranch.replace(/\//g, "-");
4127+
// Git refs already exclude Windows' other invalid characters and trailing dots or spaces.
4128+
const windowsSafeName = directoryName
4129+
.replace(/[<>"|]/g, "-")
4130+
.replace(/^(con|prn|aux|nul|com\d|lpt\d|conin\$|conout\$)(\.|$)/i, "$1-");
41274131
// Leave room for files beneath the worktree on Windows, regardless of branch length.
4128-
if (directoryName.length > 40) {
4132+
// The digest also keeps branches that differ only in replaced characters apart.
4133+
if (directoryName.length > 40 || windowsSafeName !== directoryName) {
41294134
const digest = yield* crypto
41304135
.digest("SHA-256", new TextEncoder().encode(targetBranch))
41314136
.pipe(Effect.orDie);
41324137
// Avoid splitting a surrogate pair, which Git would list back as a different path.
4133-
const prefix = directoryName.slice(0, 23).replace(/[\uD800-\uDBFF]$/, "");
4138+
const prefix = windowsSafeName.slice(0, 23).replace(/[\uD800-\uDBFF]$/, "");
41344139
directoryName = `${prefix}-${Hex.encode(digest).slice(0, 16)}`;
41354140
}
41364141
worktreePath = path.join(parentDir, repoName, directoryName);

0 commit comments

Comments
 (0)