matthewwong525/WongStack / .agents/plugins/memory
memory
Verifiedscripts/lib/primary-root.mjs
View on GitHub#!/usr/bin/env node
// The primary worktree: where durable secrets live (wiki/development/secrets.md). Every WongStack script finds it here.
// The primary is the common dir's parent, and Git must agree it is a checkout's top level; a bare repo fails that.
import { execFileSync } from 'node:child_process';
import { dirname } from 'node:path';
import { isMain, parseCli } from './cli.mjs';
export class PrimaryRootError extends Error {}
const git = (cwd, ...args) => execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
// { root, primary, linked, gitDir, commonDir }, or a PrimaryRootError that names what went wrong.
export function primaryRoot(cwd = process.cwd()) {
let root, gitDir, commonDir;
try {
[root, gitDir, commonDir] = git(cwd, 'rev-parse', '--path-format=absolute', '--show-toplevel', '--git-dir', '--git-common-dir').split('\n');
} catch (error) {
throw new PrimaryRootError(`not inside a Git checkout: ${String(error.stderr || error.message).trim().split('\n')[0]}`);
}
const linked = gitDir !== commonDir;
const primary = linked ? dirname(commonDir) : root;
let resolved;
try { resolved = git(primary, 'rev-parse', '--show-toplevel'); } catch { resolved = ''; }
if (resolved !== primary) throw new PrimaryRootError(`the parent of ${commonDir} is not a checkout, so the primary worktree is unknown`);
return { root, primary, linked, gitDir, commonDir };
}
const USAGE = 'usage: primary-root.mjs [dir] prints the primary checkout of dir (default: the current directory)';
if (isMain(import.meta.url)) {
const parsed = parseCli({ usage: USAGE, allowPositionals: true });
try {
console.log(primaryRoot(parsed.positionals[0] || process.cwd()).primary);
} catch (error) {
if (!(error instanceof PrimaryRootError)) throw error;
console.error(`primary-root: ${error.message}`);
process.exitCode = 1;
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |