Skip to content

Commit

Permalink
EditCommitMessageAction: New action
Browse files Browse the repository at this point in the history
This adds a new action for GitCommitBear. When applied it
opens up a editor for user to edit commit message of the HEAD commit.
This also makes changes is CommitBear.py to pass EditCommitMessageAction
when Result is yielded.
  • Loading branch information
akshatkarani committed Sep 4, 2019
1 parent cc5462d commit 9669515
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
30 changes: 20 additions & 10 deletions bears/vcs/CommitBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from coalib.settings.Setting import typed_list
from coalib.settings.FunctionMetadata import FunctionMetadata
from dependency_management.requirements.PipRequirement import PipRequirement
from bears.vcs.actions.EditCommitMessageAction import EditCommitMessageAction


class _CommitBear(GlobalBear):
Expand Down Expand Up @@ -188,21 +189,24 @@ def check_shortlog(self, shortlog,
'character(s). This is {} character(s) longer than '
'the limit ({} > {}).'.format(
len(shortlog), diff,
len(shortlog), shortlog_length))
len(shortlog), shortlog_length),
actions=[EditCommitMessageAction()])

if (shortlog[-1] != '.') == shortlog_trailing_period:
yield Result(self,
'Shortlog of HEAD commit contains no period at end.'
if shortlog_trailing_period else
'Shortlog of HEAD commit contains a period at end.')
'Shortlog of HEAD commit contains a period at end.',
actions=[EditCommitMessageAction()])

if shortlog_regex:
match = re.fullmatch(shortlog_regex, shortlog)
if not match:
yield Result(
self,
'Shortlog of HEAD commit does not match given regex:'
' {regex}'.format(regex=shortlog_regex))
' {regex}'.format(regex=shortlog_regex),
actions=[EditCommitMessageAction()])

if shortlog_imperative_check:
colon_pos = shortlog.find(':')
Expand All @@ -214,7 +218,8 @@ def check_shortlog(self, shortlog,
bad_word = has_flaws[0]
yield Result(self,
"Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are '{}'".format(bad_word))
"mood! Bad words are '{}'".format(bad_word),
actions=[EditCommitMessageAction()])
if shortlog_wip_check:
if 'wip' in shortlog.lower()[:4]:
yield Result(
Expand Down Expand Up @@ -267,12 +272,15 @@ def check_body(self, body,
"""
if len(body) == 0:
if force_body:
yield Result(self, 'No commit message body at HEAD.')
yield Result(self, 'No commit message body at HEAD.',
actions=[EditCommitMessageAction()])
return

if body[0] != '\n':
yield Result(self, 'No newline found between shortlog and body at '
'HEAD commit. Please add one.')
yield Result(self,
'No newline found between shortlog and body at '
'HEAD commit. Please add one.',
actions=[EditCommitMessageAction()])
return

if body_regex and not re.fullmatch(body_regex, body.strip()):
Expand All @@ -284,9 +292,11 @@ def check_body(self, body,
if any((len(line) > body_line_length and
not any(regex.search(line) for regex in ignore_regexes))
for line in body[1:]):
yield Result(self, 'Body of HEAD commit contains too long lines. '
'Commit body lines should not exceed {} '
'characters.'.format(body_line_length))
yield Result(self,
'Body of HEAD commit contains too long lines. '
'Commit body lines should not exceed {} '
'characters.'.format(body_line_length),
actions=[EditCommitMessageAction()])

def check_issue_reference(self, body,
body_close_issue: bool = False,
Expand Down
17 changes: 17 additions & 0 deletions bears/vcs/actions/EditCommitMessageAction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess
from coalib.results.result_actions.ResultAction import ResultAction


class EditCommitMessageAction(ResultAction):
"""
Opens an editor to edit the commit message of the HEAD commit.
"""

SUCCESS_MESSAGE = 'Commit message edited successfully.'

def apply(self, result, original_file_dict, file_diff_dict):
"""
Edit (C)ommit Message [Note: This may rewrite your commit history]
"""
subprocess.check_call(['git', 'commit', '-o', '--amend'])
return file_diff_dict
24 changes: 24 additions & 0 deletions tests/vcs/actions/EditCommitMessageActionTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
from unittest.mock import patch
from coala_utils.ContextManagers import retrieve_stdout
from coalib.results.Result import Result
from bears.vcs.actions.EditCommitMessageAction import EditCommitMessageAction


class EditCommitMessageActionTest(unittest.TestCase):

def setUp(self):
self.uut = EditCommitMessageAction()
self.result = Result('origin', 'message')

def test_is_applicable(self):
self.assertTrue(self.uut.is_applicable(self.result, {}, {}))

def test_apply(self):
with retrieve_stdout() as stdout:
patcher = ('bears.vcs.actions.EditCommitMessageAction.'
'subprocess.check_call')
with patch(patcher):
ret = self.uut.apply(self.result, {}, {'file': 'diff'})
self.assertEqual(ret, {'file': 'diff'})
self.assertEqual(stdout.getvalue(), '')

0 comments on commit 9669515

Please sign in to comment.