Skip to content

Commit 1d20ba7

Browse files
only21milclaude
andcommitted
fix(server): claim a free folder when the generated worktree path is taken
A folder already at a generated worktree path, such as one an earlier worktree for the same branch left behind, made `git worktree add` fail. Claim the folder with a non-recursive mkdir, step to a numbered suffix that replaces trailing hash characters when it exists, and remove the claimed folder again if the worktree is not created and the folder is still empty. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4d2fab5 commit 1d20ba7

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5052,6 +5052,36 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
50525052
);
50535053
assert.equal(yield* fileSystem.exists(created.worktree.path), true);
50545054

5055+
// A folder left at the generated path, here by an earlier worktree for the branch.
5056+
const occupied = created.worktree.path;
5057+
yield* git(cwd, ["worktree", "remove", occupied]);
5058+
yield* git(cwd, ["branch", "-D", "feature/custom-dir"]);
5059+
yield* writeTextFile(occupied, "notes.txt", "keep\n");
5060+
const recreated = yield* driver.createWorktree(
5061+
{ cwd, path: null, refName: initialBranch, newRefName: "feature/custom-dir" },
5062+
{ worktreesDirectory },
5063+
);
5064+
const claimed = `${occupied.slice(0, -2)}-2`;
5065+
assert.equal(recreated.worktree.path, claimed);
5066+
assert.equal(
5067+
yield* git(recreated.worktree.path, ["branch", "--show-current"]),
5068+
"feature/custom-dir",
5069+
);
5070+
assert.equal(
5071+
yield* fileSystem.readFileString(pathService.join(occupied, "notes.txt")),
5072+
"keep\n",
5073+
);
5074+
yield* driver
5075+
.createWorktree(
5076+
{ cwd, path: null, refName: initialBranch, newRefName: "feature/custom-dir" },
5077+
{ worktreesDirectory },
5078+
)
5079+
.pipe(Effect.flip);
5080+
assert.sameMembers(yield* fileSystem.readDirectory(pathService.dirname(occupied)), [
5081+
pathService.basename(occupied),
5082+
pathService.basename(claimed),
5083+
]);
5084+
50555085
const error = yield* driver
50565086
.createWorktree(
50575087
{ cwd, path: null, refName: initialBranch, newRefName: "feature/relative-dir" },

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,7 +4254,40 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
42544254
prefix += character;
42554255
}
42564256
const directoryName = `${prefix}-${Hex.encode(hash).slice(0, 16)}`;
4257-
worktreePath = path.join(parentDir, repoName, directoryName);
4257+
const projectDirectory = path.join(parentDir, repoName);
4258+
const toCommandError = (error: PlatformError.PlatformError) =>
4259+
new GitCommandError({
4260+
operation: "GitVcsDriver.createWorktree",
4261+
command: "mkdir",
4262+
cwd: input.cwd,
4263+
detail: `Could not create a worktree folder in ${projectDirectory}.`,
4264+
cause: error,
4265+
});
4266+
yield* fileSystem
4267+
.makeDirectory(projectDirectory, { recursive: true })
4268+
.pipe(Effect.mapError(toCommandError));
4269+
// Something can already sit at the generated path, so claim the first free folder.
4270+
for (let attempt = 1; worktreePath == null; attempt += 1) {
4271+
// The suffix replaces trailing hash characters, keeping the 40-byte bound.
4272+
const suffix = `-${attempt}`;
4273+
const candidate = path.join(
4274+
projectDirectory,
4275+
attempt === 1 ? directoryName : `${directoryName.slice(0, -suffix.length)}${suffix}`,
4276+
);
4277+
// Git checks out into the empty claimed folder; give it back if the worktree is not created.
4278+
worktreePath = yield* Effect.acquireRelease(
4279+
fileSystem.makeDirectory(candidate),
4280+
(_, exit) =>
4281+
Exit.isFailure(exit) ? Effect.ignore(removeEmptyDirectory(candidate)) : Effect.void,
4282+
).pipe(
4283+
Effect.as(candidate),
4284+
Effect.catchIf(
4285+
(error) => error.reason._tag === "AlreadyExists",
4286+
() => Effect.succeed(null),
4287+
),
4288+
Effect.mapError(toCommandError),
4289+
);
4290+
}
42584291
}
42594292
const newRefName = input.newRefName;
42604293
if (newRefName) {
@@ -4447,7 +4480,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
44474480
refName: targetBranch,
44484481
},
44494482
};
4450-
});
4483+
}, Effect.scoped);
44514484

44524485
const fetchRefIntoBranch: GitVcsDriver.GitVcsDriver["Service"]["fetchRefIntoBranch"] = Effect.fn(
44534486
"GitVcsDriver.fetchRefIntoBranch",

0 commit comments

Comments
 (0)