forked from jorisroovers/gitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contrib rule DisallowCleanupCommits(jorisroovers#312)
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
1 parent
c50eb15
commit adc9023
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
38 changes: 38 additions & 0 deletions
38
gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |