Skip to content

Commit e6a8b48

Browse files
fix(server): a new worktree no longer fails when its generated folder is left over from an earlier one (#35121)
1 parent ced85c6 commit e6a8b48

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5486,6 +5486,22 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
54865486
);
54875487
assert.equal(yield* fileSystem.exists(created.worktree.path), true);
54885488

5489+
yield* git(cwd, ["worktree", "remove", "--force", created.worktree.path]);
5490+
yield* git(cwd, ["branch", "-D", "feature/custom-dir"]);
5491+
yield* fileSystem.makeDirectory(created.worktree.path);
5492+
const leftover = pathService.join(created.worktree.path, "notes.txt");
5493+
yield* fileSystem.writeFileString(leftover, "my notes");
5494+
const recreated = yield* driver.createWorktree(
5495+
{ cwd, path: null, refName: initialBranch, newRefName: "feature/custom-dir" },
5496+
{ worktreesDirectory },
5497+
);
5498+
assert.notEqual(recreated.worktree.path, created.worktree.path);
5499+
assert.equal(
5500+
yield* git(recreated.worktree.path, ["branch", "--show-current"]),
5501+
"feature/custom-dir",
5502+
);
5503+
assert.deepEqual(yield* fileSystem.readDirectory(created.worktree.path), ["notes.txt"]);
5504+
54895505
const error = yield* driver
54905506
.createWorktree(
54915507
{ cwd, path: null, refName: initialBranch, newRefName: "feature/relative-dir" },

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4474,8 +4474,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
44744474
if (Buffer.byteLength(prefix + character) > 23) break;
44754475
prefix += character;
44764476
}
4477-
const directoryName = `${prefix}-${Hex.encode(hash).slice(0, 16)}`;
4478-
worktreePath = path.join(parentDir, repoName, directoryName);
4477+
const basePath = path.join(parentDir, repoName, `${prefix}-${Hex.encode(hash).slice(0, 16)}`);
4478+
// The name is stable per branch, so a folder left by an earlier worktree must not block it.
4479+
worktreePath = basePath;
4480+
for (
4481+
let suffix = 2;
4482+
yield* fileSystem.exists(worktreePath).pipe(Effect.orElseSucceed(() => false));
4483+
suffix++
4484+
) {
4485+
worktreePath = `${basePath}-${suffix}`;
4486+
}
44794487
}
44804488
const newRefName = input.newRefName;
44814489
if (newRefName) {

0 commit comments

Comments
 (0)