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