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.

fix(hooks): a hook stops on a Pullboard version mismatch [C1,I2]

Corey Olson6c964ae72553
diff --git a/src/hooks.js b/src/hooks.js
index 762b56a1..d4a51ef4 100644
--- a/src/hooks.js
+++ b/src/hooks.js
@@ -588,24 +588,48 @@ export function pushesTrunk(refsText, trunk) {
});
}
+/** The Pullboard version that writes the hooks, read once from the package it ships in. */
+const INSTALLER_VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
+
/**
* The shell script for one hook: it runs pullboard from this checkout's install, else the main
* checkout's, which a fresh agent worktree shares, else the PATH.
*
+ * The script carries the version that wrote it and asks the binary it found for its own before running
+ * any rule (#876): an older global install would otherwise apply its old rules under a newer CLI's
+ * guidance, and an old binary cannot be trusted to notice. A mismatch stops the hook, names both
+ * versions, and gives the one command that fixes it: install the hook's version (project or global, where
+ * the binary came from) when the found one is older, or rewrite the hooks with `pullboard init` when newer.
+ *
* @param {string} hook
+ * @param {string} [version] The installing version; tests pass their own.
* @returns {string}
*/
-export function hookScript(hook) {
- const args = hook === 'commit-msg' ? ' "$1"' : '';
+export function hookScript(hook, version = INSTALLER_VERSION) {
+ const args = hook === 'commit-msg' ? ' "$message"' : '';
return [
'#!/bin/sh',
`# ${HOOK_MARK}: runs the pullboard ${hook} checks. Edit pullboard.json, not this file.`,
+ `want='${version}'`,
+ ...(hook === 'commit-msg' ? ['message="$1"'] : []),
'root=$(git rev-parse --show-toplevel)',
'main=$(git rev-parse --path-format=absolute --git-common-dir)/..',
+ 'run() {',
+ ' bin="$1"; scope="$2"',
+ // Shell builtins only, so a hook works on a PATH that has no tail or sed: keep the last line of --version.
+ ' nl=$(printf \'\\n_\'); nl=${nl%_}; have=$("$bin" --version 2>/dev/null); have=${have##*"$nl"}',
+ ' if [ "$have" != "$want" ]; then',
+ ' newer=$(node -e \'const [a,b]=process.argv.slice(1).map(v=>String(v).split(".").map(n=>parseInt(n,10)||0));for(let i=0;i<3;i++)if(a[i]!==b[i]){process.stdout.write(a[i]>b[i]?"yes":"no");process.exit(0)}process.stdout.write("no")\' "$have" "$want" 2>/dev/null)',
+ ' if [ "$newer" = yes ]; then fix="pullboard init"; else fix="npm i $scope pullboard@$want"; fi',
+ ` echo "pullboard ${hook}: stopped before running any rule; these hooks were installed by Pullboard $want, but $bin is Pullboard \${have:-unknown}. Run: $fix" >&2`,
+ ' exit 1',
+ ' fi',
+ ` exec "$bin" hook ${hook}${args}`,
+ '}',
'for bin in "$root/node_modules/.bin/pullboard" "$main/node_modules/.bin/pullboard"; do',
- ` if [ -x "$bin" ]; then exec "$bin" hook ${hook}${args}; fi`,
+ ' if [ -x "$bin" ]; then run "$bin" -D; fi',
'done',
- `if command -v pullboard >/dev/null 2>&1; then exec pullboard hook ${hook}${args}; fi`,
+ 'if command -v pullboard >/dev/null 2>&1; then run pullboard -g; fi',
'echo "pullboard is not installed; npm i -D pullboard, or npm i -g pullboard" >&2',
'exit 1',
'',
diff --git a/test/hooks.test.js b/test/hooks.test.js
index eab71bba..2274c4ba 100644
--- a/test/hooks.test.js
+++ b/test/hooks.test.js
@@ -2,6 +2,7 @@
* The commit-message rules (C1, C2) and the pre-commit pattern checks on real staged changes.
*/
import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
import { runFixtureExecFile as execFileSync, runFixtureGit } from './fixture-child.js';
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
@@ -253,3 +254,44 @@ test('a spec-less feature commit is told a path that works [S1,B26]', () => {
assert.doesNotMatch(advice.join('\n'), /\[G1(?:,G2)?\]/u, 'a fenced example is never suggested as a citation');
}
});
+
+test('a hook names a Pullboard version mismatch instead of running old rules [C1,I2]', (t) => {
+ const box = stagedRepo(t);
+ const hooks = join(box.root, '.githooks'), bin = join(box.root, '.fakebin'), local = join(box.root, 'node_modules', '.bin');
+ mkdirSync(hooks); mkdirSync(bin);
+ box.git('config', 'core.hooksPath', '.githooks');
+ box.git('config', 'user.name', 'Fixture');
+ box.git('config', 'user.email', '[email protected]');
+ writeFileSync(join(hooks, 'commit-msg'), hookScript('commit-msg', '0.9.0'));
+ chmodSync(join(hooks, 'commit-msg'), 0o755);
+ /** A pullboard that reports one version and says so if its rules ever run. */
+ const fake = (dir, version) => {
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 'pullboard'), `#!/bin/sh\nif [ "$1" = --version ]; then echo ${version}; exit 0; fi\necho "rules of ${version} ran" >&2\nexit 0\n`);
+ chmodSync(join(dir, 'pullboard'), 0o755);
+ };
+ const env = { ...process.env, PATH: `${bin}:${process.env.PATH}`, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
+ const commit = () => spawnSync('git', ['commit', '--allow-empty', '-q', '-m', 'chore: try a commit'], { cwd: box.root, env, encoding: 'utf8' });
+
+ fake(bin, '0.8.4');
+ let result = commit();
+ assert.equal(result.status, 1, 'an older global pullboard is stopped');
+ assert.match(result.stderr, /stopped before running any rule; these hooks were installed by Pullboard 0\.9\.0, but pullboard is Pullboard 0\.8\.4\. Run: npm i -g pullboard@0\.9\.0/u);
+ assert.doesNotMatch(result.stderr, /rules of 0\.8\.4 ran/u, 'the old rules never run');
+
+ fake(local, '0.8.4');
+ result = commit();
+ assert.equal(result.status, 1, 'an older project install is stopped');
+ assert.match(result.stderr, /Pullboard 0\.8\.4\. Run: npm i -D pullboard@0\.9\.0/u, 'a project install is fixed in the project');
+ rmSync(join(box.root, 'node_modules'), { recursive: true, force: true });
+
+ fake(bin, '0.9.9');
+ result = commit();
+ assert.equal(result.status, 1, 'a newer pullboard under older hooks is stopped too');
+ assert.match(result.stderr, /Pullboard 0\.9\.9\. Run: pullboard init/u, 'newer rules rewrite the hooks with init');
+
+ fake(bin, '0.9.0');
+ result = commit();
+ assert.equal(result.status, 0, `the matching version runs: ${result.stderr}`);
+ assert.match(result.stderr, /rules of 0\.9\.0 ran/u, 'the hook runs the version that installed it');
+});