Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

perf(git): the repo facts rev-parse also answers HEAD [C7]

Corey Olsonb26502382f5d
diff --git a/src/git.js b/src/git.js
index 5e70e29c..8c55b7c2 100644
--- a/src/git.js
+++ b/src/git.js
@@ -533,23 +533,25 @@ export function repoInfo(cwd, { env } = {}) {
/**
* Read top level, worktree Git dir and shared Git dir in one process. With no inherited selector, the same process
- * also reads the primary checkout's branch and commit and keeps them as this command's mainCheckout fact, so a
- * command learns where it is and what the trunk is from one Git call.
+ * also reads this checkout's HEAD commit and the primary checkout's branch and commit, and keeps them as this
+ * command's headCommit and mainCheckout facts, so a command learns where it is, its head and the trunk from one call.
*
* @param {string} cwd
* @returns {{ root: string, commonDir: string, gitDir: string, isMain: boolean }}
*/
function readRepoInfo(cwd, env) {
const combined = !env && gitFacts.getStore()?.active && plainRepositoryEnvironment();
- const primary = combined ? ['main-worktree/HEAD', '--symbolic-full-name', 'main-worktree/HEAD'] : [];
+ const primary = combined ? ['HEAD', 'main-worktree/HEAD', '--symbolic-full-name', 'main-worktree/HEAD'] : [];
const info = tryGit(cwd, ['rev-parse', '--path-format=absolute', '--show-toplevel', '--git-dir', '--git-common-dir', ...primary], { env });
- // An unborn primary branch fails the combined call after the paths; ask for the paths alone, as before.
+ // An unborn branch here or in the primary checkout fails the combined call after the paths; ask for the paths alone.
if (combined && info.status !== 0 && info.stdout) return readRepoInfo(cwd, process.env);
const fields = info.stdout.split('\n');
- if (combined && info.status === 0 && fields.length === 5 && fields.every(Boolean)) {
- const [root, , , commit, branch] = fields;
+ if (combined && info.status === 0 && fields.length === 6 && fields.every(Boolean)) {
+ const [root, , , head, commit, branch] = fields;
+ const { facts } = gitFacts.getStore();
+ facts.set(`headCommit:${resolve(root)}`, head);
// A detached primary checkout names no branch, as symbolic-ref reports it.
- gitFacts.getStore().facts.set(`mainCheckout:${resolve(root)}`, branch.startsWith('refs/') ? { branch, commit } : null);
+ facts.set(`mainCheckout:${resolve(root)}`, branch.startsWith('refs/') ? { branch, commit } : null);
fields.length = 3;
}
if (info.status !== 0 || fields.length !== 3 || fields.some((field) => !field)) {
diff --git a/test/cli-startup.test.js b/test/cli-startup.test.js
index 8b55652c..1016ccbf 100644
--- a/test/cli-startup.test.js
+++ b/test/cli-startup.test.js
@@ -54,7 +54,8 @@ function fixture(t) {
const home = join(dir, 'home');
mkdirSync(repo);
mkdirSync(shims);
- writeFileSync(join(shims, 'git'), '#!/bin/sh\nprintf "%s\\n" "$*" >> "$PULLBOARD_GIT_CALLS"\nexec "$PULLBOARD_REAL_GIT" "$@"\n');
+ // Each line names the folder Git ran in, then its arguments, so a count can tell this repository from the code's own.
+ writeFileSync(join(shims, 'git'), '#!/bin/sh\nprintf "%s\\t%s\\n" "$(pwd -P)" "$*" >> "$PULLBOARD_GIT_CALLS"\nexec "$PULLBOARD_REAL_GIT" "$@"\n');
chmodSync(join(shims, 'git'), 0o755);
// The repository's git hooks run pullboard from PATH; they must run this checkout, not an installed release.
writeFileSync(join(shims, 'pullboard'), `#!/bin/sh\nexec '${process.execPath}' '${BIN}' "$@"\n`);
@@ -78,16 +79,17 @@ function fixture(t) {
for (const name of ['PULLBOARD_RELAY_TOKEN', 'PULLBOARD_RELAY_KEY']) delete env[name];
const hook = moduleHook(dir);
/** Run the real CLI and return its result with the modules it loaded and the Git processes it started. */
- const run = (...args) => {
+ const runIn = (cwd, ...args) => {
writeFileSync(env.PULLBOARD_GIT_CALLS, '');
writeFileSync(env.PULLBOARD_MODULE_LOG, '');
- const result = runFixtureChild(process.execPath, ['--import', hook, BIN, ...args], { cwd: repo, env, encoding: 'utf8' });
+ const result = runFixtureChild(process.execPath, ['--import', hook, BIN, ...args], { cwd, env, encoding: 'utf8' });
assert.equal(result.status, 0, `${args.join(' ')}: ${result.stdout}${result.stderr}`);
const modules = [...new Set(readFileSync(env.PULLBOARD_MODULE_LOG, 'utf8').split('\n').filter(Boolean))]
.map((url) => fileURLToPath(url)).filter((path) => path.startsWith(REPO + sep)).map((path) => path.slice(REPO.length + 1).split(sep).join('/'));
const gitCalls = readFileSync(env.PULLBOARD_GIT_CALLS, 'utf8').split('\n').filter(Boolean);
return { ...result, modules, gitCalls };
};
+ const run = (...args) => runIn(repo, ...args);
const git = (...args) => assert.equal(runFixtureChild(env.PULLBOARD_REAL_GIT, args, { cwd: repo, env, encoding: 'utf8' }).status, 0);
git('init', '-q', '-b', 'main');
writeFileSync(join(repo, 'pullboard.json'), JSON.stringify({
@@ -98,7 +100,10 @@ function fixture(t) {
git('add', '-A');
git('commit', '-q', '-m', 'chore(repo): set up the startup fixture');
run('add', 'core', 'Show me', '--specs', 'C7', '--criterion', 'it shows', '--brief', 'Files: src/a.js\nTest: it shows.');
- return { run };
+ const worktree = join(dir, 'core-1');
+ git('worktree', 'add', '-q', '-b', 'core/one', worktree);
+ runIn(worktree, 'join', 'core', '--model', 'test-model');
+ return { run, runIn, worktree };
}
test('a read command loads only its own modules [C7]', (t) => {
@@ -111,4 +116,13 @@ test('a read command loads only its own modules [C7]', (t) => {
assert.match(show.stdout, /^#1 {2}/u);
assert.deepEqual(show.modules.filter((path) => HEAVY.includes(path)), [], 'show loads no gate, relay or land module');
assert.ok(show.gitCalls.length <= 2, `show started ${show.gitCalls.length} Git processes: ${show.gitCalls.join(' | ')}`);
+ // One rev-parse in the worktree answers top level, Git dirs, HEAD and the primary checkout for the whole process;
+ // the only other is the code's own checkout, asked once where the running code came from.
+ const claim = box.runIn(box.worktree, 'claim', '1');
+ assert.match(claim.stdout, /claimed #1/u);
+ const revParses = claim.gitCalls.filter((call) => /\brev-parse\b/u.test(call));
+ const here = revParses.filter((call) => call.startsWith(`${box.worktree}\t`));
+ assert.equal(here.length, 1, `claim asked the worktree for repo facts ${here.length} times: ${here.join(' | ')}`);
+ assert.ok(revParses.length <= 2, `claim started ${revParses.length} rev-parse processes: ${revParses.join(' | ')}`);
+ assert.ok(claim.gitCalls.length <= 10, `claim started ${claim.gitCalls.length} Git processes: ${claim.gitCalls.join(' | ')}`);
});