Skip to content

Commit

Permalink
Contrib rule DisallowCleanupCommits(jorisroovers#312)
Browse files Browse the repository at this point in the history
This contrib rule will block fixup, squash and amend commits.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Co-authored-by: Joris Roovers <joris.roovers@gmail.com>
  • Loading branch information
matthiasbeyer and jorisroovers authored Aug 15, 2022
1 parent c50eb15 commit adc9023
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/contrib_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ID | Name | gitlint version | Description
------|-------------------------------------|------------------ |-------------------------------------------
CT1 | contrib-title-conventional-commits | >= 0.12.0 | Enforces [Conventional Commits](https://www.conventionalcommits.org/) commit message style on the title.
CC1 | contrib-body-requires-signed-off-by | >= 0.12.0 | Commit body must contain a `Signed-off-by` line.
CC2 | contrib-disallow-cleanup-commits | >= 0.18.0 | Commit title must not contain `fixup!`, `squash!`, `amend!`.

## CT1: contrib-title-conventional-commits ##

Expand All @@ -63,5 +64,11 @@ ID | Name | gitlint version | Description
CC1 | contrib-body-requires-signed-off-by | >= 0.12.0 | Commit body must contain a `Signed-off-by` line. This means, a line that starts with the `Signed-off-by` keyword.


## CC2: contrib-disallow-cleanup-commits ##

ID | Name | gitlint version | Description
------|----------------------------------|--------------------|-------------------------------------------
CC2 | contrib-disallow-cleanup-commits | >= 0.18.0 | Commit title must not contain `fixup!`, `squash!` or `amend!`. This means `git commit --fixup` and `git commit --squash` commits are not allowed.

## Contributing Contrib rules
We'd love for you to contribute new Contrib rules to gitlint or improve existing ones! Please visit the [Contributing](contributing) page on how to get started.
22 changes: 22 additions & 0 deletions gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from gitlint.rules import CommitRule, RuleViolation


class DisallowCleanupCommits(CommitRule):
""" This rule checks the commits for "fixup!"/"squash!"/"amend!" commits
and rejects them.
"""

name = "contrib-disallow-cleanup-commits"
id = "CC2"

def validate(self, commit):
if commit.is_fixup_commit:
return [RuleViolation(self.id, "Fixup commits are not allowed", line_nr=1)]

if commit.is_squash_commit:
return [RuleViolation(self.id, "Squash commits are not allowed", line_nr=1)]

if commit.is_fixup_amend_commit:
return [RuleViolation(self.id, "Amend commits are not allowed", line_nr=1)]

return []
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.rules import RuleViolation
from gitlint.contrib.rules.disallow_cleanup_commits import DisallowCleanupCommits

from gitlint.config import LintConfig


class ContribDisallowCleanupCommitsTest(BaseTestCase):

def test_enable(self):
# Test that rule can be enabled in config
for rule_ref in ['CC2', 'contrib-disallow-cleanup-commits']:
config = LintConfig()
config.contrib = [rule_ref]
self.assertIn(DisallowCleanupCommits(), config.rules)

def test_disallow_fixup_squash_commit(self):
# No violations when no 'fixup!' line and no 'squash!' line is present
rule = DisallowCleanupCommits()
violations = rule.validate(self.gitcommit("Föobar\n\nMy Body"))
self.assertListEqual(violations, [])

# Assert violation when 'fixup!' in title
violations = rule.validate(self.gitcommit("fixup! Föobar\n\nMy Body"))
expected_violation = RuleViolation("CC2", "Fixup commits are not allowed", line_nr=1)
self.assertListEqual(violations, [expected_violation])

# Assert violation when 'squash!' in title
violations = rule.validate(self.gitcommit("squash! Föobar\n\nMy Body"))
expected_violation = RuleViolation("CC2", "Squash commits are not allowed", line_nr=1)
self.assertListEqual(violations, [expected_violation])

# Assert violation when 'amend!' in title
violations = rule.validate(self.gitcommit("amend! Föobar\n\nMy Body"))
expected_violation = RuleViolation("CC2", "Amend commits are not allowed", line_nr=1)
self.assertListEqual(violations, [expected_violation])

0 comments on commit adc9023

Please sign in to comment.