fix: Guard worktree creation against branch === baseBranch
Throws if branch and baseBranch are identical, preventing git branch -f from force-resetting shared branches (like the initiative branch) when accidentally passed as both.
This commit is contained in:
@@ -61,10 +61,18 @@ export class SimpleGitWorktreeManager implements WorktreeManager {
|
|||||||
const worktreePath = path.join(this.worktreesDir, id);
|
const worktreePath = path.join(this.worktreesDir, id);
|
||||||
log.info({ id, branch, baseBranch }, 'creating worktree');
|
log.info({ id, branch, baseBranch }, 'creating worktree');
|
||||||
|
|
||||||
|
// Safety: never force-reset a branch to its own base — this would nuke
|
||||||
|
// shared branches like the initiative branch if passed as both branch and baseBranch.
|
||||||
|
if (branch === baseBranch) {
|
||||||
|
throw new Error(`Worktree branch and baseBranch are the same (${branch}). Use a unique branch name.`);
|
||||||
|
}
|
||||||
|
|
||||||
// Create worktree — reuse existing branch or create new one
|
// Create worktree — reuse existing branch or create new one
|
||||||
const branchExists = await this.branchExists(branch);
|
const branchExists = await this.branchExists(branch);
|
||||||
if (branchExists) {
|
if (branchExists) {
|
||||||
// Branch exists from a previous run — reset it to baseBranch and check it out
|
// Branch exists from a previous run — reset it to baseBranch and check it out.
|
||||||
|
// Only safe because branch !== baseBranch (checked above), so we're resetting
|
||||||
|
// an agent-scoped branch, not a shared branch like main or the initiative branch.
|
||||||
await this.git.raw(['branch', '-f', branch, baseBranch]);
|
await this.git.raw(['branch', '-f', branch, baseBranch]);
|
||||||
await this.git.raw(['worktree', 'add', worktreePath, branch]);
|
await this.git.raw(['worktree', 'add', worktreePath, branch]);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user