Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pre-commit.com hook to reject commit if a file contains a specific string

Is there already a pre-commit.com hook to reject commit if a file contains a specific string?

I know how to do this with a git pre-commit hook: https://stackoverflow.com/a/26836281/633961

But I would like to go the pre-commit.com way because this would streamline my way to a way which is done by other developers.

For example I would like to use the same string which other people use, too.

like image 433
guettli Avatar asked Sep 03 '26 21:09

guettli


1 Answers

The easiest way is to use a language: pygrep hook. This implements a regex matching againsta file

pygrep is especially useful for a repo: local hook which lives directly in your repository's .pre-commit-config.yaml file

an example which forbids DONTSHIP:

-   repo: local
    hooks:
    -   id: dontship
        name: DONTSHIP check
        entry: DONTSHIP
        language: pygrep
        types: [text]

disclaimer: I'm the creator of pre-commit

like image 115
Anthony Sottile Avatar answered Sep 06 '26 20:09

Anthony Sottile