Skip to content

Commit

Permalink
GitCommitBear.py: Detect revert commits
Browse files Browse the repository at this point in the history
This detects revert commits
and checks they have a reason as well.

Closes #337
  • Loading branch information
kriti21 committed Feb 16, 2018
1 parent b0b5560 commit 28aa030
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bears/vcs/git/GitCommitBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def get_host_from_remotes():
netloc = urlparse(url)[1]
return netloc.split('.')[0]

def run(self, allow_empty_commit_message: bool = False, **kwargs):
def run(self, allow_empty_commit_message: bool = False,
check_revert_commits: bool = True,
**kwargs):
"""
Check the current git commit message at HEAD.
Expand All @@ -105,6 +107,8 @@ def run(self, allow_empty_commit_message: bool = False, **kwargs):
:param allow_empty_commit_message: Whether empty commit messages are
allowed or not.
:param check_revert_commits: whether revert commits are to be
checked or not.
"""
with change_directory(self.get_config_dir() or os.getcwd()):
stdout, stderr = run_shell_command('git log -1 --pretty=%B')
Expand All @@ -123,6 +127,9 @@ def run(self, allow_empty_commit_message: bool = False, **kwargs):
yield Result(self, 'HEAD commit has no message.')
return

if shortlog.startswith('Revert') and check_revert_commits:
yield Result(self, 'Revert commit does not have a reason.')

yield from self.check_shortlog(
shortlog,
**self.get_shortlog_checks_metadata().filter_parameters(kwargs))
Expand Down
8 changes: 8 additions & 0 deletions tests/vcs/git/GitCommitBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def test_empty_message(self):
[])
self.assert_no_msgs()

def test_revert_commit(self):
self.git_commit('Revert : identifies revert commit\n')

self.assertEqual(self.run_uut(),
['Revert commit does not have a reason.'])

self.assert_no_msgs()

@unittest.mock.patch('bears.vcs.git.GitCommitBear.run_shell_command',
return_value=('one-liner-message\n', ''))
def test_pure_oneliner_message(self, patch):
Expand Down

0 comments on commit 28aa030

Please sign in to comment.