#4668·mcp

pre-commit check-license-header hook can silently corrupt commits made from a git worktree

Author: markjschreiberCreated Sep 24, 2026Updated Sep 24, 2026
Labels👮admin👮

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

yaml
-   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:

  1. The hook has no stages: key, so unlike its pyright and pytest neighbours (both stages: [pre-push]) it runs on every commit.
  2. npm exec github:<repo> resolves a git dependency by shelling out to git. Inside a worktree, git commit exports both GIT_DIR and an absolute GIT_INDEX_FILE to its hooks. The nested git inherits them and writes its own index over the committing worktree's index. git commit builds 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:

bash
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.json

The 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:

yaml
entry: env -u GIT_DIR -u GIT_INDEX_FILE -u GIT_WORK_TREE npm

or 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.