How to use the gitlint.rules function in gitlint

To help you get started, we’ve selected a few gitlint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
Asserts that a given rule clazz is valid by checking a number of its properties:
     - Rules must extend from  LineRule or CommitRule
     - Rule classes must have id and name string attributes.
       The options_spec is optional, but if set, it must be a list of gitlint Options.
     - Rule classes must have a validate method. In case of a CommitRule, validate must take a single commit parameter.
       In case of LineRule, validate must take line and commit as first and second parameters.
     - LineRule classes must have a target class attributes that is set to either
       CommitMessageTitle or CommitMessageBody.
     - Rule id's cannot start with R, T, B or M as these rule ids are reserved for gitlint itself.
    """

    # Rules must extend from LineRule or CommitRule
    if not (issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule)):
        msg = u"{0} rule class '{1}' must extend from {2}.{3} or {2}.{4}"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__, rules.CommitRule.__module__,
                                             rules.LineRule.__name__, rules.CommitRule.__name__))

    # Rules must have an id attribute
    if not hasattr(clazz, 'id') or clazz.id is None or not clazz.id:
        msg = u"{0} rule class '{1}' must have an 'id' attribute"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))

    # Rule id's cannot start with gitlint reserved letters
    if clazz.id[0].upper() in ['R', 'T', 'B', 'M']:
        msg = u"The id '{1}' of '{0}' is invalid. Gitlint reserves ids starting with R,T,B,M"
        raise rules.UserRuleError(msg.format(clazz.__name__, clazz.id[0]))

    # Rules must have a name attribute
    if not hasattr(clazz, 'name') or clazz.name is None or not clazz.name:
        msg = u"{0} rule class '{1}' must have a 'name' attribute"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))
github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
"""
    Asserts that a given rule clazz is valid by checking a number of its properties:
     - Rules must extend from  LineRule or CommitRule
     - Rule classes must have id and name string attributes.
       The options_spec is optional, but if set, it must be a list of gitlint Options.
     - Rule classes must have a validate method. In case of a CommitRule, validate must take a single commit parameter.
       In case of LineRule, validate must take line and commit as first and second parameters.
     - LineRule classes must have a target class attributes that is set to either
       CommitMessageTitle or CommitMessageBody.
     - Rule id's cannot start with R, T, B or M as these rule ids are reserved for gitlint itself.
    """

    # Rules must extend from LineRule or CommitRule
    if not (issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule)):
        msg = u"{0} rule class '{1}' must extend from {2}.{3} or {2}.{4}"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__, rules.CommitRule.__module__,
                                             rules.LineRule.__name__, rules.CommitRule.__name__))

    # Rules must have an id attribute
    if not hasattr(clazz, 'id') or clazz.id is None or not clazz.id:
        msg = u"{0} rule class '{1}' must have an 'id' attribute"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))

    # Rule id's cannot start with gitlint reserved letters
    if clazz.id[0].upper() in ['R', 'T', 'B', 'M']:
        msg = u"The id '{1}' of '{0}' is invalid. Gitlint reserves ids starting with R,T,B,M"
        raise rules.UserRuleError(msg.format(clazz.__name__, clazz.id[0]))

    # Rules must have a name attribute
    if not hasattr(clazz, 'name') or clazz.name is None or not clazz.name:
        msg = u"{0} rule class '{1}' must have a 'name' attribute"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))
github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
# check that all items in options_spec are actual gitlint options
    for option in clazz.options_spec:
        if not isinstance(option, options.RuleOption):
            msg = u"The options_spec attribute of {0} rule class '{1}' must be a list of {2}.{3}"
            raise rules.UserRuleError(msg.format(rule_type.lower(), clazz.__name__,
                                                 options.RuleOption.__module__, options.RuleOption.__name__))

    # Rules must have a validate method. We use isroutine() as it's both python 2 and 3 compatible.
    # For more info see http://stackoverflow.com/a/17019998/381010
    if not hasattr(clazz, 'validate') or not inspect.isroutine(clazz.validate):
        msg = u"{0} rule class '{1}' must have a 'validate' method"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))

    # LineRules must have a valid target: rules.CommitMessageTitle or rules.CommitMessageBody
    if issubclass(clazz, rules.LineRule):
        if clazz.target not in [rules.CommitMessageTitle, rules.CommitMessageBody]:
            msg = u"The target attribute of the {0} LineRule class '{1}' must be either {2}.{3} or {2}.{4}"
            msg = msg.format(rule_type.lower(), clazz.__name__, rules.CommitMessageTitle.__module__,
                             rules.CommitMessageTitle.__name__, rules.CommitMessageBody.__name__)
            raise rules.UserRuleError(msg)
github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
:param extra_path: absolute directory or file path to search for rule classes
    :return: The list of rule classes that are found in the given directory or module
    """

    files = []
    modules = []

    if os.path.isfile(extra_path):
        files = [os.path.basename(extra_path)]
        directory = os.path.dirname(extra_path)
    elif os.path.isdir(extra_path):
        files = os.listdir(extra_path)
        directory = extra_path
    else:
        raise rules.UserRuleError(u"Invalid extra-path: {0}".format(extra_path))

    # Filter out files that are not python modules
    for filename in files:
        if fnmatch.fnmatch(filename, '*.py'):
            # We have to treat __init__ files a bit special: add the parent dir instead of the filename, and also
            # add their parent dir to the sys.path (this fixes import issues with pypy2).
            if filename == "__init__.py":
                modules.append(os.path.basename(directory))
                sys.path.append(os.path.dirname(directory))
            else:
                modules.append(os.path.splitext(filename)[0])

    # No need to continue if there are no modules specified
    if not modules:
        return []
github jorisroovers / gitlint / gitlint / lint.py View on Github external
def commit_rules(self):
        return [rule for rule in self.config.rules if isinstance(rule, gitlint_rules.CommitRule) and
                not self.should_ignore_rule(rule)]
github jorisroovers / gitlint / gitlint / config.py View on Github external
raise LintConfigError(ustr(e))

    return wrapped


class LintConfigError(Exception):
    pass


class LintConfig(object):
    """ Class representing gitlint configuration.
        Contains active config as well as number of methods to easily get/set the config.
    """

    # Default tuple of rule classes (tuple because immutable).
    default_rule_classes = (rules.IgnoreByTitle,
                            rules.IgnoreByBody,
                            rules.TitleMaxLength,
                            rules.TitleTrailingWhitespace,
                            rules.TitleLeadingWhitespace,
                            rules.TitleTrailingPunctuation,
                            rules.TitleHardTab,
                            rules.TitleMustNotContainWord,
                            rules.TitleRegexMatches,
                            rules.BodyMaxLineLength,
                            rules.BodyMinLength,
                            rules.BodyMissing,
                            rules.BodyTrailingWhitespace,
                            rules.BodyHardTab,
                            rules.BodyFirstLineEmpty,
                            rules.BodyChangedFileMention,
                            rules.AuthorValidEmail)
github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
msg = u"The options_spec attribute of {0} rule class '{1}' must be a list of {2}.{3}"
            raise rules.UserRuleError(msg.format(rule_type.lower(), clazz.__name__,
                                                 options.RuleOption.__module__, options.RuleOption.__name__))

    # Rules must have a validate method. We use isroutine() as it's both python 2 and 3 compatible.
    # For more info see http://stackoverflow.com/a/17019998/381010
    if not hasattr(clazz, 'validate') or not inspect.isroutine(clazz.validate):
        msg = u"{0} rule class '{1}' must have a 'validate' method"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))

    # LineRules must have a valid target: rules.CommitMessageTitle or rules.CommitMessageBody
    if issubclass(clazz, rules.LineRule):
        if clazz.target not in [rules.CommitMessageTitle, rules.CommitMessageBody]:
            msg = u"The target attribute of the {0} LineRule class '{1}' must be either {2}.{3} or {2}.{4}"
            msg = msg.format(rule_type.lower(), clazz.__name__, rules.CommitMessageTitle.__module__,
                             rules.CommitMessageTitle.__name__, rules.CommitMessageBody.__name__)
            raise rules.UserRuleError(msg)
github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
if not isinstance(option, options.RuleOption):
            msg = u"The options_spec attribute of {0} rule class '{1}' must be a list of {2}.{3}"
            raise rules.UserRuleError(msg.format(rule_type.lower(), clazz.__name__,
                                                 options.RuleOption.__module__, options.RuleOption.__name__))

    # Rules must have a validate method. We use isroutine() as it's both python 2 and 3 compatible.
    # For more info see http://stackoverflow.com/a/17019998/381010
    if not hasattr(clazz, 'validate') or not inspect.isroutine(clazz.validate):
        msg = u"{0} rule class '{1}' must have a 'validate' method"
        raise rules.UserRuleError(msg.format(rule_type, clazz.__name__))

    # LineRules must have a valid target: rules.CommitMessageTitle or rules.CommitMessageBody
    if issubclass(clazz, rules.LineRule):
        if clazz.target not in [rules.CommitMessageTitle, rules.CommitMessageBody]:
            msg = u"The target attribute of the {0} LineRule class '{1}' must be either {2}.{3} or {2}.{4}"
            msg = msg.format(rule_type.lower(), clazz.__name__, rules.CommitMessageTitle.__module__,
                             rules.CommitMessageTitle.__name__, rules.CommitMessageBody.__name__)
            raise rules.UserRuleError(msg)
github jorisroovers / gitlint / gitlint / config.py View on Github external
default_rule_classes = (rules.IgnoreByTitle,
                            rules.IgnoreByBody,
                            rules.TitleMaxLength,
                            rules.TitleTrailingWhitespace,
                            rules.TitleLeadingWhitespace,
                            rules.TitleTrailingPunctuation,
                            rules.TitleHardTab,
                            rules.TitleMustNotContainWord,
                            rules.TitleRegexMatches,
                            rules.BodyMaxLineLength,
                            rules.BodyMinLength,
                            rules.BodyMissing,
                            rules.BodyTrailingWhitespace,
                            rules.BodyHardTab,
                            rules.BodyFirstLineEmpty,
                            rules.BodyChangedFileMention,
                            rules.AuthorValidEmail)

    def __init__(self):
        self.rules = RuleCollection(self.default_rule_classes)
        self._verbosity = options.IntOption('verbosity', 3, "Verbosity")
        self._ignore_merge_commits = options.BoolOption('ignore-merge-commits', True, "Ignore merge commits")
        self._ignore_fixup_commits = options.BoolOption('ignore-fixup-commits', True, "Ignore fixup commits")
        self._ignore_squash_commits = options.BoolOption('ignore-squash-commits', True, "Ignore squash commits")
        self._ignore_revert_commits = options.BoolOption('ignore-revert-commits', True, "Ignore revert commits")
        self._debug = options.BoolOption('debug', False, "Enable debug mode")
        self._extra_path = None
        target_description = "Path of the target git repository (default=current working directory)"
        self._target = options.PathOption('target', os.path.realpath(os.getcwd()), target_description)
        self._ignore = options.ListOption('ignore', [], 'List of rule-ids to ignore')
        self._contrib = options.ListOption('contrib', [], 'List of contrib-rules to enable')
        self._config_path = None
github jorisroovers / gitlint / gitlint / lint.py View on Github external
def configuration_rules(self):
        return [rule for rule in self.config.rules if
                isinstance(rule, gitlint_rules.ConfigurationRule) and not self.should_ignore_rule(rule)]