Jaydubz
Jaydubz

Reputation: 19

Pre-commit finds local hooks even after deleting the hooks directory and clearing the cache

I am using pre-commit to manage local hooks for my repository. Initially, I added a local repository containing my hooks and configured the .pre-commit-config.yaml file to reference these hooks. It worked perfectly both in my local environment and in the CI pipeline.

snippet of code from .pre-commit-config.yaml. I have installed pre_commit_hooks as a local module, configed

  - repo: local
    hooks:
      - id: trailing-whitespace
        name: Trim trailing-whitespace
        language: python
        entry: python3 -m pre_commit_hooks.trailing_whitespace_fixer
        additional_dependencies: [pre_commit_hooks] 

However, I noticed a strange behavior:

I cannot figure out how pre-commit is able to find and execute the hooks after the source directory has been deleted.

My questions:

I have tried:

  1. Cleared Pre-Commit Cache:

    • Ran pre-commit clean and manually removed the cache directory (e.g., ~/.cache/pre-commit).

    • Verified the cache directory was empty.

  2. Removed Git Hooks:

    • Checked .git/hooks/ for any residual scripts and deleted the pre-commit script.
  3. Verified Configuration:

    • Removed the .pre-commit-config.yaml file temporarily, and as expected, pre-commit failed to run.
  4. Inspected Pre-Commit’s Behavior:

    • Ran pre-commit run --verbose to trace execution but didn’t find useful information about where the hooks were being sourced from.

Upvotes: 1

Views: 69

Answers (1)

anthony sottile
anthony sottile

Reputation: 70175

pre-commit will always ensure that the configured repositories are installed. pre-commit install just installs the .git/hooks/<whatever> script (unless you specify --install-hooks

kind of the magic (and point) of pre-commit is as long as you've run pre-commit install once it will always make sure that it runs exactly what is configured

if you show your output it should be pretty obvious (it will say something about installing hooks for local ...)


or perhaps you're confused what additional_dependencies: [pre_commit_hooks] does -- this installs https://pypi.org/p/pre-commit-hooks (and definitely not your ./pre_commit_hooks -- pre-commit never installs from the repository under test)


disclaimer: I wrote pre-commit

Upvotes: 1

Related Questions