1# SPDX-License-Identifier: Apache-2.0 2 3from gitlint.rules import LineRule, RuleViolation, CommitMessageBody 4import re 5 6 7class BodyMaxLineLengthEx(LineRule): 8 """A rule to enforce a line limit of 72 characters, except for valid cases.""" 9 10 # A rule MUST have a human friendly name 11 name = "body-max-line-length-ex" 12 13 # A rule MUST have a *unique* id. 14 # We recommend starting with UL (for User-defined Line-rule) 15 id = "UL-ll" 16 17 # A line-rule MUST have a target (not required for CommitRules). 18 target = CommitMessageBody 19 20 max_len = 72 21 22 # Updated property as the commit messages is validated line by line. 23 inside_open_codeblock = False 24 25 def validate(self, line, commit): 26 # Pattern allowing: 27 # - [0]: https://foobar 28 # - [0] https://foobar 29 # - https://foobar 30 link_regex = re.compile(r"^((\[[0-9]+\]:?\s?)?https?:\/\/).*$") 31 32 is_codeblock_marker = line.startswith("```") 33 34 inside_open_codeblock_ = self.inside_open_codeblock 35 if is_codeblock_marker: 36 self.inside_open_codeblock = not self.inside_open_codeblock 37 38 if len(line) > self.max_len: 39 is_link = link_regex.match(line) 40 41 if inside_open_codeblock_: 42 return 43 44 if is_link: 45 return 46 47 return [ 48 RuleViolation(self.id, f"Line '{line}' exceeds limit of {self.max_len}") 49 ] 50