2020-06-11 00:54:34 +02:00
|
|
|
import re
|
|
|
|
from typing import List, Text
|
2017-04-04 19:27:12 +02:00
|
|
|
|
2020-04-19 22:36:58 +02:00
|
|
|
from gitlint.git import GitCommit
|
2018-02-13 03:01:22 +01:00
|
|
|
from gitlint.options import StrOption
|
2020-06-11 00:54:34 +02:00
|
|
|
from gitlint.rules import CommitMessageTitle, LineRule, RuleViolation
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
# Word list from https://github.com/m1foley/fit-commit
|
|
|
|
# Copyright (c) 2015 Mike Foley
|
|
|
|
# License: MIT
|
|
|
|
# Ref: fit_commit/validators/tense.rb
|
|
|
|
WORD_SET = {
|
|
|
|
'adds', 'adding', 'added',
|
|
|
|
'allows', 'allowing', 'allowed',
|
|
|
|
'amends', 'amending', 'amended',
|
|
|
|
'bumps', 'bumping', 'bumped',
|
|
|
|
'calculates', 'calculating', 'calculated',
|
|
|
|
'changes', 'changing', 'changed',
|
|
|
|
'cleans', 'cleaning', 'cleaned',
|
|
|
|
'commits', 'committing', 'committed',
|
|
|
|
'corrects', 'correcting', 'corrected',
|
|
|
|
'creates', 'creating', 'created',
|
|
|
|
'darkens', 'darkening', 'darkened',
|
|
|
|
'disables', 'disabling', 'disabled',
|
|
|
|
'displays', 'displaying', 'displayed',
|
2017-05-05 21:14:51 +02:00
|
|
|
'documents', 'documenting', 'documented',
|
2017-04-04 19:27:12 +02:00
|
|
|
'drys', 'drying', 'dryed',
|
|
|
|
'ends', 'ending', 'ended',
|
|
|
|
'enforces', 'enforcing', 'enforced',
|
|
|
|
'enqueues', 'enqueuing', 'enqueued',
|
|
|
|
'extracts', 'extracting', 'extracted',
|
|
|
|
'finishes', 'finishing', 'finished',
|
|
|
|
'fixes', 'fixing', 'fixed',
|
|
|
|
'formats', 'formatting', 'formatted',
|
|
|
|
'guards', 'guarding', 'guarded',
|
|
|
|
'handles', 'handling', 'handled',
|
|
|
|
'hides', 'hiding', 'hid',
|
|
|
|
'increases', 'increasing', 'increased',
|
|
|
|
'ignores', 'ignoring', 'ignored',
|
|
|
|
'implements', 'implementing', 'implemented',
|
|
|
|
'improves', 'improving', 'improved',
|
|
|
|
'keeps', 'keeping', 'kept',
|
|
|
|
'kills', 'killing', 'killed',
|
|
|
|
'makes', 'making', 'made',
|
|
|
|
'merges', 'merging', 'merged',
|
|
|
|
'moves', 'moving', 'moved',
|
|
|
|
'permits', 'permitting', 'permitted',
|
|
|
|
'prevents', 'preventing', 'prevented',
|
|
|
|
'pushes', 'pushing', 'pushed',
|
|
|
|
'rebases', 'rebasing', 'rebased',
|
|
|
|
'refactors', 'refactoring', 'refactored',
|
|
|
|
'removes', 'removing', 'removed',
|
|
|
|
'renames', 'renaming', 'renamed',
|
|
|
|
'reorders', 'reordering', 'reordered',
|
2017-05-05 21:14:51 +02:00
|
|
|
'replaces', 'replacing', 'replaced',
|
2017-04-04 19:27:12 +02:00
|
|
|
'requires', 'requiring', 'required',
|
|
|
|
'restores', 'restoring', 'restored',
|
|
|
|
'sends', 'sending', 'sent',
|
|
|
|
'sets', 'setting',
|
|
|
|
'separates', 'separating', 'separated',
|
|
|
|
'shows', 'showing', 'showed',
|
2017-05-05 21:14:51 +02:00
|
|
|
'simplifies', 'simplifying', 'simplified',
|
2017-04-04 19:27:12 +02:00
|
|
|
'skips', 'skipping', 'skipped',
|
|
|
|
'sorts', 'sorting',
|
|
|
|
'speeds', 'speeding', 'sped',
|
|
|
|
'starts', 'starting', 'started',
|
|
|
|
'supports', 'supporting', 'supported',
|
|
|
|
'takes', 'taking', 'took',
|
|
|
|
'testing', 'tested', # 'tests' excluded to reduce false negative
|
|
|
|
'truncates', 'truncating', 'truncated',
|
|
|
|
'updates', 'updating', 'updated',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
'uses', 'using', 'used',
|
2017-04-04 19:27:12 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 10:19:40 +02:00
|
|
|
imperative_forms = sorted([
|
2017-04-04 19:27:12 +02:00
|
|
|
'add', 'allow', 'amend', 'bump', 'calculate', 'change', 'clean', 'commit',
|
2017-05-05 21:14:51 +02:00
|
|
|
'correct', 'create', 'darken', 'disable', 'display', 'document', 'dry',
|
|
|
|
'end', 'enforce', 'enqueue', 'extract', 'finish', 'fix', 'format', 'guard',
|
|
|
|
'handle', 'hide', 'ignore', 'implement', 'improve', 'increase', 'keep',
|
|
|
|
'kill', 'make', 'merge', 'move', 'permit', 'prevent', 'push', 'rebase',
|
|
|
|
'refactor', 'remove', 'rename', 'reorder', 'replace', 'require', 'restore',
|
|
|
|
'send', 'separate', 'set', 'show', 'simplify', 'skip', 'sort', 'speed',
|
|
|
|
'start', 'support', 'take', 'test', 'truncate', 'update', 'use',
|
2017-04-22 10:19:40 +02:00
|
|
|
])
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def head_binary_search(key: Text, words: List[str]) -> str:
|
2017-04-04 19:27:12 +02:00
|
|
|
""" Find the imperative mood version of `word` by looking at the first
|
|
|
|
3 characters. """
|
|
|
|
|
2017-04-22 10:19:40 +02:00
|
|
|
# Edge case: 'disable' and 'display' have the same 3 starting letters.
|
2017-04-04 19:27:12 +02:00
|
|
|
if key in ['displays', 'displaying', 'displayed']:
|
|
|
|
return 'display'
|
|
|
|
|
|
|
|
lower = 0
|
|
|
|
upper = len(words) - 1
|
|
|
|
|
|
|
|
while True:
|
|
|
|
if lower > upper:
|
|
|
|
# Should not happen
|
2020-06-09 00:25:09 +02:00
|
|
|
raise Exception(f"Cannot find imperative mood of {key}")
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
mid = (lower + upper) // 2
|
|
|
|
imperative_form = words[mid]
|
|
|
|
|
|
|
|
if key[:3] == imperative_form[:3]:
|
|
|
|
return imperative_form
|
|
|
|
elif key < imperative_form:
|
|
|
|
upper = mid - 1
|
|
|
|
elif key > imperative_form:
|
|
|
|
lower = mid + 1
|
|
|
|
|
|
|
|
|
|
|
|
class ImperativeMood(LineRule):
|
|
|
|
""" This rule will enforce that the commit message title uses imperative
|
2017-05-05 21:14:51 +02:00
|
|
|
mood. This is done by checking if the first word is in `WORD_SET`, if so
|
|
|
|
show the word in the correct mood. """
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
name = "title-imperative-mood"
|
|
|
|
id = "Z1"
|
|
|
|
target = CommitMessageTitle
|
|
|
|
|
2017-05-05 21:14:51 +02:00
|
|
|
error_msg = ('The first word in commit title should be in imperative mood '
|
2017-04-04 19:27:12 +02:00
|
|
|
'("{word}" -> "{imperative}"): "{title}"')
|
|
|
|
|
2020-04-19 22:36:58 +02:00
|
|
|
def validate(self, line: Text, commit: GitCommit) -> List[RuleViolation]:
|
2017-04-04 19:27:12 +02:00
|
|
|
violations = []
|
2017-05-05 21:14:51 +02:00
|
|
|
|
|
|
|
# Ignore the section tag (ie `<section tag>: <message body>.`)
|
|
|
|
words = line.split(': ', 1)[-1].split()
|
|
|
|
first_word = words[0].lower()
|
|
|
|
|
|
|
|
if first_word in WORD_SET:
|
|
|
|
imperative = head_binary_search(first_word, imperative_forms)
|
|
|
|
violation = RuleViolation(self.id, self.error_msg.format(
|
|
|
|
word=first_word,
|
|
|
|
imperative=imperative,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
title=commit.message.title,
|
2017-05-05 21:14:51 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
violations.append(violation)
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
return violations
|
2018-02-13 03:01:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TitleMatchRegexAllowException(LineRule):
|
|
|
|
"""Allows revert commits contrary to the built-in title-match-regex rule"""
|
|
|
|
|
|
|
|
name = 'title-match-regex-allow-exception'
|
|
|
|
id = 'Z2'
|
|
|
|
target = CommitMessageTitle
|
|
|
|
options_spec = [StrOption('regex', ".*", "Regex the title should match")]
|
|
|
|
|
2020-04-19 22:36:58 +02:00
|
|
|
def validate(self, title: Text, commit: GitCommit) -> List[RuleViolation]:
|
2018-02-13 03:01:22 +01:00
|
|
|
|
|
|
|
regex = self.options['regex'].value
|
|
|
|
pattern = re.compile(regex, re.UNICODE)
|
|
|
|
if not pattern.search(title) and not title.startswith("Revert \""):
|
2020-06-09 00:25:09 +02:00
|
|
|
violation_msg = f"Title does not match regex ({regex})"
|
2018-02-13 03:01:22 +01:00
|
|
|
return [RuleViolation(self.id, violation_msg, title)]
|
|
|
|
|
|
|
|
return []
|