Files
orca/config/scripts/local-repository-worktrees.mjs
T
OrcaWinandOrcaWin b3dfd54827 perf(build): skip unnecessary work and reclaim Vite caches (#29479)
* perf(build): remove unnecessary development and config work

* feat(dev): reclaim inactive worktree Vite caches

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-16 00:56:30 -07:00

57 lines
1.6 KiB
JavaScript

import { realpath } from 'node:fs/promises'
import { resolve } from 'node:path'
import { runProcess } from '@orca/process-host'
import { decodeGitCQuotedPath } from '../../src/shared/git-cquoted-path.ts'
async function git(repoRoot, args) {
const result = await runProcess({
program: 'git',
args,
cwd: repoRoot,
timeoutMs: 30_000,
maxOutputBytes: 4 * 1024 * 1024,
terminationBarrier: true
})
if (result.code !== 0 || result.timedOut) {
throw new Error('Could not read the local Git repository')
}
return result.stdout
}
export async function localRepositoryWorktrees(repoRoot) {
const common = (await git(repoRoot, ['rev-parse', '--git-common-dir'])).replace(
process.platform === 'win32' ? /\r?\n$/ : /\n$/,
''
)
if (!common || common.includes('\n')) {
throw new Error('Unrecognised Git common directory')
}
const output = await git(repoRoot, [
'-c',
'core.quotePath=false',
'worktree',
'list',
'--porcelain'
])
const worktrees = []
for (const block of output.split(/\r?\n\r?\n/)) {
const lines = block.split(/\r?\n/).filter(Boolean)
if (lines.length === 0) {
continue
}
if (
lines.some(
(line) =>
!/^(worktree |HEAD |branch |bare$|detached$|locked(?: |$)|prunable(?: |$))/.test(line)
)
) {
throw new Error('Ambiguous worktree listing; refusing cleanup')
}
const field = lines.find((line) => line.startsWith('worktree '))
if (field && !lines.includes('bare')) {
worktrees.push(decodeGitCQuotedPath(field.slice(9)))
}
}
return { commonDirectory: await realpath(resolve(repoRoot, common)), worktrees }
}