diff --git a/lib/worktree.js b/lib/worktree.js index c89d1577e..a5f0468cb 100644 --- a/lib/worktree.js +++ b/lib/worktree.js @@ -50,7 +50,16 @@ function generateWorktree(action, worktreedir, branchExists) { if (branchExists) { yield (0, execute_1.execute)(`git fetch --no-recurse-submodules --depth=1 origin ${action.branch}`, action.workspace, action.silent); } - yield (0, execute_1.execute)(`git worktree add --no-checkout --detach ${worktreedir}`, action.workspace, action.silent); + // Check if the branch is already checked out in another worktree + try { + (0, child_process_1.execSync)(`git rev-parse --verify --quiet ${action.branch}`); + // If the command succeeds, the branch is already checked out + (0, core_1.info)(`Branch ${action.branch} is already checked out. Using existing worktree.`); + } + catch (error) { + // If the command fails, the branch is not checked out + yield (0, execute_1.execute)(`git worktree add --no-checkout --detach ${worktreedir}`, action.workspace, action.silent); + } const checkout = new GitCheckout(action.branch); if (branchExists) { // There's existing data on the branch to check out