diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1e118f4..b4614a7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.1.0 + rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 6c1c733..cadda37 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -164,9 +164,9 @@ - id: no-commit-keywords name: "Don't commit when nocommit keyword is present" entry: no-commit-keywords - language: script - always_run: true + language: python types: [text] + stages: [commit, push, manual] - id: no-commit-to-branch name: "Don't commit to branch" entry: no-commit-to-branch diff --git a/pre_commit_hooks/no_commit_keywords.py b/pre_commit_hooks/no_commit_keywords.py new file mode 100644 index 0000000..b581a68 --- /dev/null +++ b/pre_commit_hooks/no_commit_keywords.py @@ -0,0 +1,47 @@ +import argparse +import os +from typing import Optional +from typing import Sequence +import re + + +def check_for_no_commit_tokens( + filename: str, + tokens: Optional[Sequence[str]], +) -> str: + with open(filename, mode='r', encoding='utf-8') as file_processed: + lines = file_processed.read() + for token in tokens: + match = re.search(token, lines) + if match: + # no-commit token is present + return match.string + return None + + +def main(argv: Optional[Sequence[str]] = None) -> int: + parser = argparse.ArgumentParser() + parser.add_argument( + '--tokens', + help=( + 'The set of tokens, separated by space, used to block commits ' + 'if present. For example, "nocommit NOCOMMIT"' + ), + ) + parser.add_argument('filenames', nargs='*', help='Filenames to fix') + args = parser.parse_args(argv) + + return_code = 0 + tokens = args.tokens or ["nocommit"] + + for filename in args.filenames: + _, extension = os.path.splitext(filename.lower()) + match_string = check_for_no_commit_tokens(filename, tokens) + if match_string: + print(f'Found no-commit token in {filename}: {match_string}') + return_code = 1 + return return_code + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/no_commit_keywords.sh b/pre_commit_hooks/no_commit_keywords.sh deleted file mode 100644 index c9dd19e..0000000 --- a/pre_commit_hooks/no_commit_keywords.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -NOCOMMIT_PRESENT=$(git grep --ignore-case nocommit) - -if [ -n $NOCOMMIT_PRESENT ] -then - echo "#nocommit tagged - commit prevented" - echo $NOCOMMIT_PRESENT - exit 1 -else - exit 0 -fi - -