#!/usr/bin/env node /** * worktree.mjs — manage scratch git worktrees under `.debug/worktrees/` with * optional linked `frontend/node_modules`. Unlinks junctions/symlinks before * removal because Windows git can otherwise walk into and delete link targets. */ import { execFileSync } from 'node:child_process' import fs from 'node:fs' import path from 'node:path' import { resolveStorageRoot } from './paths.mjs' const STORAGE = resolveStorageRoot() /** Only `.debug/worktrees/` may hold scratch worktrees; the main working tree is never a candidate. */ const SCRATCH_DIR = path.join(STORAGE, '.debug', 'worktrees') const SCRATCH_PREFIX = SCRATCH_DIR.toLowerCase() + path.sep function runGit(args) { return execFileSync('git', args, { cwd: STORAGE, encoding: 'utf8' }).trim() } function usage() { console.error('usage: node scripts/debug/worktree.mjs add \n node scripts/debug/worktree.mjs remove [--dry-run]') process.exit(2) } function resolveScratch(target) { const worktree = path.resolve(STORAGE, target) if (!worktree.toLowerCase().startsWith(SCRATCH_PREFIX)) { console.error(`worktree: ${worktree} is outside .debug/worktrees/; the main tree is never a candidate`) process.exit(1) } return worktree } function add(args) { const [target, ref, ...extra] = args if (target === undefined || ref === undefined || ref.trim() === '' || extra.length > 0) usage() const worktree = resolveScratch(target) if (fs.lstatSync(worktree, { throwIfNoEntry: false }) !== undefined) { console.error(`worktree: ${worktree} already exists`) process.exit(1) } let commit try { commit = runGit(['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}^{commit}`]) } catch { console.error(`worktree: ${ref} is not a commit in this repository`) process.exit(1) } fs.mkdirSync(path.dirname(worktree), { recursive: true }) runGit(['worktree', 'add', '--detach', worktree, commit]) const mainFrontendModules = path.join(STORAGE, 'frontend', 'node_modules') const worktreeFrontend = path.join(worktree, 'frontend') const worktreeModules = path.join(worktreeFrontend, 'node_modules') let linked = false if (fs.existsSync(mainFrontendModules) && fs.existsSync(worktreeFrontend) && !fs.existsSync(worktreeModules)) { // A junction resolves without the privilege a symbolic link needs on Windows; // `remove` unlinks it before git sees the directory. fs.symlinkSync(mainFrontendModules, worktreeModules, process.platform === 'win32' ? 'junction' : 'dir') linked = true } console.log(`worktree: added ${path.relative(STORAGE, worktree)} at ${commit.slice(0, 12)}${linked ? ' (frontend/node_modules linked)' : ''}`) } function collectLinks(worktree) { const candidates = [] if (!fs.existsSync(worktree)) return candidates for (const name of fs.readdirSync(worktree)) { const full = path.join(worktree, name) if (fs.lstatSync(full).isSymbolicLink()) candidates.push(full) } const frontendModules = path.join(worktree, 'frontend', 'node_modules') if (fs.lstatSync(frontendModules, { throwIfNoEntry: false })?.isSymbolicLink()) { candidates.push(frontendModules) } return candidates } function remove(args) { const [target, ...flags] = args if (target === undefined) usage() const dryRun = flags.includes('--dry-run') const worktree = resolveScratch(target) const listed = runGit(['worktree', 'list', '--porcelain']) .split('\n') .filter((line) => line.startsWith('worktree ')) .map((line) => path.resolve(line.slice('worktree '.length)).toLowerCase()) if (!listed.includes(worktree.toLowerCase())) { console.error(`worktree: ${worktree} is not a linked worktree of this repository`) process.exit(1) } const links = collectLinks(worktree) for (const link of links) { console.log(`${dryRun ? 'would unlink' : 'unlink'} ${path.relative(STORAGE, link)} -> ${fs.readlinkSync(link)}`) if (!dryRun) fs.unlinkSync(link) } if (dryRun) process.exit(0) runGit(['worktree', 'remove', '--force', worktree]) console.log(`worktree: removed ${path.relative(STORAGE, worktree)} (${links.length} link${links.length === 1 ? '' : 's'} unlinked first)`) } const [action, ...args] = process.argv.slice(2) if (action === 'add') add(args) else if (action === 'remove') remove(args) else usage()