MCPcopy
hub / github.com/codeaashu/claude-code / createWorktreeForSession

Function createWorktreeForSession

src/utils/worktree.ts:702–778  ·  view source on GitHub ↗
(
  sessionId: string,
  slug: string,
  tmuxSessionName?: string,
  options?: { prNumber?: number },
)

Source from the content-addressed store, hash-verified

700}
701
702export 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,

Callers 2

setupFunction · 0.85
callFunction · 0.85

Calls 10

validateWorktreeSlugFunction · 0.85
getCwdFunction · 0.85
hasWorktreeCreateHookFunction · 0.85
logForDebuggingFunction · 0.85
getBranchFunction · 0.85
getOrCreateWorktreeFunction · 0.85
performPostCreationSetupFunction · 0.85
getInitialSettingsFunction · 0.85
saveCurrentProjectConfigFunction · 0.85

Tested by

no test coverage detected