summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/commit-message-117
-rw-r--r--examples/gitlint2
-rw-r--r--examples/my_commit_rules.py35
-rw-r--r--examples/my_configuration_rules.py69
-rw-r--r--examples/my_line_rules.py22
5 files changed, 113 insertions, 22 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/gitlint b/examples/gitlint
index b722023..0261752 100644
--- a/examples/gitlint
+++ b/examples/gitlint
@@ -55,4 +55,4 @@ ignore-merge-commits=false
# This is useful for when developers often erroneously edit certain files or git submodules.
# By specifying this rule, developers can only change the file when they explicitly reference
# it in the commit message.
-files=gitlint/rules.py,README.md
+files=gitlint-core/gitlint/rules.py,README.md
diff --git a/examples/my_commit_rules.py b/examples/my_commit_rules.py
index e12e02d..35bb836 100644
--- a/examples/my_commit_rules.py
+++ b/examples/my_commit_rules.py
@@ -1,9 +1,9 @@
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.
@@ -25,19 +25,21 @@ class BodyMaxLineCount(CommitRule):
id = "UC1"
# A rule MAY have an option_spec if its behavior should be configurable.
- options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]
+ 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
+ max_line_count = self.options["max-line-count"].value
if line_count > max_line_count:
- message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count)
+ message = f"Body contains too many lines ({line_count} > {max_line_count})"
return [RuleViolation(self.id, message, line_nr=1)]
class SignedOffBy(CommitRule):
- """ This rule will enforce that each commit contains a "Signed-Off-By" line.
- We keep things simple here and just check whether the commit body contains a line that starts with "Signed-Off-By".
+ """This rule will enforce that each commit contains a "Signed-off-by" line.
+ We keep things simple here and just check whether the commit body contains a line that starts with "Signed-off-by".
"""
# A rule MUST have a human friendly name
@@ -47,16 +49,18 @@ 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"):
+ if line.startswith("Signed-off-by"):
return
- return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)]
+ return [RuleViolation(self.id, "Body does not contain a 'Signed-off-by' line", line_nr=1)]
class BranchNamingConventions(CommitRule):
- """ This rule will enforce that a commit is part of a branch that meets certain naming conventions.
- See GitFlow for real-world example of this: https://nvie.com/posts/a-successful-git-branching-model/
+ """This rule will enforce that a commit is part of a branch that meets certain naming conventions.
+ See GitFlow for real-world example of this: https://nvie.com/posts/a-successful-git-branching-model/
"""
# A rule MUST have a human friendly name
@@ -66,11 +70,13 @@ class BranchNamingConventions(CommitRule):
id = "UC3"
# A rule MAY have an option_spec if its behavior should be configurable.
- options_spec = [ListOption('branch-prefixes', ["feature/", "hotfix/", "release/"], "Allowed branch prefixes")]
+ 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
+ allowed_branch_prefixes = self.options["branch-prefixes"].value
for branch in commit.branches:
valid_branch_name = False
@@ -80,8 +86,7 @@ class BranchNamingConventions(CommitRule):
break
if not valid_branch_name:
- msg = "Branch name '{0}' does not start with one of {1}".format(branch,
- utils.sstr(allowed_branch_prefixes))
+ msg = f"Branch name '{branch}' does not start with one of {allowed_branch_prefixes}"
violations.append(RuleViolation(self.id, msg, line_nr=1))
return violations
diff --git a/examples/my_configuration_rules.py b/examples/my_configuration_rules.py
new file mode 100644
index 0000000..7715c0b
--- /dev/null
+++ b/examples/my_configuration_rules.py
@@ -0,0 +1,69 @@
+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 = "This is my property"
diff --git a/examples/my_line_rules.py b/examples/my_line_rules.py
index cc69fb9..58b0108 100644
--- a/examples/my_line_rules.py
+++ b/examples/my_line_rules.py
@@ -2,6 +2,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
@@ -17,8 +19,8 @@ that fits your needs.
class SpecialChars(LineRule):
- """ This rule will enforce that the commit message title does not contain any of the following characters:
- $^%@!*() """
+ """This rule will enforce that the commit message title does not contain any of the following characters:
+ $^%@!*()"""
# A rule MUST have a human friendly name
name = "title-no-special-chars"
@@ -31,15 +33,23 @@ class SpecialChars(LineRule):
target = CommitMessageTitle
# A rule MAY have an option_spec if its behavior should be configurable.
- options_spec = [ListOption('special-chars', ['$', '^', '%', '@', '!', '*', '(', ')'],
- "Comma separated list of characters that should not occur in the title")]
+ options_spec = [
+ ListOption(
+ "special-chars",
+ ["$", "^", "%", "@", "!", "*", "(", ")"],
+ "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:
+ 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 = f"Title contains the special character '{char}'"
+ violation = RuleViolation(self.id, msg, line)
violations.append(violation)
return violations