Skip to content

Commit 95e69d0

Browse files
committed
fix(server): PR checkouts get short worktree folder names on Windows
1 parent d9abfdd commit 95e69d0

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,34 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
33413341
}),
33423342
);
33433343

3344+
it.effect("keeps worktree folder names short for long branch names", () =>
3345+
Effect.gen(function* () {
3346+
const fileSystem = yield* FileSystem.FileSystem;
3347+
const pathService = yield* Path.Path;
3348+
const cwd = yield* makeTmpDir();
3349+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3350+
const worktreesDirectory = yield* makeTmpDir("custom-worktrees-");
3351+
const driver = yield* GitVcsDriver.GitVcsDriver;
3352+
// Flattened, this is one 280-character folder name, over every OS's limit.
3353+
const longBranch = (last: string) =>
3354+
`feature/${"a".repeat(90)}/${"b".repeat(90)}/${last.repeat(90)}`;
3355+
3356+
const folders = new Set<string>();
3357+
for (const branch of [longBranch("c"), longBranch("d")]) {
3358+
const created = yield* driver.createWorktree(
3359+
{ cwd, path: null, refName: initialBranch, newRefName: branch },
3360+
{ worktreesDirectory },
3361+
);
3362+
const folder = pathService.basename(created.worktree.path);
3363+
assert.isAtMost(folder.length, 40);
3364+
assert.equal(yield* fileSystem.exists(created.worktree.path), true);
3365+
assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
3366+
folders.add(folder);
3367+
}
3368+
assert.equal(folders.size, 2);
3369+
}),
3370+
);
3371+
33443372
it.effect("resolves the submodule mode from the option, then t3.json", () =>
33453373
Effect.gen(function* () {
33463374
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura
152152
return Duration.min(Duration.millis(cooldownMs), STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN);
153153
}
154154

155+
// PR checkouts keep the PR's branch name, so without a cap the folder name can
156+
// be any length. On Windows every character comes out of the 260-character
157+
// budget that tools without long-path support (and git's own `.git/worktrees`
158+
// bookkeeping, even with core.longpaths) have for files inside the worktree.
159+
// Long names keep a readable prefix and a hash of the full branch.
160+
const MAX_WORKTREE_DIRECTORY_NAME_LENGTH = 40;
161+
162+
function worktreeDirectoryName(branch: string): string {
163+
const name = branch.replace(/\//g, "-");
164+
if (name.length <= MAX_WORKTREE_DIRECTORY_NAME_LENGTH) return name;
165+
let hash = 0x811c9dc5;
166+
for (let index = 0; index < branch.length; index++) {
167+
hash = Math.imul(hash ^ branch.charCodeAt(index), 0x01000193);
168+
}
169+
const suffix = (hash >>> 0).toString(16).padStart(8, "0");
170+
const prefix = name
171+
.slice(0, MAX_WORKTREE_DIRECTORY_NAME_LENGTH - suffix.length - 1)
172+
// A cut emoji leaves a lone surrogate, which git lists back as U+FFFD, so
173+
// T3's stored path would stop matching git's.
174+
.replace(/[\uD800-\uDBFF]$/, "")
175+
.replace(/[.-]+$/, "");
176+
return `${prefix}-${suffix}`;
177+
}
178+
155179
class GitRefsSnapshotCacheKey extends Data.Class<{
156180
gitCommonDir: string;
157181
epoch: number;
@@ -3679,7 +3703,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36793703
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.refName);
36803704
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.newRefName);
36813705
const targetBranch = input.newRefName ?? input.refName;
3682-
const sanitizedBranch = targetBranch.replace(/\//g, "-");
36833706
// Resolve sub/.. before taking the basename.
36843707
const repoName = path.basename(path.resolve(input.cwd));
36853708
let worktreePath = input.path;
@@ -3698,7 +3721,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36983721
detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings → Storage.`,
36993722
});
37003723
}
3701-
worktreePath = path.join(parentDir, repoName, sanitizedBranch);
3724+
worktreePath = path.join(parentDir, repoName, worktreeDirectoryName(targetBranch));
37023725
}
37033726
const args = input.newRefName
37043727
? ["worktree", "add", "-b", input.newRefName, "--", worktreePath, input.refName]

0 commit comments

Comments
 (0)