mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
ci: improve gitlint (max line length in body with exceptions)
Follow-up of 5aa1540c5d but way more
mature. We now use custom gitlint rules written in Python to better
handle the max line length, with respect to a few valid exceptions.
Recognizing code blocks or compiler output, as discussed, is not
trivial and hard to get right for all corner-cases. Therefore, this
commit is a pragmatic way forward. The CI job should be kept optional.
Allowed exceptions for the 72 line length limit are now:
1. links in the following three common patterns:
https://example.com/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links
[0] https://example.com/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links
[0]: https://example.com/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links/very-long-links
2. code blocks (anything between the three backticks)
```
let x = "very_long_very_long_very_long_very_long_very_long_very_long_very_long_very_long_very_long_very_long_"
```
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
committed by
Rob Bradford
parent
f16d45e86e
commit
77e042237d
49
scripts/gitlint/rules/BodyMaxLineLengthEx.py
Normal file
49
scripts/gitlint/rules/BodyMaxLineLengthEx.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from gitlint.rules import LineRule, RuleViolation, CommitMessageBody
|
||||
import re
|
||||
|
||||
|
||||
class BodyMaxLineLengthEx(LineRule):
|
||||
"""A rule to enforce a line limit of 72 characters, except for valid cases."""
|
||||
|
||||
# A rule MUST have a human friendly name
|
||||
name = "body-max-line-length-ex"
|
||||
|
||||
# A rule MUST have a *unique* id.
|
||||
# We recommend starting with UL (for User-defined Line-rule)
|
||||
id = "UL-ll"
|
||||
|
||||
# A line-rule MUST have a target (not required for CommitRules).
|
||||
target = CommitMessageBody
|
||||
|
||||
max_len = 72
|
||||
|
||||
# Updated property as the commit messages is validated line by line.
|
||||
inside_open_codeblock = False
|
||||
|
||||
def validate(self, line, commit):
|
||||
# Pattern allowing:
|
||||
# - [0]: https://foobar
|
||||
# - [0] https://foobar
|
||||
# - https://foobar
|
||||
link_regex = re.compile(r"^((\[[0-9]+\]:?\s?)?https?:\/\/).*$")
|
||||
|
||||
is_codeblock_marker = line.startswith("```")
|
||||
|
||||
inside_open_codeblock_ = self.inside_open_codeblock
|
||||
if is_codeblock_marker:
|
||||
self.inside_open_codeblock = not self.inside_open_codeblock
|
||||
|
||||
if len(line) > self.max_len:
|
||||
is_link = link_regex.match(line)
|
||||
|
||||
if inside_open_codeblock_:
|
||||
return
|
||||
|
||||
if is_link:
|
||||
return
|
||||
|
||||
return [
|
||||
RuleViolation(self.id, f"Line '{line}' exceeds limit of {self.max_len}")
|
||||
]
|
||||
94
scripts/gitlint/rules/TitleStartsWithComponent.py
Normal file
94
scripts/gitlint/rules/TitleStartsWithComponent.py
Normal file
@@ -0,0 +1,94 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
|
||||
import re
|
||||
|
||||
|
||||
class TitleStartsWithComponent(LineRule):
|
||||
"""A rule to enforce valid commit message title
|
||||
|
||||
Valid title format:
|
||||
component1[, component2, componentN]: submodule: summary
|
||||
|
||||
Title should have at least one component
|
||||
Components are separated by comma+space: ", "
|
||||
Components are validated to be in valid_components
|
||||
Components list is ended by a colon
|
||||
Submodules are not validated
|
||||
|
||||
"""
|
||||
|
||||
# A rule MUST have a human friendly name
|
||||
name = "title-has-valid-component"
|
||||
|
||||
# A rule MUST have a *unique* id.
|
||||
# We recommend starting with UL (for User-defined Line-rule)
|
||||
id = "UL1"
|
||||
|
||||
# A line-rule MUST have a target (not required for CommitRules).
|
||||
target = CommitMessageTitle
|
||||
|
||||
def validate(self, line, _commit):
|
||||
valid_components = (
|
||||
'api_client',
|
||||
'arch',
|
||||
'block',
|
||||
'build',
|
||||
'ch-remote',
|
||||
'ci',
|
||||
'devices',
|
||||
'docs',
|
||||
'event_monitor',
|
||||
'fuzz',
|
||||
'github',
|
||||
'gitignore',
|
||||
'gitlint',
|
||||
'hypervisor',
|
||||
'main',
|
||||
'misc',
|
||||
'net_gen',
|
||||
'net_util',
|
||||
'openapi',
|
||||
'option_parser',
|
||||
'pci',
|
||||
'performance-metrics',
|
||||
'rate_limiter',
|
||||
'README',
|
||||
'resources',
|
||||
'scripts',
|
||||
'serial_buffer',
|
||||
'test_data',
|
||||
'test_infra',
|
||||
'tests',
|
||||
'tpm',
|
||||
'tracer',
|
||||
'vhost_user_block',
|
||||
'vhost_user_net',
|
||||
'virtio-devices',
|
||||
'vm-allocator',
|
||||
'vm-device',
|
||||
'vmm',
|
||||
'vm-migration',
|
||||
'vm-virtio')
|
||||
|
||||
ptrn_title = re.compile(r'^(.+?):\s(.+)$')
|
||||
match = ptrn_title.match(line)
|
||||
|
||||
if not match:
|
||||
self.log.debug("Invalid commit title {}", line)
|
||||
return [RuleViolation(self.id, "Commit title does not comply with "
|
||||
"rule: 'component: change summary'")]
|
||||
components = match.group(1)
|
||||
summary = match.group(2)
|
||||
self.log.debug(f"\nComponents: {components}\nSummary: {summary}")
|
||||
|
||||
ptrn_components = re.compile(r',\s')
|
||||
components_list = re.split(ptrn_components, components)
|
||||
self.log.debug("components list: %s" % components_list)
|
||||
|
||||
for component in components_list:
|
||||
if component not in valid_components:
|
||||
return [RuleViolation(self.id,
|
||||
f"Invalid component: {component}, "
|
||||
"\nValid components are: {}".format(
|
||||
" ".join(valid_components)))]
|
||||
Reference in New Issue
Block a user