|  | 
| 1 |  | -"""This module contains source for commitlint""" | 
|  | 1 | +""" | 
|  | 2 | +This module provides functionality for validating commit messages according | 
|  | 3 | +to conventional commit standards. | 
|  | 4 | +
 | 
|  | 5 | +Usage: | 
|  | 6 | +------ | 
|  | 7 | +
 | 
|  | 8 | +```python | 
|  | 9 | +from commitlint import check_commit_message | 
|  | 10 | +
 | 
|  | 11 | +commit_message = "feat(module): add module documentation" | 
|  | 12 | +success, errors = check_commit_message(commit_message) | 
|  | 13 | +``` | 
|  | 14 | +""" | 
| 2 | 15 | import re | 
| 3 |  | -import sys | 
|  | 16 | +from typing import List, Tuple | 
|  | 17 | + | 
|  | 18 | +from .constants import COMMIT_MAX_LENGTH | 
|  | 19 | +from .messages import HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR | 
|  | 20 | + | 
|  | 21 | +CONVENTIONAL_COMMIT_PATTERN = ( | 
|  | 22 | +    r"(?s)"  # To explicitly make . match new line | 
|  | 23 | +    r"(?P<type>build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|bump)" | 
|  | 24 | +    r"(?P<scope>\(\S+\))?!?:" | 
|  | 25 | +    r"(?: (?P<description>[^\n\r]+))" | 
|  | 26 | +    r"((\n\n(?P<body>.*))|(\s*))?$" | 
|  | 27 | +) | 
| 4 | 28 | 
 | 
| 5 |  | -COMMIT_MAX_LENGTH = 72 | 
|  | 29 | +IGNORED_PATTERN = ( | 
|  | 30 | +    r"^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)|" | 
|  | 31 | +    r"^(Merge tag (.*?))(?:\r?\n)*$|" | 
|  | 32 | +    r"^(R|r)evert (.*)|" | 
|  | 33 | +    r"^(Merged (.*?)(in|into) (.*)|Merged PR (.*): (.*))$|" | 
|  | 34 | +    r"^Merge remote-tracking branch(\s*)(.*)$|" | 
|  | 35 | +    r"^Automatic merge(.*)$|" | 
|  | 36 | +    r"^Auto-merged (.*?) into (.*)$" | 
|  | 37 | +) | 
| 6 | 38 | 
 | 
| 7 | 39 | 
 | 
| 8 |  | -def is_conventional_commit(commit_message: str) -> bool: | 
|  | 40 | +def is_ignored(commit_message: str) -> bool: | 
| 9 | 41 |     """ | 
| 10 |  | -    Checks if a commit message follows the conventional commit format. | 
|  | 42 | +    Checks if a commit message should be ignored. | 
|  | 43 | +
 | 
|  | 44 | +    Some commit messages like merge, revert, auto merge, etc is ignored | 
|  | 45 | +    from linting. | 
| 11 | 46 | 
 | 
| 12 | 47 |     Args: | 
| 13 |  | -        commit_message (str): The commit message to be checked. | 
|  | 48 | +        commit_message (str): The commit message to check. | 
| 14 | 49 | 
 | 
| 15 | 50 |     Returns: | 
| 16 |  | -        bool: True if the commit message follows the conventional commit format, | 
| 17 |  | -              False otherwise. | 
|  | 51 | +        bool: True if the commit message should be ignored, False otherwise. | 
| 18 | 52 |     """ | 
| 19 |  | -    pattern = re.compile(r"^(\w+)(\([^\)]+\))?: .+") | 
| 20 |  | -    return bool(pattern.match(commit_message)) | 
|  | 53 | +    return bool(re.match(IGNORED_PATTERN, commit_message)) | 
| 21 | 54 | 
 | 
| 22 | 55 | 
 | 
| 23 |  | -def is_valid_length(commit_message: str, max_length: int) -> bool: | 
| 24 |  | -    """ | 
| 25 |  | -    Checks if a commit message has a valid length. | 
|  | 56 | +def remove_comments(msg: str) -> str: | 
|  | 57 | +    """Removes comments from the commit message. | 
|  | 58 | +
 | 
|  | 59 | +    For `git commit --verbose`, excluding the diff generated message, | 
|  | 60 | +    for example: | 
|  | 61 | +
 | 
|  | 62 | +    ```bash | 
|  | 63 | +    ... | 
|  | 64 | +    # ------------------------ >8 ------------------------ | 
|  | 65 | +    # Do not modify or remove the line above. | 
|  | 66 | +    # Everything below it will be ignored. | 
|  | 67 | +    diff --git a/... b/... | 
|  | 68 | +    ... | 
|  | 69 | +    ``` | 
| 26 | 70 | 
 | 
| 27 | 71 |     Args: | 
| 28 |  | -        commit_message (str): The commit message to be checked. | 
| 29 |  | -        max_length (int): The maximum allowed length for the commit message. | 
|  | 72 | +        msg(str): The commit message to remove comments. | 
| 30 | 73 | 
 | 
| 31 | 74 |     Returns: | 
| 32 |  | -        bool: True if the commit message length is valid, False otherwise. | 
|  | 75 | +        str: The commit message without comments. | 
| 33 | 76 |     """ | 
| 34 |  | -    return len(commit_message) <= max_length | 
| 35 | 77 | 
 | 
|  | 78 | +    lines: List[str] = [] | 
|  | 79 | +    for line in msg.split("\n"): | 
|  | 80 | +        if "# ------------------------ >8 ------------------------" in line: | 
|  | 81 | +            # ignoring all the verbose message below this line | 
|  | 82 | +            break | 
|  | 83 | +        if not line.startswith("#"): | 
|  | 84 | +            lines.append(line) | 
| 36 | 85 | 
 | 
| 37 |  | -def check_commit_message(commit_message: str) -> None: | 
|  | 86 | +    return "\n".join(lines) | 
|  | 87 | + | 
|  | 88 | + | 
|  | 89 | +def check_commit_message(commit_message: str) -> Tuple[bool, List[str]]: | 
| 38 | 90 |     """ | 
| 39 |  | -    Check the validity of a commit message. | 
|  | 91 | +    Checks the validity of a commit message. Returns success and error list. | 
| 40 | 92 | 
 | 
| 41 | 93 |     Args: | 
| 42 | 94 |         commit_message (str): The commit message to be validated. | 
| 43 | 95 | 
 | 
| 44 |  | -    Raises: | 
| 45 |  | -        SystemExit: Exits the program with status code 1 if the commit message | 
| 46 |  | -                    violates length or conventional commit format rules. | 
| 47 |  | -
 | 
| 48 | 96 |     Returns: | 
| 49 |  | -        None: This function does not return any value; it either exits or | 
| 50 |  | -              continues based on the validity of the commit message. | 
|  | 97 | +        Tuple[bool, List[str]]: Returns success as a first element and list | 
|  | 98 | +        of errors on the second elements. If success is true, errors will be | 
|  | 99 | +        empty. | 
| 51 | 100 |     """ | 
| 52 |  | -    if not is_valid_length(commit_message, max_length=COMMIT_MAX_LENGTH): | 
| 53 |  | -        sys.stderr.write( | 
| 54 |  | -            "Commit message is too long. " | 
| 55 |  | -            f"Max length is {COMMIT_MAX_LENGTH} characters.\n" | 
| 56 |  | -        ) | 
| 57 |  | -        sys.exit(1) | 
| 58 |  | - | 
| 59 |  | -    if not is_conventional_commit(commit_message): | 
| 60 |  | -        sys.stderr.write("Commit message does not follow conventional commit format.\n") | 
| 61 |  | -        sys.exit(1) | 
|  | 101 | +    # default values | 
|  | 102 | +    success = True | 
|  | 103 | +    errors: List[str] = [] | 
|  | 104 | + | 
|  | 105 | +    # removing unnecessary commit comments | 
|  | 106 | +    commit_message = remove_comments(commit_message) | 
|  | 107 | + | 
|  | 108 | +    # checking if commit message should be ignored | 
|  | 109 | +    if is_ignored(commit_message): | 
|  | 110 | +        return success, errors | 
|  | 111 | + | 
|  | 112 | +    # checking the length of header | 
|  | 113 | +    header = commit_message.split("\n").pop() | 
|  | 114 | +    if len(header) > COMMIT_MAX_LENGTH: | 
|  | 115 | +        success = False | 
|  | 116 | +        errors.append(HEADER_LENGTH_ERROR) | 
|  | 117 | + | 
|  | 118 | +    # matching commit message with the commit pattern | 
|  | 119 | +    pattern_match = re.match(CONVENTIONAL_COMMIT_PATTERN, commit_message) | 
|  | 120 | +    if pattern_match is None: | 
|  | 121 | +        success = False | 
|  | 122 | +        errors.append(INCORRECT_FORMAT_ERROR) | 
|  | 123 | + | 
|  | 124 | +    return success, errors | 
0 commit comments