Skip to content

Commit

Permalink
AddNewlineAction: A new action for GitCommitBear
Browse files Browse the repository at this point in the history
This adds a new action which adds a newline between
shortlog and body of commit message when applied.
This also make changes in CommitBear.py to pass
AddNewlineAction when Result is yielded
  • Loading branch information
akshatkarani committed Jun 20, 2019
1 parent fd5a5a7 commit 077b0aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
11 changes: 7 additions & 4 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.AddNewlineAction import AddNewlineAction


class _CommitBear(GlobalBear):
Expand Down Expand Up @@ -155,7 +156,7 @@ def run(self,
shortlog,
**self.get_shortlog_checks_metadata().filter_parameters(kwargs))
yield from self.check_body(
body,
body, shortlog,
**self.get_body_checks_metadata().filter_parameters(kwargs))
yield from self.check_issue_reference(
body,
Expand Down Expand Up @@ -246,7 +247,7 @@ def check_imperative(self, paragraph):
else:
return None

def check_body(self, body,
def check_body(self, body, shortlog: str = '',
body_line_length: int = 72,
force_body: bool = False,
ignore_length_regex: typed_list(str) = (),
Expand All @@ -271,8 +272,10 @@ def check_body(self, body,
return

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

if body_regex and not re.fullmatch(body_regex, body.strip()):
Expand Down
28 changes: 28 additions & 0 deletions bears/vcs/actions/AddNewlineAction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from coalib.misc.Shell import run_shell_command
from coalib.results.result_actions.ResultAction import ResultAction


class AddNewlineAction(ResultAction):

SUCCESS_MESSAGE = 'New Line added successfully.'

def __init__(self, shortlog, body):
self.shortlog = shortlog
self.body = body

@staticmethod
def is_applicable(result,
original_file_dict,
file_diff_dict,
applied_actions=()):
return 'AddNewlineAction' not in file_diff_dict

def apply(self, result, original_file_dict, file_diff_dict):
"""
Add New(L)ine
"""
new_commit_message = '{}\n\n{}'.format(self.shortlog, self.body)
command = 'git commit --amend -m "{}"'.format(new_commit_message)
stdout, err = run_shell_command(command)
file_diff_dict['AddNewlineAction'] = True
return file_diff_dict
Empty file added bears/vcs/actions/__init__.py
Empty file.

0 comments on commit 077b0aa

Please sign in to comment.