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

1

5

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://mcmap.net/q/681218/-git-hook-to-reject-commits-where-files-contain-a-specific-string

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.

Peloquin answered 12/2, 2021 at 10:36 Comment(0)
B
10

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

Berk answered 12/2, 2021 at 11:33 Comment(2)
How to check multiple strings using regex?Embosser
@Embosser that's a separate and unrelated question, but |Berk

© 2022 - 2024 — McMap. All rights reserved.