fix(server): actionable error when creating a worktree in a commit-less repo
git worktree add cannot branch from an unborn HEAD, so starting a thread in New worktree mode inside a freshly-initialized repository (no commits yet) failed with a cryptic invalid-reference error. Detect the missing base commit up front and fail with actionable guidance to make an initial commit or use the current checkout instead. Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
5d34f9ff23
commit
9c53228bb8
2 files changed
+50
No files matched your search
@@ -668,6 +668,38 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
|
||||
assert.equal(yield* fileSystem.exists(worktreePath), false);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("fails with an actionable error when the base ref has no commit yet", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* GitVcsDriver.GitVcsDriver;
|
||||
// Initialize a repository but never commit: HEAD is unborn, so there
|
||||
// is no commit to branch a worktree from.
|
||||
yield* driver.initRepo({ cwd });
|
||||
const baseBranch = yield* git(cwd, ["branch", "--show-current"]);
|
||||
const pathService = yield* Path.Path;
|
||||
const worktreePath = pathService.join(
|
||||
yield* makeTmpDir("git-worktrees-"),
|
||||
"feature-worktree",
|
||||
);
|
||||
|
||||
const error = yield* driver
|
||||
.createWorktree({
|
||||
cwd,
|
||||
path: worktreePath,
|
||||
refName: baseBranch,
|
||||
newRefName: "feature/worktree",
|
||||
})
|
||||
.pipe(Effect.flip);
|
||||
|
||||
assert.equal(error._tag, "GitCommandError");
|
||||
assert.include(error.detail, "does not point at any commit");
|
||||
assert.include(error.detail, baseBranch);
|
||||
// The worktree must not have been created.
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
assert.equal(yield* fileSystem.exists(worktreePath), false);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("commit context", () => {
|
||||
|
||||
@@ -2268,6 +2268,24 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
|
||||
? ["worktree", "add", "-b", input.newRefName, worktreePath, input.refName]
|
||||
: ["worktree", "add", worktreePath, input.refName];
|
||||
|
||||
// A worktree can only be created from a ref that already points at a commit.
|
||||
// A freshly-initialized repository (e.g. `git init` with nothing committed
|
||||
// yet) has an unborn HEAD, so `git worktree add` fails with a cryptic
|
||||
// "fatal: invalid reference: <ref>". Detect that up front and surface an
|
||||
// actionable error instead of leaking the raw git failure to the caller.
|
||||
const baseCommitStdout = yield* runGitStdout(
|
||||
"GitVcsDriver.createWorktree.resolveBase",
|
||||
input.cwd,
|
||||
["rev-parse", "--verify", "--quiet", `${input.refName}^{commit}`],
|
||||
true,
|
||||
);
|
||||
if (baseCommitStdout.trim().length === 0) {
|
||||
return yield* new GitCommandError({
|
||||
...gitCommandContext({ operation: "GitVcsDriver.createWorktree", cwd: input.cwd, args }),
|
||||
detail: `Cannot create a worktree from '${input.refName}' because it does not point at any commit yet. Make an initial commit in this repository, or start the thread in the current checkout instead.`,
|
||||
});
|
||||
}
|
||||
|
||||
yield* executeGit("GitVcsDriver.createWorktree", input.cwd, args, {
|
||||
fallbackErrorDetail: "git worktree add failed",
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user