summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--examples/commit-message-117
-rw-r--r--examples/my_commit_rules.py10
-rw-r--r--examples/my_configuration_rules.py72
-rw-r--r--examples/my_line_rules.py9
4 files changed, 97 insertions, 1 deletions
diff --git a/examples/commit-message-11 b/examples/commit-message-11
new file mode 100644
index 0000000..72c7fa8
--- /dev/null
+++ b/examples/commit-message-11
@@ -0,0 +1,7 @@
+Release: Holy Smokes, Batman!
+
+This release contains a bunch of features.
+
+- Here's a description of a feature that exceeds the default maximum line length of 80 characters
+
+$ my-fancy-tool: this line is auto-generated by our release tool and is always too long $ \ No newline at end of file
diff --git a/examples/my_commit_rules.py b/examples/my_commit_rules.py
index e12e02d..c64008f 100644
--- a/examples/my_commit_rules.py
+++ b/examples/my_commit_rules.py
@@ -1,9 +1,13 @@
+# -*- coding: utf-8 -*-
+
from gitlint.rules import CommitRule, RuleViolation
from gitlint.options import IntOption, ListOption
from gitlint import utils
"""
+Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules
+
The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that
act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them
to the entire commit. This happens exactly once per commit.
@@ -28,6 +32,8 @@ class BodyMaxLineCount(CommitRule):
options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]
def validate(self, commit):
+ self.log.debug("BodyMaxLineCount: This will be visible when running `gitlint --debug`")
+
line_count = len(commit.message.body)
max_line_count = self.options['max-line-count'].value
if line_count > max_line_count:
@@ -47,6 +53,8 @@ class SignedOffBy(CommitRule):
id = "UC2"
def validate(self, commit):
+ self.log.debug("SignedOffBy: This will be visible when running `gitlint --debug`")
+
for line in commit.message.body:
if line.startswith("Signed-Off-By"):
return
@@ -69,6 +77,8 @@ class BranchNamingConventions(CommitRule):
options_spec = [ListOption('branch-prefixes', ["feature/", "hotfix/", "release/"], "Allowed branch prefixes")]
def validate(self, commit):
+ self.log.debug("BranchNamingConventions: This line will be visible when running `gitlint --debug`")
+
violations = []
allowed_branch_prefixes = self.options['branch-prefixes'].value
for branch in commit.branches:
diff --git a/examples/my_configuration_rules.py b/examples/my_configuration_rules.py
new file mode 100644
index 0000000..58de048
--- /dev/null
+++ b/examples/my_configuration_rules.py
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+
+from gitlint.rules import ConfigurationRule
+from gitlint.options import IntOption
+
+
+"""
+Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules
+
+The ReleaseConfigurationRule class below is an example of a user-defined ConfigurationRule. Configuration rules are
+gitlint rules that are applied once per commit and BEFORE any other rules are run. Configuration Rules are meant to
+dynamically change gitlint's configuration and/or the commit that is about to be linted. A typically use-case for this
+is modifying the behavior of gitlint's rules based on a commit contents.
+
+Notes:
+- Modifying the commit object DOES NOT modify the actual git commit message in the target repo, only gitlint's copy of
+ it.
+- Modifying the config object only has effect on the commit that is being linted, subsequent commits will not
+ automatically inherit this configuration.
+"""
+
+
+class ReleaseConfigurationRule(ConfigurationRule):
+ """
+ This rule will modify gitlint's behavior for Release Commits.
+
+ This example might not be the most realistic for a real-world scenario,
+ but is meant to give an overview of what's possible.
+ """
+
+ # A rule MUST have a human friendly name
+ name = "release-configuration-rule"
+
+ # A rule MUST have a *unique* id, we recommend starting with UCR
+ # (for User-defined Configuration-Rule), but this can really be anything.
+ id = "UCR1"
+
+ # A rule MAY have an option_spec if its behavior should be configurable.
+ options_spec = [IntOption('custom-verbosity', 2, "Gitlint verbosity for release commits")]
+
+ def apply(self, config, commit):
+ self.log.debug("ReleaseConfigurationRule: This will be visible when running `gitlint --debug`")
+
+ # If the commit title starts with 'Release', we want to modify
+ # how all subsequent rules interpret that commit
+ if commit.message.title.startswith("Release"):
+
+ # If your Release commit messages are auto-generated, the
+ # body might contain trailing whitespace. Let's ignore that
+ config.ignore.append("body-trailing-whitespace")
+
+ # Similarly, the body lines might exceed 80 chars,
+ # let's set gitlint's limit to 200
+ # To set rule options use:
+ # config.set_rule_option(<rule-name>, <rule-option>, <value>)
+ config.set_rule_option("body-max-line-length", "line-length", 200)
+
+ # For kicks, let's set gitlint's verbosity to 2
+ # To set general options use
+ # config.set_general_option(<general-option>, <value>)
+ config.set_general_option("verbosity", 2)
+ # Wwe can also use custom options to make this configurable
+ config.set_general_option("verbosity", self.options['custom-verbosity'].value)
+
+ # Strip any lines starting with $ from the commit message
+ # (this only affects how gitlint sees your commit message, it does
+ # NOT modify your actual commit in git)
+ commit.message.body = [line for line in commit.message.body if not line.startswith("$")]
+
+ # You can add any extra properties you want to the commit object, these will be available later on
+ # in all rules.
+ commit.my_property = u"This is my property"
diff --git a/examples/my_line_rules.py b/examples/my_line_rules.py
index cc69fb9..820024a 100644
--- a/examples/my_line_rules.py
+++ b/examples/my_line_rules.py
@@ -1,7 +1,11 @@
+# -*- coding: utf-8 -*-
+
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
from gitlint.options import ListOption
"""
+Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules
+
The SpecialChars class below is an example of a user-defined LineRule. Line rules are gitlint rules that only act on a
single line at once. Once the rule is discovered, gitlint will automatically take care of applying this rule
against each line of the commit message title or body (whether it is applied to the title or body is determined by the
@@ -35,11 +39,14 @@ class SpecialChars(LineRule):
"Comma separated list of characters that should not occur in the title")]
def validate(self, line, _commit):
+ self.log.debug("SpecialChars: This will be visible when running `gitlint --debug`")
+
violations = []
# options can be accessed by looking them up by their name in self.options
for char in self.options['special-chars'].value:
if char in line:
- violation = RuleViolation(self.id, "Title contains the special character '{0}'".format(char), line)
+ msg = "Title contains the special character '{0}'".format(char)
+ violation = RuleViolation(self.id, msg, line)
violations.append(violation)
return violations