diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9b989f0..2423a15 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,27 @@ repos: -- repo: git://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files +#- repo: git://github.com/pre-commit/pre-commit-hooks +# rev: v3.2.0 +# hooks: +# - id: trailing-whitespace +# - id: end-of-file-fixer +# - id: check-yaml +# - id: check-added-large-files +# +#- repo: git://github.com/pycqa/pylint +# rev: pylint-2.6.0 +# hooks: +# - id: pylint +# args: ["--fail-under=8"] -- repo: git://github.com/pycqa/pylint - rev: pylint-2.6.0 +- repo: local hooks: - - id: pylint - args: ["--fail-under=8"] \ No newline at end of file + - id: commit-msg-regex-hook + name: commit-msg-regex-hook + entry: commit-msg-regex-hook + language: python + args: ["--pattern='Signed-off-by: .* <(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|'(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*')@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])>'", + "--debug"] + #args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'", + # "--debug"] + stages: [commit-msg] + diff --git a/README.md b/README.md index 6689952..15aeb17 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,28 @@ Finally you can add this to your .pre-commit-config.yaml: ``` - repo: https://github.com/dtaivpp/commit-msg-regex-hook - rev: v0.1.0 + rev: v0.2.0 hooks: - - id: commit-msg-hook - args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'"] + - id: commit-msg-regex-hook + args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'", + "--failure_message='Commits should match the pattern: Card-ID | Name | Message'"] stages: [commit-msg] ``` **note: the backslashes in regex need to be escaped need to be escaped -**double note: if you are having issues you can run with the --debug argument as well for additional logging. +**double note: if you are having issues you can run with the --debug argument as well for additional logging. +With this you can achieve so many things. A good example is verifing deveopers have signed the DCO before allowing a commit. + +``` +- repo: https://github.com/dtaivpp/commit-msg-regex-hook + rev: v0.2.0 + hooks: + - id: commit-msg-regex-hook + args: ["--pattern='Signed-off-by: .* <(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|'(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*')@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])>'", + "--failure_message='Ensure you are signing the DCO'"] + stages: [commit-msg] +``` ### Developing this project diff --git a/commit_msg_regex_hook/commit_msg_regex_hook.py b/commit_msg_regex_hook/commit_msg_regex_hook.py index 7e907a0..e8b34c6 100644 --- a/commit_msg_regex_hook/commit_msg_regex_hook.py +++ b/commit_msg_regex_hook/commit_msg_regex_hook.py @@ -92,7 +92,7 @@ def run_checks(checks: list): result = check() if not result.is_passing(): - logger.error("Check Failed:\n {failure}", failure=result.message) + logger.error(f"Check Failed:\n {result.message}") sys.exit(1) logger.debug(result.message) @@ -139,7 +139,7 @@ def message_not_empty(message: str) -> Result: """Verify the commit message is not empty """ def check(): - logger.debug("Current Message: {message}", message=message) + logger.debug(f"Current Message: {message}") if len(message.strip()) == 0: return Result("ֿError: commit message cannot be empty", FAIL) @@ -152,9 +152,9 @@ def message_pattern_match(message: str, pattern: Pattern, failure_message: str) """Verify the commit message matches the pattern """ def check(): - logger.debug("Pattern: {regex}\nMessage: {message}", regex=pattern, message=message) + logger.debug(f"Pattern: {pattern}\nMessage: {message}") - if not pattern.match(message): + if not pattern.search(message): # Fail the commit message return Result(f"""{failure_message}\n\t Pattern: {pattern}\n\t