Skip to content

Commit

Permalink
feat: introduce "strict" mode (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Aug 19, 2023
2 parents 2e0c674 + 6b4f68c commit 09c0045
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
24 changes: 24 additions & 0 deletions conventional_pre_commit/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"style",
"test",
]
AUTOSQUASH_PREFIXES = [
"amend",
"fixup",
"squash",
]


def r_types(types):
Expand All @@ -39,6 +44,11 @@ def r_subject():
return r" .+"


def r_autosquash_prefixes():
"""Regex str for autosquash prefixes."""
return "|".join(AUTOSQUASH_PREFIXES)


def conventional_types(types=[]):
"""Return a list of Conventional Commits types merged with the given types."""
if set(types) & set(CONVENTIONAL_TYPES) == set():
Expand All @@ -58,3 +68,17 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
regex = re.compile(pattern, re.DOTALL)

return bool(regex.match(input))


def has_autosquash_prefix(input):
"""
Returns True if input starts with one of the autosquash prefixes used in git.
See the documentation, please https://git-scm.com/docs/git-rebase.
It doesn't check whether the rest of the input matches Conventional Commits
formatting.
"""
pattern = f"^(({r_autosquash_prefixes()})! ).*$"
regex = re.compile(pattern, re.DOTALL)

return bool(regex.match(input))
7 changes: 7 additions & 0 deletions conventional_pre_commit/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def main(argv=[]):
parser.add_argument(
"--force-scope", action="store_false", default=True, dest="optional_scope", help="Force commit to have scope defined."
)
parser.add_argument(
"--strict", action="store_true", help="Force commit to strictly follow Conventional Commits formatting."
)

if len(argv) < 1:
argv = sys.argv[1:]
Expand All @@ -47,6 +50,10 @@ def main(argv=[]):
)
return RESULT_FAIL

if not args.strict:
if format.has_autosquash_prefix(message):
return RESULT_SUCCESS

if format.is_conventional(message, args.types, args.optional_scope):
return RESULT_SUCCESS
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "conventional_pre_commit"
version = "2.3.0"
version = "2.4.0"
description = "A pre-commit hook that checks commit messages for Conventional Commits formatting."
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ def conventional_utf8_commit_path():
@pytest.fixture
def conventional_gbk_commit_path():
return get_message_path("conventional_commit_gbk")


@pytest.fixture
def fixup_commit_path():
return get_message_path("fixup_commit")
1 change: 1 addition & 0 deletions tests/messages/fixup_commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixup! feature: implement something cool
25 changes: 25 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def test_r_subject__special_chars():
assert regex.match(" some thing")


def test_r_autosquash_prefixes():
result = format.r_autosquash_prefixes()
regex = re.compile(result)

for prefix in format.AUTOSQUASH_PREFIXES:
assert regex.match(prefix)


def test_conventional_types__default():
result = format.conventional_types()

Expand Down Expand Up @@ -210,3 +218,20 @@ def test_is_conventional__missing_delimiter():
input = "feat message"

assert not format.is_conventional(input)


@pytest.mark.parametrize(
"input,has_prefix",
[
("amend! ", True),
("fixup! ", True),
("squash! ", True),
("squash! whatever .. $12 #", True),
("squash!", False),
(" squash! ", False),
("squash!:", False),
("feat(foo):", False),
],
)
def test_has_autosquash_prefix(input, has_prefix):
assert format.has_autosquash_prefix(input) == has_prefix
12 changes: 12 additions & 0 deletions tests/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ def test_main_success__conventional_with_scope(cmd, conventional_commit_with_sco
result = subprocess.call((cmd, "--force-scope", conventional_commit_with_scope_path))

assert result == RESULT_SUCCESS


def test_main_success__fixup_commit(cmd, fixup_commit_path):
result = subprocess.call((cmd, fixup_commit_path))

assert result == RESULT_SUCCESS


def test_main_success__fail_commit(cmd, fixup_commit_path):
result = subprocess.call((cmd, "--strict", fixup_commit_path))

assert result == RESULT_FAIL

0 comments on commit 09c0045

Please sign in to comment.