diff --git a/.github/scripts/release-tag.test.mjs b/.github/scripts/release-tag.test.mjs index d98f62270..08bee7ac4 100644 --- a/.github/scripts/release-tag.test.mjs +++ b/.github/scripts/release-tag.test.mjs @@ -11,6 +11,36 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' +// A pre-push hook in a linked worktree exports GIT_DIR. Simulate that with a +// sentinel repo, then build the child env once without the hook's git +// variables. Every spawn in this file uses that env, so the sentinel must +// stay unchanged. +const sentinel = mkdtempSync(join(tmpdir(), 'virtual-git-sentinel-')) +process.env.GIT_DIR = join(sentinel, '.git') +const hookGitVars = [ + 'GIT_DIR', + 'GIT_WORK_TREE', + 'GIT_INDEX_FILE', + 'GIT_COMMON_DIR', +] +const childEnv = { ...process.env } +for (const key of hookGitVars) delete childEnv[key] +const sentinelGit = (args) => { + const result = spawnSync('git', args, { + cwd: sentinel, + encoding: 'utf8', + env: childEnv, + }) + assert.equal(result.status, 0, result.stderr) + return result.stdout.trim() +} +sentinelGit(['init', '-q', '-b', 'sentinel']) +sentinelGit(['config', 'user.name', 'sentinel']) +sentinelGit(['config', 'user.email', 'sentinel@example.invalid']) +sentinelGit(['commit', '-q', '--allow-empty', '-m', 'sentinel']) +sentinelGit(['tag', 'sentinel-tag']) +const sentinelRefs = sentinelGit(['for-each-ref']) + const script = fileURLToPath(new URL('./release-tag.mjs', import.meta.url)) const dir = mkdtempSync(join(tmpdir(), 'virtual-release-tag-')) const repo = join(dir, 'repo') @@ -48,17 +78,24 @@ console.error('unexpected gh call:', args); process.exit(2); `, ) -const chmod = spawnSync('chmod', ['+x', join(bin, 'gh')], { encoding: 'utf8' }) +const chmod = spawnSync('chmod', ['+x', join(bin, 'gh')], { + encoding: 'utf8', + env: childEnv, +}) assert.equal(chmod.status, 0, chmod.stderr) const env = { - ...process.env, + ...childEnv, PATH: `${bin}:${process.env.PATH}`, GH_LOG: ghLog, GH_MARKER: ghMarker, } function git(args) { - const result = spawnSync('git', args, { cwd: repo, encoding: 'utf8' }) + const result = spawnSync('git', args, { + cwd: repo, + encoding: 'utf8', + env: childEnv, + }) assert.equal(result.status, 0, result.stderr) return result.stdout.trim() } @@ -84,6 +121,7 @@ try { const init = spawnSync('git', ['init', '-q', '-b', 'rigel-release'], { cwd: repo, encoding: 'utf8', + env: childEnv, }) assert.equal(init.status, 0, init.stderr) git(['config', 'user.name', 'test']) @@ -183,3 +221,13 @@ try { } finally { rmSync(dir, { recursive: true, force: true }) } +try { + assert.equal( + sentinelGit(['for-each-ref']), + sentinelRefs, + 'FAIL GIT_DIR leaked into child git', + ) + console.log('ok GIT_DIR from a hook leaves the sentinel repo unchanged') +} finally { + rmSync(sentinel, { recursive: true, force: true }) +} diff --git a/.github/scripts/workflow-contract.test.mjs b/.github/scripts/workflow-contract.test.mjs index 8d770ec8d..aac6cd555 100644 --- a/.github/scripts/workflow-contract.test.mjs +++ b/.github/scripts/workflow-contract.test.mjs @@ -11,6 +11,36 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' +// A pre-push hook in a linked worktree exports GIT_DIR. Simulate that with a +// sentinel repo, then build the child env once without the hook's git +// variables. Every spawn in this file uses that env, so the sentinel must +// stay unchanged. +const sentinel = mkdtempSync(join(tmpdir(), 'virtual-git-sentinel-')) +process.env.GIT_DIR = join(sentinel, '.git') +const hookGitVars = [ + 'GIT_DIR', + 'GIT_WORK_TREE', + 'GIT_INDEX_FILE', + 'GIT_COMMON_DIR', +] +const childEnv = { ...process.env } +for (const key of hookGitVars) delete childEnv[key] +const sentinelGit = (args) => { + const result = spawnSync('git', args, { + cwd: sentinel, + encoding: 'utf8', + env: childEnv, + }) + assert.equal(result.status, 0, result.stderr) + return result.stdout.trim() +} +sentinelGit(['init', '-q', '-b', 'sentinel']) +sentinelGit(['config', 'user.name', 'sentinel']) +sentinelGit(['config', 'user.email', 'sentinel@example.invalid']) +sentinelGit(['commit', '-q', '--allow-empty', '-m', 'sentinel']) +sentinelGit(['tag', 'sentinel-tag']) +const sentinelRefs = sentinelGit(['for-each-ref']) + const scriptsDir = fileURLToPath(new URL('.', import.meta.url)) const root = fileURLToPath(new URL('../..', import.meta.url)) const publisherPath = join(root, '.github/workflows/publish-rigelbuild.yml') @@ -43,7 +73,7 @@ try { const dispatch = spawnSync(process.execPath, [resolverPath], { encoding: 'utf8', env: { - ...process.env, + ...childEnv, EVENT_NAME: 'workflow_dispatch', REF: 'refs/heads/rigel-release', GITHUB_OUTPUT: outputPath, @@ -82,13 +112,18 @@ passes( const guardDir = mkdtempSync(join(tmpdir(), 'virtual-publisher-guard-')) try { const runGit = (args) => { - const result = spawnSync('git', args, { cwd: guardDir, encoding: 'utf8' }) + const result = spawnSync('git', args, { + cwd: guardDir, + encoding: 'utf8', + env: childEnv, + }) assert.equal(result.status, 0, result.stderr) return result.stdout.trim() } const init = spawnSync('git', ['init', '-q', '-b', 'rigel-release'], { cwd: guardDir, encoding: 'utf8', + env: childEnv, }) assert.equal(init.status, 0, init.stderr) runGit(['config', 'user.name', 'test']) @@ -108,7 +143,7 @@ try { cwd: guardDir, encoding: 'utf8', env: { - ...process.env, + ...childEnv, EVENT_NAME: 'push', REF: 'refs/tags/v3.0.0-rigel.1', GITHUB_OUTPUT: join(guardDir, 'accepted-output'), @@ -128,7 +163,7 @@ try { cwd: guardDir, encoding: 'utf8', env: { - ...process.env, + ...childEnv, EVENT_NAME: 'push', REF: 'refs/tags/v3.0.0-rigel.2', GITHUB_OUTPUT: join(guardDir, 'rejected-output'), @@ -151,7 +186,7 @@ try { cwd: guardDir, encoding: 'utf8', env: { - ...process.env, + ...childEnv, EVENT_NAME: 'push', REF: 'refs/tags/v3.0.0-rigel.3', GITHUB_OUTPUT: join(guardDir, 'detached-output'), @@ -174,7 +209,7 @@ try { cwd: guardDir, encoding: 'utf8', env: { - ...process.env, + ...childEnv, EVENT_NAME: 'push', REF: 'refs/tags/v3.0.0-rc.1', GITHUB_OUTPUT: join(guardDir, 'bad-shape-output'), @@ -186,3 +221,13 @@ try { } finally { rmSync(guardDir, { recursive: true, force: true }) } +try { + assert.equal( + sentinelGit(['for-each-ref']), + sentinelRefs, + 'FAIL GIT_DIR leaked into child git', + ) + console.log('ok GIT_DIR from a hook leaves the sentinel repo unchanged') +} finally { + rmSync(sentinel, { recursive: true, force: true }) +} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index db6bb3617..5ec456c2b 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -16,7 +16,7 @@ permissions: jobs: test: name: Test - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - name: Checkout uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 diff --git a/hk.pkl b/hk.pkl new file mode 100644 index 000000000..9362031c6 --- /dev/null +++ b/hk.pkl @@ -0,0 +1,15 @@ +// Git hooks managed by hk (https://hk.jdx.dev). +amends "package://github.com/jdx/hk/releases/download/v2.11.1/hk@2.11.1#/Config.pkl" + +default_branch = "origin/rigel-release" + +hooks { + ["pre-push"] { + steps { + ["fork-release-contracts"] = new Step { + glob = List(".github/workflows/**", ".github/scripts/**") + check = "node .github/scripts/workflow-contract.test.mjs && node .github/scripts/release-tag.test.mjs" + } + } + } +}