Is there an option to run automatically a specific pre-commit hook on all the files, even if they have not been modified?

389 Views Asked by At

My usecase is quite specific: I have a file containing a version and several other files of the project need to have the same embedded version.

The idea is to have a pre-commit hook that check these other files and eventually update them at commit time.

Please note that it works fine with pre-commit run --all, but I would like to automate it when the git commit command is run.

As far as I understand, only the modified files of the repository are passed to the executable of the hook. The unmodified ones are skipped.

My .pre-commit-config.yaml file looks like:

-   repo: https://github.com/degauden/euclid-pipeline-hooks.git
    rev: 1.0.0
    hooks:
    -   id: dependency-version-fixer
        always_run: true

If I modify the version in my CMakeLists.txt and run a git commit, I get:

Dependency Version Fixer.................................................Passed
[develop d7bd259] Move to 1.5

Which is not what I expect.

If I run a pre-commit run --all, I get the expected result:

Dependency Version Fixer.................................................Failed
- hook id: dependency-version-fixer
- exit code: 1
- files were modified by this hook

Fixing SLE_IAL_Pipelines/auxdir/SLE_Detection_Pipeline/PkgDef_SLE_Detection.py
Fixing SLE_IAL_Pipelines/auxdir/SLE_Modeling_Pipeline/PkgDef_SLE_Modeling.py

How can I have the 2 python files fixed by a git commit if they have not been modified (are not in staged state I guess)?

Thanks for your help.

1

There are 1 best solutions below

0
On

your premise is kind of a bad one -- needing to update every file all the time always is a mess and is going to balloon your git repo size.

you probably want to store the version in one place and use your programming language's import mechanism to share it instead.

auto-bumping versions as part of the commit is also a bad idea because there's no good way to undo it.

if you really want this bad behaviour make your tool discover every file and do it that way -- the framework intentionally doesn't support something like what you want. you can do something like git ls-files -z and then iterate through it in your script. in that case you'd want to set pass_filenames: false as well such that pre-commit doesn't try and run it on the particular files that are changed


disclaimer: I wrote pre-commit