pre-commit check-license-header hook can silently corrupt commits made from a git worktree
Summary
The local check-license-header hook in .pre-commit-config.yaml can overwrite the git index of the worktree being committed from, causing git commit to record a commit whose tree is the license-checker repository's tree instead of the staged changes. The resulting commit is unreadable (fatal: unable to read <blob>) and contains none of the author's work.
Only commits made from a git worktree are affected. A plain top-level checkout is not.
Cause
- id: check-license-header
name: check license header
pass_filenames: false
language: system
entry: npm
args: ['exec', '--', 'github:viperproject/check-license-header#v1', 'check', '--config', './.github/workflows/check-license-header.json']Two things combine:
- The hook has no
stages:key, so unlike itspyrightandpytestneighbours (bothstages: [pre-push]) it runs on every commit. npm exec github:<repo>resolves a git dependency by shelling out togit. Inside a worktree,git commitexports bothGIT_DIRand an absoluteGIT_INDEX_FILEto its hooks. The nested git inherits them and writes its own index over the committing worktree's index.git commitbuilds the tree from the index only after hooks return, so it commits the fetched repository's tree.
In a plain checkout GIT_INDEX_FILE is relative and GIT_DIR is not exported, which is why the problem is worktree-specific.
Reproduction
In a worktree of this repo, with a healthy index copied aside:
export GIT_DIR=/path/to/repo/.git
export GIT_INDEX_FILE=/tmp/index-copy
npm exec -- github:viperproject/check-license-header#v1 check --config ./.github/workflows/check-license-header.jsonThe index goes from ~536 KB / ~3800 entries to 3644 bytes / 37 entries, and those 37 entries are the checker repo's own files (action.yml, dist/, jest.config.js, tsconfig.json, ...). An ordinary git commit in a worktree with the unmodified hook chain reproduces the same wrong tree on a real commit.
Suggested fix
Either of these stops it, and the license check still runs and still catches a missing header:
entry: env -u GIT_DIR -u GIT_INDEX_FILE -u GIT_WORK_TREE npmor move the hook to stages: [pre-push], consistent with the pyright and pytest hooks.
Pinning the #v1 ref or adding npm --offline does not help: npm still shells out to git against its own cache.
Impact
Silent data loss for contributors who use git worktree. The commit message and parent are correct, so nothing looks wrong until the commit is read back.
Source: awslabs/mcp