MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / copyWorktreeIncludeFiles

Function copyWorktreeIncludeFiles

source code/utils/worktree.ts:393–506  ·  view source on GitHub ↗
(
  repoRoot: string,
  worktreePath: string,
)

Source from the content-addressed store, hash-verified

391 * that directory is expanded with a second scoped `ls-files` call.
392 */
393export async function copyWorktreeIncludeFiles(
394 repoRoot: string,
395 worktreePath: string,
396): Promise<string[]> {
397 let includeContent: string
398 try {
399 includeContent = await readFile(join(repoRoot, '.worktreeinclude'), 'utf-8')
400 } catch {
401 return []
402 }
403
404 const patterns = includeContent
405 .split(/\r?\n/)
406 .map(line => line.trim())
407 .filter(line => line.length > 0 && !line.startsWith('#'))
408 if (patterns.length === 0) {
409 return []
410 }
411
412 // Single pass with --directory: collapses fully-gitignored dirs (node_modules/,
413 // .turbo/, etc.) into single entries instead of listing every file inside.
414 // In a large repo this cuts ~500k entries/~7s down to ~hundreds of entries/~100ms.
415 const gitignored = await execFileNoThrowWithCwd(
416 gitExe(),
417 ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory'],
418 { cwd: repoRoot },
419 )
420 if (gitignored.code !== 0 || !gitignored.stdout.trim()) {
421 return []
422 }
423
424 const entries = gitignored.stdout.trim().split('\n').filter(Boolean)
425 const matcher = ignore().add(includeContent)
426
427 // --directory emits collapsed dirs with a trailing slash; everything else is
428 // an individual file.
429 const collapsedDirs = entries.filter(e => e.endsWith('/'))
430 const files = entries.filter(e => !e.endsWith('/') && matcher.ignores(e))
431
432 // Edge case: a .worktreeinclude pattern targets a path inside a collapsed dir
433 // (e.g. pattern `config/secrets/api.key` when all of `config/secrets/` is
434 // gitignored with no tracked siblings). Expand only dirs where a pattern has
435 // that dir as its explicit path prefix (stripping redundant leading `/`), the
436 // dir falls under an anchored glob's literal prefix (e.g. `config/**/*.key`
437 // expands `config/secrets/`), or the dir itself matches a pattern. We don't
438 // expand for `**/` or anchorless patterns -- those match files in tracked dirs
439 // (already listed individually) and expanding every collapsed dir for them
440 // would defeat the perf win.
441 const dirsToExpand = collapsedDirs.filter(dir => {
442 if (
443 patterns.some(p => {
444 const normalized = p.startsWith('/') ? p.slice(1) : p
445 // Literal prefix match: pattern starts with the collapsed dir path
446 if (normalized.startsWith(dir)) return true
447 // Anchored glob: dir falls under the pattern's literal (non-glob) prefix
448 // e.g. `config/**/*.key` has literal prefix `config/` → expand `config/secrets/`
449 const globIdx = normalized.search(/[*?[]/)
450 if (globIdx > 0) {

Callers 1

performPostCreationSetupFunction · 0.85

Calls 6

readFileFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
mkdirFunction · 0.85
logForDebuggingFunction · 0.85
searchMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected