MCPcopy Create free account
hub / github.com/Noumena-Network/code / createWorktreeForSession

Function createWorktreeForSession

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

Source from the content-addressed store, hash-verified

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

Callers 2

setupFunction · 0.85
callFunction · 0.85

Calls 10

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

Tested by

no test coverage detected