(
sessionId: string,
slug: string,
tmuxSessionName?: string,
options?: { prNumber?: number },
)
| 702 | } |
| 703 | |
| 704 | export async function createWorktreeForSession( |
| 705 | sessionId: string, |
| 706 | slug: string, |
| 707 | tmuxSessionName?: string, |
| 708 | options?: { prNumber?: number }, |
| 709 | ): Promise<WorktreeSession> { |
| 710 | // Must run before the hook branch below — hooks receive the raw slug as an |
| 711 | // argument, and the git branch builds a path from it via path.join. |
| 712 | validateWorktreeSlug(slug) |
| 713 | |
| 714 | const originalCwd = getCwd() |
| 715 | |
| 716 | // Try hook-based worktree creation first (allows user-configured VCS) |
| 717 | if (hasWorktreeCreateHook()) { |
| 718 | const hookResult = await executeWorktreeCreateHook(slug) |
| 719 | logForDebugging( |
| 720 | `Created hook-based worktree at: ${hookResult.worktreePath}`, |
| 721 | ) |
| 722 | |
| 723 | currentWorktreeSession = { |
| 724 | originalCwd, |
| 725 | worktreePath: hookResult.worktreePath, |
| 726 | worktreeName: slug, |
| 727 | sessionId, |
| 728 | tmuxSessionName, |
| 729 | hookBased: true, |
| 730 | } |
| 731 | } else { |
| 732 | // Fall back to git worktree |
| 733 | const gitRoot = findGitRoot(getCwd()) |
| 734 | if (!gitRoot) { |
| 735 | throw new Error( |
| 736 | 'Cannot create a worktree: not in a git repository and no WorktreeCreate hooks are configured. ' + |
| 737 | 'Configure WorktreeCreate/WorktreeRemove hooks in settings.json to use worktree isolation with other VCS systems.', |
| 738 | ) |
| 739 | } |
| 740 | |
| 741 | const originalBranch = await getBranch() |
| 742 | |
| 743 | const createStart = Date.now() |
| 744 | const { worktreePath, worktreeBranch, headCommit, existed } = |
| 745 | await getOrCreateWorktree(gitRoot, slug, options) |
| 746 | |
| 747 | let creationDurationMs: number | undefined |
| 748 | if (existed) { |
| 749 | logForDebugging(`Resuming existing worktree at: ${worktreePath}`) |
| 750 | } else { |
| 751 | logForDebugging( |
| 752 | `Created worktree at: ${worktreePath} on branch: ${worktreeBranch}`, |
| 753 | ) |
| 754 | await performPostCreationSetup(gitRoot, worktreePath) |
| 755 | creationDurationMs = Date.now() - createStart |
| 756 | } |
| 757 | |
| 758 | currentWorktreeSession = { |
| 759 | originalCwd, |
| 760 | worktreePath, |
| 761 | worktreeName: slug, |
no test coverage detected