Skip to content

Commit 10e6d48

Browse files
committed
fix(hooks): align pre-commit.mts .env detection with commit-msg.mts (basename-based)
Bugbot flagged: pre-commit.mts tested the full path against /^\.env/ (catching only root-level), while commit-msg.mts uses basename() (catching .env at any depth). A nested packages/cli/.env.local should be blocked at commit time, not just root .env. Aligned both to basename-based matching, and added .env.precommit to pre-commit's allowlist so it matches commit-msg's allowlist.
1 parent 30f02d5 commit 10e6d48

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

.git-hooks/pre-commit.mts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Bypassable: --no-verify skips this hook entirely. Use sparingly
99
// (hotfixes, history operations, pre-build states).
1010

11+
import { basename } from 'node:path'
1112
import process from 'node:process'
1213

1314
import {
@@ -62,11 +63,18 @@ const main = (): number => {
6263
errors++
6364
}
6465

65-
// .env files (allowlist .env.example / .env.test).
66+
// .env files at any depth — allow only .env.example, .env.test,
67+
// .env.precommit (templates / tracked placeholders). Match the
68+
// commit-msg.mts behavior: a nested .env.local is just as much a
69+
// leak as a root-level one. basename() catches both.
6670
out('Checking for .env files...')
67-
const envFiles = stagedFiles.filter(
68-
f => /^\.env(\.[^/]+)?$/.test(f) && !/^\.env\.(example|test)$/.test(f),
69-
)
71+
const envFiles = stagedFiles.filter(f => {
72+
const base = basename(f)
73+
return (
74+
/^\.env(\.[^/]+)?$/.test(base) &&
75+
!/^\.env\.(example|test|precommit)$/.test(base)
76+
)
77+
})
7078
if (envFiles.length > 0) {
7179
out(red('✗ ERROR: .env file detected!'))
7280
envFiles.forEach(f => out(f))

0 commit comments

Comments
 (0)