2020-11-17 02:39:23 +01:00
|
|
|
from typing import List
|
2017-04-04 19:27:12 +02:00
|
|
|
|
2020-04-19 22:36:58 +02:00
|
|
|
from gitlint.git import GitCommit
|
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 = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"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",
|
|
|
|
"documents",
|
|
|
|
"documenting",
|
|
|
|
"documented",
|
|
|
|
"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",
|
|
|
|
"replaces",
|
|
|
|
"replacing",
|
|
|
|
"replaced",
|
|
|
|
"requires",
|
|
|
|
"requiring",
|
|
|
|
"required",
|
|
|
|
"restores",
|
|
|
|
"restoring",
|
|
|
|
"restored",
|
|
|
|
"sends",
|
|
|
|
"sending",
|
|
|
|
"sent",
|
|
|
|
"sets",
|
|
|
|
"setting",
|
|
|
|
"separates",
|
|
|
|
"separating",
|
|
|
|
"separated",
|
|
|
|
"shows",
|
|
|
|
"showing",
|
|
|
|
"showed",
|
|
|
|
"simplifies",
|
|
|
|
"simplifying",
|
|
|
|
"simplified",
|
|
|
|
"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",
|
|
|
|
"uses",
|
|
|
|
"using",
|
|
|
|
"used",
|
2017-04-04 19:27:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 02:50:08 +02:00
|
|
|
imperative_forms = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"add",
|
|
|
|
"allow",
|
|
|
|
"amend",
|
|
|
|
"bump",
|
|
|
|
"calculate",
|
|
|
|
"change",
|
|
|
|
"clean",
|
|
|
|
"commit",
|
|
|
|
"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",
|
2020-09-02 02:50:08 +02:00
|
|
|
]
|
|
|
|
imperative_forms.sort()
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
|
2020-11-17 02:39:23 +01:00
|
|
|
def head_binary_search(key: str, words: List[str]) -> str:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""Find the imperative mood version of `word` by looking at the first
|
|
|
|
3 characters."""
|
2017-04-04 19:27:12 +02:00
|
|
|
|
2017-04-22 10:19:40 +02:00
|
|
|
# Edge case: 'disable' and 'display' have the same 3 starting letters.
|
2021-02-12 08:20:45 +01:00
|
|
|
if key in ["displays", "displaying", "displayed"]:
|
|
|
|
return "display"
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
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):
|
2021-02-12 08:19:30 +01:00
|
|
|
"""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
|
2021-02-12 08:19:30 +01:00
|
|
|
show the word in the correct mood."""
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
name = "title-imperative-mood"
|
|
|
|
id = "Z1"
|
|
|
|
target = CommitMessageTitle
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
error_msg = (
|
2021-02-12 08:20:45 +01:00
|
|
|
"The first word in commit title should be in imperative mood "
|
2021-02-12 08:19:30 +01:00
|
|
|
'("{word}" -> "{imperative}"): "{title}"'
|
|
|
|
)
|
2017-04-04 19:27:12 +02:00
|
|
|
|
2020-11-17 02:39:23 +01:00
|
|
|
def validate(self, line: str, 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>.`)
|
2021-02-12 08:20:45 +01:00
|
|
|
words = line.split(": ", 1)[-1].split()
|
2017-05-05 21:14:51 +02:00
|
|
|
first_word = words[0].lower()
|
|
|
|
|
|
|
|
if first_word in WORD_SET:
|
|
|
|
imperative = head_binary_search(first_word, imperative_forms)
|
2021-02-12 08:19:30 +01:00
|
|
|
violation = RuleViolation(
|
|
|
|
self.id,
|
|
|
|
self.error_msg.format(
|
2021-02-23 23:26:07 +01:00
|
|
|
word=first_word,
|
|
|
|
imperative=imperative,
|
|
|
|
title=commit.message.title,
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
)
|
2017-05-05 21:14:51 +02:00
|
|
|
|
|
|
|
violations.append(violation)
|
2017-04-04 19:27:12 +02:00
|
|
|
|
|
|
|
return violations
|