From 077b0aad7ae2e5921f71921fcba8f4decbc23efd Mon Sep 17 00:00:00 2001 From: akshatkarani Date: Sat, 18 May 2019 20:59:24 +0530 Subject: [PATCH] AddNewlineAction: A new action for GitCommitBear 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 --- bears/vcs/CommitBear.py | 11 +++++++---- bears/vcs/actions/AddNewlineAction.py | 28 +++++++++++++++++++++++++++ bears/vcs/actions/__init__.py | 0 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 bears/vcs/actions/AddNewlineAction.py create mode 100644 bears/vcs/actions/__init__.py diff --git a/bears/vcs/CommitBear.py b/bears/vcs/CommitBear.py index a9a225f2a2..d9c009284b 100644 --- a/bears/vcs/CommitBear.py +++ b/bears/vcs/CommitBear.py @@ -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): @@ -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, @@ -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) = (), @@ -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()): diff --git a/bears/vcs/actions/AddNewlineAction.py b/bears/vcs/actions/AddNewlineAction.py new file mode 100644 index 0000000000..9370c02a23 --- /dev/null +++ b/bears/vcs/actions/AddNewlineAction.py @@ -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 diff --git a/bears/vcs/actions/__init__.py b/bears/vcs/actions/__init__.py new file mode 100644 index 0000000000..e69de29bb2