guettli
guettli

Reputation: 27816

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.

Upvotes: 5

Views: 1628

Answers (1)

anthony sottile
anthony sottile

Reputation: 69914

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

Upvotes: 10

Related Questions