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

Function getOrCreateWorktree

src/utils/worktree.ts:247–387  ·  view source on GitHub ↗

* Creates a new git worktree for the given slug, or resumes it if it already exists. * Named worktrees reuse the same path across invocations, so the existence check * prevents unconditionally running `git fetch` (which can hang waiting for credentials) * on every resume.

(
  repoRoot: string,
  slug: string,
  options?: { prNumber?: number },
)

Source from the content-addressed store, hash-verified

245 * on every resume.
246 */
247async function getOrCreateWorktree(
248 repoRoot: string,
249 slug: string,
250 options?: { prNumber?: number },
251): Promise<WorktreeCreateResult> {
252 const worktreePath = worktreePathFor(repoRoot, slug)
253 const worktreeBranch = worktreeBranchName(slug)
254
255 // Fast resume path: if the worktree already exists skip fetch and creation.
256 // Read the .git pointer file directly (no subprocess, no upward walk) — a
257 // subprocess `rev-parse HEAD` burns ~15ms on spawn overhead even for a 2ms
258 // task, and the await yield lets background spawnSyncs pile on (seen at 55ms).
259 const existingHead = await readWorktreeHeadSha(worktreePath)
260 if (existingHead) {
261 return {
262 worktreePath,
263 worktreeBranch,
264 headCommit: existingHead,
265 existed: true,
266 }
267 }
268
269 // New worktree: fetch base branch then add
270 await mkdir(getWorktreesDir(repoRoot), { recursive: true })
271
272 const fetchEnv = { ...process.env, ...GIT_NO_PROMPT_ENV }
273
274 let baseBranch: string
275 let baseSha: string | null = null
276 if (options?.prNumber) {
277 const { code: prFetchCode, stderr: prFetchStderr } =
278 await execFileNoThrowWithCwd(
279 gitExe(),
280 ['fetch', 'origin', `pull/${options.prNumber}/head`],
281 { cwd: repoRoot, stdin: 'ignore', env: fetchEnv },
282 )
283 if (prFetchCode !== 0) {
284 throw new Error(
285 `Failed to fetch PR #${options.prNumber}: ${prFetchStderr.trim() || 'PR may not exist or the repository may not have a remote named "origin"'}`,
286 )
287 }
288 baseBranch = 'FETCH_HEAD'
289 } else {
290 // If origin/<branch> already exists locally, skip fetch. In large repos
291 // (210k files, 16M objects) fetch burns ~6-8s on a local commit-graph
292 // scan before even hitting the network. A slightly stale base is fine —
293 // the user can pull in the worktree if they want latest.
294 // resolveRef reads the loose/packed ref directly; when it succeeds we
295 // already have the SHA, so the later rev-parse is skipped entirely.
296 const [defaultBranch, gitDir] = await Promise.all([
297 getDefaultBranch(),
298 resolveGitDir(repoRoot),
299 ])
300 const originRef = `origin/${defaultBranch}`
301 const originSha = gitDir
302 ? await resolveRef(gitDir, `refs/remotes/origin/${defaultBranch}`)
303 : null
304 if (originSha) {

Callers 3

createWorktreeForSessionFunction · 0.85
createAgentWorktreeFunction · 0.85
execIntoTmuxWorktreeFunction · 0.85

Calls 11

worktreePathForFunction · 0.85
worktreeBranchNameFunction · 0.85
readWorktreeHeadShaFunction · 0.85
mkdirFunction · 0.85
getWorktreesDirFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
getDefaultBranchFunction · 0.85
resolveGitDirFunction · 0.85
resolveRefFunction · 0.85
tearDownFunction · 0.85
getInitialSettingsFunction · 0.50

Tested by

no test coverage detected