Skip to content

Commit 0533b10

Browse files
committed
fix(server): PR checkouts get short worktree folder names on Windows
1 parent 7e289ae commit 0533b10

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
@@ -4471,6 +4471,34 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
44714471
}),
44724472
);
44734473

4474+
it.effect("keeps worktree folder names short for long branch names", () =>
4475+
Effect.gen(function* () {
4476+
const fileSystem = yield* FileSystem.FileSystem;
4477+
const pathService = yield* Path.Path;
4478+
const cwd = yield* makeTmpDir();
4479+
const { initialBranch } = yield* initRepoWithCommit(cwd);
4480+
const worktreesDirectory = yield* makeTmpDir("custom-worktrees-");
4481+
const driver = yield* GitVcsDriver.GitVcsDriver;
4482+
// Flattened, this is one 280-character folder name, over every OS's limit.
4483+
const longBranch = (last: string) =>
4484+
`feature/${"a".repeat(90)}/${"b".repeat(90)}/${last.repeat(90)}`;
4485+
4486+
const folders = new Set<string>();
4487+
for (const branch of [longBranch("c"), longBranch("d")]) {
4488+
const created = yield* driver.createWorktree(
4489+
{ cwd, path: null, refName: initialBranch, newRefName: branch },
4490+
{ worktreesDirectory },
4491+
);
4492+
const folder = pathService.basename(created.worktree.path);
4493+
assert.isAtMost(folder.length, 40);
4494+
assert.equal(yield* fileSystem.exists(created.worktree.path), true);
4495+
assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
4496+
folders.add(folder);
4497+
}
4498+
assert.equal(folders.size, 2);
4499+
}),
4500+
);
4501+
44744502
it.effect("resolves the submodule mode from the option, then t3.json", () =>
44754503
Effect.gen(function* () {
44764504
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
@@ -148,6 +148,30 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura
148148
return Duration.min(Duration.millis(cooldownMs), STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN);
149149
}
150150

151+
// PR checkouts keep the PR's branch name, so without a cap the folder name can
152+
// be any length. On Windows every character comes out of the 260-character
153+
// budget that tools without long-path support (and git's own `.git/worktrees`
154+
// bookkeeping, even with core.longpaths) have for files inside the worktree.
155+
// Long names keep a readable prefix and a hash of the full branch.
156+
const MAX_WORKTREE_DIRECTORY_NAME_LENGTH = 40;
157+
158+
function worktreeDirectoryName(branch: string): string {
159+
const name = branch.replace(/\//g, "-");
160+
if (name.length <= MAX_WORKTREE_DIRECTORY_NAME_LENGTH) return name;
161+
let hash = 0x811c9dc5;
162+
for (let index = 0; index < branch.length; index++) {
163+
hash = Math.imul(hash ^ branch.charCodeAt(index), 0x01000193);
164+
}
165+
const suffix = (hash >>> 0).toString(16).padStart(8, "0");
166+
const prefix = name
167+
.slice(0, MAX_WORKTREE_DIRECTORY_NAME_LENGTH - suffix.length - 1)
168+
// A cut emoji leaves a lone surrogate, which git lists back as U+FFFD, so
169+
// T3's stored path would stop matching git's.
170+
.replace(/[\uD800-\uDBFF]$/, "")
171+
.replace(/[.-]+$/, "");
172+
return `${prefix}-${suffix}`;
173+
}
174+
151175
class GitRefsSnapshotCacheKey extends Data.Class<{
152176
gitCommonDir: string;
153177
epoch: number;
@@ -3689,7 +3713,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36893713
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.refName);
36903714
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.newRefName);
36913715
const targetBranch = input.newRefName ?? input.refName;
3692-
const sanitizedBranch = targetBranch.replace(/\//g, "-");
36933716
// resolve: a cwd like `<repo>/sub/..` must not name the folder `..`.
36943717
const repoName = path.basename(path.resolve(input.cwd));
36953718
let worktreePath = input.path;
@@ -3708,7 +3731,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
37083731
detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings → Storage.`,
37093732
});
37103733
}
3711-
worktreePath = path.join(parentDir, repoName, sanitizedBranch);
3734+
worktreePath = path.join(parentDir, repoName, worktreeDirectoryName(targetBranch));
37123735
}
37133736
const args = input.newRefName
37143737
? ["worktree", "add", "-b", input.newRefName, "--", worktreePath, input.refName]

0 commit comments

Comments
 (0)