Skip to content

Commit dabd45b

Browse files
committed
fix: do not judge the fixup!, squash! and amend! subjects git writes
git commit --fixup, --squash and --fixup=amend: put "fixup! ", "squash! " or "amend! " in front of the target commit's subject. CC004 counted that prefix, so a fixup of a subject that fit the limit was rejected, and CC002 rejected every one of them for its lower-case "fixup!". CC003 skipped only "fixup! ". The subject rules (CC002-CC005) now skip these subjects, as they skip a merge or revert subject. Whether they are allowed stays CC009's call. Closes #733
1 parent 87cf6e5 commit dabd45b

3 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Priority: CLI > env > TOML > default. Booleans accept `true/false`, `yes/no`,
122122
| `commit.message_pattern` | `""` | — | `CCHK_MESSAGE_PATTERN` | Custom regex the whole message must match; when set it replaces the Conventional Commits check (still reported as CC001) |
123123
| `commit.subject_capitalized` | `false` | `--subject-capitalized` | `CCHK_SUBJECT_CAPITALIZED` | Require the subject to start with a capital letter (CC002) |
124124
| `commit.subject_imperative` | `false` | `--subject-imperative` | `CCHK_SUBJECT_IMPERATIVE` | Require the subject to use the imperative mood (CC003) |
125-
| `commit.subject_max_length` | `80` | `--subject-max-length` | `CCHK_SUBJECT_MAX_LENGTH` | Maximum subject length (CC004), not counting the ` (#123)` a GitHub squash merge adds; the `Revert "..."` and `Reapply "..."` subjects `git revert` writes are not measured; `0` turns it off. Commits by `dependabot[bot]` and `renovate[bot]` skip both length rules |
125+
| `commit.subject_max_length` | `80` | `--subject-max-length` | `CCHK_SUBJECT_MAX_LENGTH` | Maximum subject length (CC004), not counting the ` (#123)` a GitHub squash merge adds; the `Revert "..."` and `Reapply "..."` subjects `git revert` writes, and the `fixup!`, `squash!` and `amend!` subjects `git commit --fixup` and `--squash` write, are not measured; `0` turns it off. Commits by `dependabot[bot]` and `renovate[bot]` skip both length rules |
126126
| `commit.subject_min_length` | `5` | `--subject-min-length` | `CCHK_SUBJECT_MIN_LENGTH` | Minimum subject length (CC005); `0` turns it off |
127127
| `commit.allow_commit_types` | `feat, fix, docs, style, refactor, test, chore, perf, build, ci, revert` | `--allow-commit-types` | `CCHK_ALLOW_COMMIT_TYPES` | Allowed `<type>` values in the subject |
128128
| `commit.allow_merge_commits` | `true` | `--allow-merge-commits` | `CCHK_ALLOW_MERGE_COMMITS` | Allow merge commits (CC006) |

‎commit_check/engine.py‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ def is_revert_subject(subject: str) -> bool:
8787
return _GIT_REVERT_SUBJECT.match(subject) is not None
8888

8989

90+
# "git commit --fixup" writes "fixup! <subject>", "--squash" writes
91+
# "squash! <subject>" and "--fixup=amend:"/"--fixup=reword:" write
92+
# "amend! <subject>", each in front of the target commit's subject.
93+
# "git rebase --autosquash" folds these commits into their target, so the
94+
# subject behind the prefix is one already judged on its own commit, or,
95+
# after "amend! ", one about to be replaced. Git writes the prefix exactly
96+
# so, trailing space included: "fixup!! ..." or "Fixup! ..." is the author's.
97+
_GIT_AUTOSQUASH_SUBJECT = re.compile(r"^(?:fixup|squash|amend)! ")
98+
99+
100+
def is_autosquash_subject(subject: str) -> bool:
101+
"""Whether *subject* is one ``git commit --fixup`` or ``--squash`` wrote.
102+
103+
The subject rules decline to judge these, as they do a merge or revert
104+
subject. Whether they are allowed at all is ``allow_fixup_commits``'.
105+
"""
106+
return _GIT_AUTOSQUASH_SUBJECT.match(subject) is not None
107+
108+
90109
# A work-in-progress marker at the start of the subject (CC010). "^" without
91110
# re.MULTILINE and "[ \t]*" rather than "\s*" keep the match on the first
92111
# line, so a body that merely mentions WIP is not a WIP commit.
@@ -582,10 +601,11 @@ class SubjectCapitalizationValidator(SubjectValidator):
582601
"""Validates that subject starts with capital letter."""
583602

584603
def _validate_subject(self, subject: str) -> ValidationResult:
585-
# A merge subject is machine-written; the rule declines to judge it.
586-
# Only the subjects merge tools write count, so anything else is
604+
# Merge and fixup subjects are machine-written; the rule declines to
605+
# judge them. Only the subjects merge tools write, and git's exact
606+
# "fixup! ", "squash! " and "amend! ", count, so anything else is
587607
# author prose.
588-
if is_merge_subject(subject):
608+
if is_merge_subject(subject) or is_autosquash_subject(subject):
589609
return ValidationResult.SKIP
590610

591611
if self._is_capitalized(subject):
@@ -629,8 +649,8 @@ class SubjectImperativeValidator(SubjectValidator):
629649
def _validate_subject(self, subject: str) -> ValidationResult:
630650
# Merge and fixup subjects are machine-written; decline to judge them.
631651
# Only the subjects merge tools write, and git's exact "fixup! ",
632-
# count, so anything else is author prose.
633-
if is_merge_subject(subject) or subject.startswith("fixup! "):
652+
# "squash! " and "amend! ", count, so anything else is author prose.
653+
if is_merge_subject(subject) or is_autosquash_subject(subject):
634654
return ValidationResult.SKIP
635655

636656
# Extract first word, past a Conventional Commits prefix (including
@@ -728,10 +748,14 @@ def _written_by_dependency_bot(self) -> bool:
728748
return True
729749

730750
def _validate_subject(self, subject: str) -> ValidationResult:
731-
# A merge or revert subject's length is git's doing, not the
751+
# A merge, revert or fixup subject's length is git's doing, not the
732752
# author's: 'Revert "..."' adds nine characters to a subject that
733-
# already fit.
734-
if is_merge_subject(subject) or is_revert_subject(subject):
753+
# already fit, and "fixup! " seven.
754+
if (
755+
is_merge_subject(subject)
756+
or is_revert_subject(subject)
757+
or is_autosquash_subject(subject)
758+
):
735759
return ValidationResult.SKIP
736760

737761
suffix = _SQUASH_PR_NUMBER.search(subject)

‎tests/engine_test.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ValidationEngine,
3535
ValidationResult,
3636
declaration_boxes,
37+
is_autosquash_subject,
3738
is_merge_subject,
3839
is_revert_subject,
3940
long_body_lines,
@@ -1597,6 +1598,40 @@ def test_git_revert_subject_is_not_measured(self, subject):
15971598
ValidationResult.SKIP
15981599
)
15991600

1601+
@pytest.mark.parametrize(
1602+
"subject",
1603+
[
1604+
"fixup! feat(parser): accept trailing commas in the argument list of a call",
1605+
"squash! feat(parser): accept trailing commas in the argument list of a call",
1606+
"amend! feat(parser): accept trailing commas in the argument list of a call",
1607+
"fixup! fixup! feat(parser): accept trailing commas in the argument list of a call",
1608+
],
1609+
)
1610+
def test_git_fixup_subject_is_not_measured(self, subject):
1611+
"""A subject that fit the limit still fits once git puts "fixup! "
1612+
in front of it: the prefix is git's, and --autosquash folds it away."""
1613+
rule = ValidationRule(check="subject_max_length", value=72)
1614+
validator = SubjectLengthValidator(rule)
1615+
assert len(subject) > 72
1616+
assert validator.validate(ValidationContext(stdin_text=subject)) == (
1617+
ValidationResult.SKIP
1618+
)
1619+
1620+
@pytest.mark.parametrize(
1621+
"subject",
1622+
[
1623+
"fixup!! feat(parser): accept trailing commas in the argument list of a call",
1624+
"Fixup! feat(parser): accept trailing commas in the argument list of a call",
1625+
],
1626+
)
1627+
def test_author_fixup_lookalike_is_measured(self, subject):
1628+
rule = ValidationRule(check="subject_max_length", value=72)
1629+
validator = SubjectLengthValidator(rule)
1630+
validator._suppress_output = True
1631+
assert validator.validate(ValidationContext(stdin_text=subject)) == (
1632+
ValidationResult.FAIL
1633+
)
1634+
16001635
def test_author_revert_prose_is_measured(self):
16011636
rule = ValidationRule(check="subject_max_length", value=80)
16021637
validator = SubjectLengthValidator(rule)
@@ -1895,6 +1930,27 @@ def test_subject_capitalization_validator_not_capitalized(self):
18951930
result = validator.validate(context)
18961931
assert result == ValidationResult.FAIL
18971932

1933+
@pytest.mark.parametrize(
1934+
"subject",
1935+
["fixup! feat: add x", "squash! add x", "amend! fix: add x"],
1936+
)
1937+
def test_git_fixup_subject_is_not_judged(self, subject):
1938+
"""git writes the lower-case prefix; capitalizing it would stop
1939+
--autosquash from finding the commit."""
1940+
rule = ValidationRule(check="subject_capitalized")
1941+
validator = SubjectCapitalizationValidator(rule)
1942+
assert validator.validate(ValidationContext(stdin_text=subject)) == (
1943+
ValidationResult.SKIP
1944+
)
1945+
1946+
def test_author_fixup_lookalike_is_judged(self):
1947+
rule = ValidationRule(check="subject_capitalized")
1948+
validator = SubjectCapitalizationValidator(rule)
1949+
validator._suppress_output = True
1950+
assert validator.validate(ValidationContext(stdin_text="fixup!! add x")) == (
1951+
ValidationResult.FAIL
1952+
)
1953+
18981954

18991955
class TestBodyValidator:
19001956
@pytest.mark.parametrize(
@@ -4292,6 +4348,8 @@ def test_merge_and_fixup_subjects_still_bypass_the_rule(self):
42924348
not judged, and SKIP keeps that distinct from having passed."""
42934349
assert self._verdict("Merge branch 'main' into topic") == ValidationResult.SKIP
42944350
assert self._verdict("fixup! fixed the parser") == ValidationResult.SKIP
4351+
assert self._verdict("squash! fixed the parser") == ValidationResult.SKIP
4352+
assert self._verdict("amend! fixed the parser") == ValidationResult.SKIP
42954353

42964354
@pytest.mark.benchmark
42974355
def test_author_subject_starting_with_merge_is_judged(self):
@@ -4344,6 +4402,22 @@ def test_tool_written_subject(self, subject):
43444402
def test_author_prose(self, subject):
43454403
assert not is_merge_subject(subject)
43464404

4405+
@pytest.mark.parametrize(
4406+
"subject, expected",
4407+
[
4408+
("fixup! feat: x", True),
4409+
("squash! feat: x", True),
4410+
("amend! feat: x", True),
4411+
("fixup! fixup! feat: x", True),
4412+
("fixup!feat: x", False),
4413+
("fixup!! feat: x", False),
4414+
("Fixup! feat: x", False),
4415+
("feat: fixup! x", False),
4416+
],
4417+
)
4418+
def test_autosquash_subject(self, subject, expected):
4419+
assert is_autosquash_subject(subject) is expected
4420+
43474421
@pytest.mark.parametrize(
43484422
"subject, expected",
43494423
[

0 commit comments

Comments
 (0)